UNPKG

slate-hyperscript

Version:

A hyperscript helper for creating Slate documents.

9 lines (8 loc) 12.4 kB
!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),u=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(u);var f=n((function(t){var e=o.default;t.exports=function(t){var r=u(t,"string");return"symbol"===e(r)?r:String(r)},t.exports.__esModule=!0,t.exports.default=t.exports}));r(f);var a=r(n((function(t){t.exports=function(t,e,r){return(e=f(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}))); /*! * is-plain-object <https://github.com/jonschlinkert/is-plain-object> * * Copyright (c) 2014-2017, Jon Schlinkert. * Released under the MIT License. */ function s(t){return"[object Object]"===Object.prototype.toString.call(t)}function c(t){var e,r;return!1!==s(t)&&(void 0===(e=t.constructor)||!1!==s(r=e.prototype)&&!1!==r.hasOwnProperty("isPrototypeOf"))}var l=n((function(t){t.exports=function(t){if(Array.isArray(t))return t},t.exports.__esModule=!0,t.exports.default=t.exports}));r(l);var p=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,u,f=[],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)&&(f.push(n.value),f.length!==e);a=!0);}catch(t){s=!0,o=t}finally{try{if(!a&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(s)throw o}}return f}},t.exports.__esModule=!0,t.exports.default=t.exports}));r(p);var d=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(d);var h=n((function(t){t.exports=function(t,e){if(t){if("string"==typeof t)return d(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)?d(t,e):void 0}},t.exports.__esModule=!0,t.exports.default=t.exports}));r(h);var y=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(y);var x=r(n((function(t){t.exports=function(t,e){return l(t)||p(t,e)||h(t,e)||y()},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})),v=r(b),g=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(g);var w=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&&g(t,e)},t.exports.__esModule=!0,t.exports.default=t.exports}))),O=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})),m=r(O),j=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})),_=r(j),P=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,f(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}))),S=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 M(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=_(t);if(e){var o=_(this).constructor;r=Reflect.construct(n,arguments,o)}else r=n.apply(this,arguments);return m(this,r)}}var E=new WeakMap,T=new WeakMap,A=P((function t(){S(this,t)})),R=function(t){w(r,t);var e=M(r);function r(){var t,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};S(this,r),t=e.call(this),a(v(t),"offset",void 0),a(v(t),"path",void 0);var o=n.offset,i=n.path;return t.offset=o,t.path=i,t}return P(r)}(A),D=function(t){w(r,t);var e=M(r);function r(){var t,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};S(this,r),t=e.call(this),a(v(t),"offset",void 0),a(v(t),"path",void 0);var o=n.offset,i=n.path;return t.offset=o,t.path=i,t}return P(r)}(A),k=function(t){return T.get(t)};function I(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?I(Object(r),!0).forEach((function(e){a(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):I(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&&t&&"number"==typeof t.length){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,u=!0,f=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return u=t.done,t},e:function(t){f=!0,i=t},f:function(){try{u||null==r.return||r.return()}finally{if(f)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,W=function(t){var r,n=[],o=function t(r){if(null!=r){var o=n[n.length-1];if("string"==typeof r){var i={text:r};U.add(i),r=i}if(e.Text.isText(r)){var u=r;e.Text.isText(o)&&U.has(o)&&U.has(u)&&e.Text.equals(o,u,{loose:!0})?o.text+=u.text:n.push(u)}else if(e.Element.isElement(r))n.push(r);else{if(!(r instanceof A))throw new Error("Unexpected hyperscript child object: ".concat(r));var f=n[n.length-1];e.Text.isText(f)||(t(""),f=n[n.length-1]),r instanceof R?function(t,e){var r=t.text.length;E.set(t,[r,e])}(f,r):r instanceof D&&function(t,e){var r=t.text.length;T.set(t,[r,e])}(f,r)}}},i=C(t.flat(1/0));try{for(i.s();!(r=i.n()).done;){o(r.value)}}catch(t){i.e(t)}finally{i.f()}return n};function q(t,e,r){return B(B({},e),{},{children:W(r)})}function F(t,r,n){var o=W(n);if(o.length>1)throw new Error("The <text> hyperscript tag must only contain a single node's worth of children.");var i=x(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 H=function(t){return function(r,n,o){var i,u,f=[],a=C(o);try{for(a.s();!(u=a.n()).done;){var s=u.value;e.Range.isRange(s)?i=s:f.push(s)}}catch(t){a.e(t)}finally{a.f()}var c=W(f),l={},p=t();Object.assign(p,n),p.children=c;var d,h,y=C(e.Node.texts(p));try{for(y.s();!(d=y.n()).done;){var b=x(d.value,2),v=b[0],g=b[1],w=(h=v,E.get(h)),O=k(v);if(null!=w){var m=x(w,1)[0];l.anchor={path:g,offset:m}}if(null!=O){var j=x(O,1)[0];l.focus={path:g,offset:j}}}}catch(t){y.e(t)}finally{y.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 $(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 z(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}var G={anchor:function(t,e,r){return new R(e)},cursor:function(t,e,r){return[new R(e),new D(e)]},editor:H(e.createEditor),element:q,focus:function(t,e,r){return new D(e)},fragment:function(t,e,r){return W(r)},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:F},J=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.elements,r=void 0===e?{}:e,n=L(r),o=z(z(z({},G),n),t.creators),i=K(o);return i},K=function(t){return function(e,r){for(var n=arguments.length,o=new Array(n>2?n-2:0),i=2;i<n;i++)o[i-2]=arguments[i];var u=t[e];if(!u)throw new Error("No hyperscript creator found for tag: <".concat(e,">"));null==r&&(r={}),c(r)||(o=[r].concat(o),r={});var f=u(e,r,o=o.filter((function(t){return Boolean(t)})).flat());return f}},L=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 q(0,z(z({},r),e),n)}};for(var n in t)r();return e},Q=J();t.createEditor=H,t.createHyperscript=J,t.createText=F,t.jsx=Q}));