generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
90 lines (77 loc) · 1.79 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
import {
Editor,
MarkdownView,
Plugin,
WorkspaceLeaf,
} from "obsidian";
interface DigitalPaperSettings {
enabled: boolean;
}
const DEFAULT_SETTINGS: DigitalPaperSettings = {
enabled: true,
};
let oldValue: string | undefined = undefined;
export default class DigitalPaper extends Plugin {
settings: DigitalPaperSettings;
statusBarElm: HTMLElement;
async onload() {
await this.loadSettings();
this.registerEvent(
this.app.workspace.on(
"editor-change",
(editor: Editor, view: MarkdownView) => {
if (
this.settings.enabled &&
oldValue !== undefined &&
!editor.getValue().startsWith(oldValue)
) {
// user changed existing text, revert to old value
editor.setValue(oldValue);
}
oldValue = editor.getValue();
}
)
);
this.registerEvent(
this.app.workspace.on(
"active-leaf-change",
(leaf: WorkspaceLeaf) => {
if (leaf.view.getViewType() === "markdown") {
oldValue = leaf.view.editor.getValue();
}
}
)
);
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
this.statusBarElm = this.addStatusBarItem();
this.displayModeOnStatusBar();
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: "toggle",
name: "Toggle digital paper mode",
callback: () => {
this.settings.enabled = !this.settings.enabled;
this.displayModeOnStatusBar();
this.saveSettings();
},
});
}
displayModeOnStatusBar() {
if (this.settings.enabled) {
this.statusBarElm.setText("paper");
} else {
this.statusBarElm.setText("");
}
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}