import { Editor , EditorPosition , EditorSuggest , EditorSuggestContext , EditorSuggestTriggerInfo , TFile } from 'obsidian' ; import BibleReferencePlugin from '../main' ; import { VerseTypoCheck } from './VerseTypoCheck' ; import { VerseSuggesting } from './VerseSuggesting' ; import { BibleReferencePluginSettings } from './data/constants' ; /**
 * Extend the EditorSuggest to suggest bible verses.
 */ export class VerseEditorSuggestor extends EditorSuggest < VerseSuggesting > { plugin : BibleReferencePlugin ; settings : BibleReferencePluginSettings ; constructor ( plugin , settings ) { super ( plugin . app ) ; this . plugin = plugin ; this . settings = settings ; } param cursor param editor param _ onTrigger ( cursor , editor , _ ) { const currentContent = editor . getLine ( cursor . line ) . substring ( 0 , cursor . ch ) ; const match = VerseTypoCheck ( currentContent ) ; if ( match ) { console . debug ( 'trigger on' , currentContent ) ; const editorSuggestTriggerInfo = { end : cursor , start : { line : cursor . line , ch : currentContent . lastIndexOf ( match ) , } , query : match , } ; return editorSuggestTriggerInfo ; } return null ; } param context async getSuggestions ( context ) { console . debug ( 'get suggestion for query ' , context . query . toLowerCase ( ) ) ; const bookName = context . query . match ( /[123]*[A-z]{3,}/ ) . first ( ) ; const numbersPartsOfQueryString = context . query . substring ( 2 + bookName . length ) ; const numbers = numbersPartsOfQueryString . split ( /[-:]+/ ) ; const chapterNumber = parseInt ( numbers [ 0 ] ) ; const verseNumber = parseInt ( numbers [ 1 ] ) ; const verseEndNumber = numbers . length === 3 ? parseInt ( numbers [ 2 ] ) : undefined ; const suggestingVerse = new VerseSuggesting ( bookName , chapterNumber , verseNumber , verseEndNumber , this . settings . bibleVersion ) ; console . debug ( bookName , chapterNumber , verseNumber , verseEndNumber , suggestingVerse , this . settings . bibleVersion ) ; await suggestingVerse . fetchAndSetVersesText ( ) ; return [ suggestingVerse ] ; } renderSuggestion ( suggestion , el ) { const outer = el . createDiv ( { cls : "obr-suggester-container" } ) ; outer . createDiv ( { cls : "obr-shortcode" } ) . setText ( suggestion . text ) ; } selectSuggestion ( suggestion ) { if ( this . context ) { ( this . context . editor as Editor ) . replaceRange ( suggestion . ReplacementContent , this . context . start , this . context . end ) ; } } }