generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Field Suggestor! Type "BC-" at the start of a line to trigger
hideTrailField settings has been removed in favour of a standard 'BC-hide-trail'
- Loading branch information
1 parent
865cf58
commit 652da4f
Showing
6 changed files
with
234 additions
and
33 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
Editor, | ||
EditorPosition, | ||
EditorSuggest, | ||
EditorSuggestContext, | ||
EditorSuggestTriggerInfo, | ||
TFile, | ||
} from "obsidian"; | ||
import { BC_FIELDS } from "src/constants"; | ||
import type BCPlugin from "src/main"; | ||
|
||
export class FieldSuggestor extends EditorSuggest<string> { | ||
plugin: BCPlugin; | ||
|
||
constructor(plugin: BCPlugin) { | ||
super(plugin.app); | ||
this.plugin = plugin; | ||
} | ||
|
||
onTrigger( | ||
cursor: EditorPosition, | ||
editor: Editor, | ||
_: TFile | ||
): EditorSuggestTriggerInfo | null { | ||
if (this.plugin.settings.fieldSuggestor) { | ||
const sub = editor.getLine(cursor.line).substring(0, cursor.ch); | ||
const match = sub.match(/^BC-(.*)$/)?.[1]; | ||
if (match !== undefined) { | ||
return { | ||
end: cursor, | ||
start: { | ||
ch: sub.lastIndexOf(match), | ||
line: cursor.line, | ||
}, | ||
query: match, | ||
}; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
getSuggestions = (context: EditorSuggestContext) => { | ||
const { query } = context; | ||
return BC_FIELDS.map((sug) => sug.field).filter((sug) => | ||
sug.includes(query) | ||
); | ||
}; | ||
|
||
renderSuggestion(suggestion: string, el: HTMLElement): void { | ||
el.createDiv({ | ||
text: suggestion.replace("BC-", ""), | ||
cls: "BC-suggester-container", | ||
attr: { | ||
"aria-label": BC_FIELDS.find((f) => f.field === suggestion)?.desc, | ||
"aria-label-position": "right", | ||
}, | ||
}); | ||
} | ||
|
||
selectSuggestion(suggestion: string): void { | ||
const { context } = this; | ||
if (context) { | ||
const replacement = `${suggestion}${ | ||
BC_FIELDS.find((f) => f.field === suggestion)?.after | ||
}`; | ||
context.editor.replaceRange( | ||
replacement, | ||
{ ch: 0, line: context.start.line }, | ||
context.end | ||
); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.