-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwangEditor增强@人功能.user.js
453 lines (438 loc) · 17.6 KB
/
wangEditor增强@人功能.user.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
// ==UserScript==
// @name wangEditor增强@人功能
// @namespace http://tampermonkey.net/
// @version 0.2
// @description try to take over the world!
// @author You
// @match https://www.wangeditor.com/
// @grant none
// ==/UserScript==
(function () {
"use strict";
// 将已创建的编辑器清空重新生成, 拿到 对应编辑器对象
document.querySelector("#div-demo").innerHTML = "";
document.querySelector("#div-demo").removeAttribute("data-we-id");
let wangEditor = window.wangEditor;
window.wangEditor = function (...param) {
let editor = new wangEditor(...param);
// console.log(editor);
const userList = [
{
name: "wangfupeng1988",
url: "https://github.com/wangfupeng1988",
img: "",
type: "github",
},
{
name: "fuxichen",
url: "https://github.com/fuxichen",
img: "",
type: "github",
},
];
init(editor, userList);
return editor;
};
function init(editor, userList = []) {
proxyHistorySave(editor.history);
/**
* 代理编辑器历史记录的保存方法过滤因使用获取光标位置逻辑产生的虚拟节点
*/
function proxyHistorySave(history) {
let save = history.save;
history.save = (...params) => {
let flag = params[0][0]?.addedNodes?.[0]?.getAttribute?.(
"data-data"
);
console.log(flag);
if (flag == "qq632951357-charLen") {
return;
}
save.call(history, ...params);
};
}
window.userList = userList;
// editor.config.uploadImgShowBase64 = true;
let onchange = editor.config.onchange;
editor.config.onchange = function (...params) {
changeHandle(...params);
typeof onchange == "function" && onchange(...params);
};
let oldHtml = null;
let currentIndex = 0;
let tipBody;
// 监听键盘按下回车事件
let keyDownFun = (event) => {
// console.log(event)
// event.stopPropagation()
if (
event.isComposing ||
event.keyCode === 229 ||
![13, 40, 38].includes(event.keyCode)
) {
return;
}
event.preventDefault();
if (event.keyCode == 13 && userList.length != 0) {
confirmHandle();
event.returnValue = false;
} else if (event.keyCode == 40) {
// down
setCurrentIndex(currentIndex + 1);
} else if (event.keyCode == 38) {
// up
setCurrentIndex(currentIndex - 1);
}
};
function confirmHandle() {
let range = document.getSelection().getRangeAt(0)
// editor.cmd.do("forwardDelete");
// 将 @ 符号选中
// document
// .getSelection()
// .getRangeAt(0)
// .setStart(
// document.getSelection().focusNode,
// document.getSelection().focusOffset - 1
// );
let text = range.startContainer.data || range.startContainer.textContent
let startText = text.substr(0, range.endOffset - 1);
let endText = text.substring(range.endOffset, text.length)
console.log(startText, "---", endText)
let a = document.createElement("a");
a.href = userList[currentIndex].url;
a.target = "_blank";
a.class = "not-select111";
a.style.userSelect = "all";
a.setAttribute("contenteditable", "false");
a.setAttribute("unselectable", "on");
a.setAttribute("data-data", "qq632951357-link")
a.innerHTML = "@" + userList[currentIndex].name + " ";
document.getSelection().focusNode.replaceWith(a)
if (startText.length) {
a.parentNode.insertBefore(document.createTextNode(startText), a)
}
if (endText.length) {
insertAfter(document.createTextNode(endText), a)
}
// 将 @ 符号选中
document
.getSelection()
.getRangeAt(0)
.setStartAfter(
a
);
document
.getSelection()
.getRangeAt(0)
.setEndAfter(
a
);
// editor.cmd.do(
// "insertHTML",
// `<a href="${userList[currentIndex].url}" target="_blank" class="not-select111" style="user-select: all;" contenteditable="false" unselectable="on" data-data="qq632951357-link">@${userList[currentIndex].name} </a>`
// );
}
function confirmHandle2() {
editor.cmd.do("forwardDelete");
// 将 @ 符号选中
document
.getSelection()
.getRangeAt(0)
.setStart(
document.getSelection().focusNode,
document.getSelection().focusOffset - 1
);
editor.cmd.do(
"insertHTML",
`<a href="${userList[currentIndex].url}" target="_blank" class="not-select111" style="user-select: all;" contenteditable="false" unselectable="on" data-data="qq632951357-link">@${userList[currentIndex].name} </a>`
);
}
function setCurrentIndex(index) {
if (index > userList.length - 1 || index < 0) {
return;
}
let list = document.querySelectorAll(
".option__item[data-data=qq632951357-option]"
);
list[currentIndex].classList.remove("select");
list[index].classList.add("select")
currentIndex = index;
scrollTipBody(list[index]);
}
/**
* 点击外部事件
* @param {*} el 监听元素
* @param {*} callback 回调函数
*/
function clickoutside(el, callback) {
let handle = function (event) {
if (el && !el.contains(event.target)) {
typeof callback == "function" && callback(removeHandle);
}
};
let removeHandle = function () {
document.removeEventListener("click", handle);
};
document.addEventListener("click", handle);
return removeHandle;
}
/**
* 编辑器改变处理事件
* @param {*} html 改变后数据
*/
function changeHandle(html) {
if (html == oldHtml || userList.length == 0) {
return;
}
oldHtml = html;
// html 即变化之后的内容
// console.log(html);
// 获取当前选区
let range = document.getSelection();
// console.log(range);
// 判断选区的起始点和终点是否在同一个位置
if (range?.focusNode?.isConnected) {
currentIndex = 0;
removeTipBody();
document.removeEventListener("keydown", keyDownFun);
if (range.focusOffset == 0) {
return;
}
// 获取选区前一个字符
let char = range.focusNode.nodeValue?.substr(
range.focusOffset - 1,
1
);
if (char == "@") {
document.addEventListener("keydown", keyDownFun);
let info = getCursorOffset(
getParentNode(range.focusNode),
range
);
// console.log(info);
let clientRect = range.focusNode.parentNode.getBoundingClientRect();
// console.log(clientRect);
tipBody = createTipBody(
clientRect.top + clientRect.height,
clientRect.left + info.width
);
document.body.appendChild(tipBody);
createUserNode(userList, tipBody);
createBaseStyle();
clickoutside(tipBody, function (removeHandle) {
removeTipBody();
removeHandle();
});
// window.div = div;
}
}
}
/**
* 创建用户节点
* @param {*} list 用户列表
* @param {*} parentNode 插入位置节点
*/
function createUserNode(list = [], parentNode) {
list.map((v, i) => {
let optionItem = document.createElement("div");
optionItem.dataset.data = "qq632951357-option";
let optionItemClassHandle = optionItem.classList;
optionItemClassHandle.add("option__item");
let imgDiv = document.createElement("div");
let imgDivClassHandle = imgDiv.classList;
imgDivClassHandle.add("option__img-div");
imgDiv.dataset.data = "qq632951357-option";
let defaultImgUrl = "https://s3.ax1x.com/2021/01/22/s5qlgU.png";
let githubImgUrl =
"https://avatars3.githubusercontent.com/" + v.name;
let imgUrl =
v.img ||
(v.type == "github" ? githubImgUrl : defaultImgUrl);
imgDiv.innerHTML = `<img class="option__img" data-data="qq632951357-option" src="${imgUrl}"/>`;
optionItem.appendChild(imgDiv);
if (i == 0) {
optionItemClassHandle.add("select");
}
let span = document.createElement("span");
span.setAttribute("class", "option__span");
span.setAttribute("data-data", "qq632951357-option");
span.innerText = "@" + v.name;
optionItem.appendChild(span);
optionItem.onmouseover = (event) => {
optionItemClassHandle.add("mouse-select");
};
optionItem.onmouseout = (event) => {
optionItemClassHandle.remove("mouse-select");
};
optionItem.onclick = (event) => {
setCurrentIndex(i);
confirmHandle();
};
parentNode.appendChild(optionItem);
});
}
/**
* 删除提示框节点
*/
function removeTipBody() {
if (tipBody) {
tipBody.remove();
tipBody = null;
}
}
/**
* 创建提示框节点
*/
function createTipBody(x, y) {
let dom = document.createElement("div");
dom.dataset.data = "qq632951357-tip";
dom.setAttribute("class", "tip-body");
dom.style.top = x + "px";
dom.style.left = y + "px";
// range.focusNode.parentNode.blur();
return dom;
}
/**
* 移动提示框的位置,使选中元素在可视区域内
*/
function scrollTipBody(node) {
if (tipBody) {
if (
tipBody.scrollTop + tipBody.offsetHeight <=
node.offsetTop + node.offsetHeight
) {
tipBody.scrollTo(
0,
node.offsetTop -
(tipBody.offsetHeight - node.offsetHeight)
);
} else if (tipBody.scrollTop > node.offsetTop) {
tipBody.scrollTo(0, node.offsetTop);
}
}
}
/**
* 创建基础样式
*/
function createBaseStyle() {
let oldStyle = document.querySelector(
"[data-data='qq632951357-style']"
);
if (!oldStyle) {
// 为提示节点增加额外的样式
let style = document.createElement("style");
style.type = "text/css";
style.dataset.data = "qq632951357-style";
let cssText = `
.tip-body[data-data="qq632951357-tip"]::-webkit-scrollbar{display:none}
.tip-body[data-data="qq632951357-tip"]{width: 200px;height: 150px;background-color: white;position: fixed;display: flex;flex-direction: column;border-radius: 10px;padding: 0 0;box-shadow: 1px 1px 24px 1px rgba(0,0,0,0.1);z-index: 99999;overflow-y: auto;}
.option__item[data-data="qq632951357-option"]{padding: 4px 10px;display: flex;align-items: center;}
.option__item[data-data="qq632951357-option"].select{background-color: rgba(239, 239, 239, 1);}
.option__item[data-data="qq632951357-option"].mouse-select:not(.select){background-color: #f3f3f3;}
.option__img[data-data="qq632951357-option"]{width:20px;height:20px;}
.option__img-div[data-data="qq632951357-option"]{border-radius: 100%;overflow: hidden;margin-right: 5px;}
.option__span[data-data="qq632951357-option"]{overflow-x: hidden;text-overflow: ellipsis;max-width: calc(100% - 25px);}
.not-select{
-moz-user-select: none; /*火狐*/
-webkit-user-select: none; /*webkit浏览器*/
-ms-user-select: none; /*IE10*/
-khtml-user-select: none; /*早期浏览器*/
user-select: none;
-webkit-touch-callout: none;
}`;
try {
style.appendChild(document.createTextNode(cssText));
} catch (ex) {
style.styleSheet.cssText = cssText; //针对IE
}
let head = document.getElementsByTagName("head")[0];
head.appendChild(style);
}
}
// 获取某元素的父元素,且该父元素的直接父元素必须为编辑框正文节点
function getParentNode(node) {
// 获取编辑框正文节点
let editorNode = document.querySelector(
`[id="${editor.textElemId}"]`
);
if (editorNode == node.parentNode) {
return node;
} else {
return getParentNode(node.parentNode);
}
}
/**
* 获取光标在元素中的偏移坐标
* @param {*} node 节点
* @param {*} range 当前选中状态数据对象
*/
function getCursorOffset(node, range) {
// console.log(range);
let oldDiv = document.querySelector(
"[data-data='qq632951357-charLen']"
);
if (oldDiv) {
oldDiv.remove();
}
let maxWindow = parseFloat(window.getComputedStyle(node).width)
let div = node.cloneNode(true);
// div.setAttribute('id', 'qq632951357-charLen');
div.style.position = "absolute";
div.style.visibility = "hidden";
div.setAttribute("data-data", "qq632951357-charLen");
// console.log(node);
// console.log(div);
deleteNode(node, div, range);
insertAfter(div, node);
let style = window.getComputedStyle(div);
let result = {
width: parseFloat(style.width) % maxWindow,
height: parseFloat(style.height),
};
// div.remove();
return result;
}
/**
* 删除多余节点
* 删除光标后面的所有元素
* @param {*} oldNode 旧节点
* @param {*} node 新节点(备用节点)
* @param {*} range 当前选中状态数据对象
*/
function deleteNode(oldNode, node, range) {
let flag = false;
let childNodes = Array.from(node.childNodes)
for (let i = 0, len = node.childNodes.length; i < len; i++) {
if (oldNode.childNodes[i] == range.focusNode) {
flag = true;
childNodes[i].textContent = childNodes[
i
].textContent.substr(0, range.focusOffset);
} else {
if (!childNodes[i]) {
continue;
}
if (flag) {
childNodes[i].remove();
} else if (childNodes[i].childNodes.length != 0) {
deleteNode(oldNode, childNodes[i], range);
}
}
}
}
/**
* 在元素插入在目标元素之后
* @param {*} newElement 要插入的元素
* @param {*} targentElement 目标元素
*/
function insertAfter(newElement, targentElement) {
let parent = targentElement.parentNode;
if (parent.lastChild == targentElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, targentElement.nextSibling);
}
}
}
})();