-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tsx
108 lines (85 loc) · 2.1 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
import { ItemView, Plugin, TFile, WorkspaceLeaf } from "obsidian";
import { Root, createRoot } from "react-dom/client";
import CanvasSketch from "./components/CanvasSketch";
import { AppContext } from "context";
const EXTENSIONS = ["jpg", "jpeg", "png", "gif", "bmp"];
let state: { file: string | null } = {
file: null,
};
interface SimpleSketchSettings {
mySetting: string;
}
export const SKETCH_VIEW_TYPE = "sketch-canvas";
export default class SimpleSketch extends Plugin {
settings: SimpleSketchSettings;
async onload() {
this.registerView(SKETCH_VIEW_TYPE, (leaf) => new Sketch(leaf));
this.addRibbonIcon("pencil-ruler", "Simple Sketch", () => {
state = { file: null };
this.activateView();
});
this.app.workspace.on("file-menu", (menu, file) => {
if (
file instanceof TFile &&
file.extension !== null &&
!EXTENSIONS.includes(file.extension)
)
return;
menu.addItem((item) => {
item.setTitle("open with simple sketch")
.setIcon("pencil-ruler")
.onClick(() => {
this.activateView(file.path);
});
});
});
}
onunload() {}
async activateView(filePath?: string) {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = workspace.getLeaf(false);
if (!leaf) {
leaf = workspace.getLeaf(true);
}
if (filePath) {
state = { file: filePath };
}
await leaf.setViewState({
type: SKETCH_VIEW_TYPE,
active: true,
});
workspace.revealLeaf(leaf);
}
}
class Sketch extends ItemView {
root: Root | null = null;
viewState = this.leaf.getViewState().state;
constructor(leaf: WorkspaceLeaf) {
super(leaf);
}
getViewType() {
return SKETCH_VIEW_TYPE;
}
getDisplayText() {
return "Simple Sketch";
}
getIcon() {
return "pencil-ruler";
}
getState() {
return state.file;
}
async onOpen() {
this.root = createRoot(this.containerEl.children[1]);
const viewState = this.leaf.getViewState();
const filePath = viewState.state;
this.root.render(
<AppContext.Provider value={this.app}>
<CanvasSketch filePath={filePath} />
</AppContext.Provider>
);
}
async onClose() {
this.root?.unmount();
}
}