-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinked-articles-modal.ts
More file actions
89 lines (74 loc) · 2.47 KB
/
linked-articles-modal.ts
File metadata and controls
89 lines (74 loc) · 2.47 KB
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
import { Modal, Setting, type App } from 'obsidian';
export interface LinkedArticleOption {
articleId: string;
url: string;
label?: string;
}
export class LinkedArticlesModal extends Modal {
private options: LinkedArticleOption[];
private onSubmit: (selectedArticleIds: string[]) => void;
private onCancel: () => void;
private selectedIds: Set<string>;
private submitted = false;
constructor(
app: App,
options: LinkedArticleOption[],
onSubmit: (selectedArticleIds: string[]) => void,
onCancel: () => void,
) {
super(app);
this.options = options;
this.onSubmit = onSubmit;
this.onCancel = onCancel;
this.selectedIds = new Set(options.map((option) => option.articleId));
}
onOpen() {
const { contentEl } = this;
contentEl.empty();
contentEl.createEl('h2', { text: 'Import linked articles' });
contentEl.createEl('p', { text: 'Select linked articles to import.' });
this.options.forEach((option) => {
new Setting(contentEl)
.setName(this.getOptionName(option))
.setDesc(option.url)
.addToggle((toggle) =>
toggle.setValue(true).onChange((value) => {
if (value) {
this.selectedIds.add(option.articleId);
return;
}
this.selectedIds.delete(option.articleId);
}),
);
});
new Setting(contentEl)
.addButton((button) =>
button
.setButtonText('Import selected')
.setCta()
.onClick(() => {
this.submitted = true;
this.close();
this.onSubmit([...this.selectedIds]);
}),
)
.addButton((button) =>
button.setButtonText('Cancel').onClick(() => {
this.close();
}),
);
}
onClose() {
this.contentEl.empty();
if (!this.submitted) {
this.onCancel();
}
}
private getOptionName(option: LinkedArticleOption) {
const normalizedLabel = option.label?.trim();
if (normalizedLabel) {
return normalizedLabel;
}
return `Article ${option.articleId}`;
}
}