generated from dartungar/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.ts
109 lines (90 loc) · 3.46 KB
/
main.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
import { Editor, Notice, Plugin } from 'obsidian';
import { ElementCategory } from 'src/core/ElementCategory';
import { MermaidElementService } from 'src/core/elementService';
import { TextEditorService } from 'src/core/textEditorService';
import { c4DiagramElements } from 'src/elements/c4Diagram';
import { mindMapElements } from 'src/elements/mindMap';
import { quadrantElements } from 'src/elements/quadrant';
import { timelineElements } from 'src/elements/timeline';
import { MermaidPluginSettings } from 'src/settings/settings';
import { addTridentIcon } from 'src/trident-icon';
import { MermaidToolsSettingsTab } from 'src/ui/settingsTab';
import { MermaidToolbarView } from 'src/ui/toolbarView/mermaidToolbarView';
export const TRIDENT_ICON_NAME = "trident-custom";
export default class MermaidPlugin extends Plugin {
settings: MermaidPluginSettings;
private activeEditor: Editor;
public _mermaidElementService = new MermaidElementService();
private _textEditorService = new TextEditorService();
async onload(): Promise<void> {
await this.loadSettings();
addTridentIcon();
this.registerView(
MermaidToolbarView.VIEW_TYPE,
(leaf) => new MermaidToolbarView(leaf, this)
);
// keep track of last active editor
// cannot simply call this.app.workspace.activeEditor ad hoc
// because Editor will be null when using Mermaid toolbar view
this.app.workspace.on('active-leaf-change', (leaf) => {
this.activeEditor = this.app.workspace.activeEditor?.editor ?? this.activeEditor;
});
this.addRibbonIcon(TRIDENT_ICON_NAME, "Open Mermaid Toolbar", () => {
this.activateView();
});
this.addCommand({
id: "open-toolbar",
name: "Open Toolbar View",
callback: () => {
this.activateView();
},
});
this.addSettingTab(new MermaidToolsSettingsTab(this.app, this));
}
async onunload(): Promise<void> {
this.app.workspace.detachLeavesOfType(MermaidToolbarView.VIEW_TYPE);
}
async loadSettings() {
this.settings = Object.assign(
{},
MermaidPluginSettings.DefaultSettings(),
await this.loadData()
);
this.addNewCategories();
}
addNewCategories() {
if (!this.settings.elements.some(x => x.category === ElementCategory.Mindmap)) {
this.settings.elements.push(...mindMapElements);
console.log("[Mermaid Tools] added Mindmap elements");
};
if (!this.settings.elements.some(x => x.category === ElementCategory.Timeline)) {
this.settings.elements.push(...timelineElements);
console.log("[Mermaid Tools] added Timeline elements");
};
if (!this.settings.elements.some(x => x.category === ElementCategory.QuadrantChart)) {
this.settings.elements.push(...quadrantElements);
console.log("[Mermaid Tools] added QuadrantChart elements");
};
if (!this.settings.elements.some(x => x.category === ElementCategory.C4Diagram)) {
this.settings.elements.push(...c4DiagramElements);
console.log("[Mermaid Tools] added C4 diagram elements");
};
}
async saveSettings() {
await this.saveData(this.settings);
await this.activateView();
}
async activateView() {
this.app.workspace.detachLeavesOfType(MermaidToolbarView.VIEW_TYPE);
await this.app.workspace.getRightLeaf(false).setViewState({
type: MermaidToolbarView.VIEW_TYPE,
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(MermaidToolbarView.VIEW_TYPE)[0]
);
}
public insertTextAtCursor(text: string) {
this._textEditorService.insertTextAtCursor(this.activeEditor, text);
}
}