index.cjs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. 'use strict';
  2. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  3. Object.defineProperty(exports, '__esModule', {
  4. value: true
  5. });
  6. var prosemirrorTransform = require('prosemirror-transform');
  7. var prosemirrorModel = require('prosemirror-model');
  8. var prosemirrorState = require('prosemirror-state');
  9. var deleteSelection = function deleteSelection(state, dispatch) {
  10. if (state.selection.empty) return false;
  11. if (dispatch) dispatch(state.tr.deleteSelection().scrollIntoView());
  12. return true;
  13. };
  14. function atBlockStart(state, view) {
  15. var $cursor = state.selection.$cursor;
  16. if (!$cursor || (view ? !view.endOfTextblock("backward", state) : $cursor.parentOffset > 0)) return null;
  17. return $cursor;
  18. }
  19. var joinBackward = function joinBackward(state, dispatch, view) {
  20. var $cursor = atBlockStart(state, view);
  21. if (!$cursor) return false;
  22. var $cut = findCutBefore($cursor);
  23. if (!$cut) {
  24. var range = $cursor.blockRange(),
  25. target = range && prosemirrorTransform.liftTarget(range);
  26. if (target == null) return false;
  27. if (dispatch) dispatch(state.tr.lift(range, target).scrollIntoView());
  28. return true;
  29. }
  30. var before = $cut.nodeBefore;
  31. if (!before.type.spec.isolating && deleteBarrier(state, $cut, dispatch)) return true;
  32. if ($cursor.parent.content.size == 0 && (textblockAt(before, "end") || prosemirrorState.NodeSelection.isSelectable(before))) {
  33. var delStep = prosemirrorTransform.replaceStep(state.doc, $cursor.before(), $cursor.after(), prosemirrorModel.Slice.empty);
  34. if (delStep && delStep.slice.size < delStep.to - delStep.from) {
  35. if (dispatch) {
  36. var tr = state.tr.step(delStep);
  37. tr.setSelection(textblockAt(before, "end") ? prosemirrorState.Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos, -1)), -1) : prosemirrorState.NodeSelection.create(tr.doc, $cut.pos - before.nodeSize));
  38. dispatch(tr.scrollIntoView());
  39. }
  40. return true;
  41. }
  42. }
  43. if (before.isAtom && $cut.depth == $cursor.depth - 1) {
  44. if (dispatch) dispatch(state.tr["delete"]($cut.pos - before.nodeSize, $cut.pos).scrollIntoView());
  45. return true;
  46. }
  47. return false;
  48. };
  49. var joinTextblockBackward = function joinTextblockBackward(state, dispatch, view) {
  50. var $cursor = atBlockStart(state, view);
  51. if (!$cursor) return false;
  52. var $cut = findCutBefore($cursor);
  53. return $cut ? joinTextblocksAround(state, $cut, dispatch) : false;
  54. };
  55. var joinTextblockForward = function joinTextblockForward(state, dispatch, view) {
  56. var $cursor = atBlockEnd(state, view);
  57. if (!$cursor) return false;
  58. var $cut = findCutAfter($cursor);
  59. return $cut ? joinTextblocksAround(state, $cut, dispatch) : false;
  60. };
  61. function joinTextblocksAround(state, $cut, dispatch) {
  62. var before = $cut.nodeBefore,
  63. beforeText = before,
  64. beforePos = $cut.pos - 1;
  65. for (; !beforeText.isTextblock; beforePos--) {
  66. if (beforeText.type.spec.isolating) return false;
  67. var child = beforeText.lastChild;
  68. if (!child) return false;
  69. beforeText = child;
  70. }
  71. var after = $cut.nodeAfter,
  72. afterText = after,
  73. afterPos = $cut.pos + 1;
  74. for (; !afterText.isTextblock; afterPos++) {
  75. if (afterText.type.spec.isolating) return false;
  76. var _child = afterText.firstChild;
  77. if (!_child) return false;
  78. afterText = _child;
  79. }
  80. var step = prosemirrorTransform.replaceStep(state.doc, beforePos, afterPos, prosemirrorModel.Slice.empty);
  81. if (!step || step.from != beforePos || step instanceof prosemirrorTransform.ReplaceStep && step.slice.size >= afterPos - beforePos) return false;
  82. if (dispatch) {
  83. var tr = state.tr.step(step);
  84. tr.setSelection(prosemirrorState.TextSelection.create(tr.doc, beforePos));
  85. dispatch(tr.scrollIntoView());
  86. }
  87. return true;
  88. }
  89. function textblockAt(node, side) {
  90. var only = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  91. for (var scan = node; scan; scan = side == "start" ? scan.firstChild : scan.lastChild) {
  92. if (scan.isTextblock) return true;
  93. if (only && scan.childCount != 1) return false;
  94. }
  95. return false;
  96. }
  97. var selectNodeBackward = function selectNodeBackward(state, dispatch, view) {
  98. var _state$selection = state.selection,
  99. $head = _state$selection.$head,
  100. empty = _state$selection.empty,
  101. $cut = $head;
  102. if (!empty) return false;
  103. if ($head.parent.isTextblock) {
  104. if (view ? !view.endOfTextblock("backward", state) : $head.parentOffset > 0) return false;
  105. $cut = findCutBefore($head);
  106. }
  107. var node = $cut && $cut.nodeBefore;
  108. if (!node || !prosemirrorState.NodeSelection.isSelectable(node)) return false;
  109. if (dispatch) dispatch(state.tr.setSelection(prosemirrorState.NodeSelection.create(state.doc, $cut.pos - node.nodeSize)).scrollIntoView());
  110. return true;
  111. };
  112. function findCutBefore($pos) {
  113. if (!$pos.parent.type.spec.isolating) for (var i = $pos.depth - 1; i >= 0; i--) {
  114. if ($pos.index(i) > 0) return $pos.doc.resolve($pos.before(i + 1));
  115. if ($pos.node(i).type.spec.isolating) break;
  116. }
  117. return null;
  118. }
  119. function atBlockEnd(state, view) {
  120. var $cursor = state.selection.$cursor;
  121. if (!$cursor || (view ? !view.endOfTextblock("forward", state) : $cursor.parentOffset < $cursor.parent.content.size)) return null;
  122. return $cursor;
  123. }
  124. var joinForward = function joinForward(state, dispatch, view) {
  125. var $cursor = atBlockEnd(state, view);
  126. if (!$cursor) return false;
  127. var $cut = findCutAfter($cursor);
  128. if (!$cut) return false;
  129. var after = $cut.nodeAfter;
  130. if (deleteBarrier(state, $cut, dispatch)) return true;
  131. if ($cursor.parent.content.size == 0 && (textblockAt(after, "start") || prosemirrorState.NodeSelection.isSelectable(after))) {
  132. var delStep = prosemirrorTransform.replaceStep(state.doc, $cursor.before(), $cursor.after(), prosemirrorModel.Slice.empty);
  133. if (delStep && delStep.slice.size < delStep.to - delStep.from) {
  134. if (dispatch) {
  135. var tr = state.tr.step(delStep);
  136. tr.setSelection(textblockAt(after, "start") ? prosemirrorState.Selection.findFrom(tr.doc.resolve(tr.mapping.map($cut.pos)), 1) : prosemirrorState.NodeSelection.create(tr.doc, tr.mapping.map($cut.pos)));
  137. dispatch(tr.scrollIntoView());
  138. }
  139. return true;
  140. }
  141. }
  142. if (after.isAtom && $cut.depth == $cursor.depth - 1) {
  143. if (dispatch) dispatch(state.tr["delete"]($cut.pos, $cut.pos + after.nodeSize).scrollIntoView());
  144. return true;
  145. }
  146. return false;
  147. };
  148. var selectNodeForward = function selectNodeForward(state, dispatch, view) {
  149. var _state$selection2 = state.selection,
  150. $head = _state$selection2.$head,
  151. empty = _state$selection2.empty,
  152. $cut = $head;
  153. if (!empty) return false;
  154. if ($head.parent.isTextblock) {
  155. if (view ? !view.endOfTextblock("forward", state) : $head.parentOffset < $head.parent.content.size) return false;
  156. $cut = findCutAfter($head);
  157. }
  158. var node = $cut && $cut.nodeAfter;
  159. if (!node || !prosemirrorState.NodeSelection.isSelectable(node)) return false;
  160. if (dispatch) dispatch(state.tr.setSelection(prosemirrorState.NodeSelection.create(state.doc, $cut.pos)).scrollIntoView());
  161. return true;
  162. };
  163. function findCutAfter($pos) {
  164. if (!$pos.parent.type.spec.isolating) for (var i = $pos.depth - 1; i >= 0; i--) {
  165. var parent = $pos.node(i);
  166. if ($pos.index(i) + 1 < parent.childCount) return $pos.doc.resolve($pos.after(i + 1));
  167. if (parent.type.spec.isolating) break;
  168. }
  169. return null;
  170. }
  171. var joinUp = function joinUp(state, dispatch) {
  172. var sel = state.selection,
  173. nodeSel = sel instanceof prosemirrorState.NodeSelection,
  174. point;
  175. if (nodeSel) {
  176. if (sel.node.isTextblock || !prosemirrorTransform.canJoin(state.doc, sel.from)) return false;
  177. point = sel.from;
  178. } else {
  179. point = prosemirrorTransform.joinPoint(state.doc, sel.from, -1);
  180. if (point == null) return false;
  181. }
  182. if (dispatch) {
  183. var tr = state.tr.join(point);
  184. if (nodeSel) tr.setSelection(prosemirrorState.NodeSelection.create(tr.doc, point - state.doc.resolve(point).nodeBefore.nodeSize));
  185. dispatch(tr.scrollIntoView());
  186. }
  187. return true;
  188. };
  189. var joinDown = function joinDown(state, dispatch) {
  190. var sel = state.selection,
  191. point;
  192. if (sel instanceof prosemirrorState.NodeSelection) {
  193. if (sel.node.isTextblock || !prosemirrorTransform.canJoin(state.doc, sel.to)) return false;
  194. point = sel.to;
  195. } else {
  196. point = prosemirrorTransform.joinPoint(state.doc, sel.to, 1);
  197. if (point == null) return false;
  198. }
  199. if (dispatch) dispatch(state.tr.join(point).scrollIntoView());
  200. return true;
  201. };
  202. var lift = function lift(state, dispatch) {
  203. var _state$selection3 = state.selection,
  204. $from = _state$selection3.$from,
  205. $to = _state$selection3.$to;
  206. var range = $from.blockRange($to),
  207. target = range && prosemirrorTransform.liftTarget(range);
  208. if (target == null) return false;
  209. if (dispatch) dispatch(state.tr.lift(range, target).scrollIntoView());
  210. return true;
  211. };
  212. var newlineInCode = function newlineInCode(state, dispatch) {
  213. var _state$selection4 = state.selection,
  214. $head = _state$selection4.$head,
  215. $anchor = _state$selection4.$anchor;
  216. if (!$head.parent.type.spec.code || !$head.sameParent($anchor)) return false;
  217. if (dispatch) dispatch(state.tr.insertText("\n").scrollIntoView());
  218. return true;
  219. };
  220. function defaultBlockAt(match) {
  221. for (var i = 0; i < match.edgeCount; i++) {
  222. var _match$edge = match.edge(i),
  223. type = _match$edge.type;
  224. if (type.isTextblock && !type.hasRequiredAttrs()) return type;
  225. }
  226. return null;
  227. }
  228. var exitCode = function exitCode(state, dispatch) {
  229. var _state$selection5 = state.selection,
  230. $head = _state$selection5.$head,
  231. $anchor = _state$selection5.$anchor;
  232. if (!$head.parent.type.spec.code || !$head.sameParent($anchor)) return false;
  233. var above = $head.node(-1),
  234. after = $head.indexAfter(-1),
  235. type = defaultBlockAt(above.contentMatchAt(after));
  236. if (!type || !above.canReplaceWith(after, after, type)) return false;
  237. if (dispatch) {
  238. var pos = $head.after(),
  239. tr = state.tr.replaceWith(pos, pos, type.createAndFill());
  240. tr.setSelection(prosemirrorState.Selection.near(tr.doc.resolve(pos), 1));
  241. dispatch(tr.scrollIntoView());
  242. }
  243. return true;
  244. };
  245. var createParagraphNear = function createParagraphNear(state, dispatch) {
  246. var sel = state.selection,
  247. $from = sel.$from,
  248. $to = sel.$to;
  249. if (sel instanceof prosemirrorState.AllSelection || $from.parent.inlineContent || $to.parent.inlineContent) return false;
  250. var type = defaultBlockAt($to.parent.contentMatchAt($to.indexAfter()));
  251. if (!type || !type.isTextblock) return false;
  252. if (dispatch) {
  253. var side = (!$from.parentOffset && $to.index() < $to.parent.childCount ? $from : $to).pos;
  254. var tr = state.tr.insert(side, type.createAndFill());
  255. tr.setSelection(prosemirrorState.TextSelection.create(tr.doc, side + 1));
  256. dispatch(tr.scrollIntoView());
  257. }
  258. return true;
  259. };
  260. var liftEmptyBlock = function liftEmptyBlock(state, dispatch) {
  261. var $cursor = state.selection.$cursor;
  262. if (!$cursor || $cursor.parent.content.size) return false;
  263. if ($cursor.depth > 1 && $cursor.after() != $cursor.end(-1)) {
  264. var before = $cursor.before();
  265. if (prosemirrorTransform.canSplit(state.doc, before)) {
  266. if (dispatch) dispatch(state.tr.split(before).scrollIntoView());
  267. return true;
  268. }
  269. }
  270. var range = $cursor.blockRange(),
  271. target = range && prosemirrorTransform.liftTarget(range);
  272. if (target == null) return false;
  273. if (dispatch) dispatch(state.tr.lift(range, target).scrollIntoView());
  274. return true;
  275. };
  276. function splitBlockAs(splitNode) {
  277. return function (state, dispatch) {
  278. var _state$selection6 = state.selection,
  279. $from = _state$selection6.$from,
  280. $to = _state$selection6.$to;
  281. if (state.selection instanceof prosemirrorState.NodeSelection && state.selection.node.isBlock) {
  282. if (!$from.parentOffset || !prosemirrorTransform.canSplit(state.doc, $from.pos)) return false;
  283. if (dispatch) dispatch(state.tr.split($from.pos).scrollIntoView());
  284. return true;
  285. }
  286. if (!$from.parent.isBlock) return false;
  287. if (dispatch) {
  288. var atEnd = $to.parentOffset == $to.parent.content.size;
  289. var tr = state.tr;
  290. if (state.selection instanceof prosemirrorState.TextSelection || state.selection instanceof prosemirrorState.AllSelection) tr.deleteSelection();
  291. var deflt = $from.depth == 0 ? null : defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1)));
  292. var splitType = splitNode && splitNode($to.parent, atEnd);
  293. var types = splitType ? [splitType] : atEnd && deflt ? [{
  294. type: deflt
  295. }] : undefined;
  296. var can = prosemirrorTransform.canSplit(tr.doc, tr.mapping.map($from.pos), 1, types);
  297. if (!types && !can && prosemirrorTransform.canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{
  298. type: deflt
  299. }] : undefined)) {
  300. if (deflt) types = [{
  301. type: deflt
  302. }];
  303. can = true;
  304. }
  305. if (can) {
  306. tr.split(tr.mapping.map($from.pos), 1, types);
  307. if (!atEnd && !$from.parentOffset && $from.parent.type != deflt) {
  308. var first = tr.mapping.map($from.before()),
  309. $first = tr.doc.resolve(first);
  310. if (deflt && $from.node(-1).canReplaceWith($first.index(), $first.index() + 1, deflt)) tr.setNodeMarkup(tr.mapping.map($from.before()), deflt);
  311. }
  312. }
  313. dispatch(tr.scrollIntoView());
  314. }
  315. return true;
  316. };
  317. }
  318. var splitBlock = splitBlockAs();
  319. var splitBlockKeepMarks = function splitBlockKeepMarks(state, dispatch) {
  320. return splitBlock(state, dispatch && function (tr) {
  321. var marks = state.storedMarks || state.selection.$to.parentOffset && state.selection.$from.marks();
  322. if (marks) tr.ensureMarks(marks);
  323. dispatch(tr);
  324. });
  325. };
  326. var selectParentNode = function selectParentNode(state, dispatch) {
  327. var _state$selection7 = state.selection,
  328. $from = _state$selection7.$from,
  329. to = _state$selection7.to,
  330. pos;
  331. var same = $from.sharedDepth(to);
  332. if (same == 0) return false;
  333. pos = $from.before(same);
  334. if (dispatch) dispatch(state.tr.setSelection(prosemirrorState.NodeSelection.create(state.doc, pos)));
  335. return true;
  336. };
  337. var selectAll = function selectAll(state, dispatch) {
  338. if (dispatch) dispatch(state.tr.setSelection(new prosemirrorState.AllSelection(state.doc)));
  339. return true;
  340. };
  341. function joinMaybeClear(state, $pos, dispatch) {
  342. var before = $pos.nodeBefore,
  343. after = $pos.nodeAfter,
  344. index = $pos.index();
  345. if (!before || !after || !before.type.compatibleContent(after.type)) return false;
  346. if (!before.content.size && $pos.parent.canReplace(index - 1, index)) {
  347. if (dispatch) dispatch(state.tr["delete"]($pos.pos - before.nodeSize, $pos.pos).scrollIntoView());
  348. return true;
  349. }
  350. if (!$pos.parent.canReplace(index, index + 1) || !(after.isTextblock || prosemirrorTransform.canJoin(state.doc, $pos.pos))) return false;
  351. if (dispatch) dispatch(state.tr.clearIncompatible($pos.pos, before.type, before.contentMatchAt(before.childCount)).join($pos.pos).scrollIntoView());
  352. return true;
  353. }
  354. function deleteBarrier(state, $cut, dispatch) {
  355. var before = $cut.nodeBefore,
  356. after = $cut.nodeAfter,
  357. conn,
  358. match;
  359. if (before.type.spec.isolating || after.type.spec.isolating) return false;
  360. if (joinMaybeClear(state, $cut, dispatch)) return true;
  361. var canDelAfter = $cut.parent.canReplace($cut.index(), $cut.index() + 1);
  362. if (canDelAfter && (conn = (match = before.contentMatchAt(before.childCount)).findWrapping(after.type)) && match.matchType(conn[0] || after.type).validEnd) {
  363. if (dispatch) {
  364. var end = $cut.pos + after.nodeSize,
  365. wrap = prosemirrorModel.Fragment.empty;
  366. for (var i = conn.length - 1; i >= 0; i--) {
  367. wrap = prosemirrorModel.Fragment.from(conn[i].create(null, wrap));
  368. }
  369. wrap = prosemirrorModel.Fragment.from(before.copy(wrap));
  370. var tr = state.tr.step(new prosemirrorTransform.ReplaceAroundStep($cut.pos - 1, end, $cut.pos, end, new prosemirrorModel.Slice(wrap, 1, 0), conn.length, true));
  371. var joinAt = end + 2 * conn.length;
  372. if (prosemirrorTransform.canJoin(tr.doc, joinAt)) tr.join(joinAt);
  373. dispatch(tr.scrollIntoView());
  374. }
  375. return true;
  376. }
  377. var selAfter = prosemirrorState.Selection.findFrom($cut, 1);
  378. var range = selAfter && selAfter.$from.blockRange(selAfter.$to),
  379. target = range && prosemirrorTransform.liftTarget(range);
  380. if (target != null && target >= $cut.depth) {
  381. if (dispatch) dispatch(state.tr.lift(range, target).scrollIntoView());
  382. return true;
  383. }
  384. if (canDelAfter && textblockAt(after, "start", true) && textblockAt(before, "end")) {
  385. var at = before,
  386. _wrap = [];
  387. for (;;) {
  388. _wrap.push(at);
  389. if (at.isTextblock) break;
  390. at = at.lastChild;
  391. }
  392. var afterText = after,
  393. afterDepth = 1;
  394. for (; !afterText.isTextblock; afterText = afterText.firstChild) {
  395. afterDepth++;
  396. }
  397. if (at.canReplace(at.childCount, at.childCount, afterText.content)) {
  398. if (dispatch) {
  399. var _end = prosemirrorModel.Fragment.empty;
  400. for (var _i = _wrap.length - 1; _i >= 0; _i--) {
  401. _end = prosemirrorModel.Fragment.from(_wrap[_i].copy(_end));
  402. }
  403. var _tr = state.tr.step(new prosemirrorTransform.ReplaceAroundStep($cut.pos - _wrap.length, $cut.pos + after.nodeSize, $cut.pos + afterDepth, $cut.pos + after.nodeSize - afterDepth, new prosemirrorModel.Slice(_end, _wrap.length, 0), 0, true));
  404. dispatch(_tr.scrollIntoView());
  405. }
  406. return true;
  407. }
  408. }
  409. return false;
  410. }
  411. function selectTextblockSide(side) {
  412. return function (state, dispatch) {
  413. var sel = state.selection,
  414. $pos = side < 0 ? sel.$from : sel.$to;
  415. var depth = $pos.depth;
  416. while ($pos.node(depth).isInline) {
  417. if (!depth) return false;
  418. depth--;
  419. }
  420. if (!$pos.node(depth).isTextblock) return false;
  421. if (dispatch) dispatch(state.tr.setSelection(prosemirrorState.TextSelection.create(state.doc, side < 0 ? $pos.start(depth) : $pos.end(depth))));
  422. return true;
  423. };
  424. }
  425. var selectTextblockStart = selectTextblockSide(-1);
  426. var selectTextblockEnd = selectTextblockSide(1);
  427. function wrapIn(nodeType) {
  428. var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  429. return function (state, dispatch) {
  430. var _state$selection8 = state.selection,
  431. $from = _state$selection8.$from,
  432. $to = _state$selection8.$to;
  433. var range = $from.blockRange($to),
  434. wrapping = range && prosemirrorTransform.findWrapping(range, nodeType, attrs);
  435. if (!wrapping) return false;
  436. if (dispatch) dispatch(state.tr.wrap(range, wrapping).scrollIntoView());
  437. return true;
  438. };
  439. }
  440. function setBlockType(nodeType) {
  441. var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  442. return function (state, dispatch) {
  443. var applicable = false;
  444. for (var i = 0; i < state.selection.ranges.length && !applicable; i++) {
  445. var _state$selection$rang = state.selection.ranges[i],
  446. from = _state$selection$rang.$from.pos,
  447. to = _state$selection$rang.$to.pos;
  448. state.doc.nodesBetween(from, to, function (node, pos) {
  449. if (applicable) return false;
  450. if (!node.isTextblock || node.hasMarkup(nodeType, attrs)) return;
  451. if (node.type == nodeType) {
  452. applicable = true;
  453. } else {
  454. var $pos = state.doc.resolve(pos),
  455. index = $pos.index();
  456. applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType);
  457. }
  458. });
  459. }
  460. if (!applicable) return false;
  461. if (dispatch) {
  462. var tr = state.tr;
  463. for (var _i2 = 0; _i2 < state.selection.ranges.length; _i2++) {
  464. var _state$selection$rang2 = state.selection.ranges[_i2],
  465. _from = _state$selection$rang2.$from.pos,
  466. _to = _state$selection$rang2.$to.pos;
  467. tr.setBlockType(_from, _to, nodeType, attrs);
  468. }
  469. dispatch(tr.scrollIntoView());
  470. }
  471. return true;
  472. };
  473. }
  474. function markApplies(doc, ranges, type) {
  475. var _loop = function _loop(i) {
  476. var _ranges$i = ranges[i],
  477. $from = _ranges$i.$from,
  478. $to = _ranges$i.$to;
  479. var can = $from.depth == 0 ? doc.inlineContent && doc.type.allowsMarkType(type) : false;
  480. doc.nodesBetween($from.pos, $to.pos, function (node) {
  481. if (can) return false;
  482. can = node.inlineContent && node.type.allowsMarkType(type);
  483. });
  484. if (can) return {
  485. v: true
  486. };
  487. };
  488. for (var i = 0; i < ranges.length; i++) {
  489. var _ret = _loop(i);
  490. if (_typeof(_ret) === "object") return _ret.v;
  491. }
  492. return false;
  493. }
  494. function toggleMark(markType) {
  495. var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  496. return function (state, dispatch) {
  497. var _state$selection9 = state.selection,
  498. empty = _state$selection9.empty,
  499. $cursor = _state$selection9.$cursor,
  500. ranges = _state$selection9.ranges;
  501. if (empty && !$cursor || !markApplies(state.doc, ranges, markType)) return false;
  502. if (dispatch) {
  503. if ($cursor) {
  504. if (markType.isInSet(state.storedMarks || $cursor.marks())) dispatch(state.tr.removeStoredMark(markType));else dispatch(state.tr.addStoredMark(markType.create(attrs)));
  505. } else {
  506. var has = false,
  507. tr = state.tr;
  508. for (var i = 0; !has && i < ranges.length; i++) {
  509. var _ranges$i2 = ranges[i],
  510. $from = _ranges$i2.$from,
  511. $to = _ranges$i2.$to;
  512. has = state.doc.rangeHasMark($from.pos, $to.pos, markType);
  513. }
  514. for (var _i3 = 0; _i3 < ranges.length; _i3++) {
  515. var _ranges$_i = ranges[_i3],
  516. _$from = _ranges$_i.$from,
  517. _$to = _ranges$_i.$to;
  518. if (has) {
  519. tr.removeMark(_$from.pos, _$to.pos, markType);
  520. } else {
  521. var from = _$from.pos,
  522. to = _$to.pos,
  523. start = _$from.nodeAfter,
  524. end = _$to.nodeBefore;
  525. var spaceStart = start && start.isText ? /^\s*/.exec(start.text)[0].length : 0;
  526. var spaceEnd = end && end.isText ? /\s*$/.exec(end.text)[0].length : 0;
  527. if (from + spaceStart < to) {
  528. from += spaceStart;
  529. to -= spaceEnd;
  530. }
  531. tr.addMark(from, to, markType.create(attrs));
  532. }
  533. }
  534. dispatch(tr.scrollIntoView());
  535. }
  536. }
  537. return true;
  538. };
  539. }
  540. function wrapDispatchForJoin(dispatch, isJoinable) {
  541. return function (tr) {
  542. if (!tr.isGeneric) return dispatch(tr);
  543. var ranges = [];
  544. for (var i = 0; i < tr.mapping.maps.length; i++) {
  545. var map = tr.mapping.maps[i];
  546. for (var j = 0; j < ranges.length; j++) {
  547. ranges[j] = map.map(ranges[j]);
  548. }
  549. map.forEach(function (_s, _e, from, to) {
  550. return ranges.push(from, to);
  551. });
  552. }
  553. var joinable = [];
  554. for (var _i4 = 0; _i4 < ranges.length; _i4 += 2) {
  555. var from = ranges[_i4],
  556. to = ranges[_i4 + 1];
  557. var $from = tr.doc.resolve(from),
  558. depth = $from.sharedDepth(to),
  559. parent = $from.node(depth);
  560. for (var index = $from.indexAfter(depth), pos = $from.after(depth + 1); pos <= to; ++index) {
  561. var after = parent.maybeChild(index);
  562. if (!after) break;
  563. if (index && joinable.indexOf(pos) == -1) {
  564. var before = parent.child(index - 1);
  565. if (before.type == after.type && isJoinable(before, after)) joinable.push(pos);
  566. }
  567. pos += after.nodeSize;
  568. }
  569. }
  570. joinable.sort(function (a, b) {
  571. return a - b;
  572. });
  573. for (var _i5 = joinable.length - 1; _i5 >= 0; _i5--) {
  574. if (prosemirrorTransform.canJoin(tr.doc, joinable[_i5])) tr.join(joinable[_i5]);
  575. }
  576. dispatch(tr);
  577. };
  578. }
  579. function autoJoin(command, isJoinable) {
  580. var canJoin = Array.isArray(isJoinable) ? function (node) {
  581. return isJoinable.indexOf(node.type.name) > -1;
  582. } : isJoinable;
  583. return function (state, dispatch, view) {
  584. return command(state, dispatch && wrapDispatchForJoin(dispatch, canJoin), view);
  585. };
  586. }
  587. function chainCommands() {
  588. for (var _len = arguments.length, commands = new Array(_len), _key = 0; _key < _len; _key++) {
  589. commands[_key] = arguments[_key];
  590. }
  591. return function (state, dispatch, view) {
  592. for (var i = 0; i < commands.length; i++) {
  593. if (commands[i](state, dispatch, view)) return true;
  594. }
  595. return false;
  596. };
  597. }
  598. var backspace = chainCommands(deleteSelection, joinBackward, selectNodeBackward);
  599. var del = chainCommands(deleteSelection, joinForward, selectNodeForward);
  600. var pcBaseKeymap = {
  601. "Enter": chainCommands(newlineInCode, createParagraphNear, liftEmptyBlock, splitBlock),
  602. "Mod-Enter": exitCode,
  603. "Backspace": backspace,
  604. "Mod-Backspace": backspace,
  605. "Shift-Backspace": backspace,
  606. "Delete": del,
  607. "Mod-Delete": del,
  608. "Mod-a": selectAll
  609. };
  610. var macBaseKeymap = {
  611. "Ctrl-h": pcBaseKeymap["Backspace"],
  612. "Alt-Backspace": pcBaseKeymap["Mod-Backspace"],
  613. "Ctrl-d": pcBaseKeymap["Delete"],
  614. "Ctrl-Alt-Backspace": pcBaseKeymap["Mod-Delete"],
  615. "Alt-Delete": pcBaseKeymap["Mod-Delete"],
  616. "Alt-d": pcBaseKeymap["Mod-Delete"],
  617. "Ctrl-a": selectTextblockStart,
  618. "Ctrl-e": selectTextblockEnd
  619. };
  620. for (var key in pcBaseKeymap) {
  621. macBaseKeymap[key] = pcBaseKeymap[key];
  622. }
  623. var mac = typeof navigator != "undefined" ? /Mac|iP(hone|[oa]d)/.test(navigator.platform) : typeof os != "undefined" && os.platform ? os.platform() == "darwin" : false;
  624. var baseKeymap = mac ? macBaseKeymap : pcBaseKeymap;
  625. exports.autoJoin = autoJoin;
  626. exports.baseKeymap = baseKeymap;
  627. exports.chainCommands = chainCommands;
  628. exports.createParagraphNear = createParagraphNear;
  629. exports.deleteSelection = deleteSelection;
  630. exports.exitCode = exitCode;
  631. exports.joinBackward = joinBackward;
  632. exports.joinDown = joinDown;
  633. exports.joinForward = joinForward;
  634. exports.joinTextblockBackward = joinTextblockBackward;
  635. exports.joinTextblockForward = joinTextblockForward;
  636. exports.joinUp = joinUp;
  637. exports.lift = lift;
  638. exports.liftEmptyBlock = liftEmptyBlock;
  639. exports.macBaseKeymap = macBaseKeymap;
  640. exports.newlineInCode = newlineInCode;
  641. exports.pcBaseKeymap = pcBaseKeymap;
  642. exports.selectAll = selectAll;
  643. exports.selectNodeBackward = selectNodeBackward;
  644. exports.selectNodeForward = selectNodeForward;
  645. exports.selectParentNode = selectParentNode;
  646. exports.selectTextblockEnd = selectTextblockEnd;
  647. exports.selectTextblockStart = selectTextblockStart;
  648. exports.setBlockType = setBlockType;
  649. exports.splitBlock = splitBlock;
  650. exports.splitBlockAs = splitBlockAs;
  651. exports.splitBlockKeepMarks = splitBlockKeepMarks;
  652. exports.toggleMark = toggleMark;
  653. exports.wrapIn = wrapIn;