slate-hyperscript
Version:
A hyperscript helper for creating Slate documents.
2 lines (1 loc) • 14.5 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("slate")):"function"==typeof define&&define.amd?define(["exports","slate"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).SlateHyperscript={},t.Slate)}(this,(function(t,e){"use strict";function r(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function n(t,e){return t(e={exports:{}},e.exports),e.exports}var o=n((function(t){function e(r){return t.exports=e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t.exports.__esModule=!0,t.exports.default=t.exports,e(r)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports})),i=r(o),f=n((function(t){var e=o.default;t.exports=function(t,r){if("object"!==e(t)||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var o=n.call(t,r||"default");if("object"!==e(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)},t.exports.__esModule=!0,t.exports.default=t.exports}));r(f);var u=n((function(t){var e=o.default;t.exports=function(t){var r=f(t,"string");return"symbol"===e(r)?r:String(r)},t.exports.__esModule=!0,t.exports.default=t.exports}));r(u);var a=r(n((function(t){t.exports=function(t,e,r){return(e=u(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t},t.exports.__esModule=!0,t.exports.default=t.exports}))),s=n((function(t){t.exports=function(t){if(Array.isArray(t))return t},t.exports.__esModule=!0,t.exports.default=t.exports}));r(s);var c=n((function(t){t.exports=function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,f,u=[],a=!0,s=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;a=!1}else for(;!(a=(n=i.call(r)).done)&&(u.push(n.value),u.length!==e);a=!0);}catch(t){s=!0,o=t}finally{try{if(!a&&null!=r.return&&(f=r.return(),Object(f)!==f))return}finally{if(s)throw o}}return u}},t.exports.__esModule=!0,t.exports.default=t.exports}));r(c);var l=n((function(t){t.exports=function(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n},t.exports.__esModule=!0,t.exports.default=t.exports}));r(l);var p=n((function(t){t.exports=function(t,e){if(t){if("string"==typeof t)return l(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?l(t,e):void 0}},t.exports.__esModule=!0,t.exports.default=t.exports}));r(p);var d=n((function(t){t.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")},t.exports.__esModule=!0,t.exports.default=t.exports}));r(d);var h=r(n((function(t){t.exports=function(t,e){return s(t)||c(t,e)||p(t,e)||d()},t.exports.__esModule=!0,t.exports.default=t.exports}))),y=n((function(t){function e(r,n){return t.exports=e=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},t.exports.__esModule=!0,t.exports.default=t.exports,e(r,n)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports}));r(y);var v=r(n((function(t){t.exports=function(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:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&y(t,e)},t.exports.__esModule=!0,t.exports.default=t.exports}))),b=n((function(t){t.exports=function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t},t.exports.__esModule=!0,t.exports.default=t.exports}));r(b);var x=n((function(t){var e=o.default;t.exports=function(t,r){if(r&&("object"===e(r)||"function"==typeof r))return r;if(void 0!==r)throw new TypeError("Derived constructors may only return object or undefined");return b(t)},t.exports.__esModule=!0,t.exports.default=t.exports})),w=r(x),g=n((function(t){function e(r){return t.exports=e=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},t.exports.__esModule=!0,t.exports.default=t.exports,e(r)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports})),O=r(g),m=r(n((function(t){function e(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,u(n.key),n)}}t.exports=function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t},t.exports.__esModule=!0,t.exports.default=t.exports}))),j=r(n((function(t){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},t.exports.__esModule=!0,t.exports.default=t.exports})));function _(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function P(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?_(Object(r),!0).forEach((function(e){a(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):_(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function S(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var r,n=O(t);if(e){var o=O(this).constructor;r=Reflect.construct(n,arguments,o)}else r=n.apply(this,arguments);return w(this,r)}}var E=new WeakMap,M=new WeakMap,T=new WeakMap,A=m((function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};j(this,t),a(this,"offset",void 0),a(this,"path",void 0),a(this,"ref",void 0);var r=e.offset,n=e.path,o=e.ref;if(this.offset=r,this.path=n,o){if(null!=n)throw new Error("The ref prop of a token cannot be used with the path prop.");this.ref=o}})),R=function(t){v(r,t);var e=S(r);function r(){return j(this,r),e.apply(this,arguments)}return m(r)}(A),D=function(t){v(r,t);var e=S(r);function r(){return j(this,r),e.apply(this,arguments)}return m(r)}(A),k=function(t){v(r,t);var e=S(r);function r(){return j(this,r),e.apply(this,arguments)}return m(r)}(A);function H(t){var e=M.get(t);return e?Array.from(e.values(),(function(t){var e=h(t,2),r=e[0],n=e[1];return new D(P(P({},n),{},{offset:r}))})):[]}function I(t){var e=T.get(t);return e?Array.from(e.values(),(function(t){var e=h(t,2),r=e[0],n=e[1];return new k(P(P({},n),{},{offset:r}))})):[]}function W(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function B(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?W(Object(r),!0).forEach((function(e){a(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):W(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function C(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=function(t,e){if(!t)return;if("string"==typeof t)return N(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);"Object"===r&&t.constructor&&(r=t.constructor.name);if("Map"===r||"Set"===r)return Array.from(t);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return N(t,e)}(t))||e){r&&(t=r);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}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 i,f=!0,u=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return f=t.done,t},e:function(t){u=!0,i=t},f:function(){try{f||null==r.return||r.return()}finally{if(u)throw i}}}}function N(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}var U=new WeakSet;function q(t){var r=[];function n(t){if(null!=t){var o=r[r.length-1];if("string"==typeof t){var i={text:t};U.add(i),t=i}if(e.Text.isText(t)){var f=t;e.Text.isText(o)&&U.has(o)&&U.has(f)&&e.Text.equals(o,f,{loose:!0})?o.text+=f.text:r.push(f)}else if(e.Element.isElement(t))r.push(t);else{if(!(t instanceof A))throw new Error("Unexpected hyperscript child object: ".concat(t));var u=r[r.length-1];e.Text.isText(u)||(n(""),u=r[r.length-1]),t instanceof R?function(t,e){var r=t.text.length,n=E.get(t);n||(n=new Set,E.set(t,n)),n.add([r,e])}(u,t):t instanceof D?function(t,e){var r=t.text.length,n=M.get(t);n||(n=new Set,M.set(t,n)),n.add([r,e])}(u,t):t instanceof k&&function(t,e){var r=t.text.length,n=T.get(t);n||(n=new Set,T.set(t,n)),n.add([r,e])}(u,t)}}}var o,i=C(t.flat(1/0));try{for(i.s();!(o=i.n()).done;){n(o.value)}}catch(t){i.e(t)}finally{i.f()}return r}function F(t,e,r){return B(B({},e),{},{children:q(r)})}function $(t,r,n){var o=q(n);if(o.length>1)throw new Error("The <text> hyperscript tag must only contain a single node's worth of children.");var i=h(o,1)[0];if(null==i&&(i={text:""}),!e.Text.isText(i))throw new Error("\n The <text> hyperscript tag can only contain text content as children.");return U.delete(i),Object.assign(i,r),i}var z=function(t){return function(r,n,o){var i,f,u=[],a=C(o);try{for(a.s();!(f=a.n()).done;){var s=f.value;e.Range.isRange(s)?i=s:u.push(s)}}catch(t){a.e(t)}finally{a.f()}var c=q(u),l={},p=t();Object.assign(p,n),p.children=c;var d,y,v,b=C(e.Node.texts(p));try{for(b.s();!(d=b.n()).done;){var x,w=h(d.value,2),g=w[0],O=w[1],m=C((y=g,v=void 0,(v=E.get(y))?Array.from(v.values(),(function(t){var e=h(t,2),r=e[0],n=e[1];return new R(P(P({},n),{},{offset:r}))})):[]));try{for(m.s();!(x=m.n()).done;){var j=x.value,_=j.ref,S=void 0===_?l:_,M=j.offset;null!=M&&(S.anchor={path:O,offset:M})}}catch(t){m.e(t)}finally{m.f()}var T,A=C(H(g));try{for(A.s();!(T=A.n()).done;){var D=T.value,k=D.ref,W=void 0===k?l:k,B=D.offset;null!=B&&(W.focus={path:O,offset:B})}}catch(t){A.e(t)}finally{A.f()}var N,U=C(I(g));try{for(U.s();!(N=U.n()).done;){var F=N.value,$=F.ref,z=F.offset;$&&($.offset=z,$.path=O)}}catch(t){U.e(t)}finally{U.f()}}}catch(t){b.e(t)}finally{b.f()}if(l.anchor&&!l.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(!l.anchor&&l.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.");return null!=i?p.selection=i:e.Range.isRange(l)&&(p.selection=l),p}};function G(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function J(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?G(Object(r),!0).forEach((function(e){a(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):G(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var K={anchor:function(t,e,r){return new R(e)},cursor:function(t,e,r){return[new R(e),new D(e)]},editor:z(e.createEditor),element:F,focus:function(t,e,r){return new D(e)},fragment:function(t,e,r){return q(r)},point:function(t,e,r){return new k(e)},selection:function(t,e,r){var n=r.find((function(t){return t instanceof R})),o=r.find((function(t){return t instanceof D}));if(!n||null==n.offset||null==n.path)throw new Error("The <selection> hyperscript tag must have an <anchor> tag as a child with `path` and `offset` attributes defined.");if(!o||null==o.offset||null==o.path)throw new Error("The <selection> hyperscript tag must have a <focus> tag as a child with `path` and `offset` attributes defined.");return B({anchor:{offset:n.offset,path:n.path},focus:{offset:o.offset,path:o.path}},e)},text:$},L=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.elements,r=void 0===e?{}:e,n=V(r),o=J(J(J({},K),n),t.creators),i=Q(o);return i},Q=function(t){return function(r,n){for(var o=arguments.length,i=new Array(o>2?o-2:0),f=2;f<o;f++)i[f-2]=arguments[f];var u=t[r];if(!u)throw new Error("No hyperscript creator found for tag: <".concat(r,">"));null==n&&(n={}),e.isObject(n)||(i=[n].concat(i),n={});var a=u(r,n,i=i.filter((function(t){return Boolean(t)})).flat());return a}},V=function(t){var e={},r=function(){var r=t[n];if("object"!==i(r))throw new Error("Properties specified for a hyperscript shorthand should be an object, but for the custom element <".concat(n,"> tag you passed: ").concat(r));e[n]=function(t,e,n){return F(0,J(J({},r),e),n)}};for(var n in t)r();return e},X=function(){function t(){j(this,t),a(this,"path",void 0),a(this,"offset",void 0)}return m(t,[{key:"point",value:function(){var t=this.path,e=this.offset;if(null==t||null==e)throw new Error("A HyperscriptPointRef must be passed as the ref prop of a <point /> tag that is used inside an <editor>.");return{path:t,offset:e}}}]),t}(),Y=function(){function t(){j(this,t),a(this,"anchor",void 0),a(this,"focus",void 0)}return m(t,[{key:"range",value:function(){var t=this.anchor,e=this.focus;if(null==t)throw new Error("A HyperscriptRangeRef must be passed as the ref prop of an <anchor /> tag that is used inside an <editor>.");if(null==e)throw new Error("A HyperscriptRangeRef must be passed as the ref prop of a <focus /> tag that is used inside an <editor>.");return{anchor:t,focus:e}}}]),t}(),Z=L();t.HyperscriptPointRef=X,t.HyperscriptRangeRef=Y,t.createEditor=z,t.createHyperscript=L,t.createText=$,t.jsx=Z}));