generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
103 lines (86 loc) · 2.01 KB
/
main.ts
File metadata and controls
103 lines (86 loc) · 2.01 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
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
import {
App,
Command,
Editor,
MarkdownFileInfo,
MarkdownView,
Plugin,
PluginSettingTab,
Setting,
} from "obsidian";
import { v4 as uuidv4 } from "uuid";
interface UUIDGeneratorPluginSettings {
enableRepeat: boolean;
}
const DEFAULT_SETTINGS: UUIDGeneratorPluginSettings = {
enableRepeat: false,
};
export default class UUIDGenerator extends Plugin {
settings: UUIDGeneratorPluginSettings;
repeatCommand: Command;
lastUUID: string;
async onload() {
await this.loadSettings();
this.addCommand({
id: "generate-uuid-v4",
name: "Generate UUID at Cursor",
editorCallback: (editor: Editor, view: MarkdownView) => {
this.lastUUID = uuidv4();
editor.replaceSelection(this.lastUUID);
},
});
this.addCommand({
id: "repeat-uuid-v4",
name: "Repeat UUID at Cursor",
editorCheckCallback: (
checking: boolean,
editor: Editor,
ctx: MarkdownView | MarkdownFileInfo
): boolean | void => {
if (this.lastUUID == undefined) {
return false;
}
if (checking) {
return this.settings.enableRepeat;
}
editor.replaceSelection(this.lastUUID);
},
});
this.addSettingTab(new UUIDGeneratorSettingTab(this.app, this));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class UUIDGeneratorSettingTab extends PluginSettingTab {
plugin: UUIDGenerator;
constructor(app: App, plugin: UUIDGenerator) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Enable Repeat UUID at Cursor")
.setDesc(
"Enables/disables the 'Repeat UUID at Cursor' command to prevent confusion"
)
.addToggle((Boolean) =>
Boolean.setValue(this.plugin.settings.enableRepeat).onChange(
async (value) => {
this.plugin.settings.enableRepeat = value;
await this.plugin.saveSettings();
}
)
);
}
}