-
-
Save Kenishi/78cf83af91872fd5a659c929ff6f6d15 to your computer and use it in GitHub Desktop.
Gather string of note names and ids for auto-complete suggestions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const autoCompleteLinkInserter = (editor: any, data: any) => { | |
// https://stackoverflow.com/a/54081197 | |
const insertValue = data.value; | |
const lastPositon = editor.selection.getCursor(); | |
editor.jumpToMatching(); | |
const startPosition = editor.selection.getCursor(); | |
editor.session.replace({ | |
start: { row: startPosition.row, column: startPosition.column-2 }, | |
end: { row: lastPositon.row, column: lastPositon.column+2 } | |
}, insertValue); | |
}; | |
const autoCompleteNoteLinkCompleter = { | |
getCompletions: (_editor: any, _session: any, _pos: any, _prefix: string, callback: Function) => { | |
callback(null, props.autoCompleteNoteLinkSuggestions.map((note) => { | |
return { | |
completer: { | |
insertMatch: autoCompleteLinkInserter | |
}, | |
caption: note.title, | |
value: `[${note.title}](:/${note.id})`, | |
}; | |
})); | |
} | |
}; | |
useEffect(() => { | |
// Register custom doAutoLiveComplete so completer only happens for '[[' and nothing else | |
function doAutoLiveComplete(e : any) { | |
const editor = e.editor; | |
const hasCompleter = editor.completer && editor.completer.activated; | |
if(!hasCompleter && e.command.name === 'insertstring') { | |
const cursor = editor.getCursorPosition(); | |
const line = editor.session.getLine(cursor.row); | |
const lineStart = line.slice(0, cursor.column); | |
if(/\[\[/.test(lineStart)) { | |
editor.execCommand("startAutocomplete"); | |
} | |
} | |
} | |
if(editor) { | |
editor.commands.on('afterExec', doAutoLiveComplete); | |
langTools.setCompleters([autoCompleteNoteLinkCompleter]); | |
} | |
}, [editor]); | |
function renderEditor() { ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <== Line 414 | |
const searchMarkers = useSearchMarkers(showLocalSearch, localSearchMarkerOptions, props.searches, props.selectedSearchId); | |
const [autoCompleteNoteLinkSuggestions, setAutoCompleteNoteLinkSuggestions] = useState([]); | |
useEffect(() => { | |
const fetchSuggestions = async() => { | |
let suggestions = await gatherAutoCompleteSuggestions(); | |
setAutoCompleteNoteLinkSuggestions(suggestions); | |
} | |
fetchSuggestions(); | |
}, [props.noteId]); | |
async function gatherAutoCompleteSuggestions() : Promise<AutoCompleteSuggestion[]> { | |
let searchPref = await Setting.value("autoCompleteNoteSearchPref"); | |
let currentNote = await Note.load(props.noteId); | |
let selectedNotes = []; | |
if(searchPref === 'rootFolder') { | |
let allFolders = await Folder.all(); | |
const path = Folder.folderPath(allFolders, currentNote.parent_id); | |
selectedNotes = await Promise.all(path.map((folder : any) => Folder.noteIds(folder.id))); | |
// Extract notes for suggestion and flatten the array | |
selectedNotes = selectedNotes.map((folder : any) => { | |
return folder.map((note : any) => { | |
return { | |
id: note.id, | |
title: note.title | |
}; | |
}); | |
}); | |
selectedNotes = [].concat.apply([], selectedNotes); | |
} | |
else if(searchPref === 'currentFolder') { | |
let currentFolderNotes = await Folder.noteIds(currentNote.parent_id); | |
selectedNotes = currentFolderNotes.map((note : any) => { | |
return { | |
id: note.id, | |
title: note.title | |
}; | |
}); | |
} | |
else { | |
let allNotes = await Note.all(); | |
selectedNotes = allNotes.map((note : any) => { | |
return { | |
id: note.id, | |
title: note.title | |
}; | |
}); | |
} | |
return selectedNotes; | |
} | |
const editorProps:NoteBodyEditorProps = { | |
ref: editorRef, | |
contentKey: formNote.id, | |
style: styles.tinyMCE, | |
onChange: onBodyChange, | |
onWillChange: onBodyWillChange, | |
onMessage: onMessage, | |
content: formNote.body, | |
contentMarkupLanguage: formNote.markup_language, | |
contentOriginalCss: formNote.originalCss, | |
resourceInfos: resourceInfos, | |
htmlToMarkdown: htmlToMarkdown, | |
markupToHtml: markupToHtml, | |
allAssets: allAssets, | |
disabled: false, | |
theme: props.theme, | |
dispatch: props.dispatch, | |
noteToolbar: null,// renderNoteToolbar(), | |
onScroll: onScroll, | |
searchMarkers: searchMarkers, | |
visiblePanes: props.noteVisiblePanes || ['editor', 'viewer'], | |
keyboardMode: Setting.value('editor.keyboardMode'), | |
locale: Setting.value('locale'), | |
onDrop: onDrop, | |
autoCompleteNoteLinkSuggestions: autoCompleteNoteLinkSuggestions, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment