-
Notifications
You must be signed in to change notification settings - Fork 83
/
modals.ts
280 lines (240 loc) · 7.92 KB
/
modals.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import {
App,
EventRef,
FuzzyMatch,
FuzzySuggestModal,
Notice,
renderMatches,
SearchMatches,
SearchMatchPart,
} from 'obsidian';
import CitationPlugin from './main';
import { Entry } from './types';
// Stub some methods we know are there..
interface FuzzySuggestModalExt<T> extends FuzzySuggestModal<T> {
chooser: ChooserExt;
}
interface ChooserExt {
useSelectedItem(evt: MouseEvent | KeyboardEvent): void;
}
class SearchModal extends FuzzySuggestModal<Entry> {
plugin: CitationPlugin;
limit = 50;
loadingEl: HTMLElement;
eventRefs: EventRef[];
constructor(app: App, plugin: CitationPlugin) {
super(app);
this.plugin = plugin;
this.resultContainerEl.addClass('zoteroModalResults');
this.inputEl.setAttribute('spellcheck', 'false');
this.loadingEl = this.resultContainerEl.parentElement.createEl('div', {
cls: 'zoteroModalLoading',
});
this.loadingEl.createEl('div', { cls: 'zoteroModalLoadingAnimation' });
this.loadingEl.createEl('p', {
text: 'Loading citation database. Please wait...',
});
}
onOpen() {
super.onOpen();
this.eventRefs = [
this.plugin.events.on('library-load-start', () => {
this.setLoading(true);
}),
this.plugin.events.on('library-load-complete', () => {
this.setLoading(false);
}),
];
this.setLoading(this.plugin.isLibraryLoading);
// Don't immediately register keyevent listeners. If the modal was triggered
// by an "Enter" keystroke (e.g. via the Obsidian command dialog), this event
// will be received here erroneously.
setTimeout(() => {
this.inputEl.addEventListener('keydown', (ev) => this.onInputKeydown(ev));
this.inputEl.addEventListener('keyup', (ev) => this.onInputKeyup(ev));
}, 200);
}
onClose() {
this.eventRefs?.forEach((e) => this.plugin.events.offref(e));
}
getItems(): Entry[] {
if (this.plugin.isLibraryLoading) {
return [];
}
return Object.values(this.plugin.library.entries);
}
getItemText(item: Entry): string {
return `${item.title} ${item.authorString} ${item.year}`;
}
setLoading(loading: boolean): void {
if (loading) {
this.loadingEl.removeClass('d-none');
this.inputEl.disabled = true;
this.resultContainerEl.empty();
} else {
this.loadingEl.addClass('d-none');
this.inputEl.disabled = false;
this.inputEl.focus();
// @ts-ignore: not exposed in API.
this.updateSuggestions();
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onChooseItem(item: Entry, evt: MouseEvent | KeyboardEvent): void {
this.plugin.openLiteratureNote(item.id, false).catch(console.error);
}
renderSuggestion(match: FuzzyMatch<Entry>, el: HTMLElement): void {
el.empty();
const entry = match.item;
const entryTitle = entry.title || '';
const container = el.createEl('div', { cls: 'zoteroResult' });
const titleEl = container.createEl('span', {
cls: 'zoteroTitle',
});
container.createEl('span', { cls: 'zoteroCitekey', text: entry.id });
const authorsCls = entry.authorString
? 'zoteroAuthors'
: 'zoteroAuthors zoteroAuthorsEmpty';
const authorsEl = container.createEl('span', {
cls: authorsCls,
});
// Prepare to highlight string matches for each part of the search item.
// Compute offsets of each rendered element's content within the string
// returned by `getItemText`.
const allMatches = match.match.matches;
const authorStringOffset = 1 + entryTitle.length;
// Filter a match list to contain only the relevant matches for a given
// substring, and with match indices shifted relative to the start of that
// substring
const shiftMatches = (
matches: SearchMatches,
start: number,
end: number,
) => {
return matches
.map((match: SearchMatchPart) => {
const [matchStart, matchEnd] = match;
return [
matchStart - start,
Math.min(matchEnd - start, end),
] as SearchMatchPart;
})
.filter((match: SearchMatchPart) => {
const [matchStart, matchEnd] = match;
return matchStart >= 0;
});
};
// Now highlight matched strings within each element
renderMatches(
titleEl,
entryTitle,
shiftMatches(allMatches, 0, entryTitle.length),
);
if (entry.authorString) {
renderMatches(
authorsEl,
entry.authorString,
shiftMatches(
allMatches,
authorStringOffset,
authorStringOffset + entry.authorString.length,
),
);
}
}
onInputKeydown(ev: KeyboardEvent) {
if (ev.key == 'Tab') {
ev.preventDefault();
}
}
onInputKeyup(ev: KeyboardEvent) {
if (ev.key == 'Enter' || ev.key == 'Tab') {
((this as unknown) as FuzzySuggestModalExt<Entry>).chooser.useSelectedItem(
ev,
);
}
}
}
export class OpenNoteModal extends SearchModal {
constructor(app: App, plugin: CitationPlugin) {
super(app, plugin);
this.setInstructions([
{ command: '↑↓', purpose: 'to navigate' },
{ command: '↵', purpose: 'to open literature note' },
{ command: 'ctrl ↵', purpose: 'to open literature note in a new pane' },
{ command: 'tab', purpose: 'open in Zotero' },
{ command: 'shift tab', purpose: 'open PDF' },
{ command: 'esc', purpose: 'to dismiss' },
]);
}
onChooseItem(item: Entry, evt: MouseEvent | KeyboardEvent): void {
if (evt instanceof MouseEvent || evt.key == 'Enter') {
const newPane =
evt instanceof KeyboardEvent && (evt as KeyboardEvent).ctrlKey;
this.plugin.openLiteratureNote(item.id, newPane);
} else if (evt.key == 'Tab') {
if (evt.shiftKey) {
const files = item.files || [];
const pdfPaths = files.filter((path) =>
path.toLowerCase().endsWith('pdf'),
);
if (pdfPaths.length == 0) {
new Notice('This reference has no associated PDF files.');
} else {
open(`file://${pdfPaths[0]}`);
}
} else {
open(item.zoteroSelectURI);
}
}
}
}
export class InsertNoteLinkModal extends SearchModal {
constructor(app: App, plugin: CitationPlugin) {
super(app, plugin);
this.setInstructions([
{ command: '↑↓', purpose: 'to navigate' },
{ command: '↵', purpose: 'to insert literature note reference' },
{ command: 'esc', purpose: 'to dismiss' },
]);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onChooseItem(item: Entry, evt: unknown): void {
this.plugin.insertLiteratureNoteLink(item.id).catch(console.error);
}
}
export class InsertNoteContentModal extends SearchModal {
constructor(app: App, plugin: CitationPlugin) {
super(app, plugin);
this.setInstructions([
{ command: '↑↓', purpose: 'to navigate' },
{
command: '↵',
purpose: 'to insert literature note content in active pane',
},
{ command: 'esc', purpose: 'to dismiss' },
]);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onChooseItem(item: Entry, evt: unknown): void {
this.plugin.insertLiteratureNoteContent(item.id).catch(console.error);
}
}
export class InsertCitationModal extends SearchModal {
constructor(app: App, plugin: CitationPlugin) {
super(app, plugin);
this.setInstructions([
{ command: '↑↓', purpose: 'to navigate' },
{ command: '↵', purpose: 'to insert Markdown citation' },
{ command: 'shift ↵', purpose: 'to insert secondary Markdown citation' },
{ command: 'esc', purpose: 'to dismiss' },
]);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onChooseItem(item: Entry, evt: MouseEvent | KeyboardEvent): void {
const isAlternative = evt instanceof KeyboardEvent && evt.shiftKey;
this.plugin
.insertMarkdownCitation(item.id, isAlternative)
.catch(console.error);
}
}