-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-inline-style.js
161 lines (159 loc) · 4.9 KB
/
set-inline-style.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
// DOM structure should be like this:
// `
// <SOMETAG>
// <div>
// <span></span>
// ...
// </div>
// ...
// </SOMETAG>
// `
export default ({
inlineStyle,
styleValue,
isToggleable = true,
normalValue = `initial`,
execInElements = [document]
}) => {
const range = getSelection().getRangeAt(0);
const commonAncestorContainer = range.commonAncestorContainer;
// check range not empty and in target
let fi = [].findIndex.call(execInElements, (value) => {
return value.contains(commonAncestorContainer);
});
if (range.toString() === `` || fi === -1) {
return;
}
const startContainer = range.startContainer;
const endContainer = range.endContainer;
const startElement =
startContainer.nodeName === `#text`
? startContainer.parentElement
: startContainer;
const endElement =
endContainer.nodeName === `#text`
? endContainer.parentElement
: endContainer;
const startLineElement = startElement.parentElement;
const endLineElement =
endElement.tagName === `DIV` ? endElement : endElement.parentElement;
const df = range.cloneContents();
let spanList = df.querySelectorAll(`span`);
let divList = df.querySelectorAll(`div`);
// debugger
if (spanList.length < 1) {
let styleStr = startElement.getAttribute(`style`) || ``;
if (isToggleable) {
styleValue =
startElement.style[inlineStyle] === styleValue
? normalValue
: styleValue;
}
if (startElement.textContent !== df.textContent) {
let spanELement = document.createElement(`span`);
spanELement.setAttribute(`style`, styleStr);
spanELement.style[inlineStyle] = styleValue;
range.surroundContents(spanELement);
const container = document.createElement(`div`);
container.innerHTML = `<span style="${styleStr}">${
startElement.childNodes[0].nodeValue
}</span>${spanELement.outerHTML}<span style="${styleStr}">${
startElement.childNodes[2].nodeValue
}</span>`;
[].filter
.call(container.children, (element) => {
return element.textContent === ``;
})
.forEach((element) => {
element.remove();
});
startElement.outerHTML = container.innerHTML;
} else {
startElement.style[inlineStyle] = styleValue;
}
} else if (divList.length < 1) {
if (isToggleable) {
styleValue =
[].findIndex.call(spanList, (element) => {
return element.style[inlineStyle] !== styleValue;
}) === -1
? normalValue
: styleValue;
}
[].forEach.call(spanList, (element) => {
element.style[inlineStyle] = styleValue;
});
range.deleteContents();
[].filter
.call(commonAncestorContainer.querySelectorAll(`span`), (element) => {
return element.textContent === ``;
})
.forEach((element) => {
element.remove();
});
range.insertNode(df);
} else {
if (isToggleable) {
styleValue =
[].findIndex.call(spanList, (element) => {
return element.style[inlineStyle] !== styleValue;
}) === -1
? normalValue
: styleValue;
}
[].forEach.call(spanList, (element) => {
element.style[inlineStyle] = styleValue;
});
const firstElementText = df.firstElementChild.firstElementChild.textContent;
const lastElementText =
df.lastElementChild.innerHTML === ``
? ``
: df.lastElementChild.lastElementChild.textContent;
const lineStartText =
startElement.parentElement.firstElementChild.textContent;
const lineEndText =
endElement.tagName === `SPAN`
? endElement.parentElement.lastElementChild.textContent
: endElement.textContent;
let isFromLineHead = firstElementText === lineStartText;
let isToLineEnd = lastElementText === lineEndText;
range.deleteContents();
[].filter
.call(commonAncestorContainer.querySelectorAll(`span`), (element) => {
return element.innerHTML === ``;
})
.forEach((element) => {
element.remove();
});
[].filter
.call(commonAncestorContainer.querySelectorAll(`div`), (element) => {
return (
element.querySelector(`span`) === null ||
(element.firstElementChild.textContent === `` &&
element.querySelector(`br`) === null)
);
})
.forEach((element) => {
element.remove();
});
if (!isFromLineHead) {
startLineElement.insertAdjacentHTML(
`beforeend`,
df.firstElementChild.innerHTML
);
df.firstElementChild.remove();
}
if (!isToLineEnd) {
endLineElement.insertAdjacentHTML(
`afterbegin`,
df.lastElementChild.innerHTML
);
df.lastElementChild.remove();
}
if (df.children.length > 0 && df.lastElementChild.innerHTML === ``) {
df.lastElementChild.remove();
}
range.insertNode(df);
}
getSelection().removeAllRanges();
};