generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreview-notes-view.ts
186 lines (152 loc) · 4.54 KB
/
review-notes-view.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
import { icons } from "icons";
import RecentNotesPlugin from "main";
import { ItemView, WorkspaceLeaf, getIcon, Notice } from "obsidian";
import { RecentNotes, File } from "recent-notes-interface";
export const VIEW_TYPE_REVIEW_NOTES = "review-notes";
type SortOrder = "ascending" | "descending";
export class ReviewNotesView extends ItemView {
private plugin: RecentNotesPlugin;
private recentNotes: RecentNotes;
constructor(
leaf: WorkspaceLeaf,
recentNotes: RecentNotes,
plugin: RecentNotesPlugin
) {
super(leaf);
this.recentNotes = recentNotes;
this.plugin = plugin;
}
getViewType() {
return VIEW_TYPE_REVIEW_NOTES;
}
getDisplayText() {
return "Review Notes";
}
onload(): void {}
removeExtension = (filename: string): string => {
return filename.replace(/\.[^/.]+$/, "");
};
sortFiles = (files: File[], order: SortOrder): File[] => {
const collator = new Intl.Collator(undefined, { numeric: true });
return files.sort((a: File, b: File) => {
const aWithoutExtension = this.removeExtension(a.filename);
const bWithoutExtension = this.removeExtension(b.filename);
if (order === "ascending") {
return collator.compare(aWithoutExtension, bWithoutExtension);
} else {
return collator.compare(bWithoutExtension, aWithoutExtension);
}
});
};
CustomElement = (svgText: string): HTMLElement => {
const parser = new DOMParser();
return parser.parseFromString(svgText, "text/xml").documentElement;
};
async onOpen() {
const container = this.containerEl.children[1];
container.empty();
container.createEl("h2", { text: "Review Notes" });
const rootEl = createDiv({ cls: "recent-notes" });
const childrenEl = rootEl.createDiv({ cls: "" });
const navHeader = childrenEl.createDiv({ cls: "nav-header" });
const navButtons = navHeader.createDiv({
cls: "nav-buttons-container",
});
const navButtonSortAsc = navButtons.createDiv({
cls: "clickable-icon nav-action-button",
attr: {
"aria-label": "Sort by file name (A to Z)",
},
});
navButtonSortAsc.addEventListener(
"click",
async (event: MouseEvent) => {
this.sortFiles(this.recentNotes.files, "ascending");
await this.plugin.saveRecentNotes();
}
);
const navButtonSortDesc = navButtons.createDiv({
cls: "clickable-icon nav-action-button",
attr: {
"aria-label": "Sort by file name (Z to A)",
},
});
navButtonSortDesc.addEventListener(
"click",
async (event: MouseEvent) => {
this.sortFiles(this.recentNotes.files, "descending");
await this.plugin.saveRecentNotes();
}
);
const navButtonRemoveAll = navButtons.createDiv({
cls: "clickable-icon nav-action-button",
attr: {
"aria-label": "Clear all recently modified notes",
},
});
navButtonRemoveAll.addEventListener(
"click",
async (event: MouseEvent) => {
this.recentNotes.files = [];
await this.plugin.saveRecentNotes();
new Notice(
"All recently modified notes have been removed from the list"
);
}
);
navButtonSortAsc.appendChild(this.CustomElement(icons["sortAsc"]));
navButtonSortDesc.appendChild(this.CustomElement(icons["sortDesc"]));
navButtonRemoveAll.appendChild(getIcon("lucide-x")!);
this.recentNotes.files.forEach((file) => {
const navFile = childrenEl.createDiv({
cls: "nav-file",
});
const navFileTitle = navFile.createDiv({
cls: "nav-file-title recent-notes-title",
});
const navFileTitleContent = navFileTitle.createDiv({
cls: "nav-file-title-content recent-notes-content",
});
navFileTitleContent.setText(file.filename);
// navFile.addEventListener("mouseover", (ev: MouseEvent) => {
// // TODO: preview file path
// });
navFileTitleContent.addEventListener(
"click",
(event: MouseEvent) => {
const _file = this.app.vault
.getFiles()
.find((f) => f.path === file.path);
if (_file) {
const leaf = this.app.workspace.getLeaf(false);
if (leaf) {
leaf.openFile(_file);
}
} else {
new Notice(
"File not found! It may have been deleted or its path modified."
);
}
}
);
const removeButton = navFileTitle.createDiv({
cls: "recent-notes-check",
});
removeButton.appendChild(getIcon("lucide-check")!);
removeButton.addEventListener("click", async () => {
this.recentNotes.files.splice(
this.recentNotes.files.findIndex(
(item) => item.path === file.path
),
1
);
await this.plugin.saveRecentNotes();
this.onOpen();
});
});
container.appendChild(rootEl);
}
async onClose() {
// Nothing to clean up.
}
}