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
+ ] ;
2
13
3
14
export default class Colorizelt extends Plugin {
15
+ settings : ColorizeltSetting [ ] = [ ] ;
4
16
async onload ( ) {
5
17
addIcons ( )
6
- new Notice ( 'Plugin "Colorizelt" v0.0.4 load success!' ) ;
18
+ await this . loadSettings ( ) ;
19
+
20
+ this . addSettingTab ( new ColorizeltSettingTab ( this . app , this ) ) ;
7
21
8
22
//red color
9
23
this . addCommand ( {
@@ -108,6 +122,44 @@ export default class Colorizelt extends Plugin {
108
122
}
109
123
} ) ;
110
124
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
+ } ) ;
111
163
}
112
164
}
113
165
@@ -119,3 +171,57 @@ export function addIcons() {
119
171
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>' ) ;
120
172
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>' ) ;
121
173
}
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
+ }
0 commit comments