-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy paththeme.js
750 lines (679 loc) · 25.2 KB
/
theme.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
window.theme = {
element: {
themeScript: document.getElementById('themeScript') ?? document.currentScript,
},
};
/* 颜色配置文件列表 */
window.theme.lightColors = ['style/theme/Tsundoku_light.css', 'style/theme/Tsundoku_green.css'];
window.theme.darkColors = ['style/theme/Tsundoku_dark.css'];
/* DOM 节点 ID */
window.theme.IDs = {
STYLE_COLOR: 'Tsundoku-theme-css',
BUTTON_TOOLBAR_CHANGE_COLOR: 'Tsundoku-theme-button',
LOCAL_STORAGE_COLOR_HREF: 'tsundoku-color-href',
};
/* 循环迭代器 */
window.theme.Iterator = function* (items) {
// REF [ES6中的迭代器(Iterator)和生成器(Generator) - 小火柴的蓝色理想 - 博客园](https://www.cnblogs.com/xiaohuochai/p/7253466.html)
for (let i = 0; true; i = (i + 1) % items.length) {
yield items[i];
}
};
/**
* 静态资源请求 URL 添加参数
* @params {string} url 资源请求 URL
* @return {string} 返回添加参数后的 URL
*/
window.theme.addURLParam = function (
url,
param = {
// t: Date.now().toString(),
v: window.siyuan.config.appearance.themeVer,
}
) {
let new_url;
switch (true) {
case url.startsWith('//'):
new_url = new URL(`https:${url}`);
break;
case url.startsWith('http://'):
case url.startsWith('https://'):
new_url = new URL(url);
break;
case url.startsWith('/'):
new_url = new URL(url, window.location.origin);
break;
default:
new_url = new URL(url, window.location.origin + window.location.pathname);
break;
}
for (let [key, value] of Object.entries(param)) {
new_url.searchParams.set(key, value);
}
switch (true) {
case url.startsWith('//'):
return new_url.href.substring(new_url.protocol.length);
case url.startsWith('http://'):
case url.startsWith('https://'):
return new_url.href;
case url.startsWith('/'):
return new_url.href.substring(new_url.origin.length);
default:
return new_url.href.substring(
(window.location.origin + window.location.pathname).length
);
}
};
/**
* 加载 meta 标签
* @params {object} attributes 属性键值对
* @params {string} position 节点插入位置
* @params {HTMLElementNode} element 节点插入锚点
*/
window.theme.loadMeta = function (attributes, position = 'afterbegin', element = document.head) {
let meta = document.createElement('meta');
for (let [key, value] of Object.entries(attributes)) {
meta.setAttribute(key, value);
}
// document.head.insertBefore(meta, document.head.firstChild);
// [Element.insertAdjacentElement() - Web API 接口参考 | MDN](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/insertAdjacentElement)
element.insertAdjacentElement(position, meta);
};
/**
* 加载脚本文件
* @params {string} url 脚本地址
* @params {string} type 脚本类型
* @params {boolean} async 是否异步加载 & 非阻塞运行
* @params {boolean} defer 是否异步加载 & 阻塞运行
* @params {string} position 节点插入位置
* @params {HTMLElementNode} element 节点插入锚点
*/
window.theme.loadScript = function (
src,
type = 'module',
async = false,
defer = false,
position = 'beforebegin',
element = window.theme.element.themeScript
) {
const script = document.createElement('script');
if (type) script.type = type;
if (async) script.async = true;
if (defer) script.defer = true;
script.src = src;
// document.head.appendChild(script);
element.insertAdjacentElement(position, script);
};
/**
* 加载样式文件
* @params {string} href 样式地址
* @params {string} id 样式 ID
*/
window.theme.loadStyle = function (href, id = null) {
let style = document.createElement('link');
if (id) style.id = id;
style.type = 'text/css';
style.rel = 'stylesheet';
style.href = href;
document.head.appendChild(style);
};
/**
* 更新样式文件
* @params {string} id 样式文件 ID
* @params {string} href 样式文件地址
*/
window.theme.updateStyle = function (id, href) {
let style = document.getElementById(id);
if (style) {
style.setAttribute('href', href);
} else {
window.theme.loadStyle(href, id);
}
};
function create_theme_button() {
// light 主题下更新样式:为了新建窗口也能自动加载样式
let drag;
console.log(window.theme.clientMode);
if (window.theme.clientMode == 'mobile') {
drag = document.getElementsByClassName('.toolbar--border'); // 标题栏
} else {
drag = document.getElementById('barMode'); // 标题栏
}
const themeStyle = document.getElementById('themeStyle'); // 当前主题引用路径
if (themeStyle) {
const THEME_ROOT = new URL(themeStyle.href).pathname.replace('theme.css', ''); // 当前主题根目录
const colors_href = [];
// window.theme.themeMode 如果是dark就不要创建按钮了,直接用dark主题
if (window.theme.themeMode === 'dark') {
const color = window.theme.darkColors[0];
window.theme.updateStyle(window.theme.IDs.STYLE_COLOR, `${THEME_ROOT}${color}`);
return;
}
/* 通过颜色配置文件列表生成完整 URL 路径 */
window.theme.lightColors.forEach(color => colors_href.push(`${THEME_ROOT}${color}`));
window.theme.iter = window.theme.Iterator(colors_href);
var color_href = window.siyuan?.storage[window.theme.IDs.LOCAL_STORAGE_COLOR_HREF];
if (!color_href) {
localStorage.getItem(window.theme.IDs.LOCAL_STORAGE_COLOR_HREF);
}
if (color_href) {
// 将迭代器调整为当前配色
for (let i = 0; i < window.theme.lightColors.length; ++i) {
if (window.theme.iter.next().value === color_href) break;
}
} else {
// 迭代器第一个为当前配色
color_href = window.theme.iter.next().value;
localStorage.setItem(window.theme.IDs.LOCAL_STORAGE_COLOR_HREF, color_href);
setLocalStorageVal(window.theme.IDs.LOCAL_STORAGE_COLOR_HREF, color_href);
}
/* 加载配色文件 */
window.theme.updateStyle(window.theme.IDs.STYLE_COLOR, color_href);
}
if (drag && themeStyle) {
const button_change_color = document.createElement('button'); // 切换主题颜色按钮
button_change_color.id = window.theme.IDs.BUTTON_TOOLBAR_CHANGE_COLOR;
button_change_color.className = 'toolbar__item ariaLabel';
button_change_color.ariaLabel = '切换主题颜色';
button_change_color.innerHTML = `<svg><use xlink:href="#iconTheme"></use></svg>`;
button_change_color.addEventListener('click', e => {
color_href = window.theme.iter.next().value;
localStorage.setItem(window.theme.IDs.LOCAL_STORAGE_COLOR_HREF, color_href);
setLocalStorageVal(window.theme.IDs.LOCAL_STORAGE_COLOR_HREF, color_href);
window.theme.updateStyle(window.theme.IDs.STYLE_COLOR, color_href);
});
// REF [JS DOM 编程复习笔记 -- insertAdjacentHTML(九) - 知乎](https://zhuanlan.zhihu.com/p/425616377)
drag.insertAdjacentElement('afterend', button_change_color);
}
}
setTimeout(() => { }, 0);
/**
* 发送API请求
* @param {*} data
* @param {*} url
* @returns
*/
async function postRequest(data, url) {
let result;
await fetch(url, {
body: JSON.stringify(data),
method: 'POST',
headers: {
Authorization: 'Token ',
'Content-Type': 'application/json',
},
}).then(response => {
result = response.json();
});
return result;
}
/**
* 设置LocalStorage
* @param {*} ikey
* @param {*} ival
*/
async function setLocalStorageVal(ikey, ival) {
let url = '/api/storage/setLocalStorageVal';
let response = await postRequest({ app: getAppId(), key: ikey, val: ival }, url);
if (window.top.siyuan.storage != undefined) {
window.top.siyuan.storage[ikey] = ival;
}
function getAppId() {
let wsurl = window.top.siyuan.ws.ws.url;
let appIdMatchResult = wsurl.match(new RegExp(`(\\?app=|&app=)[^&]+`, 'g'));
if (appIdMatchResult.length == 1) {
return appIdMatchResult[0].substring(5);
} else if (appIdMatchResult.length > 1) {
console.warn('正则获取appId错误', appIdMatchResult);
return appIdMatchResult[0].substring(5);
} else {
console.error('正则获取appId错误', appIdMatchResult);
return '';
}
}
}
/**
* 获取主题模式
* @return {string} light 或 dark
*/
window.theme.themeMode = (() => {
/* 根据浏览器主题判断颜色模式 */
// switch (true) {
// case window.matchMedia('(prefers-color-scheme: light)').matches:
// return 'light';
// case window.matchMedia('(prefers-color-scheme: dark)').matches:
// return 'dark';
// default:
// return null;
// }
/* 根据配置选项判断主题 */
switch (window.siyuan.config.appearance.mode) {
case 0:
return 'light';
case 1:
return 'dark';
default:
return null;
}
})();
/**
* 获取窗口宽高模式
* @return {string} landscape 或 portrait
*/
window.theme.orientation = () => {
/* 根据浏览器主题判断颜色模式 */
switch (true) {
case window.matchMedia('(orientation: landscape)').matches:
/* 宽 > 高 */
return 'landscape';
case window.matchMedia('(orientation: portrait)').matches:
/* 高 > 宽 */
return 'portrait';
default:
return null;
}
};
/**
* 获取客户端模式
* @return {string} 'app' 或 'desktop' 或 'mobile'
*/
window.theme.clientMode = (() => {
const url = new URL(window.location.href);
switch (true) {
case url.pathname.startsWith('/stage/build/app/window.html'):
return 'window';
case url.pathname.startsWith('/stage/build/app'):
return 'app';
case url.pathname.startsWith('/stage/build/desktop'):
return 'desktop';
case url.pathname.startsWith('/stage/build/mobile'):
return 'mobile';
default:
return null;
}
})();
/**
* 获取语言模式
* @return {string} 'zh_CN', 'zh_CNT', 'fr_FR', 'en_US'
*/
window.theme.languageMode = window.siyuan.config.lang;
/**
* 获取思源版本号
* @return {string} 思源版本号
*/
window.theme.kernelVersion = window.siyuan.config.system.kernelVersion;
/**
* 获取操作系统
*/
window.theme.OS = window.siyuan.config.system.os;
/**
* 获得主题根目录
*/
window.theme.root = (() => {
const src = document.currentScript.getAttribute('src');
return src.substring(0, src.lastIndexOf('/'));
})();
/**
* 获取一个 Lute 对象
* @return {Lute} Lute 对象
*/
window.theme.lute = window.Lute.New();
/****************************思源API操作**************************/
async function 设置思源块属性(内容块id, 属性对象) {
let url = '/api/attr/setBlockAttrs';
return 解析响应体(
向思源请求数据(url, {
id: 内容块id,
attrs: 属性对象,
})
);
}
async function 向思源请求数据(url, data) {
let resData = null;
await fetch(url, {
body: JSON.stringify(data),
method: 'POST',
headers: {
Authorization: `Token ''`,
},
}).then(function (response) {
resData = response.json();
});
return resData;
}
async function 解析响应体(response) {
let r = await response;
return r.code === 0 ? r.data : null;
}
/* 操作 */
/**
* 获得所选择的块对应的块 ID
* @returns {string} 块 ID
* @returns {
* id: string, // 块 ID
* type: string, // 块类型
* subtype: string, // 块子类型(若没有则为 null)
* }
* @returns {null} 没有找到块 ID */
function getBlockSelected() {
let node_list = document.querySelectorAll('.protyle-wysiwyg--select');
if (node_list.length === 1 && node_list[0].dataset.nodeId != null)
return {
id: node_list[0].dataset.nodeId,
type: node_list[0].dataset.type,
subtype: node_list[0].dataset.subtype,
};
return null;
}
function ClickMonitor() {
window.addEventListener('mouseup', MenuShow);
}
function MenuShow() {
setTimeout(() => {
let selectinfo = getBlockSelected();
if (selectinfo) {
let selecttype = selectinfo.type;
let selectid = selectinfo.id;
if (
selecttype == 'NodeList' ||
selecttype == 'NodeTable' ||
// selecttype == 'NodeBlockquote' ||
selecttype == 'NodeCodeBlock'
) {
setTimeout(() => InsertMenuItem(selectid, selecttype), 0);
}
}
}, 0);
}
setTimeout(() => ClickMonitor(), 1000);
//+++++++++++++++++++++++++++++++++思源API++++++++++++++++++++++++++++++++++++
//思源官方API文档 https://github.com/siyuan-note/siyuan/blob/master/API_zh_CN.md
/**
*
* @param {*} 内容块id
* @param {*} 回调函数
* @param {*} 传递对象
*/
async function 根据ID获取人类可读路径(内容块id, then, obj = null) {
await 向思源请求数据('/api/filetree/getHPathByID', {
id: 内容块id,
}).then(v => then(v.data, obj));
}
async function 以id获取文档聚焦内容(id, then, obj = null) {
await 向思源请求数据('/api/filetree/getDoc', {
id: id,
k: '',
mode: 0,
size: 36,
}).then(v => then(v.data, obj));
}
async function 更新块(id, dataType, data, then = null, obj = null) {
await 向思源请求数据('/api/block/updateBlock', {
id: id,
dataType: dataType,
data: data,
}).then(v => {
if (then) then(v.data, obj);
});
}
async function 设置思源块属性(内容块id, 属性对象) {
let url = '/api/attr/setBlockAttrs';
return 解析响应体(
向思源请求数据(url, {
id: 内容块id,
attrs: 属性对象,
})
);
}
async function 获取块属性(内容块id, then = null, obj = null) {
let url = '/api/attr/getBlockAttrs';
return 向思源请求数据(url, {
id: 内容块id,
}).then(v => {
if (then) then(v.data, obj);
});
}
async function 向思源请求数据(url, data) {
const response = await fetch(url, {
body: JSON.stringify(data),
method: 'POST',
headers: {
Authorization: `Token ''`,
},
});
if (response.status === 200) return await response.json();
else return null;
}
async function 解析响应体(response) {
let r = await response;
return r.code === 0 ? r.data : null;
}
/****各种列表转xx的UI****/
function ViewSelect(selectid, selecttype) {
let button = document.createElement('button');
button.id = 'viewselect';
button.className = 'b3-menu__item';
button.innerHTML =
'<svg class="b3-menu__icon" style="null"><use xlink:href="#iconGlobalGraph"></use></svg><span class="b3-menu__label" style="">主题块样式更改</span><svg class="b3-menu__icon b3-menu__icon--small" style="null"><use xlink:href="#iconRight"></use></svg></button>';
button.appendChild(SubMenu(selectid, selecttype));
return button;
}
function SubMenu(selectid, selecttype, className = 'b3-menu__submenu') {
let node = document.createElement('div');
node.className = className;
console.log(selecttype);
if (selecttype == 'NodeList') {
node.appendChild(GraphView(selectid));
node.appendChild(TableView(selectid));
node.appendChild(kanbanView(selectid));
node.appendChild(DefaultView(selectid));
}
if (selecttype == 'NodeTable') {
node.appendChild(Removeth(selectid));
node.appendChild(Defaultth(selectid));
}
if (selecttype == 'NodeCodeBlock') {
node.appendChild(setCodeOutput(selectid));
node.appendChild(cancelCodeOutput(selectid));
}
return node;
}
function setCodeOutput(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 'code');
button.setAttribute('custom-attr-value', 'output');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconFiles"></use></svg><span class="b3-menu__label">设置为代码输出样式</span>`;
button.onclick = ViewMonitor;
return button;
}
function cancelCodeOutput(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 'code');
button.setAttribute('custom-attr-value', '');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconFiles"></use></svg><span class="b3-menu__label">取消代码块输出样式</span>`;
button.onclick = ViewMonitor;
return button;
}
function GraphView(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 'f');
button.setAttribute('custom-attr-value', 'dt');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconFiles"></use></svg><span class="b3-menu__label">转换为导图</span>`;
button.onclick = ViewMonitor;
return button;
}
function TableView(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 'f');
button.setAttribute('custom-attr-value', 'bg');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconTable"></use></svg><span class="b3-menu__label">转换为表格</span>`;
button.onclick = ViewMonitor;
return button;
}
function kanbanView(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 'f');
button.setAttribute('custom-attr-value', 'kb');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconMenu"></use></svg><span class="b3-menu__label">转换为看板</span>`;
button.onclick = ViewMonitor;
return button;
}
function DefaultView(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.onclick = ViewMonitor;
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 'f');
button.setAttribute('custom-attr-value', '');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconList"></use></svg><span class="b3-menu__label">恢复为列表</span>`;
return button;
}
function FixWidth(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.onclick = ViewMonitor;
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 'f');
button.setAttribute('custom-attr-value', '');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconTable"></use></svg><span class="b3-menu__label">默认宽度</span>`;
return button;
}
function AutoWidth(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 'f');
button.setAttribute('custom-attr-value', 'full');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconTable"></use></svg><span class="b3-menu__label">页面宽度</span>`;
button.onclick = ViewMonitor;
return button;
}
function Removeth(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.onclick = ViewMonitor;
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 't');
button.setAttribute('custom-attr-value', 'biaotou');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconTable"></use></svg><span class="b3-menu__label">取消表头</span>`;
return button;
}
function Defaultth(selectid) {
let button = document.createElement('button');
button.className = 'b3-menu__item';
button.setAttribute('data-node-id', selectid);
button.setAttribute('custom-attr-name', 't');
button.setAttribute('custom-attr-value', '');
button.innerHTML = `<svg class="b3-menu__icon" style=""><use xlink:href="#iconTable"></use></svg><span class="b3-menu__label">默认表头</span>`;
button.onclick = ViewMonitor;
return button;
}
function MenuSeparator(className = 'b3-menu__separator') {
let node = document.createElement('button');
node.className = className;
return node;
}
/* 操作 */
/**
* 获得所选择的块对应的块 ID
* @returns {string} 块 ID
* @returns {
* id: string, // 块 ID
* type: string, // 块类型
* subtype: string, // 块子类型(若没有则为 null)
* }
* @returns {null} 没有找到块 ID */
function getBlockSelected() {
let node_list = document.querySelectorAll('.protyle-wysiwyg--select');
if (node_list.length === 1 && node_list[0].dataset.nodeId != null)
return {
id: node_list[0].dataset.nodeId,
type: node_list[0].dataset.type,
subtype: node_list[0].dataset.subtype,
};
return null;
}
function ClickMonitor() {
window.addEventListener('mouseup', MenuShow);
}
function InsertMenuItem(selectid, selecttype) {
let commonMenu = document.querySelector('.b3-menu__items');
let target = commonMenu.querySelector('.b3-menu__separator[data-id="separator_5"]');
let selectview = commonMenu.querySelector('[id="viewselect"]');
if (target) {
console.log('test');
if (!selectview) {
// 在 target 元素后插入 ViewSelect
target.insertAdjacentElement('afterend', ViewSelect(selectid, selecttype));
}
}
}
function ViewMonitor(event) {
let id = event.currentTarget.getAttribute('data-node-id');
let attrName = 'custom-' + event.currentTarget.getAttribute('custom-attr-name');
let attrValue = event.currentTarget.getAttribute('custom-attr-value');
let blocks = document.querySelectorAll(`.protyle-wysiwyg [data-node-id="${id}"]`);
if (blocks) {
blocks.forEach(block => block.setAttribute(attrName, attrValue));
}
let attrs = {};
attrs[attrName] = attrValue;
设置思源块属性(id, attrs);
}
function link_icon_filter() {
let spans = document.querySelectorAll('span[data-type="a"]');
spans.forEach(span => {
const textContent = span.textContent.trim();
// 检查文本是否完全被方括号包裹或是否为纯数字
const isFullyWrappedInBrackets = /^\[.*\]$/.test(textContent);
const isNumber = /^\d+$/.test(textContent);
if (textContent === '*' || isFullyWrappedInBrackets || isNumber) {
span.setAttribute('custom-linkicon', 'false');
} else {
span.removeAttribute('custom-linkicon');
}
});
}
linkIconFilterInterval = setInterval(link_icon_filter, 100);
/**++++++++++++++++++++++++++++++++主题功能执行:按需调用++++++++++++++++++++++++++++++ */
window.theme.timerIds = [];
(async () => {
// 各种列表转 xx
ClickMonitor();
/* 创建主题按钮 */
create_theme_button();
const linkIconFilterInterval = setInterval(link_icon_filter, 100);
window.theme.timerIds.push(linkIconFilterInterval);
})();
function clearAllTimers() {
window.theme.timerIds.forEach(timerId => {
clearInterval(timerId);
});
// 清空数组
window.theme.timerIds.length = 0;
}
window.destroyTheme = () => {
// 删除主题切换按钮
const themeButton = document.getElementById(window.theme.IDs.BUTTON_TOOLBAR_CHANGE_COLOR);
if (themeButton) {
themeButton.remove();
}
// 删除主题加载的额外配色 css
let css_link = document.getElementById(window.theme.IDs.STYLE_COLOR);
if (css_link) {
css_link.remove();
}
// 删除列表转导图功能
window.removeEventListener('mouseup', MenuShow);
clearAllTimers();
};