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
162 lines (127 loc) · 4.31 KB
/
main.ts
File metadata and controls
162 lines (127 loc) · 4.31 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface ColorCyclePluginSettings {
textColors: string;
highlightColors: string;
}
const DEFAULT_SETTINGS: ColorCyclePluginSettings = {
textColors: 'black, blue, red, green, null',
highlightColors: 'yellow, cyan, #fa8072, #ccff00, null'
};
// Validate a single CSS color by testing on a dummy element
function isValidCssColor(color: string): boolean {
const s = new Option().style;
s.color = '';
s.color = color;
return !!s.color;
}
export default class ColorCyclePlugin extends Plugin {
settings!: ColorCyclePluginSettings;
async onload() {
await this.loadSettings();
this.addCommand({
id: 'cycle-html-text-color',
name: 'Cycle HTML Text Color on Selection',
editorCallback: (editor: Editor, view: MarkdownView) => {
const rawList = this.settings.textColors.split(',').map(c => c.trim());
const colors = rawList.filter(c => c === 'null' || isValidCssColor(c));
if (colors.length === 0) {
new Notice("No valid text colors configured.");
return;
}
this.cycleColor(editor, 'color', colors);
}
});
this.addCommand({
id: 'cycle-html-highlight-color',
name: 'Cycle HTML Highlight (Background Color) on Selection',
editorCallback: (editor: Editor, view: MarkdownView) => {
const rawList = this.settings.highlightColors.split(',').map(c => c.trim());
const colors = rawList.filter(c => c === 'null' || isValidCssColor(c));
if (colors.length === 0) {
new Notice("No valid highlight colors configured.");
return;
}
this.cycleColor(editor, 'background-color', colors);
}
});
this.addSettingTab(new ColorCycleSettingTab(this.app, this));
}
private cycleColor(editor: Editor, styleType: 'color' | 'background-color', colors: string[]) {
const selection = editor.getSelection();
if (!selection) {
new Notice("Please select some text.");
return;
}
const cursor = editor.getCursor("from");
const regex = new RegExp(`<span\\s+style="${styleType}:\\s*(.+?);?\\s*">([\\s\\S]+?)<\\/span>`);
const match = selection.match(regex);
let newText: string;
let innerText: string;
if (match) {
const currentColor = match[1].trim();
innerText = match[2];
const currentIndex = colors.indexOf(currentColor);
const nextColor = colors[(currentIndex + 1) % colors.length];
newText = nextColor === 'null' ? innerText : `<span style="${styleType}:${nextColor};">${innerText}</span>`;
} else {
innerText = selection;
newText = `<span style="${styleType}:${colors[0]};">${innerText}</span>`;
}
editor.replaceSelection(newText);
const start = cursor;
const end = {
line: start.line,
ch: start.ch + newText.length
};
editor.setSelection(start, end);
}
onunload() {
// clean up if needed
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class ColorCycleSettingTab extends PluginSettingTab {
plugin: ColorCyclePlugin;
constructor(app: App, plugin: ColorCyclePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "HTML Painter Hotkey Settings" });
new Setting(containerEl)
.setName("Text Colors")
.setDesc("Comma-separated list of text colors (e.g., black, red, #00ffcc, null)")
.addTextArea((textArea) => {
textArea
.setPlaceholder("e.g., red, green, #0044ff, null")
.setValue(this.plugin.settings.textColors)
.onChange(async (value: string) => {
this.plugin.settings.textColors = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Highlight Colors")
.setDesc("Comma-separated list of highlight (background) colors (e.g., yellow, #ffff00, null)")
.addTextArea((textArea) => {
textArea
.setPlaceholder("e.g., yellow, cyan, #fa8072, #ccff00, null")
.setValue(this.plugin.settings.highlightColors)
.onChange(async (value: string) => {
this.plugin.settings.highlightColors = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Hotkeys")
.setDesc("Assign hotkeys under Settings → Hotkeys → Search 'HTML Painter'")
.setDisabled(true);
}
}