generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tsx
117 lines (99 loc) · 2.81 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
import { Editor, MarkdownView, Notice, Plugin, WorkspaceLeaf } from "obsidian";
import * as React from "react";
import { createRoot } from "react-dom/client";
import { Widget } from "./src/Widget";
import ObsidianWidgetsCommandModal from "src/CommandModal";
import { VIEW_TYPE, WidgetView } from "src/WidgetView";
import { WidgetSettings } from "src/types/Widgets";
import { DataJson, HelperFunctions } from "src/types/HelperFunctions";
export default class ObsidianWidgets extends Plugin {
async onload() {
// Adds command
// =====================
this.addCommand({
id: "add-widget",
name: "Add widget",
editorCallback: (editor: Editor, view: MarkdownView) => {
new ObsidianWidgetsCommandModal(this.app, editor).open();
},
});
this.addCommand({
id: "add-widget-view",
name: "Open widget view",
editorCallback: () => {
this.activateView();
},
});
// Register view
// =====================
this.registerView(
VIEW_TYPE,
(leaf: WorkspaceLeaf) =>
new WidgetView(leaf, this.getHelperFunctions())
);
// Adds sidebar icon
// =====================
this.addRibbonIcon("cuboid", "Add widget", () => {
const editor =
this.app.workspace.getActiveViewOfType(MarkdownView)?.editor;
if (editor) {
new ObsidianWidgetsCommandModal(this.app, editor).open();
} else {
new Notice(
"Widgets: No cursor placement found. Please place your cursor in your note where you like the widget to be inserted."
);
}
});
// The meat and the potatoes
// =====================
this.registerMarkdownCodeBlockProcessor(
"widgets",
(source, el, ctx) => {
// @ts-ignore
const options = {} as WidgetSettings;
source
.split("\n")
.map((option: string) =>
option.split(/:(.*)/).map((option) => option.trim())
)
.forEach((optionSet) => {
// @ts-ignore
options[optionSet[0]] = optionSet[1];
});
const root = createRoot(el);
// @ts-ignore
root.render(
<Widget
settings={options}
helperFunctions={this.getHelperFunctions()}
leafId=""
/>
);
}
);
}
async onunload() {}
async activateView() {
const { workspace } = this.app;
const leaf: WorkspaceLeaf | null = workspace.getLeftLeaf(true);
await leaf.setViewState({ type: VIEW_TYPE, active: true });
// "Reveal" the leaf in case it is in a collapsed sidebar
workspace.revealLeaf(leaf);
}
writeToDataJson(data: DataJson) {
this.saveData(data);
}
readFromDataJson(): Promise<DataJson> {
return this.loadData();
}
getCurrentOpenFile() {
return this.app.workspace.getActiveFile();
}
getHelperFunctions(): HelperFunctions {
return {
writeToDataJson: this.writeToDataJson.bind(this),
readFromDataJson: this.readFromDataJson.bind(this),
getCurrentOpenFile: this.getCurrentOpenFile.bind(this),
};
}
}