Skip to content

Commit 01612af

Browse files
authored
Upload version 0.0.5
1 parent 28cd2f4 commit 01612af

File tree

4 files changed

+112
-5
lines changed

4 files changed

+112
-5
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "colorizelt",
33
"name": "Colorizelt",
4-
"version": "0.0.4",
4+
"version": "0.0.5",
55
"minAppVersion": "0.12.0",
66
"description": "Easy color and clear selected text",
77
"author": "Artsem Holub (WiNE-iNEFF)",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "colorizelt",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Easy color and clear selected text",
55
"main": "main.js",
66
"scripts": {

src/main.ts

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
import { Editor, Plugin, Notice, App, addIcon } from 'obsidian';
1+
import { Editor, Plugin, Notice, App, addIcon, setIcon, PluginSettingTab, Setting } from 'obsidian';
2+
3+
interface ColorizeltSetting {
4+
id: string;
5+
name: string;
6+
color: string;
7+
}
8+
9+
const DEFAULT_SETTINGS: ColorizeltSetting[] = [
10+
{id: "purple", name: "purple", color: "#7C00FE"},
11+
{id: "lightGreen", name: "light green", color: "#06D001"},
12+
];
213

314
export default class Colorizelt extends Plugin {
15+
settings: ColorizeltSetting[] = [];
416
async onload() {
517
addIcons()
6-
new Notice('Plugin "Colorizelt" v0.0.4 load success!');
18+
await this.loadSettings();
19+
20+
this.addSettingTab(new ColorizeltSettingTab(this.app, this));
721

822
//red color
923
this.addCommand({
@@ -108,6 +122,44 @@ export default class Colorizelt extends Plugin {
108122
}
109123
});
110124

125+
this.createButtonCommands();
126+
}
127+
128+
async loadSettings() {
129+
//await this.saveData(DEFAULT_SETTINGS);
130+
let LoadData = await this.loadData();
131+
this.settings = Array.isArray(LoadData) ? LoadData : DEFAULT_SETTINGS;
132+
}
133+
134+
async saveSettings() {
135+
await this.saveData(this.settings);
136+
this.createButtonCommands();
137+
}
138+
139+
createButtonCommands() {
140+
this.settings.forEach((button) => {
141+
addIcon(`colorizelt-pen-${button.name.toLowerCase().replace(/\s+/g, '-')}`, `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-pen-line"><path d="M12 20h9" stroke="${button.color}"/><path d="M16.376 3.622a1 1 0 0 1 3.002 3.002L7.368 18.635a2 2 0 0 1-.855.506l-2.872.838a.5.5 0 0 1-.62-.62l.838-2.872a2 2 0 0 1 .506-.854z"/></svg>`)
142+
this.addCommand({
143+
id: `color-text-${button.id}`,
144+
name: `${button.name.toLowerCase().replace(/\s+/g, '-')}`,
145+
icon: `colorizelt-pen-${button.name.toLowerCase().replace(/\s+/g, '-')}`,
146+
editorCallback: (editor: Editor) => {
147+
let selection = editor.getSelection();
148+
const replaced = `<span style="color: ${button.color.toLowerCase().replace(/\s+/g, '-')}">${selection}</span>`
149+
const regex = new RegExp(`<span style="color: ${button.color.toLowerCase().replace(/\s+/g, '-')}">(.*?)<\/span>`, 'g');
150+
let matches = Array.from(selection.matchAll(regex));
151+
152+
if (matches.length > 0) {
153+
matches.forEach((match) => {
154+
selection = selection.replace(match[0], match[1]);
155+
});
156+
editor.replaceSelection(selection);
157+
} else {
158+
editor.replaceSelection(replaced);
159+
}
160+
}
161+
});
162+
});
111163
}
112164
}
113165

@@ -119,3 +171,57 @@ export function addIcons() {
119171
addIcon("colorizelt-pen-blue", '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-pen-line"><path d="M12 20h9" stroke="blue"/><path d="M16.376 3.622a1 1 0 0 1 3.002 3.002L7.368 18.635a2 2 0 0 1-.855.506l-2.872.838a.5.5 0 0 1-.62-.62l.838-2.872a2 2 0 0 1 .506-.854z"/></svg>');
120172
addIcon("colorizelt-clear", '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-pen-line"><path d="M16.376 3.622a1 1 0 0 1 3.002 3.002L7.368 18.635a2 2 0 0 1-.855.506l-2.872.838a.5.5 0 0 1-.62-.62l.838-2.872a2 2 0 0 1 .506-.854z"/></svg>');
121173
}
174+
175+
export class ColorizeltSettingTab extends PluginSettingTab {
176+
plugin: Colorizelt;
177+
constructor(app: App, plugin: Colorizelt) {
178+
super(app, plugin);
179+
this.plugin = plugin;
180+
}
181+
display(): void {
182+
let { containerEl } = this;
183+
containerEl.createEl("h1", {text: "Colorizelt Colors Setting"});
184+
185+
this.plugin.settings.forEach((button, index) => {
186+
const setting = new Setting(containerEl)
187+
.setName(`Setting for ${button.name.toLowerCase().replace(/\s+/g, '-')}`)
188+
.addText(text => text
189+
.setValue(button.name.toLowerCase().replace(/\s+/g, '-'))
190+
.setPlaceholder('Button name')
191+
.onChange(async (value) => {
192+
button.name = value;
193+
await this.plugin.saveSettings()
194+
}))
195+
196+
setting.addColorPicker(colorPicker => colorPicker
197+
.setValue(button.color)
198+
.onChange(async (value) => {
199+
button.color = value;
200+
await this.plugin.saveSettings();
201+
}));
202+
203+
setting.addButton(btn => {
204+
btn.setButtonText("Удалить").onClick(async () => {
205+
this.plugin.settings.splice(index, 1);
206+
await this.plugin.saveSettings();
207+
this.display();
208+
});
209+
});
210+
});
211+
212+
const newButtonSetting = new Setting(containerEl)
213+
.addButton(btn => {
214+
btn.setButtonText("Добавить кнопку")
215+
.setCta()
216+
.onClick(async () => {
217+
this.plugin.settings.push({
218+
id: `button-{Date.now()}`,
219+
name: "null",
220+
color: "#000000"
221+
});
222+
await this.plugin.saveSettings();
223+
this.display();
224+
});
225+
});
226+
}
227+
}

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"0.0.1": "0.12.1",
33
"0.0.2": "0.12.1",
44
"0.0.3": "0.12.1",
5-
"0.0.4": "0.12.1"
5+
"0.0.4": "0.12.1",
6+
"0.0.5": "0.12.1"
67
}

0 commit comments

Comments
 (0)