forked from UserZYF/zhang-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.js
211 lines (193 loc) · 7.43 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
/***js form Morgan***/
/****************************思源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
}
/****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--arrow" 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;
if(selecttype=="NodeList"){
node.appendChild(GraphView(selectid))
node.appendChild(TableView(selectid))
node.appendChild(kanbanView(selectid))
node.appendChild(DefaultView(selectid))
}
if(selecttype=="NodeTable"){
node.appendChild(FixWidth(selectid))
node.appendChild(AutoWidth(selectid))
node.appendChild(Removeth(selectid))
node.appendChild(Defaultth(selectid))
}
return node;
}
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","auto")
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:not(.fn__none)>.protyle-content .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"){
setTimeout(()=>InsertMenuItem(selectid,selecttype), 0)
}
}
}, 0);
}
function InsertMenuItem(selectid,selecttype){
let commonMenu = document.getElementById("commonMenu")
let readonly = commonMenu.querySelector(".b3-menu__item--readonly")
let selectview = commonMenu.querySelector('[id="viewselect"]')
if(readonly){
if(!selectview){
commonMenu.insertBefore(ViewSelect(selectid,selecttype),readonly)
commonMenu.insertBefore(MenuSeparator(),readonly)
}
}
}
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)
}
setTimeout(()=>ClickMonitor(),1000)