forked from ycnmhd/obsidian-lineage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
129 lines (119 loc) · 4.96 KB
/
Copy pathmain.ts
File metadata and controls
129 lines (119 loc) · 4.96 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
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
import { Plugin, WorkspaceLeaf } from 'obsidian';
import { LINEAGE_VIEW_TYPE, LineageView } from './view/view';
import { createSetViewState } from 'src/obsidian/patches/create-set-view-state';
import { around } from 'monkey-around';
import { settingsReducer } from 'src/stores/settings/settings-reducer';
import { deepMerge } from 'src/helpers/deep-merge';
import { DEFAULT_SETTINGS } from 'src/stores/settings/default-settings';
import { Store } from 'src/lib/store/store';
import {
DocumentsPreferences,
Settings,
} from 'src/stores/settings/settings-type';
import { registerFileMenuEvent } from 'src/obsidian/events/workspace/register-file-menu-event';
import { addCommands } from 'src/obsidian/commands/add-commands';
import { settingsSubscriptions } from 'src/stores/settings/subscriptions/settings-subscriptions';
import { PluginState } from 'src/stores/plugin/plugin-state-type';
import { PluginStoreActions } from 'src/stores/plugin/plugin-store-actions';
import { pluginReducer } from 'src/stores/plugin/plugin-reducer';
import { DefaultPluginState } from 'src/stores/plugin/default-plugin-state';
import { StatusBar } from 'src/obsidian/status-bar/status-bar';
import { onPluginError } from 'src/lib/store/on-plugin-error';
import { customIcons, loadCustomIcons } from 'src/helpers/load-custom-icons';
import { setActiveLeaf } from 'src/obsidian/patches/set-active-leaf';
import { migrateSettings } from 'src/stores/settings/migrations/migrate-settings';
import { toggleFileViewType } from 'src/obsidian/events/workspace/effects/toggle-file-view-type';
import { getActiveFile } from 'src/obsidian/commands/helpers/get-active-file';
import { createLineageDocument } from 'src/obsidian/events/workspace/effects/create-lineage-document';
import { registerFilesMenuEvent } from 'src/obsidian/events/workspace/register-files-menu-event';
import { removeHtmlElementMarkerInPreviewMode } from 'src/obsidian/markdown-post-processors/remove-html-element-marker-in-preview-mode';
import {
minimapWorker,
rulesWorker,
statusBarWorker,
} from 'src/workers/worker-instances';
import { onVaultEvent } from 'src/stores/plugin/subscriptions/on-vault-event';
import { onWorkspaceEvent } from 'src/stores/plugin/subscriptions/on-workspace-event';
import { SettingsActions } from 'src/stores/settings/settings-store-actions';
export type SettingsStore = Store<Settings, SettingsActions>;
export type PluginStore = Store<PluginState, PluginStoreActions>;
export default class Lineage extends Plugin {
settings: SettingsStore;
store: PluginStore;
statusBar: StatusBar;
private timeoutReferences: Set<ReturnType<typeof setTimeout>> = new Set();
viewType: DocumentsPreferences = {};
async onload() {
await this.loadSettings();
this.store = new Store<PluginState, PluginStoreActions>(
DefaultPluginState(),
pluginReducer,
onPluginError,
);
loadCustomIcons();
this.registerView(
LINEAGE_VIEW_TYPE,
(leaf) => new LineageView(leaf, this),
);
addCommands(this);
this.registerPatches();
this.registerEvents();
this.statusBar = new StatusBar(this);
this.loadRibbonIcon();
this.registerMarkdownPostProcessor(
removeHtmlElementMarkerInPreviewMode,
);
}
async saveSettings() {
await this.saveData(this.settings.getValue());
}
async loadSettings() {
const rawSettings = (await this.loadData()) || {};
const settings = deepMerge(rawSettings, DEFAULT_SETTINGS());
migrateSettings(settings);
this.settings = new Store<Settings, SettingsActions>(
settings,
settingsReducer,
onPluginError,
);
this.settings.subscribe(() => {
this.saveSettings();
});
settingsSubscriptions(this);
}
private registerEvents() {
registerFileMenuEvent(this);
registerFilesMenuEvent(this);
onVaultEvent(this);
onWorkspaceEvent(this);
}
registerTimeout(timeout: ReturnType<typeof setTimeout>) {
this.timeoutReferences.add(timeout);
}
private registerPatches() {
this.register(around(this.app.workspace, { setActiveLeaf }));
const setViewState = createSetViewState(this);
// @ts-ignore
this.register(around(WorkspaceLeaf.prototype, { setViewState }));
}
private loadRibbonIcon() {
this.addRibbonIcon(
customIcons.cards.name,
'Toggle Lineage view',
() => {
const file = getActiveFile(this);
if (file) toggleFileViewType(this, file, undefined);
else createLineageDocument(this);
},
);
}
onunload() {
super.onunload();
for (const timeout of this.timeoutReferences) {
clearTimeout(timeout);
}
minimapWorker.terminate();
rulesWorker.terminate();
statusBarWorker.terminate();
}
}