UNPKG

slate-hyperscript

Version:

A hyperscript helper for creating Slate documents.

842 lines (795 loc) 31.7 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('slate')) : typeof define === 'function' && define.amd ? define(['exports', 'slate'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SlateHyperscript = {}, global.Slate)); })(this, (function (exports, slate) { 'use strict'; 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 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 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 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 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 setPrototypeOf = createCommonjsModule(function (module) { function _setPrototypeOf(o, p) { module.exports = _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }, module.exports.__esModule = true, module.exports["default"] = module.exports; return _setPrototypeOf(o, p); } module.exports = _setPrototypeOf, module.exports.__esModule = true, module.exports["default"] = module.exports; }); unwrapExports(setPrototypeOf); var inherits = createCommonjsModule(function (module) { 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); } module.exports = _inherits, module.exports.__esModule = true, module.exports["default"] = module.exports; }); var _inherits = unwrapExports(inherits); var assertThisInitialized = createCommonjsModule(function (module) { function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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(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); } module.exports = _possibleConstructorReturn, module.exports.__esModule = true, module.exports["default"] = module.exports; }); var _possibleConstructorReturn = unwrapExports(possibleConstructorReturn); var getPrototypeOf = createCommonjsModule(function (module) { function _getPrototypeOf(o) { module.exports = _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }, module.exports.__esModule = true, module.exports["default"] = module.exports; return _getPrototypeOf(o); } module.exports = _getPrototypeOf, module.exports.__esModule = true, module.exports["default"] = module.exports; }); var _getPrototypeOf = unwrapExports(getPrototypeOf); var createClass = createCommonjsModule(function (module) { 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; } module.exports = _createClass, module.exports.__esModule = true, module.exports["default"] = module.exports; }); var _createClass = unwrapExports(createClass); var classCallCheck = createCommonjsModule(function (module) { function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { 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 _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 _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; } } /** * 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 (_Token) { _inherits(AnchorToken, _Token); var _super = _createSuper(AnchorToken); function AnchorToken() { _classCallCheck(this, AnchorToken); return _super.apply(this, arguments); } return _createClass(AnchorToken); }(Token); /** * Focus tokens represent the selection's focus point. */ var FocusToken = /*#__PURE__*/function (_Token2) { _inherits(FocusToken, _Token2); var _super2 = _createSuper(FocusToken); function FocusToken() { _classCallCheck(this, FocusToken); return _super2.apply(this, arguments); } return _createClass(FocusToken); }(Token); /** * Point tokens represent arbitrary points. */ var PointToken = /*#__PURE__*/function (_Token3) { _inherits(PointToken, _Token3); var _super3 = _createSuper(PointToken); function PointToken() { _classCallCheck(this, PointToken); return _super3.apply(this, arguments); } 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(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(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(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; } /** * 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) { return; } var prev = nodes[nodes.length - 1]; if (typeof child === 'string') { var text = { text: child }; 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; }; }; 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]; } 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); } _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 }; } }]); return HyperscriptPointRef; }(); /** * 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); } _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 }; } }]); return HyperscriptRangeRef; }(); /** * 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; }));