generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Relation Suggestor with custom trigger (#286)
- Loading branch information
1 parent
7424c8b
commit d7ffaf2
Showing
8 changed files
with
207 additions
and
32 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,68 @@ | ||
import { | ||
Editor, | ||
EditorPosition, | ||
EditorSuggest, | ||
EditorSuggestContext, | ||
EditorSuggestTriggerInfo, | ||
TFile, | ||
} from "obsidian"; | ||
import type BCPlugin from "./main"; | ||
import { escapeRegex } from "./Utils/generalUtils"; | ||
import { getFields } from "./Utils/HierUtils"; | ||
|
||
export class RelationSuggestor extends EditorSuggest<string> { | ||
plugin: BCPlugin; | ||
|
||
constructor(plugin: BCPlugin) { | ||
super(plugin.app); | ||
this.plugin = plugin; | ||
} | ||
|
||
onTrigger( | ||
cursor: EditorPosition, | ||
editor: Editor, | ||
_: TFile | ||
): EditorSuggestTriggerInfo | null { | ||
const trig = this.plugin.settings.relSuggestorTrigger; | ||
const sub = editor.getLine(cursor.line).substring(0, cursor.ch); | ||
|
||
const regex = new RegExp(`.*?${escapeRegex(trig)}(.*)$`); | ||
const match = regex.exec(sub)?.[1]; | ||
|
||
if (match === undefined) return null; | ||
return { | ||
start: { | ||
ch: sub.lastIndexOf(trig), | ||
line: cursor.line, | ||
}, | ||
end: cursor, | ||
query: match, | ||
}; | ||
} | ||
|
||
getSuggestions = (context: EditorSuggestContext) => { | ||
const { query } = context; | ||
const { userHiers } = this.plugin.settings; | ||
return getFields(userHiers).filter((sug) => sug.includes(query)); | ||
}; | ||
|
||
renderSuggestion(suggestion: string, el: HTMLElement): void { | ||
el.createDiv({ | ||
text: suggestion, | ||
cls: "codeblock-suggestion", | ||
}); | ||
} | ||
|
||
selectSuggestion(suggestion: string): void { | ||
const { context } = this; | ||
if (context) { | ||
const trig = this.plugin.settings.relSuggestorTrigger; | ||
const { start, end, editor } = context; | ||
editor.replaceRange( | ||
suggestion + ":", | ||
{ ch: start.ch + 1 - trig.length, line: start.line }, | ||
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
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