index.cjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  5. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  6. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
  7. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  8. function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  9. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
  10. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  11. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
  12. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  13. Object.defineProperty(exports, '__esModule', {
  14. value: true
  15. });
  16. var prosemirrorKeymap = require('prosemirror-keymap');
  17. var prosemirrorState = require('prosemirror-state');
  18. var prosemirrorModel = require('prosemirror-model');
  19. var prosemirrorView = require('prosemirror-view');
  20. var GapCursor = function (_prosemirrorState$Sel) {
  21. _inherits(GapCursor, _prosemirrorState$Sel);
  22. var _super = _createSuper(GapCursor);
  23. function GapCursor($pos) {
  24. _classCallCheck(this, GapCursor);
  25. return _super.call(this, $pos, $pos);
  26. }
  27. _createClass(GapCursor, [{
  28. key: "map",
  29. value: function map(doc, mapping) {
  30. var $pos = doc.resolve(mapping.map(this.head));
  31. return GapCursor.valid($pos) ? new GapCursor($pos) : prosemirrorState.Selection.near($pos);
  32. }
  33. }, {
  34. key: "content",
  35. value: function content() {
  36. return prosemirrorModel.Slice.empty;
  37. }
  38. }, {
  39. key: "eq",
  40. value: function eq(other) {
  41. return other instanceof GapCursor && other.head == this.head;
  42. }
  43. }, {
  44. key: "toJSON",
  45. value: function toJSON() {
  46. return {
  47. type: "gapcursor",
  48. pos: this.head
  49. };
  50. }
  51. }, {
  52. key: "getBookmark",
  53. value: function getBookmark() {
  54. return new GapBookmark(this.anchor);
  55. }
  56. }], [{
  57. key: "fromJSON",
  58. value: function fromJSON(doc, json) {
  59. if (typeof json.pos != "number") throw new RangeError("Invalid input for GapCursor.fromJSON");
  60. return new GapCursor(doc.resolve(json.pos));
  61. }
  62. }, {
  63. key: "valid",
  64. value: function valid($pos) {
  65. var parent = $pos.parent;
  66. if (parent.isTextblock || !closedBefore($pos) || !closedAfter($pos)) return false;
  67. var override = parent.type.spec.allowGapCursor;
  68. if (override != null) return override;
  69. var deflt = parent.contentMatchAt($pos.index()).defaultType;
  70. return deflt && deflt.isTextblock;
  71. }
  72. }, {
  73. key: "findGapCursorFrom",
  74. value: function findGapCursorFrom($pos, dir) {
  75. var mustMove = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  76. search: for (;;) {
  77. if (!mustMove && GapCursor.valid($pos)) return $pos;
  78. var pos = $pos.pos,
  79. next = null;
  80. for (var d = $pos.depth;; d--) {
  81. var parent = $pos.node(d);
  82. if (dir > 0 ? $pos.indexAfter(d) < parent.childCount : $pos.index(d) > 0) {
  83. next = parent.child(dir > 0 ? $pos.indexAfter(d) : $pos.index(d) - 1);
  84. break;
  85. } else if (d == 0) {
  86. return null;
  87. }
  88. pos += dir;
  89. var $cur = $pos.doc.resolve(pos);
  90. if (GapCursor.valid($cur)) return $cur;
  91. }
  92. for (;;) {
  93. var inside = dir > 0 ? next.firstChild : next.lastChild;
  94. if (!inside) {
  95. if (next.isAtom && !next.isText && !prosemirrorState.NodeSelection.isSelectable(next)) {
  96. $pos = $pos.doc.resolve(pos + next.nodeSize * dir);
  97. mustMove = false;
  98. continue search;
  99. }
  100. break;
  101. }
  102. next = inside;
  103. pos += dir;
  104. var _$cur = $pos.doc.resolve(pos);
  105. if (GapCursor.valid(_$cur)) return _$cur;
  106. }
  107. return null;
  108. }
  109. }
  110. }]);
  111. return GapCursor;
  112. }(prosemirrorState.Selection);
  113. GapCursor.prototype.visible = false;
  114. GapCursor.findFrom = GapCursor.findGapCursorFrom;
  115. prosemirrorState.Selection.jsonID("gapcursor", GapCursor);
  116. var GapBookmark = function () {
  117. function GapBookmark(pos) {
  118. _classCallCheck(this, GapBookmark);
  119. this.pos = pos;
  120. }
  121. _createClass(GapBookmark, [{
  122. key: "map",
  123. value: function map(mapping) {
  124. return new GapBookmark(mapping.map(this.pos));
  125. }
  126. }, {
  127. key: "resolve",
  128. value: function resolve(doc) {
  129. var $pos = doc.resolve(this.pos);
  130. return GapCursor.valid($pos) ? new GapCursor($pos) : prosemirrorState.Selection.near($pos);
  131. }
  132. }]);
  133. return GapBookmark;
  134. }();
  135. function closedBefore($pos) {
  136. for (var d = $pos.depth; d >= 0; d--) {
  137. var index = $pos.index(d),
  138. parent = $pos.node(d);
  139. if (index == 0) {
  140. if (parent.type.spec.isolating) return true;
  141. continue;
  142. }
  143. for (var before = parent.child(index - 1);; before = before.lastChild) {
  144. if (before.childCount == 0 && !before.inlineContent || before.isAtom || before.type.spec.isolating) return true;
  145. if (before.inlineContent) return false;
  146. }
  147. }
  148. return true;
  149. }
  150. function closedAfter($pos) {
  151. for (var d = $pos.depth; d >= 0; d--) {
  152. var index = $pos.indexAfter(d),
  153. parent = $pos.node(d);
  154. if (index == parent.childCount) {
  155. if (parent.type.spec.isolating) return true;
  156. continue;
  157. }
  158. for (var after = parent.child(index);; after = after.firstChild) {
  159. if (after.childCount == 0 && !after.inlineContent || after.isAtom || after.type.spec.isolating) return true;
  160. if (after.inlineContent) return false;
  161. }
  162. }
  163. return true;
  164. }
  165. function gapCursor() {
  166. return new prosemirrorState.Plugin({
  167. props: {
  168. decorations: drawGapCursor,
  169. createSelectionBetween: function createSelectionBetween(_view, $anchor, $head) {
  170. return $anchor.pos == $head.pos && GapCursor.valid($head) ? new GapCursor($head) : null;
  171. },
  172. handleClick: handleClick,
  173. handleKeyDown: handleKeyDown,
  174. handleDOMEvents: {
  175. beforeinput: beforeinput
  176. }
  177. }
  178. });
  179. }
  180. var handleKeyDown = prosemirrorKeymap.keydownHandler({
  181. "ArrowLeft": arrow("horiz", -1),
  182. "ArrowRight": arrow("horiz", 1),
  183. "ArrowUp": arrow("vert", -1),
  184. "ArrowDown": arrow("vert", 1)
  185. });
  186. function arrow(axis, dir) {
  187. var dirStr = axis == "vert" ? dir > 0 ? "down" : "up" : dir > 0 ? "right" : "left";
  188. return function (state, dispatch, view) {
  189. var sel = state.selection;
  190. var $start = dir > 0 ? sel.$to : sel.$from,
  191. mustMove = sel.empty;
  192. if (sel instanceof prosemirrorState.TextSelection) {
  193. if (!view.endOfTextblock(dirStr) || $start.depth == 0) return false;
  194. mustMove = false;
  195. $start = state.doc.resolve(dir > 0 ? $start.after() : $start.before());
  196. }
  197. var $found = GapCursor.findGapCursorFrom($start, dir, mustMove);
  198. if (!$found) return false;
  199. if (dispatch) dispatch(state.tr.setSelection(new GapCursor($found)));
  200. return true;
  201. };
  202. }
  203. function handleClick(view, pos, event) {
  204. if (!view || !view.editable) return false;
  205. var $pos = view.state.doc.resolve(pos);
  206. if (!GapCursor.valid($pos)) return false;
  207. var clickPos = view.posAtCoords({
  208. left: event.clientX,
  209. top: event.clientY
  210. });
  211. if (clickPos && clickPos.inside > -1 && prosemirrorState.NodeSelection.isSelectable(view.state.doc.nodeAt(clickPos.inside))) return false;
  212. view.dispatch(view.state.tr.setSelection(new GapCursor($pos)));
  213. return true;
  214. }
  215. function beforeinput(view, event) {
  216. if (event.inputType != "insertCompositionText" || !(view.state.selection instanceof GapCursor)) return false;
  217. var $from = view.state.selection.$from;
  218. var insert = $from.parent.contentMatchAt($from.index()).findWrapping(view.state.schema.nodes.text);
  219. if (!insert) return false;
  220. var frag = prosemirrorModel.Fragment.empty;
  221. for (var i = insert.length - 1; i >= 0; i--) {
  222. frag = prosemirrorModel.Fragment.from(insert[i].createAndFill(null, frag));
  223. }
  224. var tr = view.state.tr.replace($from.pos, $from.pos, new prosemirrorModel.Slice(frag, 0, 0));
  225. tr.setSelection(prosemirrorState.TextSelection.near(tr.doc.resolve($from.pos + 1)));
  226. view.dispatch(tr);
  227. return false;
  228. }
  229. function drawGapCursor(state) {
  230. if (!(state.selection instanceof GapCursor)) return null;
  231. var node = document.createElement("div");
  232. node.className = "ProseMirror-gapcursor";
  233. return prosemirrorView.DecorationSet.create(state.doc, [prosemirrorView.Decoration.widget(state.selection.head, node, {
  234. key: "gapcursor"
  235. })]);
  236. }
  237. exports.GapCursor = GapCursor;
  238. exports.gapCursor = gapCursor;