-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
139 lines (123 loc) · 4.54 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { App, Editor, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
// Extend the App type to include the commands property
declare module 'obsidian' {
interface App {
commands: {
removeCommand(id: string): void;
commands: { [key: string]: any };
};
}
}
interface QuickTag {
id: number;
name: string;
prefix: string;
suffix: string;
}
interface QuickWrapperSettings {
quickTags: QuickTag[];
nextId: number;
}
const DEFAULT_SETTINGS: QuickWrapperSettings = {
quickTags: [],
nextId: 1
}
export default class QuickWrapperPlugin extends Plugin {
settings: QuickWrapperSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new QuickWrapperSettingsTab(this.app, this));
this.registerCommands();
// Refresh commands when layout changes
this.registerEvent(
this.app.workspace.on('layout-change', () => this.registerCommands())
);
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
this.registerCommands();
this.app.workspace.trigger('layout-change');
}
private registerCommands() {
// Remove existing commands - note that commands will persist until plugin
// is deactivated or Obsidian is restarted
Object.keys(this.app.commands.commands)
.filter(id => id.startsWith('quick-wrapper:wrap-with-'))
.forEach(id => this.app.commands.removeCommand(id));
// Add new commands
this.settings.quickTags.forEach(tag => {
if (!tag.name || !tag.prefix || !tag.suffix) return;
this.addCommand({
id: `quick-wrapper:wrap-with-${tag.id}`,
name: `Wrap with ${tag.name}`,
editorCallback: (editor: Editor) => {
const selectedText = editor.getSelection();
if (!selectedText) {
new Notice('Please select some text first');
return;
}
editor.replaceSelection(`${tag.prefix}${selectedText}${tag.suffix}`);
},
});
});
}
}
class QuickWrapperSettingsTab extends PluginSettingTab {
constructor(app: App, private plugin: QuickWrapperPlugin) {
super(app, plugin);
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Define Quick Tags' });
this.plugin.settings.quickTags.forEach((tag, index) => {
new Setting(containerEl)
.setName(`Quick Tag ${index + 1}`)
.addText(text => text
.setPlaceholder('Name (e.g., kbd)')
.setValue(tag.name)
.onChange(async value => {
tag.name = value;
await this.plugin.saveSettings();
}))
.addText(text => text
.setPlaceholder('Prefix (e.g., <kbd>)')
.setValue(tag.prefix)
.onChange(async value => {
tag.prefix = value;
await this.plugin.saveSettings();
}))
.addText(text => text
.setPlaceholder('Suffix (e.g., </kbd>)')
.setValue(tag.suffix)
.onChange(async value => {
tag.suffix = value;
await this.plugin.saveSettings();
}))
.addButton(button => button
.setIcon('trash')
.onClick(async () => {
this.plugin.settings.quickTags.splice(index, 1);
await this.plugin.saveSettings();
this.display();
}));
});
new Setting(containerEl)
.setName('Add Quick Tag')
.addButton(button => button
.setButtonText('Add')
.onClick(async () => {
this.plugin.settings.quickTags.push({
id: this.plugin.settings.nextId++,
name: '',
prefix: '',
suffix: ''
});
await this.plugin.saveSettings();
this.display();
}));
}
}