UNPKG

prosemirror-model

Version:
1,307 lines (1,306 loc) 106 kB
'use strict'; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } 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); } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } 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); } 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); }; } 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); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); } function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); } 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; } } function _isNativeFunction(fn) { try { return Function.toString.call(fn).indexOf("[native code]") !== -1; } catch (e) { return typeof fn === "function"; } } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } var OrderedMap = require('orderedmap'); function _findDiffStart(a, b, pos) { for (var i = 0;; i++) { if (i == a.childCount || i == b.childCount) return a.childCount == b.childCount ? null : pos; var childA = a.child(i), childB = b.child(i); if (childA == childB) { pos += childA.nodeSize; continue; } if (!childA.sameMarkup(childB)) return pos; if (childA.isText && childA.text != childB.text) { for (var j = 0; childA.text[j] == childB.text[j]; j++) pos++; return pos; } if (childA.content.size || childB.content.size) { var inner = _findDiffStart(childA.content, childB.content, pos + 1); if (inner != null) return inner; } pos += childA.nodeSize; } } function _findDiffEnd(a, b, posA, posB) { for (var iA = a.childCount, iB = b.childCount;;) { if (iA == 0 || iB == 0) return iA == iB ? null : { a: posA, b: posB }; var childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize; if (childA == childB) { posA -= size; posB -= size; continue; } if (!childA.sameMarkup(childB)) return { a: posA, b: posB }; if (childA.isText && childA.text != childB.text) { var same = 0, minSize = Math.min(childA.text.length, childB.text.length); while (same < minSize && childA.text[childA.text.length - same - 1] == childB.text[childB.text.length - same - 1]) { same++; posA--; posB--; } return { a: posA, b: posB }; } if (childA.content.size || childB.content.size) { var inner = _findDiffEnd(childA.content, childB.content, posA - 1, posB - 1); if (inner) return inner; } posA -= size; posB -= size; } } var Fragment = function () { function Fragment(content, size) { _classCallCheck(this, Fragment); this.content = content; this.size = size || 0; if (size == null) for (var i = 0; i < content.length; i++) this.size += content[i].nodeSize; } _createClass(Fragment, [{ key: "nodesBetween", value: function nodesBetween(from, to, f) { var nodeStart = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; var parent = arguments.length > 4 ? arguments[4] : undefined; for (var i = 0, pos = 0; pos < to; i++) { var child = this.content[i], end = pos + child.nodeSize; if (end > from && f(child, nodeStart + pos, parent || null, i) !== false && child.content.size) { var start = pos + 1; child.nodesBetween(Math.max(0, from - start), Math.min(child.content.size, to - start), f, nodeStart + start); } pos = end; } } }, { key: "descendants", value: function descendants(f) { this.nodesBetween(0, this.size, f); } }, { key: "textBetween", value: function textBetween(from, to, blockSeparator, leafText) { var text = "", first = true; this.nodesBetween(from, to, function (node, pos) { var nodeText = node.isText ? node.text.slice(Math.max(from, pos) - pos, to - pos) : !node.isLeaf ? "" : leafText ? typeof leafText === "function" ? leafText(node) : leafText : node.type.spec.leafText ? node.type.spec.leafText(node) : ""; if (node.isBlock && (node.isLeaf && nodeText || node.isTextblock) && blockSeparator) { if (first) first = false;else text += blockSeparator; } text += nodeText; }, 0); return text; } }, { key: "append", value: function append(other) { if (!other.size) return this; if (!this.size) return other; var last = this.lastChild, first = other.firstChild, content = this.content.slice(), i = 0; if (last.isText && last.sameMarkup(first)) { content[content.length - 1] = last.withText(last.text + first.text); i = 1; } for (; i < other.content.length; i++) content.push(other.content[i]); return new Fragment(content, this.size + other.size); } }, { key: "cut", value: function cut(from) { var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.size; if (from == 0 && to == this.size) return this; var result = [], size = 0; if (to > from) for (var i = 0, pos = 0; pos < to; i++) { var child = this.content[i], end = pos + child.nodeSize; if (end > from) { if (pos < from || end > to) { if (child.isText) child = child.cut(Math.max(0, from - pos), Math.min(child.text.length, to - pos));else child = child.cut(Math.max(0, from - pos - 1), Math.min(child.content.size, to - pos - 1)); } result.push(child); size += child.nodeSize; } pos = end; } return new Fragment(result, size); } }, { key: "cutByIndex", value: function cutByIndex(from, to) { if (from == to) return Fragment.empty; if (from == 0 && to == this.content.length) return this; return new Fragment(this.content.slice(from, to)); } }, { key: "replaceChild", value: function replaceChild(index, node) { var current = this.content[index]; if (current == node) return this; var copy = this.content.slice(); var size = this.size + node.nodeSize - current.nodeSize; copy[index] = node; return new Fragment(copy, size); } }, { key: "addToStart", value: function addToStart(node) { return new Fragment([node].concat(this.content), this.size + node.nodeSize); } }, { key: "addToEnd", value: function addToEnd(node) { return new Fragment(this.content.concat(node), this.size + node.nodeSize); } }, { key: "eq", value: function eq(other) { if (this.content.length != other.content.length) return false; for (var i = 0; i < this.content.length; i++) if (!this.content[i].eq(other.content[i])) return false; return true; } }, { key: "firstChild", get: function get() { return this.content.length ? this.content[0] : null; } }, { key: "lastChild", get: function get() { return this.content.length ? this.content[this.content.length - 1] : null; } }, { key: "childCount", get: function get() { return this.content.length; } }, { key: "child", value: function child(index) { var found = this.content[index]; if (!found) throw new RangeError("Index " + index + " out of range for " + this); return found; } }, { key: "maybeChild", value: function maybeChild(index) { return this.content[index] || null; } }, { key: "forEach", value: function forEach(f) { for (var i = 0, p = 0; i < this.content.length; i++) { var child = this.content[i]; f(child, p, i); p += child.nodeSize; } } }, { key: "findDiffStart", value: function findDiffStart(other) { var pos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; return _findDiffStart(this, other, pos); } }, { key: "findDiffEnd", value: function findDiffEnd(other) { var pos = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.size; var otherPos = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : other.size; return _findDiffEnd(this, other, pos, otherPos); } }, { key: "findIndex", value: function findIndex(pos) { var round = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1; if (pos == 0) return retIndex(0, pos); if (pos == this.size) return retIndex(this.content.length, pos); if (pos > this.size || pos < 0) throw new RangeError("Position ".concat(pos, " outside of fragment (").concat(this, ")")); for (var i = 0, curPos = 0;; i++) { var cur = this.child(i), end = curPos + cur.nodeSize; if (end >= pos) { if (end == pos || round > 0) return retIndex(i + 1, end); return retIndex(i, curPos); } curPos = end; } } }, { key: "toString", value: function toString() { return "<" + this.toStringInner() + ">"; } }, { key: "toStringInner", value: function toStringInner() { return this.content.join(", "); } }, { key: "toJSON", value: function toJSON() { return this.content.length ? this.content.map(function (n) { return n.toJSON(); }) : null; } }], [{ key: "fromJSON", value: function fromJSON(schema, value) { if (!value) return Fragment.empty; if (!Array.isArray(value)) throw new RangeError("Invalid input for Fragment.fromJSON"); return new Fragment(value.map(schema.nodeFromJSON)); } }, { key: "fromArray", value: function fromArray(array) { if (!array.length) return Fragment.empty; var joined, size = 0; for (var i = 0; i < array.length; i++) { var node = array[i]; size += node.nodeSize; if (i && node.isText && array[i - 1].sameMarkup(node)) { if (!joined) joined = array.slice(0, i); joined[joined.length - 1] = node.withText(joined[joined.length - 1].text + node.text); } else if (joined) { joined.push(node); } } return new Fragment(joined || array, size); } }, { key: "from", value: function from(nodes) { if (!nodes) return Fragment.empty; if (nodes instanceof Fragment) return nodes; if (Array.isArray(nodes)) return this.fromArray(nodes); if (nodes.attrs) return new Fragment([nodes], nodes.nodeSize); throw new RangeError("Can not convert " + nodes + " to a Fragment" + (nodes.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : "")); } }]); return Fragment; }(); Fragment.empty = new Fragment([], 0); var found = { index: 0, offset: 0 }; function retIndex(index, offset) { found.index = index; found.offset = offset; return found; } function compareDeep(a, b) { if (a === b) return true; if (!(a && _typeof(a) == "object") || !(b && _typeof(b) == "object")) return false; var array = Array.isArray(a); if (Array.isArray(b) != array) return false; if (array) { if (a.length != b.length) return false; for (var i = 0; i < a.length; i++) if (!compareDeep(a[i], b[i])) return false; } else { for (var p in a) if (!(p in b) || !compareDeep(a[p], b[p])) return false; for (var _p in b) if (!(_p in a)) return false; } return true; } var Mark = function () { function Mark(type, attrs) { _classCallCheck(this, Mark); this.type = type; this.attrs = attrs; } _createClass(Mark, [{ key: "addToSet", value: function addToSet(set) { var copy, placed = false; for (var i = 0; i < set.length; i++) { var other = set[i]; if (this.eq(other)) return set; if (this.type.excludes(other.type)) { if (!copy) copy = set.slice(0, i); } else if (other.type.excludes(this.type)) { return set; } else { if (!placed && other.type.rank > this.type.rank) { if (!copy) copy = set.slice(0, i); copy.push(this); placed = true; } if (copy) copy.push(other); } } if (!copy) copy = set.slice(); if (!placed) copy.push(this); return copy; } }, { key: "removeFromSet", value: function removeFromSet(set) { for (var i = 0; i < set.length; i++) if (this.eq(set[i])) return set.slice(0, i).concat(set.slice(i + 1)); return set; } }, { key: "isInSet", value: function isInSet(set) { for (var i = 0; i < set.length; i++) if (this.eq(set[i])) return true; return false; } }, { key: "eq", value: function eq(other) { return this == other || this.type == other.type && compareDeep(this.attrs, other.attrs); } }, { key: "toJSON", value: function toJSON() { var obj = { type: this.type.name }; for (var _ in this.attrs) { obj.attrs = this.attrs; break; } return obj; } }], [{ key: "fromJSON", value: function fromJSON(schema, json) { if (!json) throw new RangeError("Invalid input for Mark.fromJSON"); var type = schema.marks[json.type]; if (!type) throw new RangeError("There is no mark type ".concat(json.type, " in this schema")); var mark = type.create(json.attrs); type.checkAttrs(mark.attrs); return mark; } }, { key: "sameSet", value: function sameSet(a, b) { if (a == b) return true; if (a.length != b.length) return false; for (var i = 0; i < a.length; i++) if (!a[i].eq(b[i])) return false; return true; } }, { key: "setFrom", value: function setFrom(marks) { if (!marks || Array.isArray(marks) && marks.length == 0) return Mark.none; if (marks instanceof Mark) return [marks]; var copy = marks.slice(); copy.sort(function (a, b) { return a.type.rank - b.type.rank; }); return copy; } }]); return Mark; }(); Mark.none = []; var ReplaceError = function (_Error) { _inherits(ReplaceError, _Error); var _super = _createSuper(ReplaceError); function ReplaceError() { _classCallCheck(this, ReplaceError); return _super.apply(this, arguments); } return _createClass(ReplaceError); }(_wrapNativeSuper(Error)); var Slice = function () { function Slice(content, openStart, openEnd) { _classCallCheck(this, Slice); this.content = content; this.openStart = openStart; this.openEnd = openEnd; } _createClass(Slice, [{ key: "size", get: function get() { return this.content.size - this.openStart - this.openEnd; } }, { key: "insertAt", value: function insertAt(pos, fragment) { var content = insertInto(this.content, pos + this.openStart, fragment); return content && new Slice(content, this.openStart, this.openEnd); } }, { key: "removeBetween", value: function removeBetween(from, to) { return new Slice(removeRange(this.content, from + this.openStart, to + this.openStart), this.openStart, this.openEnd); } }, { key: "eq", value: function eq(other) { return this.content.eq(other.content) && this.openStart == other.openStart && this.openEnd == other.openEnd; } }, { key: "toString", value: function toString() { return this.content + "(" + this.openStart + "," + this.openEnd + ")"; } }, { key: "toJSON", value: function toJSON() { if (!this.content.size) return null; var json = { content: this.content.toJSON() }; if (this.openStart > 0) json.openStart = this.openStart; if (this.openEnd > 0) json.openEnd = this.openEnd; return json; } }], [{ key: "fromJSON", value: function fromJSON(schema, json) { if (!json) return Slice.empty; var openStart = json.openStart || 0, openEnd = json.openEnd || 0; if (typeof openStart != "number" || typeof openEnd != "number") throw new RangeError("Invalid input for Slice.fromJSON"); return new Slice(Fragment.fromJSON(schema, json.content), openStart, openEnd); } }, { key: "maxOpen", value: function maxOpen(fragment) { var openIsolating = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var openStart = 0, openEnd = 0; for (var n = fragment.firstChild; n && !n.isLeaf && (openIsolating || !n.type.spec.isolating); n = n.firstChild) openStart++; for (var _n = fragment.lastChild; _n && !_n.isLeaf && (openIsolating || !_n.type.spec.isolating); _n = _n.lastChild) openEnd++; return new Slice(fragment, openStart, openEnd); } }]); return Slice; }(); Slice.empty = new Slice(Fragment.empty, 0, 0); function removeRange(content, from, to) { var _content$findIndex = content.findIndex(from), index = _content$findIndex.index, offset = _content$findIndex.offset, child = content.maybeChild(index); var _content$findIndex2 = content.findIndex(to), indexTo = _content$findIndex2.index, offsetTo = _content$findIndex2.offset; if (offset == from || child.isText) { if (offsetTo != to && !content.child(indexTo).isText) throw new RangeError("Removing non-flat range"); return content.cut(0, from).append(content.cut(to)); } if (index != indexTo) throw new RangeError("Removing non-flat range"); return content.replaceChild(index, child.copy(removeRange(child.content, from - offset - 1, to - offset - 1))); } function insertInto(content, dist, insert, parent) { var _content$findIndex3 = content.findIndex(dist), index = _content$findIndex3.index, offset = _content$findIndex3.offset, child = content.maybeChild(index); if (offset == dist || child.isText) { if (parent && !parent.canReplace(index, index, insert)) return null; return content.cut(0, dist).append(insert).append(content.cut(dist)); } var inner = insertInto(child.content, dist - offset - 1, insert); return inner && content.replaceChild(index, child.copy(inner)); } function _replace($from, $to, slice) { if (slice.openStart > $from.depth) throw new ReplaceError("Inserted content deeper than insertion position"); if ($from.depth - slice.openStart != $to.depth - slice.openEnd) throw new ReplaceError("Inconsistent open depths"); return replaceOuter($from, $to, slice, 0); } function replaceOuter($from, $to, slice, depth) { var index = $from.index(depth), node = $from.node(depth); if (index == $to.index(depth) && depth < $from.depth - slice.openStart) { var inner = replaceOuter($from, $to, slice, depth + 1); return node.copy(node.content.replaceChild(index, inner)); } else if (!slice.content.size) { return close(node, replaceTwoWay($from, $to, depth)); } else if (!slice.openStart && !slice.openEnd && $from.depth == depth && $to.depth == depth) { var parent = $from.parent, content = parent.content; return close(parent, content.cut(0, $from.parentOffset).append(slice.content).append(content.cut($to.parentOffset))); } else { var _prepareSliceForRepla = prepareSliceForReplace(slice, $from), start = _prepareSliceForRepla.start, end = _prepareSliceForRepla.end; return close(node, replaceThreeWay($from, start, end, $to, depth)); } } function checkJoin(main, sub) { if (!sub.type.compatibleContent(main.type)) throw new ReplaceError("Cannot join " + sub.type.name + " onto " + main.type.name); } function joinable($before, $after, depth) { var node = $before.node(depth); checkJoin(node, $after.node(depth)); return node; } function addNode(child, target) { var last = target.length - 1; if (last >= 0 && child.isText && child.sameMarkup(target[last])) target[last] = child.withText(target[last].text + child.text);else target.push(child); } function addRange($start, $end, depth, target) { var node = ($end || $start).node(depth); var startIndex = 0, endIndex = $end ? $end.index(depth) : node.childCount; if ($start) { startIndex = $start.index(depth); if ($start.depth > depth) { startIndex++; } else if ($start.textOffset) { addNode($start.nodeAfter, target); startIndex++; } } for (var i = startIndex; i < endIndex; i++) addNode(node.child(i), target); if ($end && $end.depth == depth && $end.textOffset) addNode($end.nodeBefore, target); } function close(node, content) { node.type.checkContent(content); return node.copy(content); } function replaceThreeWay($from, $start, $end, $to, depth) { var openStart = $from.depth > depth && joinable($from, $start, depth + 1); var openEnd = $to.depth > depth && joinable($end, $to, depth + 1); var content = []; addRange(null, $from, depth, content); if (openStart && openEnd && $start.index(depth) == $end.index(depth)) { checkJoin(openStart, openEnd); addNode(close(openStart, replaceThreeWay($from, $start, $end, $to, depth + 1)), content); } else { if (openStart) addNode(close(openStart, replaceTwoWay($from, $start, depth + 1)), content); addRange($start, $end, depth, content); if (openEnd) addNode(close(openEnd, replaceTwoWay($end, $to, depth + 1)), content); } addRange($to, null, depth, content); return new Fragment(content); } function replaceTwoWay($from, $to, depth) { var content = []; addRange(null, $from, depth, content); if ($from.depth > depth) { var type = joinable($from, $to, depth + 1); addNode(close(type, replaceTwoWay($from, $to, depth + 1)), content); } addRange($to, null, depth, content); return new Fragment(content); } function prepareSliceForReplace(slice, $along) { var extra = $along.depth - slice.openStart, parent = $along.node(extra); var node = parent.copy(slice.content); for (var i = extra - 1; i >= 0; i--) node = $along.node(i).copy(Fragment.from(node)); return { start: node.resolveNoCache(slice.openStart + extra), end: node.resolveNoCache(node.content.size - slice.openEnd - extra) }; } var ResolvedPos = function () { function ResolvedPos(pos, path, parentOffset) { _classCallCheck(this, ResolvedPos); this.pos = pos; this.path = path; this.parentOffset = parentOffset; this.depth = path.length / 3 - 1; } _createClass(ResolvedPos, [{ key: "resolveDepth", value: function resolveDepth(val) { if (val == null) return this.depth; if (val < 0) return this.depth + val; return val; } }, { key: "parent", get: function get() { return this.node(this.depth); } }, { key: "doc", get: function get() { return this.node(0); } }, { key: "node", value: function node(depth) { return this.path[this.resolveDepth(depth) * 3]; } }, { key: "index", value: function index(depth) { return this.path[this.resolveDepth(depth) * 3 + 1]; } }, { key: "indexAfter", value: function indexAfter(depth) { depth = this.resolveDepth(depth); return this.index(depth) + (depth == this.depth && !this.textOffset ? 0 : 1); } }, { key: "start", value: function start(depth) { depth = this.resolveDepth(depth); return depth == 0 ? 0 : this.path[depth * 3 - 1] + 1; } }, { key: "end", value: function end(depth) { depth = this.resolveDepth(depth); return this.start(depth) + this.node(depth).content.size; } }, { key: "before", value: function before(depth) { depth = this.resolveDepth(depth); if (!depth) throw new RangeError("There is no position before the top-level node"); return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1]; } }, { key: "after", value: function after(depth) { depth = this.resolveDepth(depth); if (!depth) throw new RangeError("There is no position after the top-level node"); return depth == this.depth + 1 ? this.pos : this.path[depth * 3 - 1] + this.path[depth * 3].nodeSize; } }, { key: "textOffset", get: function get() { return this.pos - this.path[this.path.length - 1]; } }, { key: "nodeAfter", get: function get() { var parent = this.parent, index = this.index(this.depth); if (index == parent.childCount) return null; var dOff = this.pos - this.path[this.path.length - 1], child = parent.child(index); return dOff ? parent.child(index).cut(dOff) : child; } }, { key: "nodeBefore", get: function get() { var index = this.index(this.depth); var dOff = this.pos - this.path[this.path.length - 1]; if (dOff) return this.parent.child(index).cut(0, dOff); return index == 0 ? null : this.parent.child(index - 1); } }, { key: "posAtIndex", value: function posAtIndex(index, depth) { depth = this.resolveDepth(depth); var node = this.path[depth * 3], pos = depth == 0 ? 0 : this.path[depth * 3 - 1] + 1; for (var i = 0; i < index; i++) pos += node.child(i).nodeSize; return pos; } }, { key: "marks", value: function marks() { var parent = this.parent, index = this.index(); if (parent.content.size == 0) return Mark.none; if (this.textOffset) return parent.child(index).marks; var main = parent.maybeChild(index - 1), other = parent.maybeChild(index); if (!main) { var tmp = main; main = other; other = tmp; } var marks = main.marks; for (var i = 0; i < marks.length; i++) if (marks[i].type.spec.inclusive === false && (!other || !marks[i].isInSet(other.marks))) marks = marks[i--].removeFromSet(marks); return marks; } }, { key: "marksAcross", value: function marksAcross($end) { var after = this.parent.maybeChild(this.index()); if (!after || !after.isInline) return null; var marks = after.marks, next = $end.parent.maybeChild($end.index()); for (var i = 0; i < marks.length; i++) if (marks[i].type.spec.inclusive === false && (!next || !marks[i].isInSet(next.marks))) marks = marks[i--].removeFromSet(marks); return marks; } }, { key: "sharedDepth", value: function sharedDepth(pos) { for (var depth = this.depth; depth > 0; depth--) if (this.start(depth) <= pos && this.end(depth) >= pos) return depth; return 0; } }, { key: "blockRange", value: function blockRange() { var other = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this; var pred = arguments.length > 1 ? arguments[1] : undefined; if (other.pos < this.pos) return other.blockRange(this); for (var d = this.depth - (this.parent.inlineContent || this.pos == other.pos ? 1 : 0); d >= 0; d--) if (other.pos <= this.end(d) && (!pred || pred(this.node(d)))) return new NodeRange(this, other, d); return null; } }, { key: "sameParent", value: function sameParent(other) { return this.pos - this.parentOffset == other.pos - other.parentOffset; } }, { key: "max", value: function max(other) { return other.pos > this.pos ? other : this; } }, { key: "min", value: function min(other) { return other.pos < this.pos ? other : this; } }, { key: "toString", value: function toString() { var str = ""; for (var i = 1; i <= this.depth; i++) str += (str ? "/" : "") + this.node(i).type.name + "_" + this.index(i - 1); return str + ":" + this.parentOffset; } }], [{ key: "resolve", value: function resolve(doc, pos) { if (!(pos >= 0 && pos <= doc.content.size)) throw new RangeError("Position " + pos + " out of range"); var path = []; var start = 0, parentOffset = pos; for (var node = doc;;) { var _node$content$findInd = node.content.findIndex(parentOffset), index = _node$content$findInd.index, offset = _node$content$findInd.offset; var rem = parentOffset - offset; path.push(node, index, start + offset); if (!rem) break; node = node.child(index); if (node.isText) break; parentOffset = rem - 1; start += offset + 1; } return new ResolvedPos(pos, path, parentOffset); } }, { key: "resolveCached", value: function resolveCached(doc, pos) { var cache = resolveCache.get(doc); if (cache) { for (var i = 0; i < cache.elts.length; i++) { var elt = cache.elts[i]; if (elt.pos == pos) return elt; } } else { resolveCache.set(doc, cache = new ResolveCache()); } var result = cache.elts[cache.i] = ResolvedPos.resolve(doc, pos); cache.i = (cache.i + 1) % resolveCacheSize; return result; } }]); return ResolvedPos; }(); var ResolveCache = _createClass(function ResolveCache() { _classCallCheck(this, ResolveCache); this.elts = []; this.i = 0; }); var resolveCacheSize = 12, resolveCache = new WeakMap(); var NodeRange = function () { function NodeRange($from, $to, depth) { _classCallCheck(this, NodeRange); this.$from = $from; this.$to = $to; this.depth = depth; } _createClass(NodeRange, [{ key: "start", get: function get() { return this.$from.before(this.depth + 1); } }, { key: "end", get: function get() { return this.$to.after(this.depth + 1); } }, { key: "parent", get: function get() { return this.$from.node(this.depth); } }, { key: "startIndex", get: function get() { return this.$from.index(this.depth); } }, { key: "endIndex", get: function get() { return this.$to.indexAfter(this.depth); } }]); return NodeRange; }(); var emptyAttrs = Object.create(null); var Node = function () { function Node(type, attrs, content) { var marks = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : Mark.none; _classCallCheck(this, Node); this.type = type; this.attrs = attrs; this.marks = marks; this.content = content || Fragment.empty; } _createClass(Node, [{ key: "children", get: function get() { return this.content.content; } }, { key: "nodeSize", get: function get() { return this.isLeaf ? 1 : 2 + this.content.size; } }, { key: "childCount", get: function get() { return this.content.childCount; } }, { key: "child", value: function child(index) { return this.content.child(index); } }, { key: "maybeChild", value: function maybeChild(index) { return this.content.maybeChild(index); } }, { key: "forEach", value: function forEach(f) { this.content.forEach(f); } }, { key: "nodesBetween", value: function nodesBetween(from, to, f) { var startPos = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; this.content.nodesBetween(from, to, f, startPos, this); } }, { key: "descendants", value: function descendants(f) { this.nodesBetween(0, this.content.size, f); } }, { key: "textContent", get: function get() { return this.isLeaf && this.type.spec.leafText ? this.type.spec.leafText(this) : this.textBetween(0, this.content.size, ""); } }, { key: "textBetween", value: function textBetween(from, to, blockSeparator, leafText) { return this.content.textBetween(from, to, blockSeparator, leafText); } }, { key: "firstChild", get: function get() { return this.content.firstChild; } }, { key: "lastChild", get: function get() { return this.content.lastChild; } }, { key: "eq", value: function eq(other) { return this == other || this.sameMarkup(other) && this.content.eq(other.content); } }, { key: "sameMarkup", value: function sameMarkup(other) { return this.hasMarkup(other.type, other.attrs, other.marks); } }, { key: "hasMarkup", value: function hasMarkup(type, attrs, marks) { return this.type == type && compareDeep(this.attrs, attrs || type.defaultAttrs || emptyAttrs) && Mark.sameSet(this.marks, marks || Mark.none); } }, { key: "copy", value: function copy() { var content = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; if (content == this.content) return this; return new Node(this.type, this.attrs, content, this.marks); } }, { key: "mark", value: function mark(marks) { return marks == this.marks ? this : new Node(this.type, this.attrs, this.content, marks); } }, { key: "cut", value: function cut(from) { var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.content.size; if (from == 0 && to == this.content.size) return this; return this.copy(this.content.cut(from, to)); } }, { key: "slice", value: function slice(from) { var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.content.size; var includeParents = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; if (from == to) return Slice.empty; var $from = this.resolve(from), $to = this.resolve(to); var depth = includeParents ? 0 : $from.sharedDepth(to); var start = $from.start(depth), node = $from.node(depth); var content = node.content.cut($from.pos - start, $to.pos - start); return new Slice(content, $from.depth - depth, $to.depth - depth); } }, { key: "replace", value: function replace(from, to, slice) { return _replace(this.resolve(from), this.resolve(to), slice); } }, { key: "nodeAt", value: function nodeAt(pos) { for (var node = this;;) { var _node$content$findInd2 = node.content.findIndex(pos), index = _node$content$findInd2.index, offset = _node$content$findInd2.offset; node = node.maybeChild(index); if (!node) return null; if (offset == pos || node.isText) return node; pos -= offset + 1; } } }, { key: "childAfter", value: function childAfter(pos) { var _this$content$findInd = this.content.findIndex(pos), index = _this$content$findInd.index, offset = _this$content$findInd.offset; return { node: this.content.maybeChild(index), index: index, offset: offset }; } }, { key: "childBefore", value: function childBefore(pos) { if (pos == 0) return { node: null, index: 0, offset: 0 }; var _this$content$findInd2 = this.content.findIndex(pos), index = _this$content$findInd2.index, offset = _this$content$findInd2.offset; if (offset < pos) return { node: this.content.child(index), index: index, offset: offset }; var node = this.content.child(index - 1); return { node: node, index: index - 1, offset: offset - node.nodeSize }; } }, { key: "resolve", value: function resolve(pos) { return ResolvedPos.resolveCached(this, pos); } }, { key: "resolveNoCache", value: function resolveNoCache(pos) { return ResolvedPos.resolve(this, pos); } }, { key: "rangeHasMark", value: function rangeHasMark(from, to, type) { var found = false; if (to > from) this.nodesBetween(from, to, function (node) { if (type.isInSet(node.marks)) found = true; return !found; }); return found; } }, { key: "isBlock", get: function get() { return this.type.isBlock; } }, { key: "isTextblock", get: function get() { return this.type.isTextblock; } }, { key: "inlineContent", get: function get() { return this.type.inlineContent; } }, { key: "isInline", get: function get() { return this.type.isInline; } }, { key: "isText", get: function get() { return this.type.isText; } }, { key: "isLeaf", get: function get() { return this.type.isLeaf; } }, { key: "isAtom", get: function get() { return this.type.isAtom; } }, { key: "toString", value: function toString() { if (this.type.spec.toDebugString) return this.type.spec.toDebugString(this); var name = this.type.name; if (this.content.size) name += "(" + this.content.toStringInner() + ")"; return wrapMarks(this.marks, name); } }, { key: "contentMatchAt", value: function contentMatchAt(index) { var match = this.type.contentMatch.matchFragment(this.content, 0, index); if (!match) throw new Error("Called contentMatchAt on a node with invalid content"); return match; } }, { key: "canReplace", value: function canReplace(from, to) { var replacement = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Fragment.empty; var start = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; var end = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : replacement.childCount; var one = this.contentMatchAt(from).matchFragment(replacement, start, end); var two = one && one.matchFragment(this.content, to); if (!two || !two.validEnd) return false; for (var i = start; i < end; i++) if (!this.type.allowsMarks(replacement.child(i).marks)) return false; return true; } }, { key: "canReplaceWith", value: function canReplaceWith(from, to, type, marks) { if (marks && !this.type.allowsMarks(marks)) return false; var start = this.contentMatchAt(from).matchType(type); var end = start && start.matchFragment(this.content, to); return end ? end.validEnd : false; } }, { key: "canAppend", value: function canAppend(other) { if (other.content.size) return this.canReplace(this.childCount, this.childCount, other.content);else return this.type.compatibleContent(other.type); } }, { key: "check", value: function check() { this.type.checkContent(this.content); this.type.checkAttrs(this.attrs); var copy = Mark.none; for (var i = 0; i < this.marks.length; i++) { var mark = this.marks[i]; mark.type.checkAttrs(mark.attrs); copy = mark.addToSet(copy); } if (!Mark.sameSet(copy, this.marks)) throw new RangeError("Invalid collection of marks for node ".concat(this.type.name, ": ").concat(this.marks.map(function (m) { return m.type.name; }))); this.content.forEach(function (node) { return node.check(); }); } }, { key: "toJSON", value: function toJSON() { var obj = { type: this.type.name }; for (var _ in this.attrs) { obj.attrs = this.attrs; break; } if (this.content.size) obj.content = this.content.toJSON(); if (this.marks.length) obj.marks = this.marks.map(function (n) { return n.toJSON(); }); return obj; } }], [{ key: "fromJSON", value: function fromJSON(schema, json) { if (!json) throw new RangeError("Invalid input for Node.fromJSON"); var marks = undefined; if (json.marks) { if (!Array.isArray(json.marks)) throw new RangeError("Invalid mark data for Node.fromJSON"); marks = json.marks.map(schema.markFromJSON); } if (json.type == "text") { if (typeof json.text != "string") throw new RangeError("Invalid text node in JSON"); return schema.text(json.text, marks); } var content = Fragment.fromJSON(schema, json.content); var node = schema.nodeType(json.type).create(json.attrs, content, marks); node.type.checkAttrs(node.attrs); return node; } }]); return Node; }(); Node.prototype.text = undefined; var TextNode = function (_Node) { _inherits(TextNode, _Node); var _super2 = _createSuper(TextNode); function TextNode(type, attrs, content, marks) { var _this; _classCallCheck(this, TextNode); _this = _super2.call(this, type, attrs, null, marks); if (!content) throw new RangeError("Empty text nodes are not allowed"); _this.text = content; return _this; } _createClass(TextNode, [{ key: "toString", value: function toString() { if (this.type.spec.toDebugString) return this.type.spec.toDebugString(this); return wrapMarks(this.marks, JSON.stringify(this.text)); } }, { key: "textContent", get: function get() { return this.text; } }, { key: "textBetween", value: function textBetween(from, to) { return this.text.slice(from, to); } }, { key: "nodeSize", get: function get() { return this.text.length; } }, { key: "mark", value: function mark(marks) { return marks == this.marks ? this : new TextNode(this.type, this.attrs, this.text, marks); } }, { key: "withText", value: function withText(text) { if (text == this.text) return this; return new TextNode(this.type, this.attrs, text, this.marks); } }, { key: "cut", value: function cut() { var from = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.text.length; if (from == 0 && to == this.text.length) return this; return this.withText(this.text.slice(from, to)); } }, { key: "eq", value: function eq(other) { return this.sameMarkup(other) && this.text == other.text; } }, { key: "toJSON", value: function toJSON() { var base = _get(_getPrototypeOf(TextNode.prototype), "toJSON", this).call(this); base.text = this.text; return base; } }]); return TextNode; }(Node); function wrapMarks(marks, str) { for (var i = marks.length - 1; i >= 0; i--) str = marks[i].type.name + "(" + str + ")"; return str; } var ContentMatch = function () { function ContentMatch(validEnd) { _classCallCheck(this, ContentMatch); this.validEnd = validEnd; this.next = []; this.wrapCache = []; } _createClass(ContentMatch, [{ key: "matchType", value: function matchType(type) { for (var i = 0; i < this.next.length; i++) if (this.next[i].type == type) return this.next[i].next; return null; } }, { key: "matchFragment", value: function matchFragment(frag) { var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : frag.childCount; var cur = this; for (var i = start; cur && i < end; i++) cur = cur.matchType(frag.child(i).type); return cur; } }, { key: "inlineContent", get: function get() { return this.next.length != 0 && this.next[0].type.isInline; } }, { key: "defaultType", get: function get() { for (var i = 0; i < this.next.length; i++) { var type = this.next[i].type; if (!(type.isText || type.hasRequiredAttrs())) return type; } return null; } }, { key: "compatible", value: function compatible(other) { for (var i = 0; i < this.next.length; i++) for (var j = 0; j < othe