-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.js
72 lines (63 loc) · 2.72 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
// TODO 监听浮窗添加,然后展开聚焦的块(直接移除元素上的 [fold="1"] 属性即可,好像没有副作用)
(function() {
// TODO 主题改完确定没问题之后就去除所有 log 输出
(async () => {
console.log('————————执行一次主题JS————————');
})();
window.destroyTheme = () => {
console.log('————————移除一次主题————————');
// 卸载“跟踪当前所在块”的事件监听器
blockTrackCleanup();
}
/**
* 获得指定块位于的编辑区
* @param {HTMLElement} block
* @return {HTMLElement} 光标所在块位于的编辑区
* @return {null} 光标不在块内
*/
const getTargetEditor = function(block) {
while (block != null && !block.classList.contains('protyle-wysiwyg')) block = block.parentElement;
return block;
};
/**
* 获得焦点所在的块
* @return {HTMLElement} 光标所在块
* @return {null} 光标不在块内
*/
const getFocusedBlock = function() {
if (document.activeElement.classList.contains('protyle-wysiwyg')) {
let block = window.getSelection()?.focusNode?.parentElement; // 当前光标
while (block != null && block.dataset.nodeId == null) block = block.parentElement;
return block;
}
};
const focusHandler = function() {
// TODO 需要排除上层块包含 .protyle-wysiwyg--select 的情况,这种情况下样式会造成干扰。hasClosestByClassName?
// 获取当前编辑区
let block = getFocusedBlock(); // 当前光标所在块
// 当前块已经设置焦点
if (block?.classList.contains(`block-focus`)) return;
// 当前块未设置焦点
const editor = getTargetEditor(block); // 当前光标所在块位于的编辑区
if (editor) {
editor.querySelectorAll(`.block-focus`).forEach((element) => element.classList.remove(`block-focus`));
block.classList.add(`block-focus`);
// setSelector(block);
}
};
const blockTrackMain = function() {
// 跟踪当前所在块
window.addEventListener('mouseup', focusHandler, true);
window.addEventListener('keyup', focusHandler, true);
};
const blockTrackCleanup = function() {
// 移除类名
document.querySelectorAll('.block-focus').forEach((element) => element.classList.remove('block-focus'));
// 卸载事件监听器
window.removeEventListener('mouseup', focusHandler, true);
window.removeEventListener('keyup', focusHandler, true);
};
(async () => {
blockTrackMain();
})();
})();