index.cjs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var prosemirrorTransform = require('prosemirror-transform');
  6. var prosemirrorModel = require('prosemirror-model');
  7. var prosemirrorState = require('prosemirror-state');
  8. var olDOM = ["ol", 0],
  9. ulDOM = ["ul", 0],
  10. liDOM = ["li", 0];
  11. var orderedList = {
  12. attrs: {
  13. order: {
  14. "default": 1
  15. }
  16. },
  17. parseDOM: [{
  18. tag: "ol",
  19. getAttrs: function getAttrs(dom) {
  20. return {
  21. order: dom.hasAttribute("start") ? +dom.getAttribute("start") : 1
  22. };
  23. }
  24. }],
  25. toDOM: function toDOM(node) {
  26. return node.attrs.order == 1 ? olDOM : ["ol", {
  27. start: node.attrs.order
  28. }, 0];
  29. }
  30. };
  31. var bulletList = {
  32. parseDOM: [{
  33. tag: "ul"
  34. }],
  35. toDOM: function toDOM() {
  36. return ulDOM;
  37. }
  38. };
  39. var listItem = {
  40. parseDOM: [{
  41. tag: "li"
  42. }],
  43. toDOM: function toDOM() {
  44. return liDOM;
  45. },
  46. defining: true
  47. };
  48. function add(obj, props) {
  49. var copy = {};
  50. for (var prop in obj) {
  51. copy[prop] = obj[prop];
  52. }
  53. for (var _prop in props) {
  54. copy[_prop] = props[_prop];
  55. }
  56. return copy;
  57. }
  58. function addListNodes(nodes, itemContent, listGroup) {
  59. return nodes.append({
  60. ordered_list: add(orderedList, {
  61. content: "list_item+",
  62. group: listGroup
  63. }),
  64. bullet_list: add(bulletList, {
  65. content: "list_item+",
  66. group: listGroup
  67. }),
  68. list_item: add(listItem, {
  69. content: itemContent
  70. })
  71. });
  72. }
  73. function wrapInList(listType) {
  74. var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  75. return function (state, dispatch) {
  76. var _state$selection = state.selection,
  77. $from = _state$selection.$from,
  78. $to = _state$selection.$to;
  79. var range = $from.blockRange($to),
  80. doJoin = false,
  81. outerRange = range;
  82. if (!range) return false;
  83. if (range.depth >= 2 && $from.node(range.depth - 1).type.compatibleContent(listType) && range.startIndex == 0) {
  84. if ($from.index(range.depth - 1) == 0) return false;
  85. var $insert = state.doc.resolve(range.start - 2);
  86. outerRange = new prosemirrorModel.NodeRange($insert, $insert, range.depth);
  87. if (range.endIndex < range.parent.childCount) range = new prosemirrorModel.NodeRange($from, state.doc.resolve($to.end(range.depth)), range.depth);
  88. doJoin = true;
  89. }
  90. var wrap = prosemirrorTransform.findWrapping(outerRange, listType, attrs, range);
  91. if (!wrap) return false;
  92. if (dispatch) dispatch(doWrapInList(state.tr, range, wrap, doJoin, listType).scrollIntoView());
  93. return true;
  94. };
  95. }
  96. function doWrapInList(tr, range, wrappers, joinBefore, listType) {
  97. var content = prosemirrorModel.Fragment.empty;
  98. for (var i = wrappers.length - 1; i >= 0; i--) {
  99. content = prosemirrorModel.Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content));
  100. }
  101. tr.step(new prosemirrorTransform.ReplaceAroundStep(range.start - (joinBefore ? 2 : 0), range.end, range.start, range.end, new prosemirrorModel.Slice(content, 0, 0), wrappers.length, true));
  102. var found = 0;
  103. for (var _i = 0; _i < wrappers.length; _i++) {
  104. if (wrappers[_i].type == listType) found = _i + 1;
  105. }
  106. var splitDepth = wrappers.length - found;
  107. var splitPos = range.start + wrappers.length - (joinBefore ? 2 : 0),
  108. parent = range.parent;
  109. for (var _i2 = range.startIndex, e = range.endIndex, first = true; _i2 < e; _i2++, first = false) {
  110. if (!first && prosemirrorTransform.canSplit(tr.doc, splitPos, splitDepth)) {
  111. tr.split(splitPos, splitDepth);
  112. splitPos += 2 * splitDepth;
  113. }
  114. splitPos += parent.child(_i2).nodeSize;
  115. }
  116. return tr;
  117. }
  118. function splitListItem(itemType, itemAttrs) {
  119. return function (state, dispatch) {
  120. var _state$selection2 = state.selection,
  121. $from = _state$selection2.$from,
  122. $to = _state$selection2.$to,
  123. node = _state$selection2.node;
  124. if (node && node.isBlock || $from.depth < 2 || !$from.sameParent($to)) return false;
  125. var grandParent = $from.node(-1);
  126. if (grandParent.type != itemType) return false;
  127. if ($from.parent.content.size == 0 && $from.node(-1).childCount == $from.indexAfter(-1)) {
  128. if ($from.depth == 3 || $from.node(-3).type != itemType || $from.index(-2) != $from.node(-2).childCount - 1) return false;
  129. if (dispatch) {
  130. var wrap = prosemirrorModel.Fragment.empty;
  131. var depthBefore = $from.index(-1) ? 1 : $from.index(-2) ? 2 : 3;
  132. for (var d = $from.depth - depthBefore; d >= $from.depth - 3; d--) {
  133. wrap = prosemirrorModel.Fragment.from($from.node(d).copy(wrap));
  134. }
  135. var depthAfter = $from.indexAfter(-1) < $from.node(-2).childCount ? 1 : $from.indexAfter(-2) < $from.node(-3).childCount ? 2 : 3;
  136. wrap = wrap.append(prosemirrorModel.Fragment.from(itemType.createAndFill()));
  137. var start = $from.before($from.depth - (depthBefore - 1));
  138. var _tr = state.tr.replace(start, $from.after(-depthAfter), new prosemirrorModel.Slice(wrap, 4 - depthBefore, 0));
  139. var sel = -1;
  140. _tr.doc.nodesBetween(start, _tr.doc.content.size, function (node, pos) {
  141. if (sel > -1) return false;
  142. if (node.isTextblock && node.content.size == 0) sel = pos + 1;
  143. });
  144. if (sel > -1) _tr.setSelection(prosemirrorState.Selection.near(_tr.doc.resolve(sel)));
  145. dispatch(_tr.scrollIntoView());
  146. }
  147. return true;
  148. }
  149. var nextType = $to.pos == $from.end() ? grandParent.contentMatchAt(0).defaultType : null;
  150. var tr = state.tr["delete"]($from.pos, $to.pos);
  151. var types = nextType ? [itemAttrs ? {
  152. type: itemType,
  153. attrs: itemAttrs
  154. } : null, {
  155. type: nextType
  156. }] : undefined;
  157. if (!prosemirrorTransform.canSplit(tr.doc, $from.pos, 2, types)) return false;
  158. if (dispatch) dispatch(tr.split($from.pos, 2, types).scrollIntoView());
  159. return true;
  160. };
  161. }
  162. function liftListItem(itemType) {
  163. return function (state, dispatch) {
  164. var _state$selection3 = state.selection,
  165. $from = _state$selection3.$from,
  166. $to = _state$selection3.$to;
  167. var range = $from.blockRange($to, function (node) {
  168. return node.childCount > 0 && node.firstChild.type == itemType;
  169. });
  170. if (!range) return false;
  171. if (!dispatch) return true;
  172. if ($from.node(range.depth - 1).type == itemType) return liftToOuterList(state, dispatch, itemType, range);else return liftOutOfList(state, dispatch, range);
  173. };
  174. }
  175. function liftToOuterList(state, dispatch, itemType, range) {
  176. var tr = state.tr,
  177. end = range.end,
  178. endOfList = range.$to.end(range.depth);
  179. if (end < endOfList) {
  180. tr.step(new prosemirrorTransform.ReplaceAroundStep(end - 1, endOfList, end, endOfList, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(itemType.create(null, range.parent.copy())), 1, 0), 1, true));
  181. range = new prosemirrorModel.NodeRange(tr.doc.resolve(range.$from.pos), tr.doc.resolve(endOfList), range.depth);
  182. }
  183. var target = prosemirrorTransform.liftTarget(range);
  184. if (target == null) return false;
  185. tr.lift(range, target);
  186. var after = tr.mapping.map(end, -1) - 1;
  187. if (prosemirrorTransform.canJoin(tr.doc, after)) tr.join(after);
  188. dispatch(tr.scrollIntoView());
  189. return true;
  190. }
  191. function liftOutOfList(state, dispatch, range) {
  192. var tr = state.tr,
  193. list = range.parent;
  194. for (var pos = range.end, i = range.endIndex - 1, e = range.startIndex; i > e; i--) {
  195. pos -= list.child(i).nodeSize;
  196. tr["delete"](pos - 1, pos + 1);
  197. }
  198. var $start = tr.doc.resolve(range.start),
  199. item = $start.nodeAfter;
  200. if (tr.mapping.map(range.end) != range.start + $start.nodeAfter.nodeSize) return false;
  201. var atStart = range.startIndex == 0,
  202. atEnd = range.endIndex == list.childCount;
  203. var parent = $start.node(-1),
  204. indexBefore = $start.index(-1);
  205. if (!parent.canReplace(indexBefore + (atStart ? 0 : 1), indexBefore + 1, item.content.append(atEnd ? prosemirrorModel.Fragment.empty : prosemirrorModel.Fragment.from(list)))) return false;
  206. var start = $start.pos,
  207. end = start + item.nodeSize;
  208. tr.step(new prosemirrorTransform.ReplaceAroundStep(start - (atStart ? 1 : 0), end + (atEnd ? 1 : 0), start + 1, end - 1, new prosemirrorModel.Slice((atStart ? prosemirrorModel.Fragment.empty : prosemirrorModel.Fragment.from(list.copy(prosemirrorModel.Fragment.empty))).append(atEnd ? prosemirrorModel.Fragment.empty : prosemirrorModel.Fragment.from(list.copy(prosemirrorModel.Fragment.empty))), atStart ? 0 : 1, atEnd ? 0 : 1), atStart ? 0 : 1));
  209. dispatch(tr.scrollIntoView());
  210. return true;
  211. }
  212. function sinkListItem(itemType) {
  213. return function (state, dispatch) {
  214. var _state$selection4 = state.selection,
  215. $from = _state$selection4.$from,
  216. $to = _state$selection4.$to;
  217. var range = $from.blockRange($to, function (node) {
  218. return node.childCount > 0 && node.firstChild.type == itemType;
  219. });
  220. if (!range) return false;
  221. var startIndex = range.startIndex;
  222. if (startIndex == 0) return false;
  223. var parent = range.parent,
  224. nodeBefore = parent.child(startIndex - 1);
  225. if (nodeBefore.type != itemType) return false;
  226. if (dispatch) {
  227. var nestedBefore = nodeBefore.lastChild && nodeBefore.lastChild.type == parent.type;
  228. var inner = prosemirrorModel.Fragment.from(nestedBefore ? itemType.create() : null);
  229. var slice = new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(itemType.create(null, prosemirrorModel.Fragment.from(parent.type.create(null, inner)))), nestedBefore ? 3 : 1, 0);
  230. var before = range.start,
  231. after = range.end;
  232. dispatch(state.tr.step(new prosemirrorTransform.ReplaceAroundStep(before - (nestedBefore ? 3 : 1), after, before, after, slice, 1, true)).scrollIntoView());
  233. }
  234. return true;
  235. };
  236. }
  237. exports.addListNodes = addListNodes;
  238. exports.bulletList = bulletList;
  239. exports.liftListItem = liftListItem;
  240. exports.listItem = listItem;
  241. exports.orderedList = orderedList;
  242. exports.sinkListItem = sinkListItem;
  243. exports.splitListItem = splitListItem;
  244. exports.wrapInList = wrapInList;