slate
Version:
A completely customizable framework for building rich text editors.
1,381 lines (1,331 loc) • 312 kB
JavaScript
'use strict';
// eslint-disable-next-line no-redeclare
var PathRef = {
transform: function transform(ref, op) {
var current = ref.current,
affinity = ref.affinity;
if (current == null) {
return;
}
var path = Path.transform(current, op, {
affinity: affinity
});
ref.current = path;
if (path == null) {
ref.unref();
}
}
};
// eslint-disable-next-line no-redeclare
var PointRef = {
transform: function transform(ref, op) {
var current = ref.current,
affinity = ref.affinity;
if (current == null) {
return;
}
var point = Point.transform(current, op, {
affinity: affinity
});
ref.current = point;
if (point == null) {
ref.unref();
}
}
};
// eslint-disable-next-line no-redeclare
var RangeRef = {
transform: function transform(ref, op) {
var current = ref.current,
affinity = ref.affinity;
if (current == null) {
return;
}
var path = Range.transform(current, op, {
affinity: affinity
});
ref.current = path;
if (path == null) {
ref.unref();
}
}
};
var DIRTY_PATHS = new WeakMap();
var DIRTY_PATH_KEYS = new WeakMap();
var FLUSHING = new WeakMap();
var NORMALIZING = new WeakMap();
var PATH_REFS = new WeakMap();
var POINT_REFS = new WeakMap();
var RANGE_REFS = new WeakMap();
function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var arrayLikeToArray = createCommonjsModule(function (module) {
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;
}
module.exports = _arrayLikeToArray, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(arrayLikeToArray);
var arrayWithoutHoles = createCommonjsModule(function (module) {
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return arrayLikeToArray(arr);
}
module.exports = _arrayWithoutHoles, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(arrayWithoutHoles);
var iterableToArray = createCommonjsModule(function (module) {
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
module.exports = _iterableToArray, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(iterableToArray);
var unsupportedIterableToArray = createCommonjsModule(function (module) {
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);
}
module.exports = _unsupportedIterableToArray, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(unsupportedIterableToArray);
var nonIterableSpread = createCommonjsModule(function (module) {
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
module.exports = _nonIterableSpread, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(nonIterableSpread);
var toConsumableArray = createCommonjsModule(function (module) {
function _toConsumableArray(arr) {
return arrayWithoutHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableSpread();
}
module.exports = _toConsumableArray, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _toConsumableArray = unwrapExports(toConsumableArray);
// eslint-disable-next-line no-redeclare
var Path = {
ancestors: function ancestors(path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$reverse = options.reverse,
reverse = _options$reverse === void 0 ? false : _options$reverse;
var paths = Path.levels(path, options);
if (reverse) {
paths = paths.slice(1);
} else {
paths = paths.slice(0, -1);
}
return paths;
},
common: function common(path, another) {
var common = [];
for (var i = 0; i < path.length && i < another.length; i++) {
var av = path[i];
var bv = another[i];
if (typeof av !== 'number' || typeof bv !== 'number') {
throw new Error('Got non-numeric path index');
}
if (av !== bv) {
break;
}
common.push(av);
}
return common;
},
compare: function compare(path, another) {
var min = Math.min(path.length, another.length);
for (var i = 0; i < min; i++) {
if (path[i] < another[i]) return -1;
if (path[i] > another[i]) return 1;
}
return 0;
},
endsAfter: function endsAfter(path, another) {
var i = path.length - 1;
var as = path.slice(0, i);
var bs = another.slice(0, i);
var av = path[i];
var bv = another[i];
return Path.equals(as, bs) && av > bv;
},
endsAt: function endsAt(path, another) {
var i = path.length;
var as = path.slice(0, i);
var bs = another.slice(0, i);
return Path.equals(as, bs);
},
endsBefore: function endsBefore(path, another) {
var i = path.length - 1;
var as = path.slice(0, i);
var bs = another.slice(0, i);
var av = path[i];
var bv = another[i];
return Path.equals(as, bs) && av < bv;
},
equals: function equals(path, another) {
return path.length === another.length && path.every(function (n, i) {
return n === another[i];
});
},
hasPrevious: function hasPrevious(path) {
return path[path.length - 1] > 0;
},
isAfter: function isAfter(path, another) {
return Path.compare(path, another) === 1;
},
isAncestor: function isAncestor(path, another) {
return path.length < another.length && Path.compare(path, another) === 0;
},
isBefore: function isBefore(path, another) {
return Path.compare(path, another) === -1;
},
isChild: function isChild(path, another) {
return path.length === another.length + 1 && Path.compare(path, another) === 0;
},
isCommon: function isCommon(path, another) {
return path.length <= another.length && Path.compare(path, another) === 0;
},
isDescendant: function isDescendant(path, another) {
return path.length > another.length && Path.compare(path, another) === 0;
},
isParent: function isParent(path, another) {
return path.length + 1 === another.length && Path.compare(path, another) === 0;
},
isPath: function isPath(value) {
return Array.isArray(value) && value.every(function (n) {
return typeof n === 'number';
});
},
isSibling: function isSibling(path, another) {
if (path.length !== another.length) {
return false;
}
var as = path.slice(0, -1);
var bs = another.slice(0, -1);
var al = path[path.length - 1];
var bl = another[another.length - 1];
return al !== bl && Path.equals(as, bs);
},
levels: function levels(path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$reverse2 = options.reverse,
reverse = _options$reverse2 === void 0 ? false : _options$reverse2;
var list = [];
for (var i = 0; i <= path.length; i++) {
list.push(path.slice(0, i));
}
if (reverse) {
list.reverse();
}
return list;
},
next: function next(path) {
if (path.length === 0) {
throw new Error("Cannot get the next path of a root path [".concat(path, "], because it has no next index."));
}
var last = path[path.length - 1];
return path.slice(0, -1).concat(last + 1);
},
operationCanTransformPath: function operationCanTransformPath(operation) {
switch (operation.type) {
case 'insert_node':
case 'remove_node':
case 'merge_node':
case 'split_node':
case 'move_node':
return true;
default:
return false;
}
},
parent: function parent(path) {
if (path.length === 0) {
throw new Error("Cannot get the parent path of the root path [".concat(path, "]."));
}
return path.slice(0, -1);
},
previous: function previous(path) {
if (path.length === 0) {
throw new Error("Cannot get the previous path of a root path [".concat(path, "], because it has no previous index."));
}
var last = path[path.length - 1];
if (last <= 0) {
throw new Error("Cannot get the previous path of a first child path [".concat(path, "] because it would result in a negative index."));
}
return path.slice(0, -1).concat(last - 1);
},
relative: function relative(path, ancestor) {
if (!Path.isAncestor(ancestor, path) && !Path.equals(path, ancestor)) {
throw new Error("Cannot get the relative path of [".concat(path, "] inside ancestor [").concat(ancestor, "], because it is not above or equal to the path."));
}
return path.slice(ancestor.length);
},
transform: function transform(path, operation) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (!path) return null;
// PERF: use destructing instead of immer
var p = _toConsumableArray(path);
var _options$affinity = options.affinity,
affinity = _options$affinity === void 0 ? 'forward' : _options$affinity;
// PERF: Exit early if the operation is guaranteed not to have an effect.
if (path.length === 0) {
return p;
}
switch (operation.type) {
case 'insert_node':
{
var op = operation.path;
if (Path.equals(op, p) || Path.endsBefore(op, p) || Path.isAncestor(op, p)) {
p[op.length - 1] += 1;
}
break;
}
case 'remove_node':
{
var _op = operation.path;
if (Path.equals(_op, p) || Path.isAncestor(_op, p)) {
return null;
} else if (Path.endsBefore(_op, p)) {
p[_op.length - 1] -= 1;
}
break;
}
case 'merge_node':
{
var _op2 = operation.path,
position = operation.position;
if (Path.equals(_op2, p) || Path.endsBefore(_op2, p)) {
p[_op2.length - 1] -= 1;
} else if (Path.isAncestor(_op2, p)) {
p[_op2.length - 1] -= 1;
p[_op2.length] += position;
}
break;
}
case 'split_node':
{
var _op3 = operation.path,
_position = operation.position;
if (Path.equals(_op3, p)) {
if (affinity === 'forward') {
p[p.length - 1] += 1;
} else if (affinity === 'backward') ; else {
return null;
}
} else if (Path.endsBefore(_op3, p)) {
p[_op3.length - 1] += 1;
} else if (Path.isAncestor(_op3, p) && path[_op3.length] >= _position) {
p[_op3.length - 1] += 1;
p[_op3.length] -= _position;
}
break;
}
case 'move_node':
{
var _op4 = operation.path,
onp = operation.newPath;
// If the old and new path are the same, it's a no-op.
if (Path.equals(_op4, onp)) {
return p;
}
if (Path.isAncestor(_op4, p) || Path.equals(_op4, p)) {
var copy = onp.slice();
if (Path.endsBefore(_op4, onp) && _op4.length < onp.length) {
copy[_op4.length - 1] -= 1;
}
return copy.concat(p.slice(_op4.length));
} else if (Path.isSibling(_op4, onp) && (Path.isAncestor(onp, p) || Path.equals(onp, p))) {
if (Path.endsBefore(_op4, p)) {
p[_op4.length - 1] -= 1;
} else {
p[_op4.length - 1] += 1;
}
} else if (Path.endsBefore(onp, p) || Path.equals(onp, p) || Path.isAncestor(onp, p)) {
if (Path.endsBefore(_op4, p)) {
p[_op4.length - 1] -= 1;
}
p[onp.length - 1] += 1;
} else if (Path.endsBefore(_op4, p)) {
if (Path.equals(onp, p)) {
p[onp.length - 1] += 1;
}
p[_op4.length - 1] -= 1;
}
break;
}
}
return p;
}
};
var _typeof_1 = createCommonjsModule(function (module) {
function _typeof(o) {
"@babel/helpers - typeof";
return (module.exports = _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;
}, module.exports.__esModule = true, module.exports["default"] = module.exports), _typeof(o);
}
module.exports = _typeof, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _typeof = unwrapExports(_typeof_1);
var toPrimitive = createCommonjsModule(function (module) {
var _typeof = _typeof_1["default"];
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);
}
module.exports = _toPrimitive, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(toPrimitive);
var toPropertyKey = createCommonjsModule(function (module) {
var _typeof = _typeof_1["default"];
function _toPropertyKey(arg) {
var key = toPrimitive(arg, "string");
return _typeof(key) === "symbol" ? key : String(key);
}
module.exports = _toPropertyKey, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(toPropertyKey);
var defineProperty = createCommonjsModule(function (module) {
function _defineProperty(obj, key, value) {
key = toPropertyKey(key);
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
module.exports = _defineProperty, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _defineProperty = unwrapExports(defineProperty);
var arrayWithHoles = createCommonjsModule(function (module) {
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
module.exports = _arrayWithHoles, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(arrayWithHoles);
var iterableToArrayLimit = createCommonjsModule(function (module) {
function _iterableToArrayLimit(r, l) {
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (null != t) {
var e,
n,
i,
u,
a = [],
f = true,
o = false;
try {
if (i = (t = t.call(r)).next, 0 === l) {
if (Object(t) !== t) return;
f = !1;
} else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
} catch (r) {
o = true, n = r;
} finally {
try {
if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return;
} finally {
if (o) throw n;
}
}
return a;
}
}
module.exports = _iterableToArrayLimit, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(iterableToArrayLimit);
var nonIterableRest = createCommonjsModule(function (module) {
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
module.exports = _nonIterableRest, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(nonIterableRest);
var slicedToArray = createCommonjsModule(function (module) {
function _slicedToArray(arr, i) {
return arrayWithHoles(arr) || iterableToArrayLimit(arr, i) || unsupportedIterableToArray(arr, i) || nonIterableRest();
}
module.exports = _slicedToArray, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _slicedToArray = unwrapExports(slicedToArray);
var objectWithoutPropertiesLoose = createCommonjsModule(function (module) {
function _objectWithoutPropertiesLoose(source, excluded) {
if (source == null) return {};
var target = {};
var sourceKeys = Object.keys(source);
var key, i;
for (i = 0; i < sourceKeys.length; i++) {
key = sourceKeys[i];
if (excluded.indexOf(key) >= 0) continue;
target[key] = source[key];
}
return target;
}
module.exports = _objectWithoutPropertiesLoose, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(objectWithoutPropertiesLoose);
var objectWithoutProperties = createCommonjsModule(function (module) {
function _objectWithoutProperties(source, excluded) {
if (source == null) return {};
var target = objectWithoutPropertiesLoose(source, excluded);
var key, i;
if (Object.getOwnPropertySymbols) {
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
for (i = 0; i < sourceSymbolKeys.length; i++) {
key = sourceSymbolKeys[i];
if (excluded.indexOf(key) >= 0) continue;
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
target[key] = source[key];
}
}
return target;
}
module.exports = _objectWithoutProperties, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _objectWithoutProperties = unwrapExports(objectWithoutProperties);
var _excluded$4 = ["anchor", "focus"];
function ownKeys$g(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread$g(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$g(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$g(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
// eslint-disable-next-line no-redeclare
var Range = {
edges: function edges(range) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$reverse = options.reverse,
reverse = _options$reverse === void 0 ? false : _options$reverse;
var anchor = range.anchor,
focus = range.focus;
return Range.isBackward(range) === reverse ? [anchor, focus] : [focus, anchor];
},
end: function end(range) {
var _Range$edges = Range.edges(range),
_Range$edges2 = _slicedToArray(_Range$edges, 2),
end = _Range$edges2[1];
return end;
},
equals: function equals(range, another) {
return Point.equals(range.anchor, another.anchor) && Point.equals(range.focus, another.focus);
},
surrounds: function surrounds(range, target) {
var intersectionRange = Range.intersection(range, target);
if (!intersectionRange) {
return false;
}
return Range.equals(intersectionRange, target);
},
includes: function includes(range, target) {
if (Location.isRange(target)) {
if (Range.includes(range, target.anchor) || Range.includes(range, target.focus)) {
return true;
}
var _Range$edges3 = Range.edges(range),
_Range$edges4 = _slicedToArray(_Range$edges3, 2),
rs = _Range$edges4[0],
re = _Range$edges4[1];
var _Range$edges5 = Range.edges(target),
_Range$edges6 = _slicedToArray(_Range$edges5, 2),
ts = _Range$edges6[0],
te = _Range$edges6[1];
return Point.isBefore(rs, ts) && Point.isAfter(re, te);
}
var _Range$edges7 = Range.edges(range),
_Range$edges8 = _slicedToArray(_Range$edges7, 2),
start = _Range$edges8[0],
end = _Range$edges8[1];
var isAfterStart = false;
var isBeforeEnd = false;
if (Location.isPoint(target)) {
isAfterStart = Point.compare(target, start) >= 0;
isBeforeEnd = Point.compare(target, end) <= 0;
} else {
isAfterStart = Path.compare(target, start.path) >= 0;
isBeforeEnd = Path.compare(target, end.path) <= 0;
}
return isAfterStart && isBeforeEnd;
},
intersection: function intersection(range, another) {
range.anchor;
range.focus;
var rest = _objectWithoutProperties(range, _excluded$4);
var _Range$edges9 = Range.edges(range),
_Range$edges10 = _slicedToArray(_Range$edges9, 2),
s1 = _Range$edges10[0],
e1 = _Range$edges10[1];
var _Range$edges11 = Range.edges(another),
_Range$edges12 = _slicedToArray(_Range$edges11, 2),
s2 = _Range$edges12[0],
e2 = _Range$edges12[1];
var start = Point.isBefore(s1, s2) ? s2 : s1;
var end = Point.isBefore(e1, e2) ? e1 : e2;
if (Point.isBefore(end, start)) {
return null;
} else {
return _objectSpread$g({
anchor: start,
focus: end
}, rest);
}
},
isBackward: function isBackward(range) {
var anchor = range.anchor,
focus = range.focus;
return Point.isAfter(anchor, focus);
},
isCollapsed: function isCollapsed(range) {
var anchor = range.anchor,
focus = range.focus;
return Point.equals(anchor, focus);
},
isExpanded: function isExpanded(range) {
return !Range.isCollapsed(range);
},
isForward: function isForward(range) {
return !Range.isBackward(range);
},
isRange: function isRange(value) {
return isObject(value) && Point.isPoint(value.anchor) && Point.isPoint(value.focus);
},
points: function* points(range) {
yield [range.anchor, 'anchor'];
yield [range.focus, 'focus'];
},
start: function start(range) {
var _Range$edges13 = Range.edges(range),
_Range$edges14 = _slicedToArray(_Range$edges13, 1),
start = _Range$edges14[0];
return start;
},
transform: function transform(range, op) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (range === null) {
return null;
}
var _options$affinity = options.affinity,
affinity = _options$affinity === void 0 ? 'inward' : _options$affinity;
var affinityAnchor;
var affinityFocus;
if (affinity === 'inward') {
// If the range is collapsed, make sure to use the same affinity to
// avoid the two points passing each other and expanding in the opposite
// direction
var isCollapsed = Range.isCollapsed(range);
if (Range.isForward(range)) {
affinityAnchor = 'forward';
affinityFocus = isCollapsed ? affinityAnchor : 'backward';
} else {
affinityAnchor = 'backward';
affinityFocus = isCollapsed ? affinityAnchor : 'forward';
}
} else if (affinity === 'outward') {
if (Range.isForward(range)) {
affinityAnchor = 'backward';
affinityFocus = 'forward';
} else {
affinityAnchor = 'forward';
affinityFocus = 'backward';
}
} else {
affinityAnchor = affinity;
affinityFocus = affinity;
}
var anchor = Point.transform(range.anchor, op, {
affinity: affinityAnchor
});
var focus = Point.transform(range.focus, op, {
affinity: affinityFocus
});
if (!anchor || !focus) {
return null;
}
return {
anchor: anchor,
focus: focus
};
}
};
/**
* Shared the function with isElementType utility
*/
var isElement = function isElement(value) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref$deep = _ref.deep,
deep = _ref$deep === void 0 ? false : _ref$deep;
if (!isObject(value)) return false;
// PERF: No need to use the full Editor.isEditor here
var isEditor = typeof value.apply === 'function';
if (isEditor) return false;
var isChildrenValid = deep ? Node.isNodeList(value.children) : Array.isArray(value.children);
return isChildrenValid;
};
// eslint-disable-next-line no-redeclare
var Element = {
isAncestor: function isAncestor(value) {
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref2$deep = _ref2.deep,
deep = _ref2$deep === void 0 ? false : _ref2$deep;
return isObject(value) && Node.isNodeList(value.children, {
deep: deep
});
},
isElement: isElement,
isElementList: function isElementList(value) {
var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref3$deep = _ref3.deep,
deep = _ref3$deep === void 0 ? false : _ref3$deep;
return Array.isArray(value) && value.every(function (val) {
return Element.isElement(val, {
deep: deep
});
});
},
isElementProps: function isElementProps(props) {
return props.children !== undefined;
},
isElementType: function isElementType(value, elementVal) {
var elementKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'type';
return isElement(value) && value[elementKey] === elementVal;
},
matches: function matches(element, props) {
for (var key in props) {
if (key === 'children') {
continue;
}
if (element[key] !== props[key]) {
return false;
}
}
return true;
}
};
var _excluded$3 = ["text"],
_excluded2$3 = ["children"];
function ownKeys$f(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread$f(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$f(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$f(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _createForOfIteratorHelper$l(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$l(o)) || allowArrayLike) { 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$l(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$l(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$l(o, minLen); }
function _arrayLikeToArray$l(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; }
// eslint-disable-next-line no-redeclare
var Node = {
ancestor: function ancestor(root, path) {
var node = Node.get(root, path);
if (Node.isText(node)) {
throw new Error("Cannot get the ancestor node at path [".concat(path, "] because it refers to a text node instead: ").concat(Scrubber.stringify(node)));
}
return node;
},
ancestors: function ancestors(root, path) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return function* () {
var _iterator = _createForOfIteratorHelper$l(Path.ancestors(path, options)),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var p = _step.value;
var n = Node.ancestor(root, p);
var entry = [n, p];
yield entry;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}();
},
child: function child(root, index) {
if (Node.isText(root)) {
throw new Error("Cannot get the child of a text node: ".concat(Scrubber.stringify(root)));
}
if (typeof index !== 'number') {
throw new Error('Expected index to be a number');
}
var c = root.children[index];
if (c == null) {
throw new Error("Cannot get child at index `".concat(index, "` in node: ").concat(Scrubber.stringify(root)));
}
return c;
},
children: function children(root, path) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return function* () {
var _options$reverse = options.reverse,
reverse = _options$reverse === void 0 ? false : _options$reverse;
var ancestor = Node.ancestor(root, path);
var children = ancestor.children;
var index = reverse ? children.length - 1 : 0;
while (reverse ? index >= 0 : index < children.length) {
var child = Node.child(ancestor, index);
var childPath = path.concat(index);
yield [child, childPath];
index = reverse ? index - 1 : index + 1;
}
}();
},
common: function common(root, path, another) {
var p = Path.common(path, another);
var n = Node.get(root, p);
return [n, p];
},
descendant: function descendant(root, path) {
var node = Node.get(root, path);
if (Node.isEditor(node)) {
throw new Error("Cannot get the descendant node at path [".concat(path, "] because it refers to the root editor node instead: ").concat(Scrubber.stringify(node)));
}
return node;
},
descendants: function descendants(root) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return function* () {
var _iterator2 = _createForOfIteratorHelper$l(Node.nodes(root, options)),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _step2$value = _slicedToArray(_step2.value, 2),
node = _step2$value[0],
path = _step2$value[1];
if (path.length !== 0) {
// NOTE: we have to coerce here because checking the path's length does
// guarantee that `node` is not a `Editor`, but TypeScript doesn't know.
yield [node, path];
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}();
},
elements: function elements(root) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return function* () {
var _iterator3 = _createForOfIteratorHelper$l(Node.nodes(root, options)),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var _step3$value = _slicedToArray(_step3.value, 2),
node = _step3$value[0],
path = _step3$value[1];
if (Node.isElement(node)) {
yield [node, path];
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
}();
},
extractProps: function extractProps(node) {
if (Node.isText(node)) {
node.text;
var properties = _objectWithoutProperties(node, _excluded$3);
return properties;
} else {
node.children;
var _properties = _objectWithoutProperties(node, _excluded2$3);
return _properties;
}
},
first: function first(root, path) {
var p = path.slice();
var n = Node.get(root, p);
while (n) {
if (Node.isText(n) || n.children.length === 0) {
break;
} else {
n = n.children[0];
p.push(0);
}
}
return [n, p];
},
fragment: function fragment(root, range) {
var newRoot = {
children: root.children
};
var _Range$edges = Range.edges(range),
_Range$edges2 = _slicedToArray(_Range$edges, 2),
start = _Range$edges2[0],
end = _Range$edges2[1];
var nodeEntries = Node.nodes(newRoot, {
reverse: true,
pass: function pass(_ref) {
var _ref2 = _slicedToArray(_ref, 2),
path = _ref2[1];
return !Range.includes(range, path);
}
});
var _iterator4 = _createForOfIteratorHelper$l(nodeEntries),
_step4;
try {
var _loop = function _loop() {
var _step4$value = _slicedToArray(_step4.value, 2),
path = _step4$value[1];
if (!Range.includes(range, path)) {
var index = path[path.length - 1];
modifyChildren(newRoot, Path.parent(path), function (children) {
return removeChildren(children, index, 1);
});
}
if (Path.equals(path, end.path)) {
modifyLeaf(newRoot, path, function (node) {
var before = node.text.slice(0, end.offset);
return _objectSpread$f(_objectSpread$f({}, node), {}, {
text: before
});
});
}
if (Path.equals(path, start.path)) {
modifyLeaf(newRoot, path, function (node) {
var before = node.text.slice(start.offset);
return _objectSpread$f(_objectSpread$f({}, node), {}, {
text: before
});
});
}
};
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
_loop();
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
return newRoot.children;
},
get: function get(root, path) {
var node = Node.getIf(root, path);
if (node === undefined) {
throw new Error("Cannot find a descendant at path [".concat(path, "] in node: ").concat(Scrubber.stringify(root)));
}
return node;
},
getIf: function getIf(root, path) {
var node = root;
for (var i = 0; i < path.length; i++) {
var p = path[i];
if (typeof p !== 'number') {
throw new Error('Got non-numeric path index');
}
if (Node.isText(node) || !node.children[p]) {
return;
}
node = node.children[p];
}
return node;
},
has: function has(root, path) {
var node = root;
for (var i = 0; i < path.length; i++) {
var p = path[i];
if (typeof p !== 'number') {
throw new Error('Got non-numeric path index');
}
if (Node.isText(node) || !node.children[p]) {
return false;
}
node = node.children[p];
}
return true;
},
isAncestor: function isAncestor(node) {
return !Node.isText(node);
},
isEditor: function isEditor(node) {
return typeof node.apply === 'function';
},
isElement: function isElement(node) {
return Array.isArray(node.children) && typeof node.apply !== 'function';
},
isNode: function isNode(value) {
var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref3$deep = _ref3.deep,
deep = _ref3$deep === void 0 ? false : _ref3$deep;
return Text.isText(value) || Element.isElement(value, {
deep: deep
}) || Editor.isEditor(value, {
deep: deep
});
},
isNodeList: function isNodeList(value) {
var _ref4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref4$deep = _ref4.deep,
deep = _ref4$deep === void 0 ? false : _ref4$deep;
return Array.isArray(value) && value.every(function (val) {
return Node.isNode(val, {
deep: deep
});
});
},
isText: function isText(node) {
return typeof node.text === 'string';
},
last: function last(root, path) {
var p = path.slice();
var n = Node.get(root, p);
while (n) {
if (Node.isText(n) || n.children.length === 0) {
break;
} else {
var i = n.children.length - 1;
n = n.children[i];
p.push(i);
}
}
return [n, p];
},
leaf: function leaf(root, path) {
var node = Node.get(root, path);
if (!Node.isText(node)) {
throw new Error("Cannot get the leaf node at path [".concat(path, "] because it refers to a non-leaf node: ").concat(Scrubber.stringify(node)));
}
return node;
},
levels: function levels(root, path) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return function* () {
var _iterator5 = _createForOfIteratorHelper$l(Path.levels(path, options)),
_step5;
try {
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
var p = _step5.value;
var n = Node.get(root, p);
yield [n, p];
}
} catch (err) {
_iterator5.e(err);
} finally {
_iterator5.f();
}
}();
},
matches: function matches(node, props) {
return Node.isElement(node) && Element.isElementProps(props) && Element.matches(node, props) || Node.isText(node) && Text.isTextProps(props) && Text.matches(node, props);
},
nodes: function nodes(root) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return function* () {
var pass = options.pass,
_options$reverse2 = options.reverse,
reverse = _options$reverse2 === void 0 ? false : _options$reverse2;
var _options$from = options.from,
from = _options$from === void 0 ? [] : _options$from,
to = options.to;
var visited = new Set();
var p = [];
var n = root;
while (true) {
if (to && (reverse ? Path.isBefore(p, to) : Path.isAfter(p, to))) {
break;
}
if (!visited.has(n)) {
yield [n, p];
}
// If we're allowed to go downward and we haven't descended yet, do.
if (!visited.has(n) && !Node.isText(n) && n.children.length !== 0 && (pass == null || pass([n, p]) === false)) {
visited.add(n);
var nextIndex = reverse ? n.children.length - 1 : 0;
if (Path.isAncestor(p, from)) {
nextIndex = from[p.length];
}
p = p.concat(nextIndex);
n = Node.get(root, p);
continue;
}
// If we're at the root and we can't go down, we're done.
if (p.length === 0) {
break;
}
// If we're going forward...
if (!reverse) {
var newPath = Path.next(p);
if (Node.has(root, newPath)) {
p = newPath;
n = Node.get(root, p);
continue;
}
}
// If we're going backward...
if (reverse && p[p.length - 1] !== 0) {
var _newPath = Path.previous(p);
p = _newPath;
n = Node.get(root, p);
continue;
}
// Otherwise we're going upward...
p = Path.parent(p);
n = Node.get(root, p);
visited.add(n);
}
}();
},
parent: function parent(root, path) {
var parentPath = Path.parent(path);
var node = Node.get(root, parentPath);
if (Node.isText(node)) {
// this can happen if `path` points somewhere that doesnt exist and it's where a child of a text node would be
throw new Error("Cannot get the parent of path [".concat(path, "] because it does not exist in the root."));
}
return node;
},
string: function string(node) {
if (Node.isText(node)) {
return node.text;
} else {
return node.children.map(Node.string).join('');
}
},
texts: function texts(root) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return function* () {
var _iterator6 = _createForOfIteratorHelper$l(Node.nodes(root, options)),
_step6;
try {
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
var _step6$value = _slicedToArray(_step6.value, 2),
node = _step6$value[0],
path = _step6$value[1];
if (Node.isText(node)) {
yield [node, path];
}
}
} catch (err) {
_iterator6.e(err);
} finally {
_iterator6.f();
}
}();
}
};
function ownKeys$e(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread$e(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$e(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$e(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
// eslint-disable-next-line no-redeclare
var Operation = {
isNodeOperation: function isNodeOperation(value) {
return Operation.isOperation(value) && value.type.endsWith('_node');
},
isOperation: function isOperation(value) {
if (!isObject(value)) {
return false;
}
switch (value.type) {
case 'insert_node':
return Path.isPath(value.path) && Node.isNode(value.node);
case 'insert_text':
return typeof value.offset === 'number' && typeof value.text === 'string' && Path.isPath(value.path);
case 'merge_node':
return typeof value.position === 'number' && Path.isPath(value.path) && isObject(value.properties);
case 'move_node':
return Path.isPath(value.path) && Path.isPath(value.newPath);
case 'remove_node':
return Path.isPath(value.path) && Node.isNode(value.node);
case 'remove_text':
return typeof value.offset === 'number' && typeof value.text === 'string' && Path.isPath(value.path);
case 'set_node':
return Path.isPath(value.path) && isObject(value.properties) && isObject(value.newProperties);
case 'set_selection':
return value.properties === null && Range.isRange(value.newProperties) || value.newProperties === null && Range.isRange(value.properties) || isObject(value.properties) && isObject(value.newProperties);
case 'split_node':
return Path.isPath(value.path) && typeof value.position === 'number' && isObject(value.properties);
default:
return false;
}
},
isOperationList: function isOperationList(value) {
return Array.isArray(value) && value.every(function (val) {
return Operation.isOperation(val);
});
},
isSelectionOperation: function isSelectionOperation(value) {
return Operation.isOperation(value) && value.type.endsWith('_selection');
},
isTextOperation: function isTextOperation(value) {
return Operation.isOperation(value) && value.type.endsWith('_text');
},
inverse: function inverse(op) {
switch (op.type) {
case 'insert_node':
{
return _objectSpread$e(_objectSpread$e({}, op), {}, {
type: 'remove_node'
});
}
case 'insert_text':
{
return _objectSpread$e(_objectSpread$e({}, op), {}, {
type: 'remove_text'
});
}
case 'merge_node':
{
return _objectSpread$e(_objectSpread$e({}, op), {}, {
type: 'split_node',
path: Path.previous(op.path)
});
}
case 'move_node':
{
var newPath = op.newPath,
path = op.path;
// PERF: in this case the move operation is a no-op anyways.
if (Path.equals(newPath, path)) {
return op;
}
// If the move happens completely within a single parent the path and
// newPath are stable with respect to each other.
if (Path.isSibling(path, newPath)) {
return _objectSpread$e(_objectSpread$e({}, op), {}, {
path: newPath,
newPath: path
});
}
// If the move does not happen within a single parent it is possible
// for the move to impact the true path to the location where the node
// was removed from and where it was inserted. We have to adjust for this
// and find the original path. We can accomplish this (only in non-sibling)
// moves by looking at the impact of the move operation on the node
// after the original move path.
var inversePath = Path.transform(path, op);
var inverseNewPath = Path.transform(Path.next(path), op);
return _objectSpread$e(_objectSpread$e({}, op), {}, {
path: inversePath,
newPath: inverseNewPath
});
}
case 'remove_node':
{
return _objectSpread$e(_objectSpread$e({}, op), {}, {
type: 'insert_node'
});
}
case 'remove_text':
{
return _objectSpread$e(_objectSpread$e({}, op), {}, {
type: 'insert_text'
});
}
case 'set_node':
{
var properties = op.properties,
newProperties = op.newProperties;
return _objectSpread$e(_objectSpread$e({}, op), {}, {
properties: newProperties,
newProperties: properties
});
}
case 'set_selection':
{
var _properties = op.properties,
_newProperties = op.newProperties;
if (_properties == null) {
return _objectSpread$e(_objectSpread$e({}, op), {}, {
properties: _newProperties,
newProperties: null
});
} else if (_newProperties == null) {
return _objectSpread$e(_objectSpread$e({}, op), {}, {
properties: null,
newProperties: _properties
});
} else {
return _objectSpread$e(_objectSpread$e({}, op), {}, {
properties: _newProperties,
newProperties: _properties
});
}
}
case 'split_node':
{
return _objectSpread$e(_objectSpread$e({}, op), {}, {
type: 'merge_node',
path: Path.next(op.path)
});
}
}
}
};
var isObject = function isObject(value) {
return _typeof(value) === 'object' && value !== null;
};
/**
* Custom deep equal comparison for Slate nodes. Since Slate only supports plain
* values, arrays and nested objects, we don't need a general purpose deep
* equality check.
*
* Slate objects are designed to be serialised, so missing keys are deliberately
* normalised to undefined.
*/
var isDeepEqual = function isDeepEqual(a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false;
for (var i = 0; i < a.length; i++) {
if (!isDeepEqual(a[i], b[i])) return false;
}
return true;
}
if (isObject(a) && isObject(b)) {
for (var key in a) {
var valueA = Object.hasOwn(a, key) ? a[key] : undefined;
var valueB = Object.hasOwn(b, key) ? b[key] : undefined;
if (!isDeepEqual(valueA, valueB)) return false;
}
// Deep object equality is only necessary in one direction; in the reverse
// direction we are only looking for keys that are missing. As above,
// undefined keys treated the same as missing keys.
for (var _key in b) {
if (a[_key] === undefined && b[_key] !== undefined) return false;
}
return true;
}
return a === b;
};
/**
* Get the default location to insert content into the editor.
* By default, use the selection as the target location. But if there is
* no selection, insert at the end of the document since that is such a
* common use case when inserting from a non-selected state.
*/
var getDefaultInsertLocation = function getDefaultInsertLocation(editor) {
if (editor.selection) {
return editor.selection;
} else if (editor.children.length > 0) {
return Editor.end(editor, []);
} else {
return [0];
}
};
var matchPath = function matchPath(editor, path) {
var _Editor$node = Editor.node(editor, path),
_Editor$node2 = _slicedToArray(_Editor$node, 1),
node = _Editor$node2[0];
return function (n) {
return n === node;
};
};
function _createForOfIteratorHelper$k(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$k(o)) || allowArrayLike) { if (it) o = it; va