@bloklabs/react 1.2.6 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +277 -220
- package/package.json +2 -2
- package/src/BlokEditor.tsx +1 -1
- package/src/createReactBlock.tsx +151 -8
- package/src/createReactInlineTool.tsx +12 -0
- package/src/index.ts +3 -0
- package/src/provide-blok.tsx +1 -1
- package/src/useBlok.ts +102 -3
- package/types/index.d.ts +40 -2
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`react`),t=require(`@bloklabs/core`),n=require(`@bloklabs/core/adapters`),r=require(`react/jsx-runtime`),i=require(`react-dom`);var a=new WeakMap;function o(e,t){a.set(e,t)}function s(e){return a.get(e)}function c(e){a.delete(e)}function l(e){"@babel/helpers - typeof";return l=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},l(e)}function u(e,t){if(l(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(l(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function d(e){var t=u(e,`string`);return l(t)==`symbol`?t:t+``}function f(e,t,n){return(t=d(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function p(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function m(e){for(var t=1;t<arguments.length;t++){var n=arguments[t]==null?{}:arguments[t];t%2?p(Object(n),!0).forEach(function(t){f(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):p(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}var h=(0,e.createContext)({});function g({defaults:e,children:t}){return(0,r.jsx)(h.Provider,{value:e,children:t})}function _(){return(0,e.useContext)(h)}function v(e,t){if(Object.keys(e).length===0)return t;let n=m({},e);for(let e of Object.keys(t)){let r=t[e];r!==void 0&&(n[e]=r)}return(e.tools!==void 0||t.tools!==void 0)&&(n.tools=m(m({},e.tools),t.tools)),n}var y=`__blokPortalRegistry`,b=`__blokToolName`,x=()=>{let e={snapshot:new Map},t=new Set,n=new Map,r=n=>{e.snapshot=n,t.forEach(e=>e())};return{register(t,i){let a=new Map(e.snapshot),o=i.toolName===void 0?void 0:n.get(i.toolName);a.set(t,o===void 0?i:m(m({},i),{},{props:m(m({},i.props),{},{config:o})})),r(a)},unregister(t){if(!e.snapshot.has(t))return;let n=new Map(e.snapshot);n.delete(t),r(n)},setProps(t,n){let i=e.snapshot.get(t);if(i===void 0)return;let a=new Map(e.snapshot);a.set(t,m(m({},i),{},{props:m(m({},i.props),n)})),r(a)},setToolConfig(t,i){n.set(t,i);let a=[...e.snapshot].filter(([,e])=>e.toolName===t);if(a.length===0)return;let o=new Map(e.snapshot);for(let[e,t]of a)o.set(e,m(m({},t),{},{props:m(m({},t.props),{},{config:i})}));r(o)},subscribe(e){return t.add(e),()=>{t.delete(e)}},getSnapshot(){return e.snapshot}}},S=new WeakMap;function C(e,t){S.set(e,t)}function w(e){return S.get(e)}function T(e){S.delete(e)}var E=e=>{if(typeof e!=`object`||!e)return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null},D=(e,t)=>t.reduce((e,t)=>{if(Array.isArray(e))return typeof t==`number`?e[t]:void 0;if(E(e))return e[t]},e),O=(e,t)=>{let n=(e,t)=>{if(typeof e==`function`)return typeof t==`function`?t:e;if(Array.isArray(e)){let r=Array.isArray(t)?t:[];return e.map((e,t)=>n(e,r[t]))}if(E(e)){let r=E(t)?t:{},i={};for(let[t,a]of Object.entries(e))i[t]=n(a,r[t]);return i}return e},r=n(t,e);return E(r)?r:{}},k=(e,t)=>{if(!E(e))return e;let n={};for(let[r,i]of Object.entries(e)){if(!E(i)||!E(i.config)){n[r]=i;continue}let e=new WeakSet,a=(n,i)=>{if(typeof n==`function`){let e=n;return function(...n){let a=t(),o=E(a)?a[r]:void 0,s=D(E(o)?o.config:void 0,i);return Reflect.apply(typeof s==`function`?s:e,this,n)}}if(Array.isArray(n))return e.has(n)?n:(e.add(n),n.map((e,t)=>a(e,[...i,t])));if(E(n)){if(e.has(n))return n;e.add(n);let t={};for(let[e,r]of Object.entries(n))t[e]=a(r,[...i,e]);return t}return n};n[r]=m(m({},i),{},{config:a(i.config,[])})}return n},A=e=>{let t=typeof e==`function`?e:e==null?void 0:e.class;return typeof t==`function`&&t.__isBlokReactBlock===!0},j=e=>{let t=typeof e==`function`?e:e==null?void 0:e.class;return typeof t==`function`&&t.__isBlokReactInlineTool===!0},M=(e,t)=>{if(typeof e!=`object`||!e)return e;let n={};for(let[i,a]of Object.entries(e)){var r;if(!A(a)&&!j(a)){n[i]=a;continue}let e=typeof a==`function`?{class:a}:m({},a);e.config=m(m({},(r=e.config)==null?{}:r),{},{[y]:t,[b]:i}),n[i]=e}return n};function N(r,i){let a=v((0,e.useContext)(h),r),[s,l]=(0,e.useState)(null),u=(0,e.useRef)(a);u.current=a;let d=(0,e.useRef)({editor:null,holder:null,destroyTimeout:null,isDestroyed:!1,isEditorReady:!1,depsToken:null}),f=(0,e.useMemo)(()=>({}),i==null?[]:i),p=(0,e.useRef)(a.data),g=(0,e.useRef)(null),_=(0,e.useRef)(a.data),y=(0,e.useRef)(Promise.resolve()),b=(0,e.useRef)(void 0),S=(0,e.useRef)(null),w=(0,e.useRef)(void 0),E=(0,e.useRef)(new Map);(0,e.useEffect)(()=>{if(typeof window>`u`)return;let e=d.current;if(e.destroyTimeout!==null&&(clearTimeout(e.destroyTimeout),e.destroyTimeout=null),e.editor!==null&&!e.isDestroyed&&e.depsToken===f)return e.isEditorReady&&l(e.editor),()=>{P(e,l)};if(e.editor!==null&&!e.isDestroyed){c(e.editor),T(e.editor);try{e.editor.destroy()}catch(e){}e.editor=null,e.holder=null,e.isDestroyed=!0,e.isEditorReady=!1,l(null)}let n=document.createElement(`div`);e.holder=n,e.isDestroyed=!1,e.isEditorReady=!1,e.depsToken=f;let r=u.current,i=x(),a=r.tools===void 0?void 0:k(r.tools,()=>u.current.tools),s=a===void 0?void 0:M(a,i);S.current=i,w.current=a,E.current=new Map;let h=m(m(m({},r),s===void 0?{}:{tools:s}),{},{holder:n,onReady:(...e)=>{var t,n;(t=(n=u.current).onReady)==null||t.call(n,...e)},onChange:(...e)=>{var t,n;(t=(n=u.current).onChange)==null||t.call(n,...e)},onEnter:(...e)=>{var t,n;return(t=(n=u.current).onEnter)==null?void 0:t.call(n,...e)}});r.onSave&&(h.onSave=(...e)=>{var t,n;p.current=e[0],(t=(n=u.current).onSave)==null||t.call(n,...e)}),r.onBeforeRender&&(h.onBeforeRender=(...e)=>{var t,n,r;return(t=(n=(r=u.current).onBeforeRender)==null?void 0:n.call(r,...e))==null?e[0]:t}),r.onAfterRender&&(h.onAfterRender=(...e)=>{var t,n;(t=(n=u.current).onAfterRender)==null||t.call(n,...e)});let g=new t.Blok(h);return e.editor=g,_.current=r.data,o(g,n),C(g,i),g.isReady.then(()=>{e.editor===g&&!e.isDestroyed&&(e.isEditorReady=!0,l(g))}).catch(()=>{if(e.editor===g&&!e.isDestroyed){c(g),T(g);try{g.destroy()}catch(e){}e.editor=null,e.holder=null,e.isDestroyed=!0,e.isEditorReady=!1,l(null)}}),()=>{P(e,l)}},[f]),(0,e.useEffect)(()=>{let e=S.current,t=d.current;if(e===null||t.editor===null||t.isDestroyed)return;let r=u.current.tools;if(typeof r!=`object`||!r)return;let i=w.current;for(let[t,o]of Object.entries(r)){var a;if(!A(o))continue;let r=typeof o==`function`||(a=o.config)==null?{}:a,s=i==null?void 0:i[t],c=O(typeof s==`object`&&s?s.config:void 0,r),l=E.current.get(t);if(l!==void 0&&(0,n.deepEqual)(c,l))continue;let u=Object.freeze(c);E.current.set(t,u),e.setToolConfig(t,u)}});let D=(0,n.normalizeReadOnlyConfig)(a.readOnly).enabled;(0,e.useEffect)(()=>{s!==null&&s.readOnly.set(D)},[s,D]);let{autofocus:j}=a;(0,e.useEffect)(()=>{s===null||!j||s.focus()},[s,j]);let{theme:N}=a;(0,e.useEffect)(()=>{s===null||N===void 0||s.theme.set(N)},[s,N]);let{width:F}=a;(0,e.useEffect)(()=>{s===null||F===void 0||s.width.set(F)},[s,F]);let{placeholder:I}=a;(0,e.useEffect)(()=>{s===null||I===void 0||s.placeholder.set(I)},[s,I]);let{data:L}=a;return(0,e.useEffect)(()=>{if(s===null||L===void 0)return;let e=d.current;if(!(e.editor!==s||e.isDestroyed)){if(g.current!==s&&(g.current=s,b.current=void 0,(0,n.deepEqual)(L,_.current))){p.current=L;return}(0,n.deepEqual)(L,p.current)||(0,n.deepEqual)(L,b.current)||(b.current=L,y.current=y.current.catch(()=>void 0).then(()=>s.render(L)).then(()=>{p.current=L},()=>{}).then(()=>{b.current===L&&(b.current=void 0)}))}},[s,L]),s}function P(e,t){e.destroyTimeout=setTimeout(()=>{if(e.editor!==null){c(e.editor),T(e.editor);try{e.editor.destroy()}catch(e){}e.editor=null,e.holder=null,e.isDestroyed=!0,e.isEditorReady=!1,e.destroyTimeout=null,t(null)}},0)}var F=class extends e.Component{constructor(...e){super(...e),this.state={failed:!1}}static getDerivedStateFromError(){return{failed:!0}}componentDidCatch(){}render(){return this.state.failed?null:this.props.children}};function I({registry:t}){let n=(0,e.useSyncExternalStore)(t.subscribe,t.getSnapshot,t.getSnapshot);return(0,r.jsx)(r.Fragment,{children:Array.from(n.entries()).map(([e,t])=>{let n=t.component;return(0,i.createPortal)((0,r.jsx)(F,{children:(0,r.jsx)(n,m({},t.props))}),t.hostEl,e)})})}function L(e,t){if(e==null)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(t.includes(r))continue;n[r]=e[r]}return n}function R(e,t){if(e==null)return{};var n,r,i=L(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.includes(n)||{}.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}var z=[`editor`],B=(0,e.forwardRef)(function(t,n){let{editor:i}=t,a=R(t,z),o=(0,e.useRef)(null),c=e=>{if(o.current=e,typeof n==`function`)n(e);else if(n!=null){let t=n;t.current=e}};(0,e.useEffect)(()=>{if(i===null||o.current===null)return;let e=s(i);if(e!==void 0)return o.current.appendChild(e),()=>{e.remove()}},[i]);let l=i===null?void 0:w(i);return(0,r.jsx)(`div`,m(m({ref:c},a),{},{children:l===void 0?null:(0,r.jsx)(I,{registry:l})}))}),V=`autofocus.defaultBlock.dataModel.placeholder.sanitizer.hideToolbar.maxHistoryLength.historyDebounceTime.newGroupDelay.globalUndoRedo.tools.data.minHeight.logLevel.readOnly.i18n.link.linkPaste.onBeforePaste.onReady.onChange.onSave.onEnter.onBeforeRender.onAfterRender.inlineToolbar.tunes.style.theme.onThemeChange.scrollToBlock.user.resolveUser.notifierPosition.notifier.width`.split(`.`),H=[`deps`,`onReady`,`onBlocksRendered`,`onBlockRendered`],U=new Set(V),W=(0,e.forwardRef)(function(t,n){let{deps:i,onReady:a,onBlocksRendered:o,onBlockRendered:s}=t,c=R(t,H),l={},u={};for(let e of Object.keys(c)){let t=c[e];U.has(e)?l[e]=t:u[e]=t}let d=N(l,i);(0,e.useImperativeHandle)(n,()=>d,[d]);let f=(0,e.useRef)(a);f.current=a;let p=(0,e.useRef)(null);(0,e.useEffect)(()=>{if(d!==null&&p.current!==d){var e;p.current=d,(e=f.current)==null||e.call(f,d)}},[d]);let h=(0,e.useRef)(o);h.current=o;let g=(0,e.useRef)(s);g.current=s;let _=!!o,v=!!s;return(0,e.useEffect)(()=>{if(d===null)return;let e=[];if(_){let t=e=>{var t;return(t=h.current)==null?void 0:t.call(h,e)};d.on(`blocks:rendered`,t),e.push([`blocks:rendered`,t])}if(v){let t=e=>{var t;return(t=g.current)==null?void 0:t.call(g,e)};d.on(`block:rendered`,t),e.push([`block:rendered`,t])}return()=>{for(let[t,n]of e)d.off(t,n)}},[d,_,v]),(0,r.jsx)(B,m({editor:d},u))}),G=`block changed`;function K(t){let r=(0,e.useRef)(0),i=(0,e.useRef)(t);i.current!==t&&(i.current=t,r.current=0);let a=(0,e.useCallback)(e=>{if(t===null)return()=>void 0;let n=()=>{r.current+=1,e()};return t.on(G,n),()=>t.off(G,n)},[t]),o=(0,e.useCallback)(()=>r.current,[]);return(0,e.useSyncExternalStore)(a,o,o),(0,e.useMemo)(()=>t===null?n.EMPTY_API:(0,n.createBlocksApiForEditor)(t),[t])}var q=[y,b];function J(t){var a;return a=class{static get toolbox(){return t.toolbox}static get isReadOnlySupported(){return!0}constructor(i){var a,o;this.hostEl=null,this.pendingDispatch=!1,this.commit=e=>{var r;let i=(0,n.fillDefaults)(t.propSchema,m(m({},this.mirror),e));(0,n.deepEqual)(i,this.mirror)||(this.mirror=i,this.lastRendered=i,(r=this.registry)==null||r.setProps(this.blockApi.id,{data:i}),this.flushDispatch())},this.blockApi=i.block;let s=(a=i.config)==null?{}:a;this.registry=s[y];let c=s[b];this.toolName=typeof c==`string`?c:void 0;let l={};for(let[e,t]of Object.entries(s))q.includes(e)||(l[e]=t);this.toolConfig=Object.freeze(l);let u=i.api;this.pointerDrag=()=>{var e;return(u==null||(e=u.blocks)==null?void 0:e.isPointerDragActive)===!0},this.mirror=(0,n.fillDefaults)(t.propSchema,(o=i.data)==null?{}:o),this.lastRendered=this.mirror,this.readOnly=i.readOnly;let d=this.blockApi;this.childrenComponent=function(){let t=(0,e.useRef)(null);return(0,e.useEffect)(()=>{t.current!==null&&(0,n.mountChildBlocks)(t.current,d.getChildren())}),(0,r.jsx)(`div`,{ref:t,[n.DATA_ATTR.nestedBlocks]:``})}}render(){var e;let n=document.createElement(`div`);return n.setAttribute(`data-blok-mutation-free`,`true`),this.hostEl=n,(e=this.registry)==null||e.register(this.blockApi.id,{hostEl:n,component:t.component,props:this.buildProps(),toolName:this.toolName}),n}rendered(){var e;(e=t.onRendered)==null||e.call(t,this.blockApi)}save(){return this.mirror}async setData(e){var r=this;let i=(0,n.fillDefaults)(t.propSchema,e==null?{}:e);return(0,n.deepEqual)(i,r.lastRendered)?!0:(r.mirror=i,r.lastRendered=i,r.pushProps({data:i}),!0)}setReadOnly(e){this.readOnly=e,this.pushProps({readOnly:e})}moved(){var e;(e=t.onMoved)==null||e.call(t,this.blockApi)}removed(){var e,n;(e=this.registry)==null||e.unregister(this.blockApi.id),(n=t.onRemoved)==null||n.call(t,this.blockApi)}destroy(){var e;(e=this.registry)==null||e.unregister(this.blockApi.id)}buildProps(){return{data:this.mirror,commit:this.commit,block:this.blockApi,readOnly:this.readOnly,config:this.toolConfig,BlockChildren:this.childrenComponent}}pushProps(e){let t=this.registry;t!==void 0&&(0,i.flushSync)(()=>{t.setProps(this.blockApi.id,e)})}flushDispatch(){if(!this.pointerDrag()){this.pendingDispatch=!1,this.blockApi.dispatchChange();return}if(this.pendingDispatch)return;this.pendingDispatch=!0;let e=()=>{if(this.pointerDrag()){requestAnimationFrame(e);return}this.pendingDispatch=!1,this.blockApi.dispatchChange()};requestAnimationFrame(e)}},a.__isBlokReactBlock=!0,a}var Y=[y,b],X=e=>`__blok-inline-tool__:${e}:${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;function Z(e){var t;return t=class{static get title(){return e.title}static get shortcut(){return e.shortcut}static get sanitize(){return e.sanitize}static get isReadOnlySupported(){var t;return(t=e.isReadOnlySupported)==null?!1:t}constructor(t){var n;this.active=!1,this.registered=!1;let r=(n=t==null?void 0:t.config)==null?{}:n;this.registry=r[y];let i=r[b];this.toolName=typeof i==`string`?i:e.type,this.portalKey=X(this.toolName);let a={};for(let[e,t]of Object.entries(r))Y.includes(e)||(a[e]=t);this.toolConfig=Object.freeze(a)}render(){let t=document.createElement(`div`);return t.setAttribute(`data-blok-mutation-free`,`true`),this.registry!==void 0&&(this.registry.register(this.portalKey,{hostEl:t,component:e.component,props:{active:this.active,config:this.toolConfig},toolName:this.toolName}),this.registered=!0),{name:this.toolName,title:e.title,icon:t,onActivate:()=>{if(typeof e.surround!=`function`)return;let t=window.getSelection();!t||t.rangeCount===0||e.surround(t.getRangeAt(0))},isActive:()=>this.refreshActiveState()}}destroy(){var e;this.registered&&(this.registered=!1,(e=this.registry)==null||e.unregister(this.portalKey))}refreshActiveState(){let t=typeof e.checkState==`function`?!!e.checkState(window.getSelection()):!1;if(t!==this.active&&(this.active=t,this.registered)){var n;(n=this.registry)==null||n.setProps(this.portalKey,{active:t})}return t}},t.__isBlokReactInlineTool=!0,t.isInline=!0,t}exports.BLOK_PORTAL_REGISTRY_CONFIG_KEY=y,exports.BLOK_TOOL_NAME_CONFIG_KEY=b,exports.BlockPortalHost=I,exports.BlokContent=B,exports.BlokEditor=W,exports.BlokProvider=g,exports.createBlockPortalRegistry=x,exports.createReactBlock=J,exports.createReactInlineTool=Z,exports.useBlocks=K,exports.useBlok=N,exports.useBlokDefaults=_;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("react"),t=require("@bloklabs/core"),n=require("@bloklabs/core/adapters"),r=require("react/jsx-runtime"),i=require("react-dom"),a=require("react-dom/client");var o=new WeakMap;function s(e,t){o.set(e,t)}function c(e){return o.get(e)}function l(e){o.delete(e)}function u(e){"@babel/helpers - typeof";return u=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},u(e)}function d(e,t){if(u(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(u(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function f(e){var t=d(e,`string`);return u(t)==`symbol`?t:t+``}function p(e,t,n){return(t=f(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function m(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function h(e){for(var t=1;t<arguments.length;t++){var n=arguments[t]==null?{}:arguments[t];t%2?m(Object(n),!0).forEach(function(t){p(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):m(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}var g=(0,e.createContext)({});function _({defaults:e,children:t}){return(0,r.jsx)(g.Provider,{value:e,children:t})}function v(){return(0,e.useContext)(g)}function y(e,t){if(Object.keys(e).length===0)return t;let n=h({},e);for(let e of Object.keys(t)){let r=t[e];r!==void 0&&(n[e]=r)}return(e.tools!==void 0||t.tools!==void 0)&&(n.tools=h(h({},e.tools),t.tools)),n}var b=`__blokPortalRegistry`,x=`__blokToolName`,S=()=>{let e={snapshot:new Map},t=new Set,n=new Map,r=n=>{e.snapshot=n,t.forEach(e=>e())};return{register(t,i){let a=new Map(e.snapshot),o=i.toolName===void 0?void 0:n.get(i.toolName);a.set(t,o===void 0?i:h(h({},i),{},{props:h(h({},i.props),{},{config:o})})),r(a)},unregister(t){if(!e.snapshot.has(t))return;let n=new Map(e.snapshot);n.delete(t),r(n)},setProps(t,n){let i=e.snapshot.get(t);if(i===void 0)return;let a=new Map(e.snapshot);a.set(t,h(h({},i),{},{props:h(h({},i.props),n)})),r(a)},setToolConfig(t,i){n.set(t,i);let a=[...e.snapshot].filter(([,e])=>e.toolName===t);if(a.length===0)return;let o=new Map(e.snapshot);for(let[e,t]of a)o.set(e,h(h({},t),{},{props:h(h({},t.props),{},{config:i})}));r(o)},subscribe(e){return t.add(e),()=>{t.delete(e)}},getSnapshot(){return e.snapshot}}},C=new WeakMap;function w(e,t){C.set(e,t)}function T(e){return C.get(e)}function E(e){C.delete(e)}var D=e=>{if(typeof e!=`object`||!e)return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null},O=(e,t)=>t.reduce((e,t)=>{if(Array.isArray(e))return typeof t==`number`?e[t]:void 0;if(D(e))return e[t]},e),k=(e,t)=>{let n=(e,t)=>{if(typeof e==`function`)return typeof t==`function`?t:e;if(Array.isArray(e)){let r=Array.isArray(t)?t:[];return e.map((e,t)=>n(e,r[t]))}if(D(e)){let r=D(t)?t:{},i={};for(let[t,a]of Object.entries(e))i[t]=n(a,r[t]);return i}return e},r=n(t,e);return D(r)?r:{}},A=(e,t)=>{if(!D(e))return e;let n={};for(let[r,i]of Object.entries(e)){if(!D(i)||!D(i.config)){n[r]=i;continue}let e=new WeakSet,a=(n,i)=>{if(typeof n==`function`){let e=n;return function(...n){let a=t(),o=D(a)?a[r]:void 0,s=O(D(o)?o.config:void 0,i);return Reflect.apply(typeof s==`function`?s:e,this,n)}}if(Array.isArray(n))return e.has(n)?n:(e.add(n),n.map((e,t)=>a(e,[...i,t])));if(D(n)){if(e.has(n))return n;e.add(n);let t={};for(let[e,r]of Object.entries(n))t[e]=a(r,[...i,e]);return t}return n};n[r]=h(h({},i),{},{config:a(i.config,[])})}return n},j=e=>{let t=typeof e==`function`?e:e==null?void 0:e.class;return typeof t==`function`&&t.__isBlokReactBlock===!0},M=e=>{let t=typeof e==`function`?e:e==null?void 0:e.class;return typeof t==`function`&&t.__isBlokReactInlineTool===!0},N=(e,t)=>{if(typeof e!=`object`||!e)return e;let n={};for(let[i,a]of Object.entries(e)){var r;if(!j(a)&&!M(a)){n[i]=a;continue}let e=typeof a==`function`?{class:a}:h({},a);e.config=h(h({},(r=e.config)==null?{}:r),{},{[b]:t,[x]:i}),n[i]=e}return n};function P(r,i){var a,o;let c=y((0,e.useContext)(g),r),[u,d]=(0,e.useState)(null),f=(0,e.useRef)(c);f.current=c;let p=(0,e.useRef)({editor:null,holder:null,destroyTimeout:null,isDestroyed:!1,isEditorReady:!1,depsToken:null}),m=(0,e.useMemo)(()=>({}),i==null?[]:i),_=(0,e.useRef)(c.data),v=(0,e.useRef)((0,n.createEmittedEchoWindow)()),b=(0,e.useRef)(null),x=(0,e.useRef)(c.data),C=(0,e.useRef)(Promise.resolve()),T=(0,e.useRef)(void 0),D=(0,e.useRef)(null),O=(0,e.useRef)(void 0),M=(0,e.useRef)(new Map),P=(0,e.useRef)(new Map);(0,e.useEffect)(()=>{var e;if(typeof window>`u`)return;let n=p.current;if(n.destroyTimeout!==null&&(clearTimeout(n.destroyTimeout),n.destroyTimeout=null),n.editor!==null&&!n.isDestroyed&&n.depsToken===m)return n.isEditorReady&&d(n.editor),()=>{F(n,d)};if(n.editor!==null&&!n.isDestroyed){l(n.editor),E(n.editor);try{n.editor.destroy()}catch(e){}n.editor=null,n.holder=null,n.isDestroyed=!0,n.isEditorReady=!1,d(null)}let r=document.createElement(`div`);n.holder=r,n.isDestroyed=!1,n.isEditorReady=!1,n.depsToken=m;let i=f.current,a=S(),o=i.tools===void 0?void 0:A(i.tools,()=>f.current.tools),c=o===void 0?void 0:N(o,a);D.current=a,O.current=o,M.current=new Map,P.current=new Map(Object.entries((e=i.tools)==null?{}:e).map(([e,t])=>[e,typeof t==`object`&&t?t.toolbox:void 0]));let u=h(h(h({},i),c===void 0?{}:{tools:c}),{},{holder:r,onReady:(...e)=>{var t,n;(t=(n=f.current).onReady)==null||t.call(n,...e)},onChange:(...e)=>{var t,n;(t=(n=f.current).onChange)==null||t.call(n,...e)},onEnter:(...e)=>{var t,n;return(t=(n=f.current).onEnter)==null?void 0:t.call(n,...e)}});i.onSave&&(u.onSave=(...e)=>{var t,n;_.current=e[0],v.current.record(e[0]),(t=(n=f.current).onSave)==null||t.call(n,...e)}),i.onBeforeRender&&(u.onBeforeRender=(...e)=>{var t,n,r;return(t=(n=(r=f.current).onBeforeRender)==null?void 0:n.call(r,...e))==null?e[0]:t}),i.onAfterRender&&(u.onAfterRender=(...e)=>{var t,n;(t=(n=f.current).onAfterRender)==null||t.call(n,...e)});let g=new t.Blok(u);return n.editor=g,x.current=i.data,s(g,r),w(g,a),g.isReady.then(()=>{n.editor===g&&!n.isDestroyed&&(n.isEditorReady=!0,d(g))}).catch(()=>{if(n.editor===g&&!n.isDestroyed){l(g),E(g);try{g.destroy()}catch(e){}n.editor=null,n.holder=null,n.isDestroyed=!0,n.isEditorReady=!1,d(null)}}),()=>{F(n,d)}},[m]),(0,e.useEffect)(()=>{let e=D.current,t=p.current;if(e===null||t.editor===null||t.isDestroyed)return;let r=f.current.tools;if(typeof r!=`object`||!r)return;let i=O.current;for(let[t,o]of Object.entries(r)){var a;if(!j(o))continue;let r=typeof o==`function`||(a=o.config)==null?{}:a,s=i==null?void 0:i[t],c=k(typeof s==`object`&&s?s.config:void 0,r),l=M.current.get(t);if(l!==void 0&&(0,n.deepEqual)(c,l))continue;let u=Object.freeze(c);M.current.set(t,u),e.setToolConfig(t,u)}}),(0,e.useEffect)(()=>{let e=p.current;if(e.editor===null||e.isDestroyed||!e.isEditorReady)return;let t=f.current.tools;if(!(typeof t!=`object`||!t))for(let[i,a]of Object.entries(t)){var r;if(!P.current.has(i))continue;let t=typeof a==`object`&&a?a.toolbox:void 0;(0,n.deepEqual)(t,P.current.get(i))||(P.current.set(i,t),(r=e.editor.tools)==null||r.update(i,{toolbox:t}))}});let I=(0,n.normalizeReadOnlyConfig)(c.readOnly).enabled;(0,e.useEffect)(()=>{u!==null&&u.readOnly.set(I)},[u,I]);let{autofocus:L}=c;(0,e.useEffect)(()=>{u===null||!L||u.focus()},[u,L]);let{theme:R}=c;(0,e.useEffect)(()=>{u===null||R===void 0||u.theme.set(R)},[u,R]);let{width:z}=c;(0,e.useEffect)(()=>{u===null||z===void 0||u.width.set(z)},[u,z]);let B=(a=c.style)==null?void 0:a.tokens,V=(0,e.useRef)((o=c.style)==null?void 0:o.tokens);(0,e.useEffect)(()=>{u===null||B===void 0||(0,n.deepEqual)(B,V.current)||(V.current=B,u.tokens.set(B))},[u,B]);let{placeholder:H}=c;(0,e.useEffect)(()=>{u===null||H===void 0||u.placeholder.set(H)},[u,H]);let{data:U}=c;return(0,e.useEffect)(()=>{if(u===null||U===void 0)return;let e=p.current;if(!(e.editor!==u||e.isDestroyed)){if(b.current!==u&&(b.current=u,T.current=void 0,v.current.clear(),(0,n.deepEqual)(U,x.current))){_.current=U;return}(0,n.deepEqual)(U,_.current)||(0,n.deepEqual)(U,T.current)||v.current.matches(U)||(v.current.clear(),T.current=U,C.current=C.current.catch(()=>void 0).then(()=>u.render(U)).then(()=>{_.current=U},()=>{}).then(()=>{T.current===U&&(T.current=void 0)}))}},[u,U]),u}function F(e,t){e.destroyTimeout=setTimeout(()=>{if(e.editor!==null){l(e.editor),E(e.editor);try{e.editor.destroy()}catch(e){}e.editor=null,e.holder=null,e.isDestroyed=!0,e.isEditorReady=!1,e.destroyTimeout=null,t(null)}},0)}var I=class extends e.Component{constructor(...e){super(...e),this.state={failed:!1}}static getDerivedStateFromError(){return{failed:!0}}componentDidCatch(){}render(){return this.state.failed?null:this.props.children}};function L({registry:t}){let n=(0,e.useSyncExternalStore)(t.subscribe,t.getSnapshot,t.getSnapshot);return(0,r.jsx)(r.Fragment,{children:Array.from(n.entries()).map(([e,t])=>{let n=t.component;return(0,i.createPortal)((0,r.jsx)(I,{children:(0,r.jsx)(n,h({},t.props))}),t.hostEl,e)})})}function R(e,t){if(e==null)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(t.includes(r))continue;n[r]=e[r]}return n}function z(e,t){if(e==null)return{};var n,r,i=R(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.includes(n)||{}.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}var B=[`editor`],V=(0,e.forwardRef)(function(t,n){let{editor:i}=t,a=z(t,B),o=(0,e.useRef)(null),s=e=>{if(o.current=e,typeof n==`function`)n(e);else if(n!=null){let t=n;t.current=e}};(0,e.useEffect)(()=>{if(i===null||o.current===null)return;let e=c(i);if(e!==void 0)return o.current.appendChild(e),()=>{e.remove()}},[i]);let l=i===null?void 0:T(i);return(0,r.jsx)(`div`,h(h({ref:s},a),{},{children:l===void 0?null:(0,r.jsx)(L,{registry:l})}))}),H=`autofocus.defaultBlock.dataModel.placeholder.sanitizer.hideToolbar.maxHistoryLength.historyDebounceTime.newGroupDelay.globalUndoRedo.tools.data.minHeight.logLevel.readOnly.i18n.link.linkPaste.onBeforePaste.onReady.onChange.onSave.onEnter.onBeforeRender.onAfterRender.inlineToolbar.tunes.style.theme.onThemeChange.scrollToBlock.user.resolveUser.notifierPosition.notifier.width`.split(`.`),U=[`deps`,`onReady`,`onBlocksRendered`,`onBlockRendered`],W=new Set(H),G=(0,e.forwardRef)(function(t,n){let{deps:i,onReady:a,onBlocksRendered:o,onBlockRendered:s}=t,c=z(t,U),l={},u={};for(let e of Object.keys(c)){let t=c[e];W.has(e)?l[e]=t:u[e]=t}let d=P(l,i);(0,e.useImperativeHandle)(n,()=>d,[d]);let f=(0,e.useRef)(a);f.current=a;let p=(0,e.useRef)(null);(0,e.useEffect)(()=>{if(d!==null&&p.current!==d){var e;p.current=d,(e=f.current)==null||e.call(f,d)}},[d]);let m=(0,e.useRef)(o);m.current=o;let g=(0,e.useRef)(s);g.current=s;let _=!!o,v=!!s;return(0,e.useEffect)(()=>{if(d===null)return;let e=[];if(_){let t=e=>{var t;return(t=m.current)==null?void 0:t.call(m,e)};d.on(`blocks:rendered`,t),e.push([`blocks:rendered`,t])}if(v){let t=e=>{var t;return(t=g.current)==null?void 0:t.call(g,e)};d.on(`block:rendered`,t),e.push([`block:rendered`,t])}return()=>{for(let[t,n]of e)d.off(t,n)}},[d,_,v]),(0,r.jsx)(V,h({editor:d},u))}),K=`block changed`;function q(t){let r=(0,e.useRef)(0),i=(0,e.useRef)(t);i.current!==t&&(i.current=t,r.current=0);let a=(0,e.useCallback)(e=>{if(t===null)return()=>void 0;let n=()=>{r.current+=1,e()};return t.on(K,n),()=>t.off(K,n)},[t]),o=(0,e.useCallback)(()=>r.current,[]);return(0,e.useSyncExternalStore)(a,o,o),(0,e.useMemo)(()=>t===null?n.EMPTY_API:(0,n.createBlocksApiForEditor)(t),[t])}var J=[`commit`,`readOnly`],Y=[b,x];function X(t){var o;let s=e=>{if(typeof document>`u`)return null;let t=document.createElement(`div`),n=(0,a.createRoot)(t);(0,i.flushSync)(()=>{n.render(e)});let r=t.innerHTML;return n.unmount(),r===``?null:r},c=()=>{let n={complete:!0},r=t=>{if(!(0,e.isValidElement)(t.icon))return t;let r=s(t.icon);return r===null?(n.complete=!1,t):h(h({},t),{},{icon:r})};return t.toolbox===void 0?{value:void 0,complete:n.complete}:{value:Array.isArray(t.toolbox)?t.toolbox.map(r):r(t.toolbox),complete:n.complete}},l={resolved:!1,value:void 0},u=()=>{if(l.resolved)return l.value;let{value:e,complete:t}=c();return t&&(l.value=e,l.resolved=!0),e},d=t.viewComponent===void 0?t.component:e=>{let n=t.viewComponent;if(e.readOnly&&n!==void 0){let{commit:t,readOnly:i}=e;return(0,r.jsx)(n,h({},z(e,J)))}let i=t.component;return(0,r.jsx)(i,h({},e))};return o=class{static get toolbox(){return u()}static get isReadOnlySupported(){return!0}constructor(i){var a,o;this.hostEl=null,this.pendingDispatch=!1,this.commit=e=>{var r;let i=(0,n.fillDefaults)(t.propSchema,h(h({},this.mirror),e));(0,n.deepEqual)(i,this.mirror)||(this.mirror=i,this.lastRendered=i,(r=this.registry)==null||r.setProps(this.blockApi.id,{data:i}),this.flushDispatch())},this.blockApi=i.block;let s=(a=i.config)==null?{}:a;this.registry=s[b];let c=s[x];this.toolName=typeof c==`string`?c:void 0;let l={};for(let[e,t]of Object.entries(s))Y.includes(e)||(l[e]=t);this.toolConfig=Object.freeze(l);let u=i.api;this.pointerDrag=()=>{var e;return(u==null||(e=u.blocks)==null?void 0:e.isPointerDragActive)===!0},this.mirror=(0,n.fillDefaults)(t.propSchema,(o=i.data)==null?{}:o),this.lastRendered=this.mirror,this.readOnly=i.readOnly;let d=this.blockApi;this.childrenComponent=function(){let t=(0,e.useRef)(null);return(0,e.useEffect)(()=>{t.current!==null&&(0,n.mountChildBlocks)(t.current,d.getChildren())}),(0,r.jsx)(`div`,{ref:t,[n.DATA_ATTR.nestedBlocks]:``})}}render(){var e;let t=document.createElement(`div`);return t.setAttribute(`data-blok-mutation-free`,`true`),this.hostEl=t,(e=this.registry)==null||e.register(this.blockApi.id,{hostEl:t,component:d,props:this.buildProps(),toolName:this.toolName}),t}rendered(){var e;(e=t.onRendered)==null||e.call(t,this.blockApi)}save(){return this.mirror}async setData(e){var r=this;let i=(0,n.fillDefaults)(t.propSchema,e==null?{}:e);return(0,n.deepEqual)(i,r.lastRendered)?!0:(r.mirror=i,r.lastRendered=i,r.pushProps({data:i}),!0)}setReadOnly(e){this.readOnly=e,this.pushProps({readOnly:e})}moved(){var e;(e=t.onMoved)==null||e.call(t,this.blockApi)}removed(){var e,n;(e=this.registry)==null||e.unregister(this.blockApi.id),(n=t.onRemoved)==null||n.call(t,this.blockApi)}destroy(){var e;(e=this.registry)==null||e.unregister(this.blockApi.id)}buildProps(){return{data:this.mirror,commit:this.commit,block:this.blockApi,readOnly:this.readOnly,config:this.toolConfig,BlockChildren:this.childrenComponent}}pushProps(e){let t=this.registry;t!==void 0&&(0,i.flushSync)(()=>{t.setProps(this.blockApi.id,e)})}flushDispatch(){if(!this.pointerDrag()){this.pendingDispatch=!1,this.blockApi.dispatchChange();return}if(this.pendingDispatch)return;this.pendingDispatch=!0;let e=()=>{if(this.pointerDrag()){requestAnimationFrame(e);return}this.pendingDispatch=!1,this.blockApi.dispatchChange()};requestAnimationFrame(e)}},o.__isBlokReactBlock=!0,o}var Z=[b,x],Q=e=>`__blok-inline-tool__:${e}:${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;function $(e){var t;return t=class{static get title(){return e.title}static get titleKey(){return e.titleKey}static get shortcut(){return e.shortcut}static get sanitize(){return e.sanitize}static get isReadOnlySupported(){var t;return(t=e.isReadOnlySupported)!=null&&t}constructor(t){var n;this.active=!1,this.registered=!1;let r=(n=t==null?void 0:t.config)==null?{}:n;this.registry=r[b];let i=r[x];this.toolName=typeof i==`string`?i:e.type,this.portalKey=Q(this.toolName);let a={};for(let[e,t]of Object.entries(r))Z.includes(e)||(a[e]=t);this.toolConfig=Object.freeze(a)}render(){let t=document.createElement(`div`);return t.setAttribute(`data-blok-mutation-free`,`true`),this.registry!==void 0&&(this.registry.register(this.portalKey,{hostEl:t,component:e.component,props:{active:this.active,config:this.toolConfig},toolName:this.toolName}),this.registered=!0),{name:this.toolName,title:e.title,icon:t,onActivate:()=>{if(typeof e.surround!=`function`)return;let t=window.getSelection();!t||t.rangeCount===0||e.surround(t.getRangeAt(0))},isActive:()=>this.refreshActiveState()}}destroy(){var e;this.registered&&(this.registered=!1,(e=this.registry)==null||e.unregister(this.portalKey))}refreshActiveState(){let t=typeof e.checkState==`function`&&!!e.checkState(window.getSelection());if(t!==this.active&&(this.active=t,this.registered)){var n;(n=this.registry)==null||n.setProps(this.portalKey,{active:t})}return t}},t.__isBlokReactInlineTool=!0,t.isInline=!0,t}exports.BLOK_PORTAL_REGISTRY_CONFIG_KEY=b,exports.BLOK_TOOL_NAME_CONFIG_KEY=x,exports.BlockPortalHost=L,exports.BlokContent=V,exports.BlokEditor=G,exports.BlokProvider=_,exports.createBlockPortalRegistry=S,exports.createReactBlock=X,exports.createReactInlineTool=$,exports.useBlocks=q,exports.useBlok=P,exports.useBlokDefaults=v;
|