generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
107 lines (90 loc) · 3.04 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
import { App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface RemoveSTXSettings {
hideIfNoSTX: boolean;
}
const DEFAULT_SETTINGS: RemoveSTXSettings = {
hideIfNoSTX: true,
}
export default class RemoveSTX extends Plugin {
settings: RemoveSTXSettings;
stxChar = new RegExp("\x02", "g");
async onload() {
await this.loadSettings();
this.addRibbonIcon('x-circle', 'Remove Start-of-Text (STX)', (evt: MouseEvent) => {
// TODO: Add another view type because this only works in editing mode. It should work in preview mode, too.
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
const editor = markdownView.editor;
const wholeText = editor.getValue();
const stxCount = (wholeText.match(this.stxChar) || []).length;
if (stxCount === 0) {
new Notice('No STX characters found.');
} else {
const newText = wholeText.replace(this.stxChar, '');
editor.setValue(newText);
new Notice(`Removed ${stxCount} STX characters.`);
}
} else {
new Notice('No active markdown editor.');
}
});
const statusBarElement = this.addStatusBarItem();
this.updateStatusBar(statusBarElement);
this.registerEvent(this.app.workspace.on('editor-change', (editor: Editor) => {
this.updateStatusBar(statusBarElement);
}));
this.addCommand({
id: 'remove-start-of-text-editor',
name: 'Remove start-of-text character',
editorCallback: (editor: Editor) => {
const wholeText = editor.getValue();
const newText = wholeText.replace(this.stxChar, '');
editor.setValue(newText);
}
});
this.addSettingTab(new RemoveSTXSettings(this.app, this));
}
updateStatusBar(statusBarElement: HTMLElement) {
// TODO: This only updates when the editor changes.
// TODO: Problem is that the editor doesn't change when Obsidian opens a new file.
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
const wholeText = markdownView.editor.getValue();
const stxCount = (wholeText.match(this.stxChar) || []).length;
if (stxCount === 0 && this.settings.hideIfNoSTX) {
statusBarElement.setText('');
} else {
statusBarElement.setText(`${stxCount.toString()} STX`);
}
} // TODO: else?
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class RemoveSTXSettings extends PluginSettingTab {
plugin: RemoveSTX;
constructor(app: App, plugin: RemoveSTX) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Hide If No STX')
.setDesc('Hide the status bar item if there are no STX characters in the current file.')
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.hideIfNoSTX);
toggle.onChange(async (value) => {
this.plugin.settings.hideIfNoSTX = value;
await this.plugin.saveSettings();
});
});
}
}