import { Editor , EditorPosition } from 'obsidian' ; import { DEFAULT_SETTINGS } from 'settings' ; interface WordBoundaries { start : { line : number ; ch : number ; } ; end : { line : number ; ch : number ; } ; } export class EditorExtensions { public static getSelectedText ( editor ) { if ( ! editor . somethingSelected ( ) ) { let wordBoundaries = this . getWordBoundaries ( editor ) ; editor . setSelection ( wordBoundaries . start , wordBoundaries . end ) ; } return editor . getSelection ( ) ; } private static cursorWithinBoundaries ( cursor , match ) { let startIndex = match . index ; let endIndex = match . index + match [ 0 ] . length ; return startIndex <= cursor . ch && cursor . ch <= endIndex ; } private static getWordBoundaries ( editor ) { let startCh , endCh ; let cursor = editor . getCursor ( ) ; let lineText = editor . getLine ( cursor . line ) ; let linksInLine = lineText . matchAll ( DEFAULT_SETTINGS . linkLineRegex ) ; for ( let match of linksInLine ) { if ( this . cursorWithinBoundaries ( cursor , match ) ) { return { start : { line : cursor . line , ch : match . index } , end : { line : cursor . line , ch : match . index + match [ 0 ] . length } , } ; } } let urlsInLine = lineText . matchAll ( DEFAULT_SETTINGS . lineRegex ) ; for ( let match of urlsInLine ) { if ( this . cursorWithinBoundaries ( cursor , match ) ) { return { start : { line : cursor . line , ch : match . index } , end : { line : cursor . line , ch : match . index + match [ 0 ] . length } , } ; } } return { start : cursor , end : cursor , } ; } public static getEditorPositionFromIndex ( content , index ) { let substr = content . substr ( 0 , index ) ; let l = 0 ; let offset = - 1 ; let r = - 1 ; for ( ; ( r = substr . indexOf ( "\n" , r + 1 ) ) !== - 1 ; l ++ , offset = r ) ; offset += 1 ; let ch = content . substr ( offset , index - offset ) . length ; return { line : l , ch : ch } ; } }