index.cjs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
  4. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  5. 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); }
  6. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  7. 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); }; }
  8. 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); }
  9. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  10. 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; } }
  11. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. 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); } }
  14. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  15. Object.defineProperty(exports, '__esModule', {
  16. value: true
  17. });
  18. var prosemirrorModel = require('prosemirror-model');
  19. var prosemirrorTransform = require('prosemirror-transform');
  20. var classesById = Object.create(null);
  21. var Selection = function () {
  22. function Selection($anchor, $head, ranges) {
  23. _classCallCheck(this, Selection);
  24. this.$anchor = $anchor;
  25. this.$head = $head;
  26. this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))];
  27. }
  28. _createClass(Selection, [{
  29. key: "anchor",
  30. get: function get() {
  31. return this.$anchor.pos;
  32. }
  33. }, {
  34. key: "head",
  35. get: function get() {
  36. return this.$head.pos;
  37. }
  38. }, {
  39. key: "from",
  40. get: function get() {
  41. return this.$from.pos;
  42. }
  43. }, {
  44. key: "to",
  45. get: function get() {
  46. return this.$to.pos;
  47. }
  48. }, {
  49. key: "$from",
  50. get: function get() {
  51. return this.ranges[0].$from;
  52. }
  53. }, {
  54. key: "$to",
  55. get: function get() {
  56. return this.ranges[0].$to;
  57. }
  58. }, {
  59. key: "empty",
  60. get: function get() {
  61. var ranges = this.ranges;
  62. for (var i = 0; i < ranges.length; i++) {
  63. if (ranges[i].$from.pos != ranges[i].$to.pos) return false;
  64. }
  65. return true;
  66. }
  67. }, {
  68. key: "content",
  69. value: function content() {
  70. return this.$from.doc.slice(this.from, this.to, true);
  71. }
  72. }, {
  73. key: "replace",
  74. value: function replace(tr) {
  75. var content = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : prosemirrorModel.Slice.empty;
  76. var lastNode = content.content.lastChild,
  77. lastParent = null;
  78. for (var i = 0; i < content.openEnd; i++) {
  79. lastParent = lastNode;
  80. lastNode = lastNode.lastChild;
  81. }
  82. var mapFrom = tr.steps.length,
  83. ranges = this.ranges;
  84. for (var _i = 0; _i < ranges.length; _i++) {
  85. var _ranges$_i = ranges[_i],
  86. $from = _ranges$_i.$from,
  87. $to = _ranges$_i.$to,
  88. mapping = tr.mapping.slice(mapFrom);
  89. tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), _i ? prosemirrorModel.Slice.empty : content);
  90. if (_i == 0) selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1);
  91. }
  92. }
  93. }, {
  94. key: "replaceWith",
  95. value: function replaceWith(tr, node) {
  96. var mapFrom = tr.steps.length,
  97. ranges = this.ranges;
  98. for (var i = 0; i < ranges.length; i++) {
  99. var _ranges$i = ranges[i],
  100. $from = _ranges$i.$from,
  101. $to = _ranges$i.$to,
  102. mapping = tr.mapping.slice(mapFrom);
  103. var from = mapping.map($from.pos),
  104. to = mapping.map($to.pos);
  105. if (i) {
  106. tr.deleteRange(from, to);
  107. } else {
  108. tr.replaceRangeWith(from, to, node);
  109. selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1);
  110. }
  111. }
  112. }
  113. }, {
  114. key: "getBookmark",
  115. value: function getBookmark() {
  116. return TextSelection.between(this.$anchor, this.$head).getBookmark();
  117. }
  118. }], [{
  119. key: "findFrom",
  120. value: function findFrom($pos, dir) {
  121. var textOnly = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  122. var inner = $pos.parent.inlineContent ? new TextSelection($pos) : findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly);
  123. if (inner) return inner;
  124. for (var depth = $pos.depth - 1; depth >= 0; depth--) {
  125. var found = dir < 0 ? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly) : findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly);
  126. if (found) return found;
  127. }
  128. return null;
  129. }
  130. }, {
  131. key: "near",
  132. value: function near($pos) {
  133. var bias = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  134. return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0));
  135. }
  136. }, {
  137. key: "atStart",
  138. value: function atStart(doc) {
  139. return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc);
  140. }
  141. }, {
  142. key: "atEnd",
  143. value: function atEnd(doc) {
  144. return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc);
  145. }
  146. }, {
  147. key: "fromJSON",
  148. value: function fromJSON(doc, json) {
  149. if (!json || !json.type) throw new RangeError("Invalid input for Selection.fromJSON");
  150. var cls = classesById[json.type];
  151. if (!cls) throw new RangeError("No selection type ".concat(json.type, " defined"));
  152. return cls.fromJSON(doc, json);
  153. }
  154. }, {
  155. key: "jsonID",
  156. value: function jsonID(id, selectionClass) {
  157. if (id in classesById) throw new RangeError("Duplicate use of selection JSON ID " + id);
  158. classesById[id] = selectionClass;
  159. selectionClass.prototype.jsonID = id;
  160. return selectionClass;
  161. }
  162. }]);
  163. return Selection;
  164. }();
  165. Selection.prototype.visible = true;
  166. var SelectionRange = _createClass(function SelectionRange($from, $to) {
  167. _classCallCheck(this, SelectionRange);
  168. this.$from = $from;
  169. this.$to = $to;
  170. });
  171. var warnedAboutTextSelection = false;
  172. function checkTextSelection($pos) {
  173. if (!warnedAboutTextSelection && !$pos.parent.inlineContent) {
  174. warnedAboutTextSelection = true;
  175. console["warn"]("TextSelection endpoint not pointing into a node with inline content (" + $pos.parent.type.name + ")");
  176. }
  177. }
  178. var TextSelection = function (_Selection) {
  179. _inherits(TextSelection, _Selection);
  180. var _super = _createSuper(TextSelection);
  181. function TextSelection($anchor) {
  182. var $head = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : $anchor;
  183. _classCallCheck(this, TextSelection);
  184. checkTextSelection($anchor);
  185. checkTextSelection($head);
  186. return _super.call(this, $anchor, $head);
  187. }
  188. _createClass(TextSelection, [{
  189. key: "$cursor",
  190. get: function get() {
  191. return this.$anchor.pos == this.$head.pos ? this.$head : null;
  192. }
  193. }, {
  194. key: "map",
  195. value: function map(doc, mapping) {
  196. var $head = doc.resolve(mapping.map(this.head));
  197. if (!$head.parent.inlineContent) return Selection.near($head);
  198. var $anchor = doc.resolve(mapping.map(this.anchor));
  199. return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head);
  200. }
  201. }, {
  202. key: "replace",
  203. value: function replace(tr) {
  204. var content = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : prosemirrorModel.Slice.empty;
  205. _get(_getPrototypeOf(TextSelection.prototype), "replace", this).call(this, tr, content);
  206. if (content == prosemirrorModel.Slice.empty) {
  207. var marks = this.$from.marksAcross(this.$to);
  208. if (marks) tr.ensureMarks(marks);
  209. }
  210. }
  211. }, {
  212. key: "eq",
  213. value: function eq(other) {
  214. return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head;
  215. }
  216. }, {
  217. key: "getBookmark",
  218. value: function getBookmark() {
  219. return new TextBookmark(this.anchor, this.head);
  220. }
  221. }, {
  222. key: "toJSON",
  223. value: function toJSON() {
  224. return {
  225. type: "text",
  226. anchor: this.anchor,
  227. head: this.head
  228. };
  229. }
  230. }], [{
  231. key: "fromJSON",
  232. value: function fromJSON(doc, json) {
  233. if (typeof json.anchor != "number" || typeof json.head != "number") throw new RangeError("Invalid input for TextSelection.fromJSON");
  234. return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head));
  235. }
  236. }, {
  237. key: "create",
  238. value: function create(doc, anchor) {
  239. var head = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : anchor;
  240. var $anchor = doc.resolve(anchor);
  241. return new this($anchor, head == anchor ? $anchor : doc.resolve(head));
  242. }
  243. }, {
  244. key: "between",
  245. value: function between($anchor, $head, bias) {
  246. var dPos = $anchor.pos - $head.pos;
  247. if (!bias || dPos) bias = dPos >= 0 ? 1 : -1;
  248. if (!$head.parent.inlineContent) {
  249. var found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true);
  250. if (found) $head = found.$head;else return Selection.near($head, bias);
  251. }
  252. if (!$anchor.parent.inlineContent) {
  253. if (dPos == 0) {
  254. $anchor = $head;
  255. } else {
  256. $anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor;
  257. if ($anchor.pos < $head.pos != dPos < 0) $anchor = $head;
  258. }
  259. }
  260. return new TextSelection($anchor, $head);
  261. }
  262. }]);
  263. return TextSelection;
  264. }(Selection);
  265. Selection.jsonID("text", TextSelection);
  266. var TextBookmark = function () {
  267. function TextBookmark(anchor, head) {
  268. _classCallCheck(this, TextBookmark);
  269. this.anchor = anchor;
  270. this.head = head;
  271. }
  272. _createClass(TextBookmark, [{
  273. key: "map",
  274. value: function map(mapping) {
  275. return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head));
  276. }
  277. }, {
  278. key: "resolve",
  279. value: function resolve(doc) {
  280. return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head));
  281. }
  282. }]);
  283. return TextBookmark;
  284. }();
  285. var NodeSelection = function (_Selection2) {
  286. _inherits(NodeSelection, _Selection2);
  287. var _super2 = _createSuper(NodeSelection);
  288. function NodeSelection($pos) {
  289. var _this;
  290. _classCallCheck(this, NodeSelection);
  291. var node = $pos.nodeAfter;
  292. var $end = $pos.node(0).resolve($pos.pos + node.nodeSize);
  293. _this = _super2.call(this, $pos, $end);
  294. _this.node = node;
  295. return _this;
  296. }
  297. _createClass(NodeSelection, [{
  298. key: "map",
  299. value: function map(doc, mapping) {
  300. var _mapping$mapResult = mapping.mapResult(this.anchor),
  301. deleted = _mapping$mapResult.deleted,
  302. pos = _mapping$mapResult.pos;
  303. var $pos = doc.resolve(pos);
  304. if (deleted) return Selection.near($pos);
  305. return new NodeSelection($pos);
  306. }
  307. }, {
  308. key: "content",
  309. value: function content() {
  310. return new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(this.node), 0, 0);
  311. }
  312. }, {
  313. key: "eq",
  314. value: function eq(other) {
  315. return other instanceof NodeSelection && other.anchor == this.anchor;
  316. }
  317. }, {
  318. key: "toJSON",
  319. value: function toJSON() {
  320. return {
  321. type: "node",
  322. anchor: this.anchor
  323. };
  324. }
  325. }, {
  326. key: "getBookmark",
  327. value: function getBookmark() {
  328. return new NodeBookmark(this.anchor);
  329. }
  330. }], [{
  331. key: "fromJSON",
  332. value: function fromJSON(doc, json) {
  333. if (typeof json.anchor != "number") throw new RangeError("Invalid input for NodeSelection.fromJSON");
  334. return new NodeSelection(doc.resolve(json.anchor));
  335. }
  336. }, {
  337. key: "create",
  338. value: function create(doc, from) {
  339. return new NodeSelection(doc.resolve(from));
  340. }
  341. }, {
  342. key: "isSelectable",
  343. value: function isSelectable(node) {
  344. return !node.isText && node.type.spec.selectable !== false;
  345. }
  346. }]);
  347. return NodeSelection;
  348. }(Selection);
  349. NodeSelection.prototype.visible = false;
  350. Selection.jsonID("node", NodeSelection);
  351. var NodeBookmark = function () {
  352. function NodeBookmark(anchor) {
  353. _classCallCheck(this, NodeBookmark);
  354. this.anchor = anchor;
  355. }
  356. _createClass(NodeBookmark, [{
  357. key: "map",
  358. value: function map(mapping) {
  359. var _mapping$mapResult2 = mapping.mapResult(this.anchor),
  360. deleted = _mapping$mapResult2.deleted,
  361. pos = _mapping$mapResult2.pos;
  362. return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos);
  363. }
  364. }, {
  365. key: "resolve",
  366. value: function resolve(doc) {
  367. var $pos = doc.resolve(this.anchor),
  368. node = $pos.nodeAfter;
  369. if (node && NodeSelection.isSelectable(node)) return new NodeSelection($pos);
  370. return Selection.near($pos);
  371. }
  372. }]);
  373. return NodeBookmark;
  374. }();
  375. var AllSelection = function (_Selection3) {
  376. _inherits(AllSelection, _Selection3);
  377. var _super3 = _createSuper(AllSelection);
  378. function AllSelection(doc) {
  379. _classCallCheck(this, AllSelection);
  380. return _super3.call(this, doc.resolve(0), doc.resolve(doc.content.size));
  381. }
  382. _createClass(AllSelection, [{
  383. key: "replace",
  384. value: function replace(tr) {
  385. var content = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : prosemirrorModel.Slice.empty;
  386. if (content == prosemirrorModel.Slice.empty) {
  387. tr["delete"](0, tr.doc.content.size);
  388. var sel = Selection.atStart(tr.doc);
  389. if (!sel.eq(tr.selection)) tr.setSelection(sel);
  390. } else {
  391. _get(_getPrototypeOf(AllSelection.prototype), "replace", this).call(this, tr, content);
  392. }
  393. }
  394. }, {
  395. key: "toJSON",
  396. value: function toJSON() {
  397. return {
  398. type: "all"
  399. };
  400. }
  401. }, {
  402. key: "map",
  403. value: function map(doc) {
  404. return new AllSelection(doc);
  405. }
  406. }, {
  407. key: "eq",
  408. value: function eq(other) {
  409. return other instanceof AllSelection;
  410. }
  411. }, {
  412. key: "getBookmark",
  413. value: function getBookmark() {
  414. return AllBookmark;
  415. }
  416. }], [{
  417. key: "fromJSON",
  418. value: function fromJSON(doc) {
  419. return new AllSelection(doc);
  420. }
  421. }]);
  422. return AllSelection;
  423. }(Selection);
  424. Selection.jsonID("all", AllSelection);
  425. var AllBookmark = {
  426. map: function map() {
  427. return this;
  428. },
  429. resolve: function resolve(doc) {
  430. return new AllSelection(doc);
  431. }
  432. };
  433. function findSelectionIn(doc, node, pos, index, dir) {
  434. var text = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
  435. if (node.inlineContent) return TextSelection.create(doc, pos);
  436. for (var i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {
  437. var child = node.child(i);
  438. if (!child.isAtom) {
  439. var inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text);
  440. if (inner) return inner;
  441. } else if (!text && NodeSelection.isSelectable(child)) {
  442. return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0));
  443. }
  444. pos += child.nodeSize * dir;
  445. }
  446. return null;
  447. }
  448. function selectionToInsertionEnd(tr, startLen, bias) {
  449. var last = tr.steps.length - 1;
  450. if (last < startLen) return;
  451. var step = tr.steps[last];
  452. if (!(step instanceof prosemirrorTransform.ReplaceStep || step instanceof prosemirrorTransform.ReplaceAroundStep)) return;
  453. var map = tr.mapping.maps[last],
  454. end;
  455. map.forEach(function (_from, _to, _newFrom, newTo) {
  456. if (end == null) end = newTo;
  457. });
  458. tr.setSelection(Selection.near(tr.doc.resolve(end), bias));
  459. }
  460. var UPDATED_SEL = 1,
  461. UPDATED_MARKS = 2,
  462. UPDATED_SCROLL = 4;
  463. var Transaction = function (_prosemirrorTransform) {
  464. _inherits(Transaction, _prosemirrorTransform);
  465. var _super4 = _createSuper(Transaction);
  466. function Transaction(state) {
  467. var _this2;
  468. _classCallCheck(this, Transaction);
  469. _this2 = _super4.call(this, state.doc);
  470. _this2.curSelectionFor = 0;
  471. _this2.updated = 0;
  472. _this2.meta = Object.create(null);
  473. _this2.time = Date.now();
  474. _this2.curSelection = state.selection;
  475. _this2.storedMarks = state.storedMarks;
  476. return _this2;
  477. }
  478. _createClass(Transaction, [{
  479. key: "selection",
  480. get: function get() {
  481. if (this.curSelectionFor < this.steps.length) {
  482. this.curSelection = this.curSelection.map(this.doc, this.mapping.slice(this.curSelectionFor));
  483. this.curSelectionFor = this.steps.length;
  484. }
  485. return this.curSelection;
  486. }
  487. }, {
  488. key: "setSelection",
  489. value: function setSelection(selection) {
  490. if (selection.$from.doc != this.doc) throw new RangeError("Selection passed to setSelection must point at the current document");
  491. this.curSelection = selection;
  492. this.curSelectionFor = this.steps.length;
  493. this.updated = (this.updated | UPDATED_SEL) & ~UPDATED_MARKS;
  494. this.storedMarks = null;
  495. return this;
  496. }
  497. }, {
  498. key: "selectionSet",
  499. get: function get() {
  500. return (this.updated & UPDATED_SEL) > 0;
  501. }
  502. }, {
  503. key: "setStoredMarks",
  504. value: function setStoredMarks(marks) {
  505. this.storedMarks = marks;
  506. this.updated |= UPDATED_MARKS;
  507. return this;
  508. }
  509. }, {
  510. key: "ensureMarks",
  511. value: function ensureMarks(marks) {
  512. if (!prosemirrorModel.Mark.sameSet(this.storedMarks || this.selection.$from.marks(), marks)) this.setStoredMarks(marks);
  513. return this;
  514. }
  515. }, {
  516. key: "addStoredMark",
  517. value: function addStoredMark(mark) {
  518. return this.ensureMarks(mark.addToSet(this.storedMarks || this.selection.$head.marks()));
  519. }
  520. }, {
  521. key: "removeStoredMark",
  522. value: function removeStoredMark(mark) {
  523. return this.ensureMarks(mark.removeFromSet(this.storedMarks || this.selection.$head.marks()));
  524. }
  525. }, {
  526. key: "storedMarksSet",
  527. get: function get() {
  528. return (this.updated & UPDATED_MARKS) > 0;
  529. }
  530. }, {
  531. key: "addStep",
  532. value: function addStep(step, doc) {
  533. _get(_getPrototypeOf(Transaction.prototype), "addStep", this).call(this, step, doc);
  534. this.updated = this.updated & ~UPDATED_MARKS;
  535. this.storedMarks = null;
  536. }
  537. }, {
  538. key: "setTime",
  539. value: function setTime(time) {
  540. this.time = time;
  541. return this;
  542. }
  543. }, {
  544. key: "replaceSelection",
  545. value: function replaceSelection(slice) {
  546. this.selection.replace(this, slice);
  547. return this;
  548. }
  549. }, {
  550. key: "replaceSelectionWith",
  551. value: function replaceSelectionWith(node) {
  552. var inheritMarks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  553. var selection = this.selection;
  554. if (inheritMarks) node = node.mark(this.storedMarks || (selection.empty ? selection.$from.marks() : selection.$from.marksAcross(selection.$to) || prosemirrorModel.Mark.none));
  555. selection.replaceWith(this, node);
  556. return this;
  557. }
  558. }, {
  559. key: "deleteSelection",
  560. value: function deleteSelection() {
  561. this.selection.replace(this);
  562. return this;
  563. }
  564. }, {
  565. key: "insertText",
  566. value: function insertText(text, from, to) {
  567. var schema = this.doc.type.schema;
  568. if (from == null) {
  569. if (!text) return this.deleteSelection();
  570. return this.replaceSelectionWith(schema.text(text), true);
  571. } else {
  572. if (to == null) to = from;
  573. to = to == null ? from : to;
  574. if (!text) return this.deleteRange(from, to);
  575. var marks = this.storedMarks;
  576. if (!marks) {
  577. var $from = this.doc.resolve(from);
  578. marks = to == from ? $from.marks() : $from.marksAcross(this.doc.resolve(to));
  579. }
  580. this.replaceRangeWith(from, to, schema.text(text, marks));
  581. if (!this.selection.empty) this.setSelection(Selection.near(this.selection.$to));
  582. return this;
  583. }
  584. }
  585. }, {
  586. key: "setMeta",
  587. value: function setMeta(key, value) {
  588. this.meta[typeof key == "string" ? key : key.key] = value;
  589. return this;
  590. }
  591. }, {
  592. key: "getMeta",
  593. value: function getMeta(key) {
  594. return this.meta[typeof key == "string" ? key : key.key];
  595. }
  596. }, {
  597. key: "isGeneric",
  598. get: function get() {
  599. for (var _ in this.meta) {
  600. return false;
  601. }
  602. return true;
  603. }
  604. }, {
  605. key: "scrollIntoView",
  606. value: function scrollIntoView() {
  607. this.updated |= UPDATED_SCROLL;
  608. return this;
  609. }
  610. }, {
  611. key: "scrolledIntoView",
  612. get: function get() {
  613. return (this.updated & UPDATED_SCROLL) > 0;
  614. }
  615. }]);
  616. return Transaction;
  617. }(prosemirrorTransform.Transform);
  618. function bind(f, self) {
  619. return !self || !f ? f : f.bind(self);
  620. }
  621. var FieldDesc = _createClass(function FieldDesc(name, desc, self) {
  622. _classCallCheck(this, FieldDesc);
  623. this.name = name;
  624. this.init = bind(desc.init, self);
  625. this.apply = bind(desc.apply, self);
  626. });
  627. var baseFields = [new FieldDesc("doc", {
  628. init: function init(config) {
  629. return config.doc || config.schema.topNodeType.createAndFill();
  630. },
  631. apply: function apply(tr) {
  632. return tr.doc;
  633. }
  634. }), new FieldDesc("selection", {
  635. init: function init(config, instance) {
  636. return config.selection || Selection.atStart(instance.doc);
  637. },
  638. apply: function apply(tr) {
  639. return tr.selection;
  640. }
  641. }), new FieldDesc("storedMarks", {
  642. init: function init(config) {
  643. return config.storedMarks || null;
  644. },
  645. apply: function apply(tr, _marks, _old, state) {
  646. return state.selection.$cursor ? tr.storedMarks : null;
  647. }
  648. }), new FieldDesc("scrollToSelection", {
  649. init: function init() {
  650. return 0;
  651. },
  652. apply: function apply(tr, prev) {
  653. return tr.scrolledIntoView ? prev + 1 : prev;
  654. }
  655. })];
  656. var Configuration = _createClass(function Configuration(schema, plugins) {
  657. var _this3 = this;
  658. _classCallCheck(this, Configuration);
  659. this.schema = schema;
  660. this.plugins = [];
  661. this.pluginsByKey = Object.create(null);
  662. this.fields = baseFields.slice();
  663. if (plugins) plugins.forEach(function (plugin) {
  664. if (_this3.pluginsByKey[plugin.key]) throw new RangeError("Adding different instances of a keyed plugin (" + plugin.key + ")");
  665. _this3.plugins.push(plugin);
  666. _this3.pluginsByKey[plugin.key] = plugin;
  667. if (plugin.spec.state) _this3.fields.push(new FieldDesc(plugin.key, plugin.spec.state, plugin));
  668. });
  669. });
  670. var EditorState = function () {
  671. function EditorState(config) {
  672. _classCallCheck(this, EditorState);
  673. this.config = config;
  674. }
  675. _createClass(EditorState, [{
  676. key: "schema",
  677. get: function get() {
  678. return this.config.schema;
  679. }
  680. }, {
  681. key: "plugins",
  682. get: function get() {
  683. return this.config.plugins;
  684. }
  685. }, {
  686. key: "apply",
  687. value: function apply(tr) {
  688. return this.applyTransaction(tr).state;
  689. }
  690. }, {
  691. key: "filterTransaction",
  692. value: function filterTransaction(tr) {
  693. var ignore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
  694. for (var i = 0; i < this.config.plugins.length; i++) {
  695. if (i != ignore) {
  696. var plugin = this.config.plugins[i];
  697. if (plugin.spec.filterTransaction && !plugin.spec.filterTransaction.call(plugin, tr, this)) return false;
  698. }
  699. }
  700. return true;
  701. }
  702. }, {
  703. key: "applyTransaction",
  704. value: function applyTransaction(rootTr) {
  705. if (!this.filterTransaction(rootTr)) return {
  706. state: this,
  707. transactions: []
  708. };
  709. var trs = [rootTr],
  710. newState = this.applyInner(rootTr),
  711. seen = null;
  712. for (;;) {
  713. var haveNew = false;
  714. for (var i = 0; i < this.config.plugins.length; i++) {
  715. var plugin = this.config.plugins[i];
  716. if (plugin.spec.appendTransaction) {
  717. var n = seen ? seen[i].n : 0,
  718. oldState = seen ? seen[i].state : this;
  719. var tr = n < trs.length && plugin.spec.appendTransaction.call(plugin, n ? trs.slice(n) : trs, oldState, newState);
  720. if (tr && newState.filterTransaction(tr, i)) {
  721. tr.setMeta("appendedTransaction", rootTr);
  722. if (!seen) {
  723. seen = [];
  724. for (var j = 0; j < this.config.plugins.length; j++) {
  725. seen.push(j < i ? {
  726. state: newState,
  727. n: trs.length
  728. } : {
  729. state: this,
  730. n: 0
  731. });
  732. }
  733. }
  734. trs.push(tr);
  735. newState = newState.applyInner(tr);
  736. haveNew = true;
  737. }
  738. if (seen) seen[i] = {
  739. state: newState,
  740. n: trs.length
  741. };
  742. }
  743. }
  744. if (!haveNew) return {
  745. state: newState,
  746. transactions: trs
  747. };
  748. }
  749. }
  750. }, {
  751. key: "applyInner",
  752. value: function applyInner(tr) {
  753. if (!tr.before.eq(this.doc)) throw new RangeError("Applying a mismatched transaction");
  754. var newInstance = new EditorState(this.config),
  755. fields = this.config.fields;
  756. for (var i = 0; i < fields.length; i++) {
  757. var field = fields[i];
  758. newInstance[field.name] = field.apply(tr, this[field.name], this, newInstance);
  759. }
  760. return newInstance;
  761. }
  762. }, {
  763. key: "tr",
  764. get: function get() {
  765. return new Transaction(this);
  766. }
  767. }, {
  768. key: "reconfigure",
  769. value: function reconfigure(config) {
  770. var $config = new Configuration(this.schema, config.plugins);
  771. var fields = $config.fields,
  772. instance = new EditorState($config);
  773. for (var i = 0; i < fields.length; i++) {
  774. var name = fields[i].name;
  775. instance[name] = this.hasOwnProperty(name) ? this[name] : fields[i].init(config, instance);
  776. }
  777. return instance;
  778. }
  779. }, {
  780. key: "toJSON",
  781. value: function toJSON(pluginFields) {
  782. var result = {
  783. doc: this.doc.toJSON(),
  784. selection: this.selection.toJSON()
  785. };
  786. if (this.storedMarks) result.storedMarks = this.storedMarks.map(function (m) {
  787. return m.toJSON();
  788. });
  789. if (pluginFields && _typeof(pluginFields) == 'object') for (var prop in pluginFields) {
  790. if (prop == "doc" || prop == "selection") throw new RangeError("The JSON fields `doc` and `selection` are reserved");
  791. var plugin = pluginFields[prop],
  792. state = plugin.spec.state;
  793. if (state && state.toJSON) result[prop] = state.toJSON.call(plugin, this[plugin.key]);
  794. }
  795. return result;
  796. }
  797. }], [{
  798. key: "create",
  799. value: function create(config) {
  800. var $config = new Configuration(config.doc ? config.doc.type.schema : config.schema, config.plugins);
  801. var instance = new EditorState($config);
  802. for (var i = 0; i < $config.fields.length; i++) {
  803. instance[$config.fields[i].name] = $config.fields[i].init(config, instance);
  804. }
  805. return instance;
  806. }
  807. }, {
  808. key: "fromJSON",
  809. value: function fromJSON(config, json, pluginFields) {
  810. if (!json) throw new RangeError("Invalid input for EditorState.fromJSON");
  811. if (!config.schema) throw new RangeError("Required config field 'schema' missing");
  812. var $config = new Configuration(config.schema, config.plugins);
  813. var instance = new EditorState($config);
  814. $config.fields.forEach(function (field) {
  815. if (field.name == "doc") {
  816. instance.doc = prosemirrorModel.Node.fromJSON(config.schema, json.doc);
  817. } else if (field.name == "selection") {
  818. instance.selection = Selection.fromJSON(instance.doc, json.selection);
  819. } else if (field.name == "storedMarks") {
  820. if (json.storedMarks) instance.storedMarks = json.storedMarks.map(config.schema.markFromJSON);
  821. } else {
  822. if (pluginFields) for (var prop in pluginFields) {
  823. var plugin = pluginFields[prop],
  824. state = plugin.spec.state;
  825. if (plugin.key == field.name && state && state.fromJSON && Object.prototype.hasOwnProperty.call(json, prop)) {
  826. instance[field.name] = state.fromJSON.call(plugin, config, json[prop], instance);
  827. return;
  828. }
  829. }
  830. instance[field.name] = field.init(config, instance);
  831. }
  832. });
  833. return instance;
  834. }
  835. }]);
  836. return EditorState;
  837. }();
  838. function bindProps(obj, self, target) {
  839. for (var prop in obj) {
  840. var val = obj[prop];
  841. if (val instanceof Function) val = val.bind(self);else if (prop == "handleDOMEvents") val = bindProps(val, self, {});
  842. target[prop] = val;
  843. }
  844. return target;
  845. }
  846. var Plugin = function () {
  847. function Plugin(spec) {
  848. _classCallCheck(this, Plugin);
  849. this.spec = spec;
  850. this.props = {};
  851. if (spec.props) bindProps(spec.props, this, this.props);
  852. this.key = spec.key ? spec.key.key : createKey("plugin");
  853. }
  854. _createClass(Plugin, [{
  855. key: "getState",
  856. value: function getState(state) {
  857. return state[this.key];
  858. }
  859. }]);
  860. return Plugin;
  861. }();
  862. var keys = Object.create(null);
  863. function createKey(name) {
  864. if (name in keys) return name + "$" + ++keys[name];
  865. keys[name] = 0;
  866. return name + "$";
  867. }
  868. var PluginKey = function () {
  869. function PluginKey() {
  870. var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "key";
  871. _classCallCheck(this, PluginKey);
  872. this.key = createKey(name);
  873. }
  874. _createClass(PluginKey, [{
  875. key: "get",
  876. value: function get(state) {
  877. return state.config.pluginsByKey[this.key];
  878. }
  879. }, {
  880. key: "getState",
  881. value: function getState(state) {
  882. return state[this.key];
  883. }
  884. }]);
  885. return PluginKey;
  886. }();
  887. exports.AllSelection = AllSelection;
  888. exports.EditorState = EditorState;
  889. exports.NodeSelection = NodeSelection;
  890. exports.Plugin = Plugin;
  891. exports.PluginKey = PluginKey;
  892. exports.Selection = Selection;
  893. exports.SelectionRange = SelectionRange;
  894. exports.TextSelection = TextSelection;
  895. exports.Transaction = Transaction;