Skip to content

Commit 56be185

Browse files
committed
✨ misc(setting-utils)
1 parent 8daa01a commit 56be185

File tree

2 files changed

+74
-54
lines changed

2 files changed

+74
-54
lines changed

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,6 @@ export default class PluginSample extends Plugin {
164164
});
165165

166166
this.settingUtils = new SettingUtils(this, STORAGE_NAME);
167-
168-
try {
169-
this.settingUtils.load();
170-
} catch (error) {
171-
console.error("Error loading settings storage, probably empty config json:", error);
172-
}
173-
174167
this.settingUtils.addItem({
175168
key: "Input",
176169
value: "",
@@ -274,6 +267,13 @@ export default class PluginSample extends Plugin {
274267
description: this.i18n.hintDesc,
275268
});
276269

270+
try {
271+
this.settingUtils.load();
272+
} catch (error) {
273+
console.error("Error loading settings storage, probably empty config json:", error);
274+
}
275+
276+
277277
this.protyleSlash = [{
278278
filter: ["insert emoji 😊", "插入表情 😊", "crbqwx"],
279279
html: `<div class="b3-list-item__first"><span class="b3-list-item__text">${this.i18n.insertEmoji}</span><span class="b3-list-item__meta">😊</span></div>`,

src/libs/setting-utils.ts

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,44 @@
33
* @Author : frostime
44
* @Date : 2023-12-17 18:28:19
55
* @FilePath : /src/libs/setting-utils.ts
6-
* @LastEditTime : 2024-04-27 16:38:09
6+
* @LastEditTime : 2024-04-28 17:43:43
77
* @Description :
88
*/
99

1010
import { Plugin, Setting } from 'siyuan';
1111

12+
const valueOf = (ele: HTMLElement) => {
13+
if (ele instanceof HTMLInputElement) {
14+
if (ele.type === 'checkbox') {
15+
return ele.checked;
16+
} else {
17+
return ele.value;
18+
}
19+
} else if (ele instanceof HTMLSelectElement) {
20+
return ele.value;
21+
} else if (ele instanceof HTMLTextAreaElement) {
22+
return ele.value;
23+
} else {
24+
return ele.textContent;
25+
}
26+
}
27+
28+
const setValue = (ele: HTMLElement, value: any) => {
29+
if (ele instanceof HTMLInputElement) {
30+
if (ele.type === 'checkbox') {
31+
ele.checked = value;
32+
} else {
33+
ele.value = value;
34+
}
35+
} else if (ele instanceof HTMLSelectElement) {
36+
ele.value = value;
37+
} else if (ele instanceof HTMLTextAreaElement) {
38+
ele.value = value;
39+
} else {
40+
ele.textContent = value;
41+
}
42+
}
43+
1244
export class SettingUtils {
1345
plugin: Plugin;
1446
name: string;
@@ -31,10 +63,9 @@ export class SettingUtils {
3163
let data = this.dump();
3264
if (callback !== undefined) {
3365
callback(data);
34-
} else {
35-
this.plugin.data[this.name] = data;
36-
this.save();
3766
}
67+
this.plugin.data[this.name] = data;
68+
this.save();
3869
},
3970
destroyCallback: () => {
4071
//从值恢复元素
@@ -73,21 +104,6 @@ export class SettingUtils {
73104
return this.settings.get(key)?.value;
74105
}
75106

76-
/**
77-
* Read in real time,
78-
* do not read from the configuration file
79-
* @param key key name
80-
* @returns value in html
81-
*/
82-
take(key: string) {
83-
let element = this.elements.get(key) as any;
84-
if (!element){
85-
return
86-
}
87-
this.settings.set(key, element.value)
88-
return element.value
89-
}
90-
91107
/**
92108
* Set data to this.settings,
93109
* but do not save it to the configuration file
@@ -113,20 +129,38 @@ export class SettingUtils {
113129
if (item) {
114130
item.value = value;
115131
this.updateElementFromValue(key);
116-
await this.save()
132+
await this.save();
133+
}
134+
}
135+
136+
/**
137+
* Read in the value of element instead of setting obj in real time
138+
* @param key key name
139+
* @param apply whether to apply the value to the setting object
140+
* if true, the value will be applied to the setting object
141+
* @returns value in html
142+
*/
143+
take(key: string, apply: boolean = false) {
144+
let element = this.elements.get(key) as any;
145+
if (!element){
146+
return
117147
}
148+
if (apply) {
149+
this.updateValueFromElement(key);
150+
}
151+
return valueOf(element)
118152
}
119153

120154
/**
121155
* Read data from html and save it
122156
* @param key key name
123157
* @param value value
124-
* @return value in html
158+
* @return value in html
125159
*/
126160
async takeAndSave(key: string) {
127-
let value = this.take(key)
128-
await this.save()
129-
return value
161+
let value = this.take(key, true);
162+
await this.save();
163+
return value;
130164
}
131165

132166
/**
@@ -248,47 +282,27 @@ export class SettingUtils {
248282
title: item.title,
249283
description: item?.description,
250284
createActionElement: () => {
285+
this.updateElementFromValue(item.key);
251286
let element = this.getElement(item.key);
252287
return element;
253288
}
254289
})
255290
}
256291

257292
/**
258-
* Set the value in the setting to the value of the element
259-
* and return the element information
293+
* return the setting element
260294
* @param key key name
261295
* @returns element
262296
*/
263297
getElement(key: string) {
264-
let item = this.settings.get(key);
298+
// let item = this.settings.get(key);
265299
let element = this.elements.get(key) as any;
266-
switch (item.type) {
267-
case 'checkbox':
268-
element.value = element.checked ? true : false;
269-
element.checked = item.value;
270-
break;
271-
case 'select':
272-
element.value = item.value;
273-
break;
274-
case 'slider':
275-
element.value = item.value;
276-
element.ariaLabel = item.value;
277-
break;
278-
case 'textinput':
279-
element.value = item.value;
280-
break;
281-
case 'textarea':
282-
element.value = item.value;
283-
break;
284-
}
285300
return element;
286301
}
287302

288303
private updateValueFromElement(key: string) {
289304
let item = this.settings.get(key);
290305
let element = this.elements.get(key) as any;
291-
// console.debug(element, element?.value);
292306
switch (item.type) {
293307
case 'checkbox':
294308
item.value = element.checked;
@@ -305,6 +319,9 @@ export class SettingUtils {
305319
case 'textarea':
306320
item.value = element.value;
307321
break;
322+
case 'number':
323+
item.value = parseInt(element.value);
324+
break;
308325
}
309326
}
310327

@@ -327,6 +344,9 @@ export class SettingUtils {
327344
case 'textarea':
328345
element.value = item.value;
329346
break;
347+
case 'number':
348+
element.value = item.value;
349+
break;
330350
}
331351
}
332352
}

0 commit comments

Comments
 (0)