This repository has been archived by the owner on Mar 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
insertOrderedList.js
50 lines (47 loc) · 1.84 KB
/
insertOrderedList.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
wysihtml5.commands.insertOrderedList = {
exec: function(composer, command) {
var doc = composer.doc,
selectedNode = composer.selection.getSelectedNode(),
list = wysihtml5.dom.getParentElement(selectedNode, { nodeName: "OL" }),
otherList = wysihtml5.dom.getParentElement(selectedNode, { nodeName: "UL" }),
tempClassName = "_wysihtml5-temp-" + new Date().getTime(),
isEmpty,
tempElement;
if (!list && !otherList && composer.commands.support(command)) {
doc.execCommand(command, false, null);
return;
}
if (list) {
// Unwrap list
// <ol><li>foo</li><li>bar</li></ol>
// becomes:
// foo<br>bar<br>
composer.selection.executeAndRestore(function() {
wysihtml5.dom.resolveList(list, composer.config.useLineBreaks);
});
} else if (otherList) {
// Turn an unordered list into an ordered list
// <ul><li>foo</li><li>bar</li></ul>
// becomes:
// <ol><li>foo</li><li>bar</li></ol>
composer.selection.executeAndRestore(function() {
wysihtml5.dom.renameElement(otherList, "ol");
});
} else {
// Create list
composer.commands.exec("formatBlock", "div", tempClassName);
tempElement = doc.querySelector("." + tempClassName);
isEmpty = tempElement.innerHTML === "" || tempElement.innerHTML === wysihtml5.INVISIBLE_SPACE || tempElement.innerHTML === "<br>";
composer.selection.executeAndRestore(function() {
list = wysihtml5.dom.convertToList(tempElement, "ol");
});
if (isEmpty) {
composer.selection.selectNode(list.querySelector("li"), true);
}
}
},
state: function(composer) {
var selectedNode = composer.selection.getSelectedNode();
return wysihtml5.dom.getParentElement(selectedNode, { nodeName: "OL" });
}
};