55
66// import moment from 'moment/min/moment-with-locales'
77import moment from 'moment/min/moment-with-locales'
8- import { getBlockUnderHeading } from './NPParagraph'
98import * as dt from '@helpers/dateTime'
109import { clo , JSP , logDebug , logError , logInfo , logTimer , logWarn , timer } from '@helpers/dev'
1110import { getFolderDisplayName , getFolderFromFilename , getRegularNotesInFolder } from '@helpers/folders'
12- import { displayTitle , isValidUUID } from '@helpers/general'
11+ import { displayTitle , isDecoratedCommandBarAvailable , isValidUUID } from '@helpers/general'
1312import { calendarNotesSortedByChanged , noteType } from '@helpers/note'
1413import { displayTitleWithRelDate , getDateStrFromRelativeDateString , getRelativeDates } from '@helpers/NPdateTime'
1514import { endOfFrontmatterLineIndex , ensureFrontmatter , getFrontmatterAttributes , getFrontmatterAttribute } from '@helpers/NPFrontMatter'
1615import { findStartOfActivePartOfNote , findEndOfActivePartOfNote } from '@helpers/paragraph'
16+ import { getBlockUnderHeading , getSelectedParagraphsWithCorrectLineIndex } from '@helpers/NPParagraph'
1717import { caseInsensitiveIncludes , caseInsensitiveSubstringMatch , getCorrectedHashtagsFromNote } from '@helpers/search'
1818import { parseTeamspaceFilename } from '@helpers/teamspace'
1919import { isOpen , isClosed , isDone , isScheduled } from '@helpers/utils'
@@ -81,7 +81,7 @@ export async function chooseNoteV2(
8181 const sortedNoteList = noteList.sort((first, second) => second . changedDate - first . changedDate ) // most recent first
8282
8383 // Form the options to give to the CommandBar
84- // Note: We will set up the more advanced options for the `CommandBar.showOptions` call, but downgrade them if we're not running v3.18+ (b1413)
84+ // Note: We will set up the more advanced options for the `CommandBar.showOptions` call, but downgrade them if we're not running v3.18+
8585 /**
8686 * type TCommandBarOptionObject = {
8787 * text: string,
@@ -180,7 +180,7 @@ export async function chooseNoteV2(
180180
181181 // Now show the options to the user
182182 let noteToReturn = null
183- if (NotePlan.environment.buildVersion >= 1413 ) {
183+ if (isDecoratedCommandBarAvailable() ) {
184184 // logDebug('chooseNoteV2', `Using 3.18.0's advanced options for CommandBar.showOptions call`)
185185 // use the more advanced options to the `CommandBar.showOptions` call
186186 const { index } = await CommandBar . showOptions ( opts , promptText )
@@ -211,8 +211,10 @@ export async function printNote(noteIn: ?TNote, alsoShowParagraphs: boolean = fa
211211 logWarn ( 'note/printNote()' , `No valid note found. Stopping.` )
212212 return
213213 }
214+ const usingEditor = ( noteIn == null || note . filename === Editor . filename )
214215
215- console . log ( `# ${ note . type } Note: '${ displayTitle ( note ) } '${ noteIn == null ? ' from Editor' : '' } :` )
216+ console . log ( `# '${ displayTitle ( note ) } '${ usingEditor ? ' (from Editor)' : '' } :` )
217+ console . log ( `- type ${ note . type } ` )
216218 // If it's a Teamspace note, show some details
217219 if ( note . isTeamspaceNote ) {
218220 // $FlowIgnore[incompatible-type]
@@ -225,7 +227,7 @@ export async function printNote(noteIn: ?TNote, alsoShowParagraphs: boolean = fa
225227 if (note.type === 'Notes') {
226228 const startOfActive = findStartOfActivePartOfNote ( note )
227229 const endOfActive = findEndOfActivePartOfNote ( note )
228- console . log ( `- # paragraphs: ${ note . paragraphs . length } (Active part: ${ String ( startOfActive ) } -${ String ( endOfActive ) } )` )
230+ console . log ( `- # paragraphs: ${ note . paragraphs . length } (Active part: ${ String ( startOfActive ) } -${ String ( endOfActive ) } )` )
229231 } else {
230232 // Calendar note
231233 console . log ( dt . getDateStringFromCalendarFilename ( note . filename ) )
@@ -237,29 +239,38 @@ export async function printNote(noteIn: ?TNote, alsoShowParagraphs: boolean = fa
237239 console.log(`- hashtags: ${ note . hashtags ?. join ( ', ' ) ?? '-' } `)
238240 console.log(`- mentions: ${ note . mentions ?. join ( ', ' ) ?? '-' } `)
239241
240- // Get frontmatter details
241- const FMAttribs = getFrontmatterAttributes(note)
242- console.log(`- has ${ Object . keys ( FMAttribs ) . length } frontmatter keys: ${ Object . keys ( FMAttribs ) . join ( '\n ' ) } `)
243-
244242 if (note.paragraphs.length > 0 ) {
245243 const open = note . paragraphs . filter ( ( p ) => isOpen ( p ) ) . length
246244 const done = note . paragraphs . filter ( ( p ) => isDone ( p ) ) . length
247245 const closed = note . paragraphs . filter ( ( p ) => isClosed ( p ) ) . length
248246 const scheduled = note . paragraphs . filter ( ( p ) => isScheduled ( p ) ) . length
249247 console . log ( `- open: ${ String ( open ) } \n- done: ${ String ( done ) } \n- closed: ${ String ( closed ) } \n- scheduled: ${ String ( scheduled ) } ` )
250248 if ( alsoShowParagraphs ) {
251- console . log ( `Paragraphs` )
249+ console . log ( `Paragraphs: ` )
252250 note . paragraphs . map ( ( p ) => {
253251 const referencedParas = DataStore . referencedBlocks ( p )
254252 console . log ( ` ${ p . lineIndex } : ${ p . type } ${ p . rawContent } ${ ( referencedParas . length >= 1 ) ? ` 🆔 has ${ referencedParas . length } sync copies` : '' } ` )
255253 } )
256254 }
257255 }
258256
257+ // Get frontmatter details
258+ const FMAttribs = getFrontmatterAttributes ( note )
259+ console . log ( `- ${String ( Object . keys ( FMAttribs ) . length ) } frontmatter keys : ${Object . keys ( FMAttribs ) . join ( '\n ') } `)
260+
261+ // If using the Editor, now show the selection and selected paragraphs
262+ if ( usingEditor ) {
263+ console . log ( `Selection: start: ${ String ( Editor . selection ?. start ) } , end: ${ String ( Editor . selection ?. end ) } {${ Editor . selectedText ?? '-' } }` )
264+ console . log ( `Rendered Selection: start: ${ String ( Editor . renderedSelection ?. start ) } , end: ${ String ( Editor . renderedSelection ?. end ) } ` )
265+ console . log ( `${ Editor . selectedParagraphs . length } Selected paragraph(s):\n${ String ( Editor . selectedParagraphs . map ( ( p ) => `- ${ p . lineIndex } : ${ p . content } ` ) . join ( '\n' ) ) } ` )
266+ const correctedSelectedParagraphs = getSelectedParagraphsWithCorrectLineIndex ( )
267+ console . log ( `${ correctedSelectedParagraphs . length } Corrected selected paragraph(s) with lineIndex taking into account frontmatter lines:\n${ String ( correctedSelectedParagraphs . map ( ( p ) => `- ${ p . lineIndex } : ${ p . content } ` ) . join ( '\n' ) ) } ` )
268+ }
269+
259270 // Now show .backlinks
260271 if (note.backlinks?.length > 0 ) {
261- console . log ( `Backlinks` )
262- console . log ( `- ${ String ( note . backlinks . length ) } backlinked notes ` )
272+ console . log ( `Backlinks: ` )
273+ console . log ( `- ${ String ( note . backlinks . length ) } backlinked note(s) ` )
263274 // $FlowIgnore[prop-missing]
264275 const flatBacklinkParas = getFlatListOfBacklinks ( note ) ?? [ ] // Note: this requires DataStore
265276 console . log ( `- ${ String ( flatBacklinkParas . length ) } backlink paras:` )
@@ -342,7 +353,11 @@ export function getNoteFromFilename(filenameIn: string): TNote | null {
342353 foundNote = DataStore . noteByFilename ( filenameIn , 'Notes' , teamspaceID )
343354 ?? DataStore . noteByFilename ( filenameIn , 'Calendar' , teamspaceID )
344355 ?? null
345- logDebug ( 'NPnote/getNoteFromFilename' , `Found teamspace note '${ displayTitle ( foundNote ) } ' from ${ filenameIn } ` )
356+ if ( foundNote != null ) {
357+ logDebug ( 'NPnote/getNoteFromFilename' , `Found teamspace note '${ displayTitle ( foundNote ) } ' from ${ filenameIn } ` )
358+ } else {
359+ throw new Error ( `No teamspace note found for ${ filenameIn } ` )
360+ }
346361 } else {
347362 // Check for private notes
348363 foundNote = DataStore . projectNoteByFilename ( filenameIn ) ?? null
@@ -1262,10 +1277,10 @@ export async function getNoteFromParamOrUser(
12621277 purpose : string ,
12631278 noteTitleArg : string = '' ,
12641279 notesIn ?: Array < TNote > ,
1265- ) : Promise < TNote | null > {
1280+ ) : Promise < ? TNote > {
12661281 // Note: deliberately no try/catch so that failure can stop processing
12671282 const startTime = new Date ( )
1268- let note : TNote | null
1283+ let note : ? TNote
12691284 let noteTitleArgIsCalendarNote : boolean = false
12701285 logDebug ( 'getNoteFromParamOrUser' , `starting with purpose '${ purpose } ' / noteTitleArg '${ noteTitleArg } ' / with ${ notesIn ?. length ?? 0 } notesIn` )
12711286
@@ -1276,8 +1291,9 @@ export async function getNoteFromParamOrUser(
12761291 const possDateStr = getDateStrFromRelativeDateString ( noteTitleArg )
12771292 if ( possDateStr ) {
12781293 noteTitleArgIsCalendarNote = true
1294+ // $FlowFixMe[incompatible-type]
12791295 note = getOrMakeCalendarNote ( possDateStr )
1280- if ( note ) {
1296+ if ( note != null ) {
12811297 logDebug ( 'getNoteFromParamOrUser' , `Found match with relative date '${ noteTitleArg } ' = filename ${ note ?. filename ?? '(error)' } ` )
12821298 return note
12831299 }
@@ -1317,8 +1333,9 @@ export async function getNoteFromParamOrUser(
13171333 // Preferably we'll use the last parameter, but if not calculate the list of notes to check
13181334 // const notesToCheck = getNotesToCheck(notesIn)
13191335 const result = await chooseNoteV2 ( `Select note for new ${ purpose } ` , notesIn , true , true , false , false )
1320- if ( typeof result !== 'boolean' ) {
1336+ if ( note != null && typeof result !== 'boolean' ) {
13211337 note = result
1338+ // $FlowIgnore[incompatible-call] tested note is not null here
13221339 logDebug ( 'getNoteFromParamOrUser' , `- found note '${ displayTitle ( note ) } '` )
13231340 }
13241341 }
0 commit comments