mirror of
https://github.com/gnoxnahte/obsidian-auto-embed.git
synced 2026-07-22 09:50:24 +00:00
- User pastes url - Plugin suggests to embed (has an option to cancel or tap anywhere else to cancel) - After accepting to embed, mark the link for embedding [ae:embed](link)
69 lines
No EOL
2.1 KiB
TypeScript
69 lines
No EOL
2.1 KiB
TypeScript
import AutoEmbedPlugin from "main";
|
|
import { Editor, EditorPosition, EditorSuggest, EditorSuggestContext, EditorSuggestTriggerInfo, TFile } from "obsidian";
|
|
|
|
class Suggestion {
|
|
choice: string;
|
|
|
|
constructor(choice: string) {
|
|
this.choice = choice;
|
|
}
|
|
}
|
|
|
|
export default class SuggestEmbed extends EditorSuggest<Suggestion> {
|
|
private plugin: AutoEmbedPlugin;
|
|
private editor: Editor;
|
|
|
|
constructor(plugin: AutoEmbedPlugin) {
|
|
super(plugin.app);
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
onTrigger(cursor: EditorPosition, editor: Editor, file: TFile | null): EditorSuggestTriggerInfo | null {
|
|
console.log("on trigger");
|
|
this.editor = editor;
|
|
if (!this.plugin.pasteInfo.trigger)
|
|
return null;
|
|
|
|
this.plugin.pasteInfo.trigger = false;
|
|
console.log("Cursor-ch: " + cursor.ch);
|
|
|
|
return {
|
|
start: cursor,
|
|
end: cursor,
|
|
query: this.plugin.pasteInfo.text,
|
|
}
|
|
}
|
|
|
|
getSuggestions(context: EditorSuggestContext): Suggestion[] | Promise<Suggestion[]> {
|
|
console.log("get suggestion");
|
|
return [new Suggestion("Embed Link"), new Suggestion("Cancel")];
|
|
}
|
|
|
|
renderSuggestion(suggestion: Suggestion, el: HTMLElement): void {
|
|
console.log("render suggestion");
|
|
el.setText(suggestion.choice);
|
|
}
|
|
|
|
selectSuggestion(suggestion: Suggestion, e: KeyboardEvent | MouseEvent): void {
|
|
console.log("select suggestion");
|
|
const cursor = this.editor.getCursor();
|
|
if (suggestion.choice === "Embed Link") {
|
|
console.log("Selected create embed");
|
|
const text = this.plugin.pasteInfo.text;
|
|
this.plugin.markToEmbed(
|
|
{
|
|
text: text,
|
|
start: {
|
|
line: cursor.line,
|
|
ch: cursor.ch - text.length
|
|
},
|
|
end: cursor
|
|
}
|
|
,this.editor);
|
|
}
|
|
else
|
|
console.log("select suggestion: " + suggestion.choice);
|
|
|
|
this.close();
|
|
}
|
|
} |