gnoxnahte_obsidian-auto-embed/suggestEmbed.ts
GnoxNahte db45c00a7e Completed asking user if they want to add embeds after pasting url (Code cleanup needed)
- 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)
2024-03-09 10:18:47 +08:00

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();
}
}