generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paththeme-picker-modal.ts
77 lines (62 loc) · 2.03 KB
/
theme-picker-modal.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
import { App, FuzzySuggestModal, KeymapEventListener } from "obsidian";
export default class ThemePickerPluginModal extends FuzzySuggestModal<string> {
DEFAULT_THEME_KEY = "";
DEFAULT_THEME_TEXT = "None";
initialTheme: string;
previewing = false;
constructor(app: App) {
super(app);
//@ts-ignore
this.bgEl.setAttribute("style", "background-color: transparent");
this.modalEl.classList.add("theme-picker-modal");
//@ts-ignore
const originalArrowUpEvent = this.scope.keys.find((key) => key.key === "ArrowUp");
//@ts-ignore
const originalArrowDownEvent = this.scope.keys.find((key) => key.key === "ArrowDown")
const newFunction = function(originalFunc: KeymapEventListener, modal: ThemePickerPluginModal) {
function newCallback(e: KeyboardEvent) {
originalFunc(e, null);
//@ts-ignore
modal.setTheme(modal.chooser.values[modal.chooser.selectedItem].item);
modal.previewing = true;
}
return newCallback;
}
originalArrowUpEvent.func = newFunction(originalArrowUpEvent.func, this);
originalArrowDownEvent.func = newFunction(originalArrowDownEvent.func, this);
}
onOpen() {
super.onOpen();
//@ts-ignore
this.initialTheme = this.getItems().find(theme => theme === app.customCss.theme)
//@ts-ignore
this.chooser.setSelectedItem(this.getItems().findIndex(theme => theme === app.customCss.theme));
//@ts-ignore
this.chooser.suggestions[this.chooser.selectedItem].scrollIntoViewIfNeeded();
}
onClose() {
super.onClose();
if (this.previewing) {
this.setTheme(this.initialTheme);
}
}
getItems(): any[] {
//@ts-ignore
return [this.DEFAULT_THEME_KEY, ...Object.keys(this.app.customCss.themes), ...this.app.customCss.oldThemes];
}
getItemText(item: any): string {
if (item === this.DEFAULT_THEME_KEY) {
return this.DEFAULT_THEME_TEXT;
} else {
return item;
}
}
onChooseItem(item: any, evt: MouseEvent | KeyboardEvent): void {
this.previewing = false;
this.setTheme(item);
}
setTheme(themeName: string) {
//@ts-ignore
this.app.customCss.setTheme(themeName);
}
}