-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.ts
More file actions
75 lines (62 loc) · 1.44 KB
/
Copy pathmain.ts
File metadata and controls
75 lines (62 loc) · 1.44 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
import { Plugin, ItemView, WorkspaceLeaf } from "obsidian";
import WordCraftComponent from "./WordCraftView.svelte";
import { mount, unmount } from "svelte";
export const VIEW_TYPE_WORDCRAFT = "wordcraft-view";
export default class WordCraft extends Plugin {
async onload() {
this.registerView(
VIEW_TYPE_WORDCRAFT,
(leaf) => new WordCraftView(leaf)
);
this.addRibbonIcon("book-open-text", "Open WordCraft", () => {
this.activateView();
});
this.addCommand({
id: "open-view",
name: "Open view",
callback: () => {
this.activateView();
},
});
}
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_WORDCRAFT);
if (leaves.length > 0) {
leaf = leaves[0];
} else {
leaf = workspace.getRightLeaf(false);
await leaf?.setViewState({
type: VIEW_TYPE_WORDCRAFT,
active: true,
});
}
if (leaf) {
workspace.revealLeaf(leaf);
}
}
}
export class WordCraftView extends ItemView {
component: ReturnType<typeof WordCraftComponent> | undefined;
constructor(leaf: WorkspaceLeaf) {
super(leaf);
this.icon = "book-open-text";
}
getViewType() {
return VIEW_TYPE_WORDCRAFT;
}
getDisplayText() {
return "WordCraft";
}
async onOpen() {
this.component = mount(WordCraftComponent, {
target: this.contentEl,
});
}
async onClose() {
if (this.component) {
unmount(this.component);
}
}
}