WIP: Note editing, markdown to html
[oweals/karmaworld.git] / karmaworld / apps / wysihtml5 / static / wysihtml5 / wysihtml-0.4.17 / src / commands / outdentList.js
1 wysihtml5.commands.outdentList = {
2   exec: function(composer, command, value) {
3     var listEls = composer.selection.getSelectionParentsByTag('LI');
4     if (listEls) {
5       return this.tryToPullLiLevel(listEls, composer);
6     }
7     return false;
8   },
9
10   state: function(composer, command) {
11       return false;
12   },
13
14   tryToPullLiLevel: function(liNodes, composer) {
15     var listNode, outerListNode, outerLiNode, list, prevLi, liNode, afterList,
16         found = false,
17         that = this;
18
19     composer.selection.executeAndRestoreRangy(function() {
20
21       for (var i = liNodes.length; i--;) {
22         liNode = liNodes[i];
23         if (liNode.parentNode) {
24           listNode = liNode.parentNode;
25
26           if (listNode.tagName === 'OL' || listNode.tagName === 'UL') {
27             found = true;
28
29             outerListNode = wysihtml5.dom.getParentElement(listNode.parentNode, { nodeName: ['OL', 'UL']}, false, composer.element);
30             outerLiNode = wysihtml5.dom.getParentElement(listNode.parentNode, { nodeName: ['LI']}, false, composer.element);
31
32             if (outerListNode && outerLiNode) {
33
34               if (liNode.nextSibling) {
35                 afterList = that.getAfterList(listNode, liNode);
36                 liNode.appendChild(afterList);
37               }
38               outerListNode.insertBefore(liNode, outerLiNode.nextSibling);
39
40             } else {
41
42               if (liNode.nextSibling) {
43                 afterList = that.getAfterList(listNode, liNode);
44                 liNode.appendChild(afterList);
45               }
46
47               for (var j = liNode.childNodes.length; j--;) {
48                 listNode.parentNode.insertBefore(liNode.childNodes[j], listNode.nextSibling);
49               }
50
51               listNode.parentNode.insertBefore(document.createElement('br'), listNode.nextSibling);
52               liNode.parentNode.removeChild(liNode);
53
54             }
55
56             // cleanup
57             if (listNode.childNodes.length === 0) {
58                 listNode.parentNode.removeChild(listNode);
59             }
60           }
61         }
62       }
63
64     });
65     return found;
66   },
67
68   getAfterList: function(listNode, liNode) {
69     var nodeName = listNode.nodeName,
70         newList = document.createElement(nodeName);
71
72     while (liNode.nextSibling) {
73       newList.appendChild(liNode.nextSibling);
74     }
75     return newList;
76   }
77
78 };