-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimal-list.js
217 lines (174 loc) · 6.53 KB
/
minimal-list.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
const minimalList = (function() {
function containingElement(node, elementNameList) {
if(!node || node.nodeName === "body") {
return null;
}
if(elementNameList.indexOf(node.nodeName) >= 0) {
return node;
}
return containingElement(node.parentNode, elementNameList);
}
function containingList(node) {
return containingElement(node, ["UL", "OL"]);
}
function atOuterIndent(currentNode) {
if(currentNode.nodeName === "OL" || currentNode.nodeName === "UL") {
return false;
}
const outerParent = containingList(currentNode).parentElement;
return outerParent != null && outerParent.hasAttribute("contentEditable");
}
function atListTop(currentNode, editorNode) {
return (currentNode.nodeName === "LI" &&
editorNode.firstChild.firstChild === currentNode);
}
function switchListType(currentNode) {
const currentOffset = window.getSelection().anchorOffset;
// make sure current node is text or list item
const listNode = containingList(currentNode);
const fragment = document.createDocumentFragment();
while(listNode.firstChild) {
fragment.appendChild(listNode.firstChild);
}
const newListType = listNode.tagName === "UL" ? "OL" : "UL";
const newListNode = document.createElement(newListType);
newListNode.appendChild(fragment);
listNode.parentNode.replaceChild(newListNode, listNode);
window.getSelection().collapse(currentNode, currentOffset);
}
function handleStrike(node) {
const currentOffset = window.getSelection().anchorOffset;
if (node.nodeName !== "#text") {
return;
}
if (node.parentNode.nodeName === "LI") {
switchStrikes(node.parentNode, true);
} else if(node.parentNode.nodeName === "STRIKE") {
switchStrikes(node.parentNode.parentNode, false);
}
window.getSelection().collapse(node, currentOffset);
}
function switchStrikes(listItem, strikeOn) {
// only apply to list items
if(listItem.nodeName !== "LI") {
return;
}
const text = listItem.firstChild;
if(strikeOn && text.nodeName === "#text") {
const strike = document.createElement("strike");
strike.appendChild(text);
listItem.appendChild(strike);
} else if(!strikeOn && text.nodeName === "STRIKE") {
listItem.appendChild(text.firstChild);
text.remove();
}
// get sublist
listItem.id = "current";
const sublist = document.querySelector("#current+ol,#current+ul");
listItem.removeAttribute("id");
if(sublist) {
// change strikes in sublist
for(let child of sublist.children) {
switchStrikes(child, strikeOn);
}
}
}
function isList(node) {
return ["UL", "OL"].indexOf(node.nodeName) >= 0;
}
// Replacement for document.execCommand("indent")
function indent(node) {
const offset = window.getSelection().anchorOffset;
const listItem = containingElement(node, ["LI"]);
const prevSibling = listItem.previousSibling;
const nextSibling = listItem.nextSibling;
// defined in both branches below
let newList = undefined;
// if previous sibling is list, append to that list
if (prevSibling && isList(prevSibling)) {
prevSibling.appendChild(listItem);
newList = prevSibling;
}
// otherwise create new list
else {
const listNode = containingList(node);
const listType = listNode.nodeName;
const innerList = document.createElement(listType);
listNode.replaceChild(innerList, listItem);
innerList.appendChild(listItem);
newList = innerList;
}
// if next sibling is list, append its items to current item's new list
if (nextSibling && isList(nextSibling)) {
while(nextSibling.firstChild) {
newList.appendChild(nextSibling.firstChild);
}
nextSibling.remove();
}
// restore cursor
window.getSelection().collapse(node, offset);
}
function handleKeyUp() {
const currentNode = window.getSelection().anchorNode;
let listItem = containingElement(currentNode, ["LI"]);
if(listItem) {
for (let child of listItem.children) {
if (child.nodeName === "BR") {
child.remove();
}
}
}
}
function handleKeyDown(kbEvent) {
const editorNode = this;
const currentNode = window.getSelection().anchorNode;
switch (kbEvent.key) {
case "Tab":
// prevent tabbing out of editor
kbEvent.preventDefault();
if (kbEvent.shiftKey) {
// only outdent if not at top level
if (!atOuterIndent(currentNode)) {
document.execCommand("outdent");
}
} else {
indent(currentNode);
}
break;
// prevent the top level list from being deleted
case "Enter":
case "Backspace":
if (atListTop(currentNode, editorNode)) {
kbEvent.preventDefault();
}
break;
case "l":
if(kbEvent.ctrlKey) {
switchListType(currentNode);
}
break;
case "x":
if(kbEvent.ctrlKey) {
handleStrike(currentNode);
}
break;
default:
break;
}
}
function minimalList (selector, fresh) {
const container = document.querySelector(selector);
container.setAttribute("contentEditable", "true");
if (fresh) {
const list = document.createElement("ol");
list.appendChild(document.createElement("li"));
container.appendChild(list);
} else {
// normalize existing editor content
container.innerHTML = container.innerHTML.replace(/(\r|\n|\r\n)/gm, "");
}
container.addEventListener("keydown", handleKeyDown);
container.addEventListener("keyup", handleKeyUp)
}
return minimalList;
})();