all files / src/js/base/editing/ Bullet.js

97.73% Statements 86/88
78.95% Branches 30/38
100% Functions 23/23
97.73% Lines 86/88
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                                                                                                                                                                                                                                        
import $ from 'jquery';
import lists from '../core/lists';
import func from '../core/func';
import dom from '../core/dom';
import range from '../core/range';
 
export default class Bullet {
  /**
   * toggle ordered list
   */
  insertOrderedList(editable) {
    this.toggleList('OL', editable);
  }
 
  /**
   * toggle unordered list
   */
  insertUnorderedList(editable) {
    this.toggleList('UL', editable);
  }
 
  /**
   * indent
   */
  indent(editable) {
    const rng = range.create(editable).wrapBodyInlineWithPara();
 
    const paras = rng.nodes(dom.isPara, { includeAncestor: true });
    const clustereds = lists.clusterBy(paras, func.peq2('parentNode'));
 
    $.each(clustereds, (idx, paras) => {
      const head = lists.head(paras);
      if (dom.isLi(head)) {
        this.wrapList(paras, head.parentNode.nodeName);
      } else {
        $.each(paras, (idx, para) => {
          $(para).css('marginLeft', (idx, val) => {
            return (parseInt(val, 10) || 0) + 25;
          });
        });
      }
    });
 
    rng.select();
  }
 
  /**
   * outdent
   */
  outdent(editable) {
    const rng = range.create(editable).wrapBodyInlineWithPara();
 
    const paras = rng.nodes(dom.isPara, { includeAncestor: true });
    const clustereds = lists.clusterBy(paras, func.peq2('parentNode'));
 
    $.each(clustereds, (idx, paras) => {
      const head = lists.head(paras);
      if (dom.isLi(head)) {
        this.releaseList([paras]);
      } else {
        $.each(paras, (idx, para) => {
          $(para).css('marginLeft', (idx, val) => {
            val = (parseInt(val, 10) || 0);
            return val > 25 ? val - 25 : '';
          });
        });
      }
    });
 
    rng.select();
  }
 
  /**
   * toggle list
   *
   * @param {String} listName - OL or UL
   */
  toggleList(listName, editable) {
    const rng = range.create(editable).wrapBodyInlineWithPara();
 
    let paras = rng.nodes(dom.isPara, { includeAncestor: true });
    const bookmark = rng.paraBookmark(paras);
    const clustereds = lists.clusterBy(paras, func.peq2('parentNode'));
 
    // paragraph to list
    if (lists.find(paras, dom.isPurePara)) {
      let wrappedParas = [];
      $.each(clustereds, (idx, paras) => {
        wrappedParas = wrappedParas.concat(this.wrapList(paras, listName));
      });
      paras = wrappedParas;
    // list to paragraph or change list style
    } else {
      const diffLists = rng.nodes(dom.isList, {
        includeAncestor: true
      }).filter((listNode) => {
        return !$.nodeName(listNode, listName);
      });
 
      if (diffLists.length) {
        $.each(diffLists, (idx, listNode) => {
          dom.replace(listNode, listName);
        });
      } else {
        paras = this.releaseList(clustereds, true);
      }
    }
 
    range.createFromParaBookmark(bookmark, paras).select();
  }
 
  /**
   * @param {Node[]} paras
   * @param {String} listName
   * @return {Node[]}
   */
  wrapList(paras, listName) {
    const head = lists.head(paras);
    const last = lists.last(paras);
 
    const prevList = dom.isList(head.previousSibling) && head.previousSibling;
    const nextList = dom.isList(last.nextSibling) && last.nextSibling;
 
    const listNode = prevList || dom.insertAfter(dom.create(listName || 'UL'), last);
 
    // P to LI
    paras = paras.map((para) => {
      return dom.isPurePara(para) ? dom.replace(para, 'LI') : para;
    });
 
    // append to list(<ul>, <ol>)
    dom.appendChildNodes(listNode, paras);
 
    Iif (nextList) {
      dom.appendChildNodes(listNode, lists.from(nextList.childNodes));
      dom.remove(nextList);
    }
 
    return paras;
  }
 
  /**
   * @method releaseList
   *
   * @param {Array[]} clustereds
   * @param {Boolean} isEscapseToBody
   * @return {Node[]}
   */
  releaseList(clustereds, isEscapseToBody) {
    let releasedParas = [];
 
    $.each(clustereds, (idx, paras) => {
      const head = lists.head(paras);
      const last = lists.last(paras);
 
      const headList = isEscapseToBody ? dom.lastAncestor(head, dom.isList) : head.parentNode;
      const lastList = headList.childNodes.length > 1 ? dom.splitTree(headList, {
        node: last.parentNode,
        offset: dom.position(last) + 1
      }, {
        isSkipPaddingBlankHTML: true
      }) : null;
 
      const middleList = dom.splitTree(headList, {
        node: head.parentNode,
        offset: dom.position(head)
      }, {
        isSkipPaddingBlankHTML: true
      });
 
      paras = isEscapseToBody ? dom.listDescendant(middleList, dom.isLi)
        : lists.from(middleList.childNodes).filter(dom.isLi);
 
      // LI to P
      if (isEscapseToBody || !dom.isList(headList.parentNode)) {
        paras = paras.map((para) => {
          return dom.replace(para, 'P');
        });
      }
 
      $.each(lists.from(paras).reverse(), (idx, para) => {
        dom.insertAfter(para, headList);
      });
 
      // remove empty lists
      const rootLists = lists.compact([headList, middleList, lastList]);
      $.each(rootLists, (idx, rootList) => {
        const listNodes = [rootList].concat(dom.listDescendant(rootList, dom.isList));
        $.each(listNodes.reverse(), (idx, listNode) => {
          Eif (!dom.nodeLength(listNode)) {
            dom.remove(listNode, true);
          }
        });
      });
 
      releasedParas = releasedParas.concat(paras);
    });
 
    return releasedParas;
  }
}