-
Notifications
You must be signed in to change notification settings - Fork 13
/
suggest.ts
110 lines (103 loc) · 2.36 KB
/
suggest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import {
App,
Editor,
EditorPosition,
EditorSuggest,
EditorSuggestContext,
EditorSuggestTriggerInfo,
MarkdownView,
TFile,
} from 'obsidian';
import type ObsidianLinkEmbedPlugin from 'main';
interface IDateCompletion {
choice: string;
}
export default class EmbedSuggest extends EditorSuggest<IDateCompletion> {
private plugin: ObsidianLinkEmbedPlugin;
private editor: Editor;
private cursor: EditorPosition;
constructor(app: App, plugin: ObsidianLinkEmbedPlugin) {
super(app);
this.app = app;
this.plugin = plugin;
}
getSuggestions(context: EditorSuggestContext): IDateCompletion[] {
// catch-all if there are no matches
if (this.plugin.settings.rmDismiss) {
return [{ choice: 'Create Embed' }];
}
return [{ choice: 'Dismiss' }, { choice: 'Create Embed' }];
}
renderSuggestion(suggestion: IDateCompletion, el: HTMLElement): void {
el.setText(suggestion.choice);
}
selectSuggestion(
suggestion: IDateCompletion,
event: KeyboardEvent | MouseEvent,
): void {
if (suggestion.choice == 'Create Embed') {
const cursor = this.editor.getCursor();
this.plugin.embedUrl(
this.editor,
{
can: true,
text: this.plugin.pasteInfo.text,
boundary: {
start: {
line: cursor.line,
ch: cursor.ch - this.plugin.pasteInfo.text.length,
},
end: cursor,
},
},
[this.plugin.settings.primary, this.plugin.settings.backup],
true,
);
}
this.close();
}
onTrigger(
cursor: EditorPosition,
editor: Editor,
file: TFile,
): EditorSuggestTriggerInfo {
if (!this.plugin.pasteInfo.trigger) {
return null;
}
this.plugin.pasteInfo.trigger = false;
this.editor = editor;
this.cursor = cursor;
if (this.plugin.settings.autoEmbedWhenEmpty) {
const cursor = this.editor.getCursor();
if (cursor.ch - this.plugin.pasteInfo.text.length == 0) {
this.plugin.embedUrl(
this.editor,
{
can: true,
text: this.plugin.pasteInfo.text,
boundary: {
start: {
line: cursor.line,
ch:
cursor.ch -
this.plugin.pasteInfo.text.length,
},
end: cursor,
},
},
[this.plugin.settings.primary, this.plugin.settings.backup],
true,
);
return null;
}
}
if (!this.plugin.settings.popup) {
return null;
}
return {
start: cursor,
end: cursor,
query: this.plugin.pasteInfo.text,
};
}
}