-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-DR7QqU83.js
132 lines (130 loc) · 4.11 KB
/
index-DR7QqU83.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
/*!
* sofill v1.1.10
* https://github.com/Hi-Windom/Sofill
* https://www.npmjs.com/package/sofill
* https://jsr.io/@sisi/sofill
*/
'use strict';
function getActualWidthOfChars(text, options) {
// ref https://juejin.cn/post/7091990279565082655
const { size, family = "Microsoft YaHei" } = options;
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
ctx.font = `${size}px ${family}`;
const metrics = ctx.measureText(text);
const actual = Math.abs(metrics.actualBoundingBoxLeft) +
Math.abs(metrics.actualBoundingBoxRight);
return Math.max(metrics.width, actual);
}
/**
* 向指定父级创建追加一个子元素,并可选添加ID,
* @param {Element} fatherElement
* @param {string} addElementTxt 要创建添加的元素标签
* @param {string} setId
* @returns addElementObject
*/
function addinsertCreateElement(fatherElement, addElementTxt, setId = null) {
var element = document.createElement(addElementTxt);
if (setId)
element.id = setId;
fatherElement.appendChild(element);
return element;
}
/**
* 向指定元素后创建插入一个元素,可选添加ID
* @param {*} targetElement 目标元素
* @param {*} addElementTxt 要创建添加的元素标签
* @param {*} setId 为创建元素设置ID
*/
function insertCreateAfter(targetElement, addElementTxt, setId = null) {
var element = document.createElement(addElementTxt);
if (setId)
element.id = setId;
var parent = targetElement.parentNode; //得到父节点
if (parent.lastChild === targetElement) {
parent.appendChild(element);
return element;
}
else {
parent.insertBefore(element, targetElement.nextSibling); //否则,当前节点的下一个节点之前添加
return element;
}
}
/**
* 递归DOM元素查找深度子级的第一个符合条件的元素
* @param {*} element 要查找DOM元素
* @param {*} judgeFun 查找函数 : fun(v) return true or false
* @returns element
*/
function diguiTooONE(element, judgeFun) {
if (element == null)
return null;
if (judgeFun == null)
return null;
return digui(element);
function digui(elem) {
var child = elem.children;
if ((child.length = 0))
return null;
for (let index = 0; index < child.length; index++) {
const element2 = child[index];
if (judgeFun(element2)) {
return element2;
}
else {
var item = digui(element2);
if (item == null)
continue;
return item;
}
}
return null;
}
}
function CopyDOM(from, to) {
var upperDiv = document.querySelector(from);
var belowNode = document.querySelector(to);
var newDom = upperDiv.cloneNode(true);
belowNode.appendChild(newDom);
}
function MoveDOM(from, to) {
var upperDiv = document.querySelector(from);
var belowNode = document.querySelector(to);
belowNode.appendChild(upperDiv);
}
function MoveChildren(from, to) {
var upperDiv = document.querySelector(from);
var upperUl = upperDiv.children;
var len = upperDiv.childElementCount;
var belowNode = document.querySelector(to);
for (var i = 0; i < len; i++) {
belowNode.appendChild(upperUl[0]);
}
}
const bodyAC = (N) => {
document.body.classList.add(N);
};
const bodyRC = (N) => {
document.body.classList.remove(N);
};
const bodyCC = (N) => {
return document.body.classList.contains(N);
};
const eSetProperty = (p, pv) => {
document.documentElement.style.setProperty(p, pv);
};
const eRemoveProperty = (p) => {
document.documentElement.style.removeProperty(p);
};
exports.CopyDOM = CopyDOM;
exports.MoveChildren = MoveChildren;
exports.MoveDOM = MoveDOM;
exports.addinsertCreateElement = addinsertCreateElement;
exports.bodyAC = bodyAC;
exports.bodyCC = bodyCC;
exports.bodyRC = bodyRC;
exports.diguiTooONE = diguiTooONE;
exports.eRemoveProperty = eRemoveProperty;
exports.eSetProperty = eSetProperty;
exports.getActualWidthOfChars = getActualWidthOfChars;
exports.insertCreateAfter = insertCreateAfter;