-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tsx
120 lines (105 loc) · 4.3 KB
/
main.tsx
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
import { App, Platform, Plugin, PluginSettingTab, Setting, WorkspaceLeaf } from 'obsidian';
import { createRoot, Root } from "react-dom/client";
import GayToolbar from './src/GayTOOLBAR';
import DEFAULT_SETTINGS from './src/Settings/DEFAULT_SETTINGS';
import { usePlugin, useSettings, useEditor } from './src/StateManagement'
import { GayToolbarSettings } from 'types';
export default class GayToolbarPlugin extends Plugin {
settings: GayToolbarSettings;
toolbarRoot: Root;
toolbarNode: HTMLElement;
unsubscribePositionStore: () => void;
async onload() {
await this.loadSettings()
if (this.settings.mobileOnly && Platform.isDesktop) {
this.addSettingTab(new GayToolbarSettingsTab(this.app, this));
return;
}
this.addCommand({
id: "edit-toolbar",
name: "Toggle edit mode",
callback: () => {
useEditor.setState(prev => {
// drag ops (on android at least) hide keyboard and there's no way around it,
// so this ensures consistency at least
// @ts-ignore Capacitor exists on mobile because Obsidian mobile is built on it
Platform.isMobile && window.Capacitor.Plugins.Keyboard.hide();
return { isEditing: !prev.isEditing }
})
},
});
this.addCommand({
id: "load-default-settings",
name: "Load default settings",
callback: () => {
useSettings.setState(prev => ({ ...DEFAULT_SETTINGS, configs: [...prev.configs] }));
},
});
this.app.workspace.onLayoutReady(() => {
this.toolbarRoot?.unmount?.();
this.toolbarNode?.remove();
document.querySelector('.gay-toolbar-container')?.remove() // not sure why this is sometimes necessary
const parentNode = document.querySelector('.app-container')
if (parentNode) {
this.toolbarNode = createDiv('gay-toolbar-container');
this.toolbarRoot = createRoot(this.toolbarNode);
this.toolbarRoot.render(<GayToolbar />);
parentNode.insertBefore(this.toolbarNode, parentNode.querySelector('.status-bar'));
}
});
}
async saveSettings(newSettings?: GayToolbarSettings | undefined) {
await this.saveData(newSettings || this.settings)
}
async loadSettings() {
// TODO: clean this up
this.settings = await this.loadData()
if (!this.settings) {
this.settings = DEFAULT_SETTINGS;
this.saveSettings(this.settings)
}
usePlugin.setState({ plugin: this });
useSettings.setState(this.settings);
this.unsubscribePositionStore = useSettings.subscribe(state => {
// couldn't I juse use `this`? but then I'd need to bind...?
const plugin = usePlugin.getState().plugin
if (plugin) {
plugin.settings = state;
plugin.saveSettings(plugin.settings)
}
else
throw new Error('plugin undefined in useStore subscription')
});
}
onunload() {
this.toolbarRoot?.unmount?.();
this.toolbarNode?.remove();
document.querySelector('.gay-toolbar-container')?.remove() // not sure why this is sometimes necessary
this.unsubscribePositionStore?.();
}
}
class GayToolbarSettingsTab extends PluginSettingTab {
plugin: GayToolbarPlugin;
constructor(app: App, plugin: GayToolbarPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
// Clear previous settings
containerEl.empty();
containerEl.createEl("h2", { text: "Gay Toolbar Settings" });
// Setting: settingTwo (toggle)
new Setting(containerEl)
.setName("Mobile only")
.setDesc("Restart to apply changes")
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.mobileOnly)
.onChange(async (value) => {
this.plugin.settings.mobileOnly = value;
await this.plugin.saveSettings();
})
);
}
}