slate-hyperscript
Version:
A hyperscript helper for creating Slate documents.
852 lines (801 loc) • 30.8 kB
JavaScript
;
var slate = require('slate');
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 _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 objectWithoutPropertiesLoose = createCommonjsModule(function (module) {
function _objectWithoutPropertiesLoose(r, e) {
if (null == r) return {};
var t = {};
for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
if (-1 !== e.indexOf(n)) continue;
t[n] = r[n];
}
return t;
}
module.exports = _objectWithoutPropertiesLoose, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(objectWithoutPropertiesLoose);
var objectWithoutProperties = createCommonjsModule(function (module) {
function _objectWithoutProperties(e, t) {
if (null == e) return {};
var o,
r,
i = objectWithoutPropertiesLoose(e, t);
if (Object.getOwnPropertySymbols) {
var n = Object.getOwnPropertySymbols(e);
for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
}
return i;
}
module.exports = _objectWithoutProperties, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _objectWithoutProperties = unwrapExports(objectWithoutProperties);
var toPrimitive_1 = createCommonjsModule(function (module) {
var _typeof = _typeof_1["default"];
function toPrimitive(t, r) {
if ("object" != _typeof(t) || !t) return t;
var e = t[Symbol.toPrimitive];
if (void 0 !== e) {
var i = e.call(t, r || "default");
if ("object" != _typeof(i)) return i;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return ("string" === r ? String : Number)(t);
}
module.exports = toPrimitive, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(toPrimitive_1);
var toPropertyKey_1 = createCommonjsModule(function (module) {
var _typeof = _typeof_1["default"];
function toPropertyKey(t) {
var i = toPrimitive_1(t, "string");
return "symbol" == _typeof(i) ? i : i + "";
}
module.exports = toPropertyKey, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(toPropertyKey_1);
var defineProperty = createCommonjsModule(function (module) {
function _defineProperty(e, r, t) {
return (r = toPropertyKey_1(r)) in e ? Object.defineProperty(e, r, {
value: t,
enumerable: true,
configurable: true,
writable: true
}) : e[r] = t, e;
}
module.exports = _defineProperty, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _defineProperty = unwrapExports(defineProperty);
var arrayWithHoles = createCommonjsModule(function (module) {
function _arrayWithHoles(r) {
if (Array.isArray(r)) return r;
}
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 arrayLikeToArray = createCommonjsModule(function (module) {
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
module.exports = _arrayLikeToArray, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(arrayLikeToArray);
var unsupportedIterableToArray = createCommonjsModule(function (module) {
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) return arrayLikeToArray(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? arrayLikeToArray(r, a) : void 0;
}
}
module.exports = _unsupportedIterableToArray, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(unsupportedIterableToArray);
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(r, e) {
return arrayWithHoles(r) || iterableToArrayLimit(r, e) || unsupportedIterableToArray(r, e) || nonIterableRest();
}
module.exports = _slicedToArray, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _slicedToArray = unwrapExports(slicedToArray);
var assertThisInitialized = createCommonjsModule(function (module) {
function _assertThisInitialized(e) {
if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
return e;
}
module.exports = _assertThisInitialized, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(assertThisInitialized);
var possibleConstructorReturn = createCommonjsModule(function (module) {
var _typeof = _typeof_1["default"];
function _possibleConstructorReturn(t, e) {
if (e && ("object" == _typeof(e) || "function" == typeof e)) return e;
if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
return assertThisInitialized(t);
}
module.exports = _possibleConstructorReturn, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _possibleConstructorReturn = unwrapExports(possibleConstructorReturn);
var getPrototypeOf = createCommonjsModule(function (module) {
function _getPrototypeOf(t) {
return module.exports = _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
return t.__proto__ || Object.getPrototypeOf(t);
}, module.exports.__esModule = true, module.exports["default"] = module.exports, _getPrototypeOf(t);
}
module.exports = _getPrototypeOf, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _getPrototypeOf = unwrapExports(getPrototypeOf);
var setPrototypeOf = createCommonjsModule(function (module) {
function _setPrototypeOf(t, e) {
return module.exports = _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
return t.__proto__ = e, t;
}, module.exports.__esModule = true, module.exports["default"] = module.exports, _setPrototypeOf(t, e);
}
module.exports = _setPrototypeOf, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
unwrapExports(setPrototypeOf);
var inherits = createCommonjsModule(function (module) {
function _inherits(t, e) {
if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
t.prototype = Object.create(e && e.prototype, {
constructor: {
value: t,
writable: true,
configurable: true
}
}), Object.defineProperty(t, "prototype", {
writable: false
}), e && setPrototypeOf(t, e);
}
module.exports = _inherits, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _inherits = unwrapExports(inherits);
var createClass = createCommonjsModule(function (module) {
function _defineProperties(e, r) {
for (var t = 0; t < r.length; t++) {
var o = r[t];
o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, toPropertyKey_1(o.key), o);
}
}
function _createClass(e, r, t) {
return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
writable: false
}), e;
}
module.exports = _createClass, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _createClass = unwrapExports(createClass);
var classCallCheck = createCommonjsModule(function (module) {
function _classCallCheck(a, n) {
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
}
module.exports = _classCallCheck, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _classCallCheck = unwrapExports(classCallCheck);
function ownKeys$2(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$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
/**
* A weak map to hold anchor tokens.
*/
var ANCHORS = new WeakMap();
/**
* A weak map to hold focus tokens.
*/
var FOCI = new WeakMap();
/**
* A weak map to hold point tokens.
*/
var POINTS = new WeakMap();
/**
* All tokens inherit from a single constructor for `instanceof` checking.
*/
var Token = /*#__PURE__*/_createClass(function Token() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Token);
_defineProperty(this, "offset", void 0);
_defineProperty(this, "path", void 0);
_defineProperty(this, "ref", void 0);
var offset = props.offset,
path = props.path,
ref = props.ref;
this.offset = offset;
this.path = path;
if (ref) {
if (path != null) {
throw new Error('The ref prop of a token cannot be used with the path prop.');
}
this.ref = ref;
}
});
/**
* Anchor tokens represent the selection's anchor point.
*/
var AnchorToken = /*#__PURE__*/function (_Token2) {
function AnchorToken() {
_classCallCheck(this, AnchorToken);
return _callSuper(this, AnchorToken, arguments);
}
_inherits(AnchorToken, _Token2);
return _createClass(AnchorToken);
}(Token);
/**
* Focus tokens represent the selection's focus point.
*/
var FocusToken = /*#__PURE__*/function (_Token3) {
function FocusToken() {
_classCallCheck(this, FocusToken);
return _callSuper(this, FocusToken, arguments);
}
_inherits(FocusToken, _Token3);
return _createClass(FocusToken);
}(Token);
/**
* Point tokens represent arbitrary points.
*/
var PointToken = /*#__PURE__*/function (_Token4) {
function PointToken() {
_classCallCheck(this, PointToken);
return _callSuper(this, PointToken, arguments);
}
_inherits(PointToken, _Token4);
return _createClass(PointToken);
}(Token);
/**
* Add an anchor token to the end of a text node.
*/
function addAnchorToken(text, token) {
var offset = text.text.length;
var anchors = ANCHORS.get(text);
if (!anchors) {
anchors = new Set();
ANCHORS.set(text, anchors);
}
anchors.add([offset, token]);
}
/**
* Get the associated anchor tokens for a text node.
*/
function getAnchors(text) {
var anchors = ANCHORS.get(text);
if (!anchors) return [];
return Array.from(anchors.values(), function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
offset = _ref2[0],
token = _ref2[1];
return new AnchorToken(_objectSpread$2(_objectSpread$2({}, token), {}, {
offset: offset
}));
});
}
/**
* Add a focus token to the end of a text node.
*/
function addFocusToken(text, token) {
var offset = text.text.length;
var foci = FOCI.get(text);
if (!foci) {
foci = new Set();
FOCI.set(text, foci);
}
foci.add([offset, token]);
}
/**
* Get the associated focus tokens for a text node.
*/
function getFoci(text) {
var foci = FOCI.get(text);
if (!foci) return [];
return Array.from(foci.values(), function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
offset = _ref4[0],
token = _ref4[1];
return new FocusToken(_objectSpread$2(_objectSpread$2({}, token), {}, {
offset: offset
}));
});
}
/**
* Add a point token to the end of a text node.
*/
function addPointToken(text, token) {
var offset = text.text.length;
var points = POINTS.get(text);
if (!points) {
points = new Set();
POINTS.set(text, points);
}
points.add([offset, token]);
}
/**
* Get the associated point tokens for a text node.
*/
function getPoints(text) {
var points = POINTS.get(text);
if (!points) return [];
return Array.from(points.values(), function (_ref5) {
var _ref6 = _slicedToArray(_ref5, 2),
offset = _ref6[0],
token = _ref6[1];
return new PointToken(_objectSpread$2(_objectSpread$2({}, token), {}, {
offset: offset
}));
});
}
function ownKeys$1(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$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: true } : { done: false, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = true, u = false; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = true, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
* Resolve the descedants of a node by normalizing the children that can be
* passed into a hyperscript creator function.
*/
var STRINGS = new WeakSet();
function resolveDescendants(children) {
var nodes = [];
function addChild(child) {
if (child == null || typeof child === 'boolean') {
return;
}
var prev = nodes[nodes.length - 1];
if (typeof child === 'string' || typeof child === 'number') {
var text = {
text: child.toString()
};
STRINGS.add(text);
child = text;
}
if (slate.Text.isText(child)) {
var c = child; // HACK: fix typescript complaining
if (slate.Text.isText(prev) && STRINGS.has(prev) && STRINGS.has(c) && slate.Text.equals(prev, c, {
loose: true
})) {
prev.text += c.text;
} else {
nodes.push(c);
}
} else if (slate.Element.isElement(child)) {
nodes.push(child);
} else if (child instanceof Token) {
var n = nodes[nodes.length - 1];
if (!slate.Text.isText(n)) {
addChild('');
n = nodes[nodes.length - 1];
}
if (child instanceof AnchorToken) {
addAnchorToken(n, child);
} else if (child instanceof FocusToken) {
addFocusToken(n, child);
} else if (child instanceof PointToken) {
addPointToken(n, child);
}
} else {
throw new Error("Unexpected hyperscript child object: ".concat(child));
}
}
var _iterator = _createForOfIteratorHelper(children.flat(Infinity)),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var child = _step.value;
addChild(child);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return nodes;
}
/**
* Create an anchor token.
*/
function createAnchor(tagName, attributes, children) {
return new AnchorToken(attributes);
}
/**
* Create an anchor and a focus token.
*/
function createCursor(tagName, attributes, children) {
return [new AnchorToken(attributes), new FocusToken(attributes)];
}
/**
* Create an `Element` object.
*/
function createElement(tagName, attributes, children) {
return _objectSpread$1(_objectSpread$1({}, attributes), {}, {
children: resolveDescendants(children)
});
}
/**
* Create a focus token.
*/
function createFocus(tagName, attributes, children) {
return new FocusToken(attributes);
}
/**
* Create a fragment.
*/
function createFragment(tagName, attributes, children) {
return resolveDescendants(children);
}
/**
* Create a point token.
*/
function createPoint(tagName, attributes, children) {
return new PointToken(attributes);
}
/**
* Create a `Selection` object.
*/
function createSelection(tagName, attributes, children) {
var anchor = children.find(function (c) {
return c instanceof AnchorToken;
});
var focus = children.find(function (c) {
return c instanceof FocusToken;
});
if (!anchor || anchor.offset == null || anchor.path == null) {
throw new Error("The <selection> hyperscript tag must have an <anchor> tag as a child with `path` and `offset` attributes defined.");
}
if (!focus || focus.offset == null || focus.path == null) {
throw new Error("The <selection> hyperscript tag must have a <focus> tag as a child with `path` and `offset` attributes defined.");
}
return _objectSpread$1({
anchor: {
offset: anchor.offset,
path: anchor.path
},
focus: {
offset: focus.offset,
path: focus.path
}
}, attributes);
}
/**
* Create a `Text` object.
*/
function createText(tagName, attributes, children) {
var nodes = resolveDescendants(children);
if (nodes.length > 1) {
throw new Error("The <text> hyperscript tag must only contain a single node's worth of children.");
}
var _nodes = _slicedToArray(nodes, 1),
node = _nodes[0];
if (node == null) {
node = {
text: ''
};
}
if (!slate.Text.isText(node)) {
throw new Error("\n The <text> hyperscript tag can only contain text content as children.");
}
// COMPAT: If they used the <text> tag we want to guarantee that it won't be
// merge with other string children.
STRINGS["delete"](node);
Object.assign(node, attributes);
return node;
}
/**
* Create a top-level `Editor` object.
*/
var createEditor = function createEditor(makeEditor) {
return function (tagName, attributes, children) {
var otherChildren = [];
var selectionChild;
var _iterator2 = _createForOfIteratorHelper(children),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var child = _step2.value;
if (slate.Range.isRange(child)) {
selectionChild = child;
} else {
otherChildren.push(child);
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
var descendants = resolveDescendants(otherChildren);
var selection = {};
var editor = makeEditor();
Object.assign(editor, attributes);
editor.children = descendants;
// Search the document's texts to see if any of them have tokens associated
// that need incorporated into the selection.
var _iterator3 = _createForOfIteratorHelper(slate.Node.texts(editor)),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var _step3$value = _slicedToArray(_step3.value, 2),
node = _step3$value[0],
path = _step3$value[1];
var _iterator4 = _createForOfIteratorHelper(getAnchors(node)),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var _step4$value = _step4.value,
_step4$value$ref = _step4$value.ref,
ref = _step4$value$ref === void 0 ? selection : _step4$value$ref,
offset = _step4$value.offset;
if (offset != null) {
ref.anchor = {
path: path,
offset: offset
};
}
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
var _iterator5 = _createForOfIteratorHelper(getFoci(node)),
_step5;
try {
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
var _step5$value = _step5.value,
_step5$value$ref = _step5$value.ref,
_ref = _step5$value$ref === void 0 ? selection : _step5$value$ref,
_offset = _step5$value.offset;
if (_offset != null) {
_ref.focus = {
path: path,
offset: _offset
};
}
}
} catch (err) {
_iterator5.e(err);
} finally {
_iterator5.f();
}
var _iterator6 = _createForOfIteratorHelper(getPoints(node)),
_step6;
try {
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
var _step6$value = _step6.value,
_ref2 = _step6$value.ref,
_offset2 = _step6$value.offset;
if (_ref2) {
_ref2.offset = _offset2;
_ref2.path = path;
}
}
} catch (err) {
_iterator6.e(err);
} finally {
_iterator6.f();
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
if (selection.anchor && !selection.focus) {
throw new Error("Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<anchor />`. For collapsed selections, use `<cursor />` instead.");
}
if (!selection.anchor && selection.focus) {
throw new Error("Slate hyperscript ranges must have both `<anchor />` and `<focus />` defined if one is defined, but you only defined `<focus />`. For collapsed selections, use `<cursor />` instead.");
}
if (selectionChild != null) {
editor.selection = selectionChild;
} else if (slate.Range.isRange(selection)) {
editor.selection = selection;
}
return editor;
};
};
var _excluded = ["__self", "__source"];
function ownKeys(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) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/**
* The default creators for Slate objects.
*/
var DEFAULT_CREATORS = {
anchor: createAnchor,
cursor: createCursor,
editor: createEditor(slate.createEditor),
element: createElement,
focus: createFocus,
fragment: createFragment,
point: createPoint,
selection: createSelection,
text: createText
};
/**
* Create a Slate hyperscript function with `options`.
*/
var createHyperscript = function createHyperscript() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _options$elements = options.elements,
elements = _options$elements === void 0 ? {} : _options$elements;
var elementCreators = normalizeElements(elements);
var creators = _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_CREATORS), elementCreators), options.creators);
var jsx = createFactory(creators);
return jsx;
};
/**
* Create a Slate hyperscript function with `options`.
*/
var createFactory = function createFactory(creators) {
var jsx = function jsx(tagName, attributes) {
for (var _len = arguments.length, children = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
children[_key - 2] = arguments[_key];
}
if (slate.isObject(attributes)) {
var _attributes = attributes;
_attributes.__self;
_attributes.__source;
var rest = _objectWithoutProperties(_attributes, _excluded);
attributes = rest;
}
if (typeof tagName === 'function') {
return tagName(_objectSpread({
children: children
}, attributes));
}
var creator = creators[tagName];
if (!creator) {
throw new Error("No hyperscript creator found for tag: <".concat(tagName, ">"));
}
if (attributes == null) {
attributes = {};
}
if (!slate.isObject(attributes)) {
children = [attributes].concat(children);
attributes = {};
}
children = children.filter(function (child) {
return Boolean(child);
}).flat();
var ret = creator(tagName, attributes, children);
return ret;
};
return jsx;
};
/**
* Normalize a dictionary of element shorthands into creator functions.
*/
var normalizeElements = function normalizeElements(elements) {
var creators = {};
var _loop = function _loop() {
var props = elements[tagName];
if (_typeof(props) !== 'object') {
throw new Error("Properties specified for a hyperscript shorthand should be an object, but for the custom element <".concat(tagName, "> tag you passed: ").concat(props));
}
creators[tagName] = function (tagName, attributes, children) {
return createElement('element', _objectSpread(_objectSpread({}, props), attributes), children);
};
};
for (var tagName in elements) {
_loop();
}
return creators;
};
/**
* Hyperscript point refs can be used to construct arbitrary points using the
* ref prop of a <point /> tag.
*/
var HyperscriptPointRef = /*#__PURE__*/function () {
function HyperscriptPointRef() {
_classCallCheck(this, HyperscriptPointRef);
_defineProperty(this, "path", void 0);
_defineProperty(this, "offset", void 0);
}
return _createClass(HyperscriptPointRef, [{
key: "point",
value: function point() {
var path = this.path,
offset = this.offset;
if (path == null || offset == null) {
throw new Error('A HyperscriptPointRef must be passed as the ref prop of a <point /> tag that is used inside an <editor>.');
}
return {
path: path,
offset: offset
};
}
}]);
}();
/**
* Hyperscript range refs can be used to construct arbitrary range using the ref
* props of <anchor /> and <focus /> tags.
*/
var HyperscriptRangeRef = /*#__PURE__*/function () {
function HyperscriptRangeRef() {
_classCallCheck(this, HyperscriptRangeRef);
_defineProperty(this, "anchor", void 0);
_defineProperty(this, "focus", void 0);
}
return _createClass(HyperscriptRangeRef, [{
key: "range",
value: function range() {
var anchor = this.anchor,
focus = this.focus;
if (anchor == null) {
throw new Error('A HyperscriptRangeRef must be passed as the ref prop of an <anchor /> tag that is used inside an <editor>.');
}
if (focus == null) {
throw new Error('A HyperscriptRangeRef must be passed as the ref prop of a <focus /> tag that is used inside an <editor>.');
}
return {
anchor: anchor,
focus: focus
};
}
}]);
}();
/**
* The default hyperscript factory that ships with Slate, without custom tags.
*/
var jsx = createHyperscript();
exports.HyperscriptPointRef = HyperscriptPointRef;
exports.HyperscriptRangeRef = HyperscriptRangeRef;
exports.createEditor = createEditor;
exports.createHyperscript = createHyperscript;
exports.createText = createText;
exports.jsx = jsx;
//# sourceMappingURL=index.js.map