@craftjs/utils
Advanced tools
Comparing version 0.2.0-beta.7 to 0.2.0-beta.8
@@ -6,2 +6,10 @@ # Change Log | ||
# [0.2.0-beta.8](https://github.com/prevwong/craft.js/compare/v0.2.0-beta.7...v0.2.0-beta.8) (2022-08-30) | ||
**Note:** Version bump only for package @craftjs/utils | ||
# [0.2.0-beta.7](https://github.com/prevwong/craft.js/compare/v0.2.0-beta.6...v0.2.0-beta.7) (2022-08-30) | ||
@@ -8,0 +16,0 @@ |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var produce=require("immer"),isEqualWith=require("lodash/isEqualWith"),React=require("react"),isEqual=require("shallowequal"),nanoid=require("nanoid"),invariant=require("tiny-invariant"),ReactDOM=require("react-dom");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var produce__default=_interopDefaultLegacy(produce),isEqualWith__default=_interopDefaultLegacy(isEqualWith),React__default=_interopDefaultLegacy(React),isEqual__default=_interopDefaultLegacy(isEqual),invariant__default=_interopDefaultLegacy(invariant),ReactDOM__default=_interopDefaultLegacy(ReactDOM),ROOT_NODE="ROOT",DEPRECATED_ROOT_NODE="canvas-ROOT",ERROR_NOPARENT="Parent id cannot be ommited",ERROR_DUPLICATE_NODEID="Attempting to add a node with duplicated id",ERROR_INVALID_NODEID="Node does not exist, it may have been removed",ERROR_TOP_LEVEL_ELEMENT_NO_ID='A <Element /> that is used inside a User Component must specify an `id` prop, eg: <Element id="text_element">...</Element> ',ERROR_MISSING_PLACEHOLDER_PLACEMENT="Placeholder required placement info (parent, index, or where) is missing",ERROR_MOVE_CANNOT_DROP="Node cannot be dropped into target parent",ERROR_MOVE_INCOMING_PARENT="Target parent rejects incoming node",ERROR_MOVE_OUTGOING_PARENT="Current parent rejects outgoing node",ERROR_MOVE_NONCANVAS_CHILD="Cannot move node that is not a direct child of a Canvas node",ERROR_MOVE_TO_NONCANVAS_PARENT="Cannot move node into a non-Canvas parent",ERROR_MOVE_TOP_LEVEL_NODE="A top-level Node cannot be moved",ERROR_MOVE_ROOT_NODE="Root Node cannot be moved",ERROR_MOVE_TO_DESCENDANT="Cannot move node into a descendant",ERROR_NOT_IN_RESOLVER="The component type specified for this node (%node_type%) does not exist in the resolver",ERROR_INFINITE_CANVAS="The component specified in the <Canvas> `is` prop has additional Canvas specified in it's render template.",ERROR_CANNOT_DRAG="The node has specified a canDrag() rule that prevents it from being dragged",ERROR_INVALID_NODE_ID="Invalid parameter Node Id specified",ERROR_DELETE_TOP_LEVEL_NODE="Attempting to delete a top-level Node",ERROR_RESOLVER_NOT_AN_OBJECT="Resolver in <Editor /> has to be an object. For (de)serialization Craft.js needs a list of all the User Components. \n \nMore info: https://craft.js.org/r/docs/api/editor#props",ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER="An Error occurred while deserializing components: Cannot find component <%displayName% /> in resolver map. Please check your resolver in <Editor />\n\nAvailable components in resolver: %availableComponents%\n\nMore info: https://craft.js.org/r/docs/api/editor#props",ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT="You can only use useEditor in the context of <Editor />. \n\nPlease only use useEditor in components that are children of the <Editor /> component.",ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT="You can only use useNode in the context of <Editor />. \n\nPlease only use useNode in components that are children of the <Editor /> component.",_extendStatics=function(e,t){return(_extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function __extends(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}_extendStatics(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var _assign=function(){return(_assign=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function __spreadArrays(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var r=Array(e),o=0;for(t=0;t<n;t++)for(var i=arguments[t],s=0,a=i.length;s<a;s++,o++)r[o]=i[s];return r}var HISTORY_ACTIONS={UNDO:"HISTORY_UNDO",REDO:"HISTORY_REDO",THROTTLE:"HISTORY_THROTTLE",IGNORE:"HISTORY_IGNORE",MERGE:"HISTORY_MERGE",CLEAR:"HISTORY_CLEAR"},History=function(){function e(){this.timeline=[],this.pointer=-1}return e.prototype.add=function(e,t){0===e.length&&0===t.length||(this.pointer=this.pointer+1,this.timeline.length=this.pointer,this.timeline[this.pointer]={patches:e,inversePatches:t,timestamp:Date.now()})},e.prototype.throttleAdd=function(e,t,n){if(void 0===n&&(n=500),0!==e.length||0!==t.length){if(this.timeline.length&&this.pointer>=0){var r=this.timeline[this.pointer],o=r.patches,i=r.inversePatches,s=r.timestamp;if((new Date).getTime()-s<n)return void(this.timeline[this.pointer]={timestamp:s,patches:__spreadArrays(o,e),inversePatches:__spreadArrays(t,i)})}this.add(e,t)}},e.prototype.merge=function(e,t){if(0!==e.length||0!==t.length)if(this.timeline.length&&this.pointer>=0){var n=this.timeline[this.pointer],r=n.inversePatches;this.timeline[this.pointer]={timestamp:n.timestamp,patches:__spreadArrays(n.patches,e),inversePatches:__spreadArrays(t,r)}}else this.add(e,t)},e.prototype.clear=function(){this.timeline=[],this.pointer=-1},e.prototype.canUndo=function(){return this.pointer>=0},e.prototype.canRedo=function(){return this.pointer<this.timeline.length-1},e.prototype.undo=function(e){if(this.canUndo()){var t=this.timeline[this.pointer].inversePatches;return this.pointer=this.pointer-1,produce.applyPatches(e,t)}},e.prototype.redo=function(e){if(this.canRedo())return this.pointer=this.pointer+1,produce.applyPatches(e,this.timeline[this.pointer].patches)},e}();function useMethods(e,t,n,r){var o,i=React.useMemo((function(){return new History}),[]),s=React.useRef([]),a=React.useRef();"function"==typeof e?o=e:(o=e.methods,s.current=e.ignoreHistoryForActions,a.current=e.normalizeHistory);var c=React.useRef(r);c.current=r;var u=React.useRef(t),R=React.useMemo((function(){var e=a.current,t=s.current,r=c.current;return function(s,a){var c,u=n&&createQuery(n,(function(){return s}),i),R=produce.produceWithPatches(s,(function(e){var t,n;switch(a.type){case HISTORY_ACTIONS.UNDO:return i.undo(e);case HISTORY_ACTIONS.REDO:return i.redo(e);case HISTORY_ACTIONS.CLEAR:return i.clear(),_assign({},e);case HISTORY_ACTIONS.IGNORE:case HISTORY_ACTIONS.MERGE:case HISTORY_ACTIONS.THROTTLE:var r=a.payload,s=r[0],c=r.slice(1);(t=o(e,u))[s].apply(t,c);break;default:(n=o(e,u))[a.type].apply(n,a.payload)}})),p=R[0],d=R[1],E=R[2];return c=p,r&&r(p,s,{type:a.type,params:a.payload,patches:d},u,(function(e){var t=produce.produceWithPatches(p,e);c=t[0],d=__spreadArrays(d,t[1]),E=__spreadArrays(t[2],E)})),[HISTORY_ACTIONS.UNDO,HISTORY_ACTIONS.REDO].includes(a.type)&&e&&(c=produce__default.default(c,e)),__spreadArrays(t,[HISTORY_ACTIONS.UNDO,HISTORY_ACTIONS.REDO,HISTORY_ACTIONS.IGNORE,HISTORY_ACTIONS.CLEAR]).includes(a.type)||(a.type===HISTORY_ACTIONS.THROTTLE?i.throttleAdd(d,E,a.config&&a.config.rate):a.type===HISTORY_ACTIONS.MERGE?i.merge(d,E):i.add(d,E)),c}}),[i,o,n]),p=React.useCallback((function(){return u.current}),[]),d=React.useMemo((function(){return new Watcher(p)}),[p]),E=React.useCallback((function(e){var t=R(u.current,e);u.current=t,d.notify()}),[R]);React.useEffect((function(){d.notify()}),[d]);var l=React.useMemo((function(){return n?createQuery(n,(function(){return u.current}),i):[]}),[i,n]),_=React.useMemo((function(){var e=Object.keys(o(null,null)),t=s.current;return _assign(_assign({},e.reduce((function(e,t){return e[t]=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return E({type:t,payload:e})},e}),{})),{history:{undo:function(){return E({type:HISTORY_ACTIONS.UNDO})},redo:function(){return E({type:HISTORY_ACTIONS.REDO})},clear:function(){return E({type:HISTORY_ACTIONS.CLEAR})},throttle:function(n){return _assign({},e.filter((function(e){return!t.includes(e)})).reduce((function(e,t){return e[t]=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return E({type:HISTORY_ACTIONS.THROTTLE,payload:__spreadArrays([t],e),config:{rate:n}})},e}),{}))},ignore:function(){return _assign({},e.filter((function(e){return!t.includes(e)})).reduce((function(e,t){return e[t]=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return E({type:HISTORY_ACTIONS.IGNORE,payload:__spreadArrays([t],e)})},e}),{}))},merge:function(){return _assign({},e.filter((function(e){return!t.includes(e)})).reduce((function(e,t){return e[t]=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return E({type:HISTORY_ACTIONS.MERGE,payload:__spreadArrays([t],e)})},e}),{}))}}})}),[o]);return React.useMemo((function(){return{getState:p,subscribe:function(e,t,n){return d.subscribe(e,t,n)},actions:_,query:l,history:i}}),[_,l,d,p,i])}function createQuery(e,t,n){var r=Object.keys(e()).reduce((function(n,r){var o;return _assign(_assign({},n),((o={})[r]=function(){for(var n,o=[],i=0;i<arguments.length;i++)o[i]=arguments[i];return(n=e(t()))[r].apply(n,o)},o))}),{});return _assign(_assign({},r),{history:{canUndo:function(){return n.canUndo()},canRedo:function(){return n.canRedo()}}})}produce.enableMapSet(),produce.enablePatches();var Watcher=function(){function e(e){this.subscribers=[],this.getState=e}return e.prototype.subscribe=function(e,t,n){var r=this,o=new Subscriber((function(){return e(r.getState())}),t,n);return this.subscribers.push(o),this.unsubscribe.bind(this,o)},e.prototype.unsubscribe=function(e){if(this.subscribers.length){var t=this.subscribers.indexOf(e);if(t>-1)return this.subscribers.splice(t,1)}},e.prototype.notify=function(){this.subscribers.forEach((function(e){return e.collect()}))},e}(),Subscriber=function(){function e(e,t,n){void 0===n&&(n=!1),this.collector=e,this.onChange=t,n&&this.collect()}return e.prototype.collect=function(){try{var e=this.collector();isEqualWith__default.default(e,this.collected)||(this.collected=e,this.onChange&&this.onChange(this.collected))}catch(e){console.warn(e)}},e}(),getDOMInfo=function(e){var t=e.getBoundingClientRect(),n=t.x,r=t.y,o=t.top,i=t.left,s=t.bottom,a=t.right,c=t.width,u=t.height,R=window.getComputedStyle(e),p={left:parseInt(R.marginLeft),right:parseInt(R.marginRight),bottom:parseInt(R.marginBottom),top:parseInt(R.marginTop)},d={left:parseInt(R.paddingLeft),right:parseInt(R.paddingRight),bottom:parseInt(R.paddingBottom),top:parseInt(R.paddingTop)};return{x:n,y:r,top:o,left:i,bottom:s,right:a,width:c,height:u,outerWidth:Math.round(c+p.left+p.right),outerHeight:Math.round(u+p.top+p.bottom),margin:p,padding:d,inFlow:e.parentElement&&!!function(t){var n=getComputedStyle(t);if(!(R.overflow&&"visible"!==R.overflow||"none"!==n.float||"grid"===n.display||"flex"===n.display&&"column"!==n["flex-direction"])){switch(R.position){case"static":case"relative":break;default:return}switch(e.tagName){case"TR":case"TBODY":case"THEAD":case"TFOOT":return!0}switch(R.display){case"block":case"list-item":case"table":case"flex":case"grid":return!0}}}(e.parentElement)}};function useCollector(e,t){var n=e.subscribe,r=e.getState,o=e.actions,i=e.query,s=React.useRef(!0),a=React.useRef(null),c=React.useRef(t);c.current=t;var u=React.useCallback((function(e){return _assign(_assign({},e),{actions:o,query:i})}),[o,i]);s.current&&t&&(a.current=t(r(),i),s.current=!1);var R=React.useState(u(a.current)),p=R[0],d=R[1];return React.useEffect((function(){var e;return c.current&&(e=n((function(e){return c.current(e,i)}),(function(e){d(u(e))}))),function(){e&&e()}}),[u,i,n]),p}var getRandomId=function(e){return void 0===e&&(e=10),nanoid.nanoid(e)},ConnectorRegistry=function(){function e(){this.isEnabled=!0,this.elementIdMap=new WeakMap,this.registry=new Map}return e.prototype.getElementId=function(e){var t=this.elementIdMap.get(e);if(t)return t;var n=getRandomId();return this.elementIdMap.set(e,n),n},e.prototype.getConnectorId=function(e,t){return t+"--"+this.getElementId(e)},e.prototype.register=function(e,t){var n=this,r=this.getByElement(e,t.name);if(r){if(isEqual__default.default(t.required,r.required))return r;this.getByElement(e,t.name).disable()}var o=null,i=this.getConnectorId(e,t.name);return this.registry.set(i,{id:i,required:t.required,enable:function(){o&&o(),o=t.connector(e,t.required,t.options)},disable:function(){o&&o()},remove:function(){return n.remove(i)}}),this.isEnabled&&this.registry.get(i).enable(),this.registry.get(i)},e.prototype.get=function(e){return this.registry.get(e)},e.prototype.remove=function(e){var t=this.get(e);t&&(t.disable(),this.registry.delete(t.id))},e.prototype.enable=function(){this.isEnabled=!0,this.registry.forEach((function(e){e.enable()}))},e.prototype.disable=function(){this.isEnabled=!1,this.registry.forEach((function(e){e.disable()}))},e.prototype.getByElement=function(e,t){return this.get(this.getConnectorId(e,t))},e.prototype.removeByElement=function(e,t){return this.remove(this.getConnectorId(e,t))},e.prototype.clear=function(){this.disable(),this.elementIdMap=new WeakMap,this.registry=new Map},e}();function isEventBlockedByDescendant(e,t,n){e.craft||(e.craft={stopPropagation:function(){},blockedEvents:{}});for(var r=e.craft&&e.craft.blockedEvents[t]||[],o=0;o<r.length;o++){var i=r[o];if(n!==i&&n.contains(i))return!0}return!1}exports.EventHandlerUpdates=void 0,function(e){e[e.HandlerDisabled=0]="HandlerDisabled",e[e.HandlerEnabled=1]="HandlerEnabled"}(exports.EventHandlerUpdates||(exports.EventHandlerUpdates={}));var EventHandlers=function(){function e(e){this.registry=new ConnectorRegistry,this.subscribers=new Set,this.options=e}return e.prototype.listen=function(e){var t=this;return this.subscribers.add(e),function(){return t.subscribers.delete(e)}},e.prototype.disable=function(){this.onDisable&&this.onDisable(),this.registry.disable(),this.subscribers.forEach((function(e){e(exports.EventHandlerUpdates.HandlerDisabled)}))},e.prototype.enable=function(){this.onEnable&&this.onEnable(),this.registry.enable(),this.subscribers.forEach((function(e){e(exports.EventHandlerUpdates.HandlerEnabled)}))},e.prototype.cleanup=function(){this.disable(),this.subscribers.clear(),this.registry.clear()},e.prototype.addCraftEventListener=function(e,t,n,r){var o=function(r){isEventBlockedByDescendant(r,t,e)||(r.craft.stopPropagation=function(){r.craft.blockedEvents[t]||(r.craft.blockedEvents[t]=[]),r.craft.blockedEvents[t].push(e)},n(r))};return e.addEventListener(t,o,r),function(){return e.removeEventListener(t,o,r)}},e.prototype.createConnectorsUsage=function(){var e=this,t=this.handlers(),n=new Set,r=!1,o=new Map;return{connectors:Object.entries(t).reduce((function(t,i){var s,a=i[0],c=i[1];return _assign(_assign({},t),((s={})[a]=function(t,i,s){var u=function(){var r=e.registry.register(t,{required:i,name:a,options:s,connector:c});return n.add(r.id),r};return r?u():o.set(e.registry.getConnectorId(t,a),u),t},s))}),{}),register:function(){r=!0,o.forEach((function(e){e()})),o.clear()},cleanup:function(){r=!1,n.forEach((function(t){return e.registry.remove(t)}))}}},e.prototype.derive=function(e,t){return new e(this,t)},e.prototype.createProxyHandlers=function(e,t){var n=[],r=e.handlers();return t(new Proxy(r,{get:function(e,t,o){return t in r==0?Reflect.get(e,t,o):function(e){for(var o=[],i=1;i<arguments.length;i++)o[i-1]=arguments[i];var s=r[t].apply(r,__spreadArrays([e],o));s&&n.push(s)}}})),function(){n.forEach((function(e){e()}))}},e.prototype.reflect=function(e){return this.createProxyHandlers(this,e)},e}(),DerivedEventHandlers=function(e){function t(t,n){var r=e.call(this,n)||this;return r.derived=t,r.options=n,r.unsubscribeParentHandlerListener=r.derived.listen((function(e){switch(e){case exports.EventHandlerUpdates.HandlerEnabled:return r.enable();case exports.EventHandlerUpdates.HandlerDisabled:return r.disable();default:return}})),r}return __extends(t,e),t.prototype.inherit=function(e){return this.createProxyHandlers(this.derived,e)},t.prototype.cleanup=function(){e.prototype.cleanup.call(this),this.unsubscribeParentHandlerListener()},t}(EventHandlers);function setRef(e,t){t&&("function"==typeof e?e(t):e.current=t)}function cloneWithRef(e,t){var n=e.ref;return invariant__default.default("string"!=typeof n,"Cannot connect to an element with an existing string ref. Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. Read more: https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute"),React.cloneElement(e,n?{ref:function(e){setRef(n,e),setRef(t,e)}}:{ref:t})}function throwIfCompositeComponentElement(e){if("string"!=typeof e.type)throw new Error}function wrapHookToRecognizeElement(e){return function(t){void 0===t&&(t=null);for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];if(!React.isValidElement(t)){if(!t)return;var o=t;return o&&e.apply(void 0,__spreadArrays([o],n)),o}var i=t;return throwIfCompositeComponentElement(i),cloneWithRef(i,e)}}function wrapConnectorHooks(e){return Object.keys(e).reduce((function(t,n){return t[n]=wrapHookToRecognizeElement((function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return e[n].apply(e,t)})),t}),{})}var RenderIndicator=function(e){var t=e.parentDom,n=React__default.default.createElement("div",{style:_assign({position:"fixed",display:"block",opacity:1,borderStyle:"solid",borderWidth:"1px",borderColor:"transparent",zIndex:99999},e.style)});return t&&t.ownerDocument!==document?ReactDOM__default.default.createPortal(n,t.ownerDocument.body):n},useEffectOnce=function(e){React.useEffect(e,[])},deprecationWarning=function(e,t){var n="Deprecation warning: "+e+" will be deprecated in future relases.",r=t.suggest,o=t.doc;r&&(n+=" Please use "+r+" instead."),o&&(n+="("+o+")"),console.warn(n)},isClientSide=function(){return"undefined"!=typeof window},isLinux=function(){return isClientSide()&&/Linux/i.test(window.navigator.userAgent)},isChromium=function(){return isClientSide()&&/Chrome/i.test(window.navigator.userAgent)};exports.DEPRECATED_ROOT_NODE=DEPRECATED_ROOT_NODE,exports.DerivedEventHandlers=DerivedEventHandlers,exports.ERROR_CANNOT_DRAG=ERROR_CANNOT_DRAG,exports.ERROR_DELETE_TOP_LEVEL_NODE=ERROR_DELETE_TOP_LEVEL_NODE,exports.ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER=ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER,exports.ERROR_DUPLICATE_NODEID=ERROR_DUPLICATE_NODEID,exports.ERROR_INFINITE_CANVAS=ERROR_INFINITE_CANVAS,exports.ERROR_INVALID_NODEID=ERROR_INVALID_NODEID,exports.ERROR_INVALID_NODE_ID=ERROR_INVALID_NODE_ID,exports.ERROR_MISSING_PLACEHOLDER_PLACEMENT=ERROR_MISSING_PLACEHOLDER_PLACEMENT,exports.ERROR_MOVE_CANNOT_DROP=ERROR_MOVE_CANNOT_DROP,exports.ERROR_MOVE_INCOMING_PARENT=ERROR_MOVE_INCOMING_PARENT,exports.ERROR_MOVE_NONCANVAS_CHILD=ERROR_MOVE_NONCANVAS_CHILD,exports.ERROR_MOVE_OUTGOING_PARENT=ERROR_MOVE_OUTGOING_PARENT,exports.ERROR_MOVE_ROOT_NODE=ERROR_MOVE_ROOT_NODE,exports.ERROR_MOVE_TOP_LEVEL_NODE=ERROR_MOVE_TOP_LEVEL_NODE,exports.ERROR_MOVE_TO_DESCENDANT=ERROR_MOVE_TO_DESCENDANT,exports.ERROR_MOVE_TO_NONCANVAS_PARENT=ERROR_MOVE_TO_NONCANVAS_PARENT,exports.ERROR_NOPARENT=ERROR_NOPARENT,exports.ERROR_NOT_IN_RESOLVER=ERROR_NOT_IN_RESOLVER,exports.ERROR_RESOLVER_NOT_AN_OBJECT=ERROR_RESOLVER_NOT_AN_OBJECT,exports.ERROR_TOP_LEVEL_ELEMENT_NO_ID=ERROR_TOP_LEVEL_ELEMENT_NO_ID,exports.ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT=ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT,exports.ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT=ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT,exports.EventHandlers=EventHandlers,exports.HISTORY_ACTIONS=HISTORY_ACTIONS,exports.History=History,exports.ROOT_NODE=ROOT_NODE,exports.RenderIndicator=RenderIndicator,exports.cloneWithRef=cloneWithRef,exports.createQuery=createQuery,exports.deprecationWarning=deprecationWarning,exports.getDOMInfo=getDOMInfo,exports.getRandomId=getRandomId,exports.isChromium=isChromium,exports.isClientSide=isClientSide,exports.isLinux=isLinux,exports.useCollector=useCollector,exports.useEffectOnce=useEffectOnce,exports.useMethods=useMethods,exports.wrapConnectorHooks=wrapConnectorHooks,exports.wrapHookToRecognizeElement=wrapHookToRecognizeElement; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var produce=require("immer"),isEqualWith=require("lodash/isEqualWith"),React=require("react"),isEqual=require("shallowequal"),nanoid=require("nanoid"),invariant=require("tiny-invariant"),ReactDOM=require("react-dom");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var produce__default=_interopDefaultLegacy(produce),isEqualWith__default=_interopDefaultLegacy(isEqualWith),React__default=_interopDefaultLegacy(React),isEqual__default=_interopDefaultLegacy(isEqual),invariant__default=_interopDefaultLegacy(invariant),ReactDOM__default=_interopDefaultLegacy(ReactDOM),ROOT_NODE="ROOT",DEPRECATED_ROOT_NODE="canvas-ROOT",ERROR_NOPARENT="Parent id cannot be ommited",ERROR_DUPLICATE_NODEID="Attempting to add a node with duplicated id",ERROR_INVALID_NODEID="Node does not exist, it may have been removed",ERROR_TOP_LEVEL_ELEMENT_NO_ID='A <Element /> that is used inside a User Component must specify an `id` prop, eg: <Element id="text_element">...</Element> ',ERROR_MISSING_PLACEHOLDER_PLACEMENT="Placeholder required placement info (parent, index, or where) is missing",ERROR_MOVE_CANNOT_DROP="Node cannot be dropped into target parent",ERROR_MOVE_INCOMING_PARENT="Target parent rejects incoming node",ERROR_MOVE_OUTGOING_PARENT="Current parent rejects outgoing node",ERROR_MOVE_NONCANVAS_CHILD="Cannot move node that is not a direct child of a Canvas node",ERROR_MOVE_TO_NONCANVAS_PARENT="Cannot move node into a non-Canvas parent",ERROR_MOVE_TOP_LEVEL_NODE="A top-level Node cannot be moved",ERROR_MOVE_ROOT_NODE="Root Node cannot be moved",ERROR_MOVE_TO_DESCENDANT="Cannot move node into a descendant",ERROR_NOT_IN_RESOLVER="The component type specified for this node (%node_type%) does not exist in the resolver",ERROR_INFINITE_CANVAS="The component specified in the <Canvas> `is` prop has additional Canvas specified in it's render template.",ERROR_CANNOT_DRAG="The node has specified a canDrag() rule that prevents it from being dragged",ERROR_INVALID_NODE_ID="Invalid parameter Node Id specified",ERROR_DELETE_TOP_LEVEL_NODE="Attempting to delete a top-level Node",ERROR_RESOLVER_NOT_AN_OBJECT="Resolver in <Editor /> has to be an object. For (de)serialization Craft.js needs a list of all the User Components. \n \nMore info: https://craft.js.org/r/docs/api/editor#props",ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER="An Error occurred while deserializing components: Cannot find component <%displayName% /> in resolver map. Please check your resolver in <Editor />\n\nAvailable components in resolver: %availableComponents%\n\nMore info: https://craft.js.org/r/docs/api/editor#props",ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT="You can only use useEditor in the context of <Editor />. \n\nPlease only use useEditor in components that are children of the <Editor /> component.",ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT="You can only use useNode in the context of <Editor />. \n\nPlease only use useNode in components that are children of the <Editor /> component.",_extendStatics=function(e,t){return(_extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])})(e,t)};function __extends(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}_extendStatics(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var _assign=function(){return(_assign=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function __spreadArrays(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var r=Array(e),o=0;for(t=0;t<n;t++)for(var i=arguments[t],s=0,a=i.length;s<a;s++,o++)r[o]=i[s];return r}var HISTORY_ACTIONS={UNDO:"HISTORY_UNDO",REDO:"HISTORY_REDO",THROTTLE:"HISTORY_THROTTLE",IGNORE:"HISTORY_IGNORE",MERGE:"HISTORY_MERGE",CLEAR:"HISTORY_CLEAR"},History=function(){function e(){this.timeline=[],this.pointer=-1}return e.prototype.add=function(e,t){0===e.length&&0===t.length||(this.pointer=this.pointer+1,this.timeline.length=this.pointer,this.timeline[this.pointer]={patches:e,inversePatches:t,timestamp:Date.now()})},e.prototype.throttleAdd=function(e,t,n){if(void 0===n&&(n=500),0!==e.length||0!==t.length){if(this.timeline.length&&this.pointer>=0){var r=this.timeline[this.pointer],o=r.patches,i=r.inversePatches,s=r.timestamp;if((new Date).getTime()-s<n)return void(this.timeline[this.pointer]={timestamp:s,patches:__spreadArrays(o,e),inversePatches:__spreadArrays(t,i)})}this.add(e,t)}},e.prototype.merge=function(e,t){if(0!==e.length||0!==t.length)if(this.timeline.length&&this.pointer>=0){var n=this.timeline[this.pointer],r=n.inversePatches;this.timeline[this.pointer]={timestamp:n.timestamp,patches:__spreadArrays(n.patches,e),inversePatches:__spreadArrays(t,r)}}else this.add(e,t)},e.prototype.clear=function(){this.timeline=[],this.pointer=-1},e.prototype.canUndo=function(){return this.pointer>=0},e.prototype.canRedo=function(){return this.pointer<this.timeline.length-1},e.prototype.undo=function(e){if(this.canUndo()){var t=this.timeline[this.pointer].inversePatches;return this.pointer=this.pointer-1,produce.applyPatches(e,t)}},e.prototype.redo=function(e){if(this.canRedo())return this.pointer=this.pointer+1,produce.applyPatches(e,this.timeline[this.pointer].patches)},e}();function useMethods(e,t,n,r){var o,i=React.useMemo((function(){return new History}),[]),s=React.useRef([]),a=React.useRef();"function"==typeof e?o=e:(o=e.methods,s.current=e.ignoreHistoryForActions,a.current=e.normalizeHistory);var c=React.useRef(r);c.current=r;var u=React.useRef(t),R=React.useMemo((function(){var e=a.current,t=s.current,r=c.current;return function(s,a){var c,u=n&&createQuery(n,(function(){return s}),i),R=produce.produceWithPatches(s,(function(e){var t,n;switch(a.type){case HISTORY_ACTIONS.UNDO:return i.undo(e);case HISTORY_ACTIONS.REDO:return i.redo(e);case HISTORY_ACTIONS.CLEAR:return i.clear(),_assign({},e);case HISTORY_ACTIONS.IGNORE:case HISTORY_ACTIONS.MERGE:case HISTORY_ACTIONS.THROTTLE:var r=a.payload,s=r[0],c=r.slice(1);(t=o(e,u))[s].apply(t,c);break;default:(n=o(e,u))[a.type].apply(n,a.payload)}})),p=R[0],d=R[1],E=R[2];return c=p,r&&r(p,s,{type:a.type,params:a.payload,patches:d},u,(function(e){var t=produce.produceWithPatches(p,e);c=t[0],d=__spreadArrays(d,t[1]),E=__spreadArrays(t[2],E)})),[HISTORY_ACTIONS.UNDO,HISTORY_ACTIONS.REDO].includes(a.type)&&e&&(c=produce__default.default(c,e)),__spreadArrays(t,[HISTORY_ACTIONS.UNDO,HISTORY_ACTIONS.REDO,HISTORY_ACTIONS.IGNORE,HISTORY_ACTIONS.CLEAR]).includes(a.type)||(a.type===HISTORY_ACTIONS.THROTTLE?i.throttleAdd(d,E,a.config&&a.config.rate):a.type===HISTORY_ACTIONS.MERGE?i.merge(d,E):i.add(d,E)),c}}),[i,o,n]),p=React.useCallback((function(){return u.current}),[]),d=React.useMemo((function(){return new Watcher(p)}),[p]),E=React.useCallback((function(e){var t=R(u.current,e);u.current=t,d.notify()}),[R,d]);React.useEffect((function(){d.notify()}),[d]);var l=React.useMemo((function(){return n?createQuery(n,(function(){return u.current}),i):[]}),[i,n]),_=React.useMemo((function(){var e=Object.keys(o(null,null)),t=s.current;return _assign(_assign({},e.reduce((function(e,t){return e[t]=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return E({type:t,payload:e})},e}),{})),{history:{undo:function(){return E({type:HISTORY_ACTIONS.UNDO})},redo:function(){return E({type:HISTORY_ACTIONS.REDO})},clear:function(){return E({type:HISTORY_ACTIONS.CLEAR})},throttle:function(n){return _assign({},e.filter((function(e){return!t.includes(e)})).reduce((function(e,t){return e[t]=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return E({type:HISTORY_ACTIONS.THROTTLE,payload:__spreadArrays([t],e),config:{rate:n}})},e}),{}))},ignore:function(){return _assign({},e.filter((function(e){return!t.includes(e)})).reduce((function(e,t){return e[t]=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return E({type:HISTORY_ACTIONS.IGNORE,payload:__spreadArrays([t],e)})},e}),{}))},merge:function(){return _assign({},e.filter((function(e){return!t.includes(e)})).reduce((function(e,t){return e[t]=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return E({type:HISTORY_ACTIONS.MERGE,payload:__spreadArrays([t],e)})},e}),{}))}}})}),[E,o]);return React.useMemo((function(){return{getState:p,subscribe:function(e,t,n){return d.subscribe(e,t,n)},actions:_,query:l,history:i}}),[_,l,d,p,i])}function createQuery(e,t,n){var r=Object.keys(e()).reduce((function(n,r){var o;return _assign(_assign({},n),((o={})[r]=function(){for(var n,o=[],i=0;i<arguments.length;i++)o[i]=arguments[i];return(n=e(t()))[r].apply(n,o)},o))}),{});return _assign(_assign({},r),{history:{canUndo:function(){return n.canUndo()},canRedo:function(){return n.canRedo()}}})}produce.enableMapSet(),produce.enablePatches();var Watcher=function(){function e(e){this.subscribers=[],this.getState=e}return e.prototype.subscribe=function(e,t,n){var r=this,o=new Subscriber((function(){return e(r.getState())}),t,n);return this.subscribers.push(o),this.unsubscribe.bind(this,o)},e.prototype.unsubscribe=function(e){if(this.subscribers.length){var t=this.subscribers.indexOf(e);if(t>-1)return this.subscribers.splice(t,1)}},e.prototype.notify=function(){this.subscribers.forEach((function(e){return e.collect()}))},e}(),Subscriber=function(){function e(e,t,n){void 0===n&&(n=!1),this.collector=e,this.onChange=t,n&&this.collect()}return e.prototype.collect=function(){try{var e=this.collector();isEqualWith__default.default(e,this.collected)||(this.collected=e,this.onChange&&this.onChange(this.collected))}catch(e){console.warn(e)}},e}(),getDOMInfo=function(e){var t=e.getBoundingClientRect(),n=t.x,r=t.y,o=t.top,i=t.left,s=t.bottom,a=t.right,c=t.width,u=t.height,R=window.getComputedStyle(e),p={left:parseInt(R.marginLeft),right:parseInt(R.marginRight),bottom:parseInt(R.marginBottom),top:parseInt(R.marginTop)},d={left:parseInt(R.paddingLeft),right:parseInt(R.paddingRight),bottom:parseInt(R.paddingBottom),top:parseInt(R.paddingTop)};return{x:n,y:r,top:o,left:i,bottom:s,right:a,width:c,height:u,outerWidth:Math.round(c+p.left+p.right),outerHeight:Math.round(u+p.top+p.bottom),margin:p,padding:d,inFlow:e.parentElement&&!!function(t){var n=getComputedStyle(t);if(!(R.overflow&&"visible"!==R.overflow||"none"!==n.float||"grid"===n.display||"flex"===n.display&&"column"!==n["flex-direction"])){switch(R.position){case"static":case"relative":break;default:return}switch(e.tagName){case"TR":case"TBODY":case"THEAD":case"TFOOT":return!0}switch(R.display){case"block":case"list-item":case"table":case"flex":case"grid":return!0}}}(e.parentElement)}};function useCollector(e,t){var n=e.subscribe,r=e.getState,o=e.actions,i=e.query,s=React.useRef(!0),a=React.useRef(null),c=React.useRef(t);c.current=t;var u=React.useCallback((function(e){return _assign(_assign({},e),{actions:o,query:i})}),[o,i]);s.current&&t&&(a.current=t(r(),i),s.current=!1);var R=React.useState(u(a.current)),p=R[0],d=R[1];return React.useEffect((function(){var e;return c.current&&(e=n((function(e){return c.current(e,i)}),(function(e){d(u(e))}))),function(){e&&e()}}),[u,i,n]),p}var getRandomId=function(e){return void 0===e&&(e=10),nanoid.nanoid(e)},ConnectorRegistry=function(){function e(){this.isEnabled=!0,this.elementIdMap=new WeakMap,this.registry=new Map}return e.prototype.getElementId=function(e){var t=this.elementIdMap.get(e);if(t)return t;var n=getRandomId();return this.elementIdMap.set(e,n),n},e.prototype.getConnectorId=function(e,t){return t+"--"+this.getElementId(e)},e.prototype.register=function(e,t){var n=this,r=this.getByElement(e,t.name);if(r){if(isEqual__default.default(t.required,r.required))return r;this.getByElement(e,t.name).disable()}var o=null,i=this.getConnectorId(e,t.name);return this.registry.set(i,{id:i,required:t.required,enable:function(){o&&o(),o=t.connector(e,t.required,t.options)},disable:function(){o&&o()},remove:function(){return n.remove(i)}}),this.isEnabled&&this.registry.get(i).enable(),this.registry.get(i)},e.prototype.get=function(e){return this.registry.get(e)},e.prototype.remove=function(e){var t=this.get(e);t&&(t.disable(),this.registry.delete(t.id))},e.prototype.enable=function(){this.isEnabled=!0,this.registry.forEach((function(e){e.enable()}))},e.prototype.disable=function(){this.isEnabled=!1,this.registry.forEach((function(e){e.disable()}))},e.prototype.getByElement=function(e,t){return this.get(this.getConnectorId(e,t))},e.prototype.removeByElement=function(e,t){return this.remove(this.getConnectorId(e,t))},e.prototype.clear=function(){this.disable(),this.elementIdMap=new WeakMap,this.registry=new Map},e}();function isEventBlockedByDescendant(e,t,n){e.craft||(e.craft={stopPropagation:function(){},blockedEvents:{}});for(var r=e.craft&&e.craft.blockedEvents[t]||[],o=0;o<r.length;o++){var i=r[o];if(n!==i&&n.contains(i))return!0}return!1}exports.EventHandlerUpdates=void 0,function(e){e[e.HandlerDisabled=0]="HandlerDisabled",e[e.HandlerEnabled=1]="HandlerEnabled"}(exports.EventHandlerUpdates||(exports.EventHandlerUpdates={}));var EventHandlers=function(){function e(e){this.registry=new ConnectorRegistry,this.subscribers=new Set,this.options=e}return e.prototype.listen=function(e){var t=this;return this.subscribers.add(e),function(){return t.subscribers.delete(e)}},e.prototype.disable=function(){this.onDisable&&this.onDisable(),this.registry.disable(),this.subscribers.forEach((function(e){e(exports.EventHandlerUpdates.HandlerDisabled)}))},e.prototype.enable=function(){this.onEnable&&this.onEnable(),this.registry.enable(),this.subscribers.forEach((function(e){e(exports.EventHandlerUpdates.HandlerEnabled)}))},e.prototype.cleanup=function(){this.disable(),this.subscribers.clear(),this.registry.clear()},e.prototype.addCraftEventListener=function(e,t,n,r){var o=function(r){isEventBlockedByDescendant(r,t,e)||(r.craft.stopPropagation=function(){r.craft.blockedEvents[t]||(r.craft.blockedEvents[t]=[]),r.craft.blockedEvents[t].push(e)},n(r))};return e.addEventListener(t,o,r),function(){return e.removeEventListener(t,o,r)}},e.prototype.createConnectorsUsage=function(){var e=this,t=this.handlers(),n=new Set,r=!1,o=new Map;return{connectors:Object.entries(t).reduce((function(t,i){var s,a=i[0],c=i[1];return _assign(_assign({},t),((s={})[a]=function(t,i,s){var u=function(){var r=e.registry.register(t,{required:i,name:a,options:s,connector:c});return n.add(r.id),r};return o.set(e.registry.getConnectorId(t,a),u),r&&u(),t},s))}),{}),register:function(){r=!0,o.forEach((function(e){e()}))},cleanup:function(){r=!1,n.forEach((function(t){return e.registry.remove(t)}))}}},e.prototype.derive=function(e,t){return new e(this,t)},e.prototype.createProxyHandlers=function(e,t){var n=[],r=e.handlers();return t(new Proxy(r,{get:function(e,t,o){return t in r==0?Reflect.get(e,t,o):function(e){for(var o=[],i=1;i<arguments.length;i++)o[i-1]=arguments[i];var s=r[t].apply(r,__spreadArrays([e],o));s&&n.push(s)}}})),function(){n.forEach((function(e){e()}))}},e.prototype.reflect=function(e){return this.createProxyHandlers(this,e)},e}(),DerivedEventHandlers=function(e){function t(t,n){var r=e.call(this,n)||this;return r.derived=t,r.options=n,r.unsubscribeParentHandlerListener=r.derived.listen((function(e){switch(e){case exports.EventHandlerUpdates.HandlerEnabled:return r.enable();case exports.EventHandlerUpdates.HandlerDisabled:return r.disable();default:return}})),r}return __extends(t,e),t.prototype.inherit=function(e){return this.createProxyHandlers(this.derived,e)},t.prototype.cleanup=function(){e.prototype.cleanup.call(this),this.unsubscribeParentHandlerListener()},t}(EventHandlers);function setRef(e,t){t&&("function"==typeof e?e(t):e.current=t)}function cloneWithRef(e,t){var n=e.ref;return invariant__default.default("string"!=typeof n,"Cannot connect to an element with an existing string ref. Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. Read more: https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute"),React.cloneElement(e,n?{ref:function(e){setRef(n,e),setRef(t,e)}}:{ref:t})}function throwIfCompositeComponentElement(e){if("string"!=typeof e.type)throw new Error}function wrapHookToRecognizeElement(e){return function(t){void 0===t&&(t=null);for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];if(!React.isValidElement(t)){if(!t)return;var o=t;return o&&e.apply(void 0,__spreadArrays([o],n)),o}var i=t;return throwIfCompositeComponentElement(i),cloneWithRef(i,e)}}function wrapConnectorHooks(e){return Object.keys(e).reduce((function(t,n){return t[n]=wrapHookToRecognizeElement((function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return e[n].apply(e,t)})),t}),{})}var RenderIndicator=function(e){var t=e.parentDom,n=React__default.default.createElement("div",{style:_assign({position:"fixed",display:"block",opacity:1,borderStyle:"solid",borderWidth:"1px",borderColor:"transparent",zIndex:99999},e.style)});return t&&t.ownerDocument!==document?ReactDOM__default.default.createPortal(n,t.ownerDocument.body):n},useEffectOnce=function(e){React.useEffect(e,[])},deprecationWarning=function(e,t){var n="Deprecation warning: "+e+" will be deprecated in future relases.",r=t.suggest,o=t.doc;r&&(n+=" Please use "+r+" instead."),o&&(n+="("+o+")"),console.warn(n)},isClientSide=function(){return"undefined"!=typeof window},isLinux=function(){return isClientSide()&&/Linux/i.test(window.navigator.userAgent)},isChromium=function(){return isClientSide()&&/Chrome/i.test(window.navigator.userAgent)};exports.DEPRECATED_ROOT_NODE=DEPRECATED_ROOT_NODE,exports.DerivedEventHandlers=DerivedEventHandlers,exports.ERROR_CANNOT_DRAG=ERROR_CANNOT_DRAG,exports.ERROR_DELETE_TOP_LEVEL_NODE=ERROR_DELETE_TOP_LEVEL_NODE,exports.ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER=ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER,exports.ERROR_DUPLICATE_NODEID=ERROR_DUPLICATE_NODEID,exports.ERROR_INFINITE_CANVAS=ERROR_INFINITE_CANVAS,exports.ERROR_INVALID_NODEID=ERROR_INVALID_NODEID,exports.ERROR_INVALID_NODE_ID=ERROR_INVALID_NODE_ID,exports.ERROR_MISSING_PLACEHOLDER_PLACEMENT=ERROR_MISSING_PLACEHOLDER_PLACEMENT,exports.ERROR_MOVE_CANNOT_DROP=ERROR_MOVE_CANNOT_DROP,exports.ERROR_MOVE_INCOMING_PARENT=ERROR_MOVE_INCOMING_PARENT,exports.ERROR_MOVE_NONCANVAS_CHILD=ERROR_MOVE_NONCANVAS_CHILD,exports.ERROR_MOVE_OUTGOING_PARENT=ERROR_MOVE_OUTGOING_PARENT,exports.ERROR_MOVE_ROOT_NODE=ERROR_MOVE_ROOT_NODE,exports.ERROR_MOVE_TOP_LEVEL_NODE=ERROR_MOVE_TOP_LEVEL_NODE,exports.ERROR_MOVE_TO_DESCENDANT=ERROR_MOVE_TO_DESCENDANT,exports.ERROR_MOVE_TO_NONCANVAS_PARENT=ERROR_MOVE_TO_NONCANVAS_PARENT,exports.ERROR_NOPARENT=ERROR_NOPARENT,exports.ERROR_NOT_IN_RESOLVER=ERROR_NOT_IN_RESOLVER,exports.ERROR_RESOLVER_NOT_AN_OBJECT=ERROR_RESOLVER_NOT_AN_OBJECT,exports.ERROR_TOP_LEVEL_ELEMENT_NO_ID=ERROR_TOP_LEVEL_ELEMENT_NO_ID,exports.ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT=ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT,exports.ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT=ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT,exports.EventHandlers=EventHandlers,exports.HISTORY_ACTIONS=HISTORY_ACTIONS,exports.History=History,exports.ROOT_NODE=ROOT_NODE,exports.RenderIndicator=RenderIndicator,exports.cloneWithRef=cloneWithRef,exports.createQuery=createQuery,exports.deprecationWarning=deprecationWarning,exports.getDOMInfo=getDOMInfo,exports.getRandomId=getRandomId,exports.isChromium=isChromium,exports.isClientSide=isClientSide,exports.isLinux=isLinux,exports.useCollector=useCollector,exports.useEffectOnce=useEffectOnce,exports.useMethods=useMethods,exports.wrapConnectorHooks=wrapConnectorHooks,exports.wrapHookToRecognizeElement=wrapHookToRecognizeElement; |
@@ -1,1 +0,1 @@ | ||
import t,{applyPatches as e,enableMapSet as n,enablePatches as r,produceWithPatches as i}from"immer";import o from"lodash/isEqualWith";import a,{useMemo as s,useRef as c,useCallback as u,useEffect as l,useState as p,cloneElement as f,isValidElement as d}from"react";import h from"shallowequal";import{nanoid as y}from"nanoid";import g from"tiny-invariant";import m from"react-dom";var v="ROOT",b="canvas-ROOT",E="Parent id cannot be ommited",w="Attempting to add a node with duplicated id",O="Node does not exist, it may have been removed",R='A <Element /> that is used inside a User Component must specify an `id` prop, eg: <Element id="text_element">...</Element> ',T="Placeholder required placement info (parent, index, or where) is missing",C="Node cannot be dropped into target parent",I="Target parent rejects incoming node",H="Current parent rejects outgoing node",D="Cannot move node that is not a direct child of a Canvas node",P="Cannot move node into a non-Canvas parent",x="A top-level Node cannot be moved",N="Root Node cannot be moved",A="Cannot move node into a descendant",k="The component type specified for this node (%node_type%) does not exist in the resolver",L="The component specified in the <Canvas> `is` prop has additional Canvas specified in it's render template.",M="The node has specified a canDrag() rule that prevents it from being dragged",S="Invalid parameter Node Id specified",_="Attempting to delete a top-level Node",j="Resolver in <Editor /> has to be an object. For (de)serialization Craft.js needs a list of all the User Components. \n \nMore info: https://craft.js.org/r/docs/api/editor#props",U="An Error occurred while deserializing components: Cannot find component <%displayName% /> in resolver map. Please check your resolver in <Editor />\n\nAvailable components in resolver: %availableComponents%\n\nMore info: https://craft.js.org/r/docs/api/editor#props",q="You can only use useEditor in the context of <Editor />. \n\nPlease only use useEditor in components that are children of the <Editor /> component.",G="You can only use useNode in the context of <Editor />. \n\nPlease only use useNode in components that are children of the <Editor /> component.",Y=function(t,e){return(Y=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)},B=function(){return(B=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function W(){for(var t=0,e=0,n=arguments.length;e<n;e++)t+=arguments[e].length;var r=Array(t),i=0;for(e=0;e<n;e++)for(var o=arguments[e],a=0,s=o.length;a<s;a++,i++)r[i]=o[a];return r}var z={UNDO:"HISTORY_UNDO",REDO:"HISTORY_REDO",THROTTLE:"HISTORY_THROTTLE",IGNORE:"HISTORY_IGNORE",MERGE:"HISTORY_MERGE",CLEAR:"HISTORY_CLEAR"},F=function(){function t(){this.timeline=[],this.pointer=-1}return t.prototype.add=function(t,e){0===t.length&&0===e.length||(this.pointer=this.pointer+1,this.timeline.length=this.pointer,this.timeline[this.pointer]={patches:t,inversePatches:e,timestamp:Date.now()})},t.prototype.throttleAdd=function(t,e,n){if(void 0===n&&(n=500),0!==t.length||0!==e.length){if(this.timeline.length&&this.pointer>=0){var r=this.timeline[this.pointer],i=r.patches,o=r.inversePatches,a=r.timestamp;if((new Date).getTime()-a<n)return void(this.timeline[this.pointer]={timestamp:a,patches:W(i,t),inversePatches:W(e,o)})}this.add(t,e)}},t.prototype.merge=function(t,e){if(0!==t.length||0!==e.length)if(this.timeline.length&&this.pointer>=0){var n=this.timeline[this.pointer],r=n.inversePatches;this.timeline[this.pointer]={timestamp:n.timestamp,patches:W(n.patches,t),inversePatches:W(e,r)}}else this.add(t,e)},t.prototype.clear=function(){this.timeline=[],this.pointer=-1},t.prototype.canUndo=function(){return this.pointer>=0},t.prototype.canRedo=function(){return this.pointer<this.timeline.length-1},t.prototype.undo=function(t){if(this.canUndo()){var n=this.timeline[this.pointer].inversePatches;return this.pointer=this.pointer-1,e(t,n)}},t.prototype.redo=function(t){if(this.canRedo())return this.pointer=this.pointer+1,e(t,this.timeline[this.pointer].patches)},t}();function J(e,n,r,o){var a,p=s((function(){return new F}),[]),f=c([]),d=c();"function"==typeof e?a=e:(a=e.methods,f.current=e.ignoreHistoryForActions,d.current=e.normalizeHistory);var h=c(o);h.current=o;var y=c(n),g=s((function(){var e=d.current,n=f.current,o=h.current;return function(s,c){var u,l=r&&K(r,(function(){return s}),p),f=i(s,(function(t){var e,n;switch(c.type){case z.UNDO:return p.undo(t);case z.REDO:return p.redo(t);case z.CLEAR:return p.clear(),B({},t);case z.IGNORE:case z.MERGE:case z.THROTTLE:var r=c.payload,i=r[0],o=r.slice(1);(e=a(t,l))[i].apply(e,o);break;default:(n=a(t,l))[c.type].apply(n,c.payload)}})),d=f[0],h=f[1],y=f[2];return u=d,o&&o(d,s,{type:c.type,params:c.payload,patches:h},l,(function(t){var e=i(d,t);u=e[0],h=W(h,e[1]),y=W(e[2],y)})),[z.UNDO,z.REDO].includes(c.type)&&e&&(u=t(u,e)),W(n,[z.UNDO,z.REDO,z.IGNORE,z.CLEAR]).includes(c.type)||(c.type===z.THROTTLE?p.throttleAdd(h,y,c.config&&c.config.rate):c.type===z.MERGE?p.merge(h,y):p.add(h,y)),u}}),[p,a,r]),m=u((function(){return y.current}),[]),v=s((function(){return new Q(m)}),[m]),b=u((function(t){var e=g(y.current,t);y.current=e,v.notify()}),[g]);l((function(){v.notify()}),[v]);var E=s((function(){return r?K(r,(function(){return y.current}),p):[]}),[p,r]),w=s((function(){var t=Object.keys(a(null,null)),e=f.current;return B(B({},t.reduce((function(t,e){return t[e]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return b({type:e,payload:t})},t}),{})),{history:{undo:function(){return b({type:z.UNDO})},redo:function(){return b({type:z.REDO})},clear:function(){return b({type:z.CLEAR})},throttle:function(n){return B({},t.filter((function(t){return!e.includes(t)})).reduce((function(t,e){return t[e]=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return b({type:z.THROTTLE,payload:W([e],t),config:{rate:n}})},t}),{}))},ignore:function(){return B({},t.filter((function(t){return!e.includes(t)})).reduce((function(t,e){return t[e]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return b({type:z.IGNORE,payload:W([e],t)})},t}),{}))},merge:function(){return B({},t.filter((function(t){return!e.includes(t)})).reduce((function(t,e){return t[e]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return b({type:z.MERGE,payload:W([e],t)})},t}),{}))}}})}),[a]);return s((function(){return{getState:m,subscribe:function(t,e,n){return v.subscribe(t,e,n)},actions:w,query:E,history:p}}),[w,E,v,m,p])}function K(t,e,n){var r=Object.keys(t()).reduce((function(n,r){var i;return B(B({},n),((i={})[r]=function(){for(var n,i=[],o=0;o<arguments.length;o++)i[o]=arguments[o];return(n=t(e()))[r].apply(n,i)},i))}),{});return B(B({},r),{history:{canUndo:function(){return n.canUndo()},canRedo:function(){return n.canRedo()}}})}n(),r();var Q=function(){function t(t){this.subscribers=[],this.getState=t}return t.prototype.subscribe=function(t,e,n){var r=this,i=new V((function(){return t(r.getState())}),e,n);return this.subscribers.push(i),this.unsubscribe.bind(this,i)},t.prototype.unsubscribe=function(t){if(this.subscribers.length){var e=this.subscribers.indexOf(t);if(e>-1)return this.subscribers.splice(e,1)}},t.prototype.notify=function(){this.subscribers.forEach((function(t){return t.collect()}))},t}(),V=function(){function t(t,e,n){void 0===n&&(n=!1),this.collector=t,this.onChange=e,n&&this.collect()}return t.prototype.collect=function(){try{var t=this.collector();o(t,this.collected)||(this.collected=t,this.onChange&&this.onChange(this.collected))}catch(t){console.warn(t)}},t}(),X=function(t){var e=t.getBoundingClientRect(),n=e.x,r=e.y,i=e.top,o=e.left,a=e.bottom,s=e.right,c=e.width,u=e.height,l=window.getComputedStyle(t),p={left:parseInt(l.marginLeft),right:parseInt(l.marginRight),bottom:parseInt(l.marginBottom),top:parseInt(l.marginTop)},f={left:parseInt(l.paddingLeft),right:parseInt(l.paddingRight),bottom:parseInt(l.paddingBottom),top:parseInt(l.paddingTop)};return{x:n,y:r,top:i,left:o,bottom:a,right:s,width:c,height:u,outerWidth:Math.round(c+p.left+p.right),outerHeight:Math.round(u+p.top+p.bottom),margin:p,padding:f,inFlow:t.parentElement&&!!function(e){var n=getComputedStyle(e);if(!(l.overflow&&"visible"!==l.overflow||"none"!==n.float||"grid"===n.display||"flex"===n.display&&"column"!==n["flex-direction"])){switch(l.position){case"static":case"relative":break;default:return}switch(t.tagName){case"TR":case"TBODY":case"THEAD":case"TFOOT":return!0}switch(l.display){case"block":case"list-item":case"table":case"flex":case"grid":return!0}}}(t.parentElement)}};function Z(t,e){var n=t.subscribe,r=t.getState,i=t.actions,o=t.query,a=c(!0),s=c(null),f=c(e);f.current=e;var d=u((function(t){return B(B({},t),{actions:i,query:o})}),[i,o]);a.current&&e&&(s.current=e(r(),o),a.current=!1);var h=p(d(s.current)),y=h[0],g=h[1];return l((function(){var t;return f.current&&(t=n((function(t){return f.current(t,o)}),(function(t){g(d(t))}))),function(){t&&t()}}),[d,o,n]),y}var $,tt=function(t){return void 0===t&&(t=10),y(t)},et=function(){function t(){this.isEnabled=!0,this.elementIdMap=new WeakMap,this.registry=new Map}return t.prototype.getElementId=function(t){var e=this.elementIdMap.get(t);if(e)return e;var n=tt();return this.elementIdMap.set(t,n),n},t.prototype.getConnectorId=function(t,e){return e+"--"+this.getElementId(t)},t.prototype.register=function(t,e){var n=this,r=this.getByElement(t,e.name);if(r){if(h(e.required,r.required))return r;this.getByElement(t,e.name).disable()}var i=null,o=this.getConnectorId(t,e.name);return this.registry.set(o,{id:o,required:e.required,enable:function(){i&&i(),i=e.connector(t,e.required,e.options)},disable:function(){i&&i()},remove:function(){return n.remove(o)}}),this.isEnabled&&this.registry.get(o).enable(),this.registry.get(o)},t.prototype.get=function(t){return this.registry.get(t)},t.prototype.remove=function(t){var e=this.get(t);e&&(e.disable(),this.registry.delete(e.id))},t.prototype.enable=function(){this.isEnabled=!0,this.registry.forEach((function(t){t.enable()}))},t.prototype.disable=function(){this.isEnabled=!1,this.registry.forEach((function(t){t.disable()}))},t.prototype.getByElement=function(t,e){return this.get(this.getConnectorId(t,e))},t.prototype.removeByElement=function(t,e){return this.remove(this.getConnectorId(t,e))},t.prototype.clear=function(){this.disable(),this.elementIdMap=new WeakMap,this.registry=new Map},t}();!function(t){t[t.HandlerDisabled=0]="HandlerDisabled",t[t.HandlerEnabled=1]="HandlerEnabled"}($||($={}));var nt=function(){function t(t){this.registry=new et,this.subscribers=new Set,this.options=t}return t.prototype.listen=function(t){var e=this;return this.subscribers.add(t),function(){return e.subscribers.delete(t)}},t.prototype.disable=function(){this.onDisable&&this.onDisable(),this.registry.disable(),this.subscribers.forEach((function(t){t($.HandlerDisabled)}))},t.prototype.enable=function(){this.onEnable&&this.onEnable(),this.registry.enable(),this.subscribers.forEach((function(t){t($.HandlerEnabled)}))},t.prototype.cleanup=function(){this.disable(),this.subscribers.clear(),this.registry.clear()},t.prototype.addCraftEventListener=function(t,e,n,r){var i=function(r){(function(t,e,n){t.craft||(t.craft={stopPropagation:function(){},blockedEvents:{}});for(var r=t.craft&&t.craft.blockedEvents[e]||[],i=0;i<r.length;i++){var o=r[i];if(n!==o&&n.contains(o))return!0}return!1})(r,e,t)||(r.craft.stopPropagation=function(){r.craft.blockedEvents[e]||(r.craft.blockedEvents[e]=[]),r.craft.blockedEvents[e].push(t)},n(r))};return t.addEventListener(e,i,r),function(){return t.removeEventListener(e,i,r)}},t.prototype.createConnectorsUsage=function(){var t=this,e=this.handlers(),n=new Set,r=!1,i=new Map;return{connectors:Object.entries(e).reduce((function(e,o){var a,s=o[0],c=o[1];return B(B({},e),((a={})[s]=function(e,o,a){var u=function(){var r=t.registry.register(e,{required:o,name:s,options:a,connector:c});return n.add(r.id),r};return r?u():i.set(t.registry.getConnectorId(e,s),u),e},a))}),{}),register:function(){r=!0,i.forEach((function(t){t()})),i.clear()},cleanup:function(){r=!1,n.forEach((function(e){return t.registry.remove(e)}))}}},t.prototype.derive=function(t,e){return new t(this,e)},t.prototype.createProxyHandlers=function(t,e){var n=[],r=t.handlers();return e(new Proxy(r,{get:function(t,e,i){return e in r==0?Reflect.get(t,e,i):function(t){for(var i=[],o=1;o<arguments.length;o++)i[o-1]=arguments[o];var a=r[e].apply(r,W([t],i));a&&n.push(a)}}})),function(){n.forEach((function(t){t()}))}},t.prototype.reflect=function(t){return this.createProxyHandlers(this,t)},t}(),rt=function(t){function e(e,n){var r=t.call(this,n)||this;return r.derived=e,r.options=n,r.unsubscribeParentHandlerListener=r.derived.listen((function(t){switch(t){case $.HandlerEnabled:return r.enable();case $.HandlerDisabled:return r.disable();default:return}})),r}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}Y(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(e,t),e.prototype.inherit=function(t){return this.createProxyHandlers(this.derived,t)},e.prototype.cleanup=function(){t.prototype.cleanup.call(this),this.unsubscribeParentHandlerListener()},e}(nt);function it(t,e){e&&("function"==typeof t?t(e):t.current=e)}function ot(t,e){var n=t.ref;return g("string"!=typeof n,"Cannot connect to an element with an existing string ref. Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. Read more: https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute"),f(t,n?{ref:function(t){it(n,t),it(e,t)}}:{ref:e})}function at(t){if("string"!=typeof t.type)throw new Error}function st(t){return function(e){void 0===e&&(e=null);for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];if(!d(e)){if(!e)return;var i=e;return i&&t.apply(void 0,W([i],n)),i}var o=e;return at(o),ot(o,t)}}function ct(t){return Object.keys(t).reduce((function(e,n){return e[n]=st((function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return t[n].apply(t,e)})),e}),{})}var ut=function(t){var e=t.parentDom,n=a.createElement("div",{style:B({position:"fixed",display:"block",opacity:1,borderStyle:"solid",borderWidth:"1px",borderColor:"transparent",zIndex:99999},t.style)});return e&&e.ownerDocument!==document?m.createPortal(n,e.ownerDocument.body):n},lt=function(t){l(t,[])},pt=function(t,e){var n="Deprecation warning: "+t+" will be deprecated in future relases.",r=e.suggest,i=e.doc;r&&(n+=" Please use "+r+" instead."),i&&(n+="("+i+")"),console.warn(n)},ft=function(){return"undefined"!=typeof window},dt=function(){return ft()&&/Linux/i.test(window.navigator.userAgent)},ht=function(){return ft()&&/Chrome/i.test(window.navigator.userAgent)};export{b as DEPRECATED_ROOT_NODE,rt as DerivedEventHandlers,M as ERROR_CANNOT_DRAG,_ as ERROR_DELETE_TOP_LEVEL_NODE,U as ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER,w as ERROR_DUPLICATE_NODEID,L as ERROR_INFINITE_CANVAS,O as ERROR_INVALID_NODEID,S as ERROR_INVALID_NODE_ID,T as ERROR_MISSING_PLACEHOLDER_PLACEMENT,C as ERROR_MOVE_CANNOT_DROP,I as ERROR_MOVE_INCOMING_PARENT,D as ERROR_MOVE_NONCANVAS_CHILD,H as ERROR_MOVE_OUTGOING_PARENT,N as ERROR_MOVE_ROOT_NODE,x as ERROR_MOVE_TOP_LEVEL_NODE,A as ERROR_MOVE_TO_DESCENDANT,P as ERROR_MOVE_TO_NONCANVAS_PARENT,E as ERROR_NOPARENT,k as ERROR_NOT_IN_RESOLVER,j as ERROR_RESOLVER_NOT_AN_OBJECT,R as ERROR_TOP_LEVEL_ELEMENT_NO_ID,q as ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT,G as ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT,$ as EventHandlerUpdates,nt as EventHandlers,z as HISTORY_ACTIONS,F as History,v as ROOT_NODE,ut as RenderIndicator,ot as cloneWithRef,K as createQuery,pt as deprecationWarning,X as getDOMInfo,tt as getRandomId,ht as isChromium,ft as isClientSide,dt as isLinux,Z as useCollector,lt as useEffectOnce,J as useMethods,ct as wrapConnectorHooks,st as wrapHookToRecognizeElement}; | ||
import t,{applyPatches as e,enableMapSet as n,enablePatches as r,produceWithPatches as i}from"immer";import o from"lodash/isEqualWith";import a,{useMemo as s,useRef as c,useCallback as u,useEffect as l,useState as p,cloneElement as f,isValidElement as d}from"react";import h from"shallowequal";import{nanoid as y}from"nanoid";import g from"tiny-invariant";import m from"react-dom";var v="ROOT",b="canvas-ROOT",E="Parent id cannot be ommited",w="Attempting to add a node with duplicated id",O="Node does not exist, it may have been removed",R='A <Element /> that is used inside a User Component must specify an `id` prop, eg: <Element id="text_element">...</Element> ',T="Placeholder required placement info (parent, index, or where) is missing",C="Node cannot be dropped into target parent",I="Target parent rejects incoming node",H="Current parent rejects outgoing node",D="Cannot move node that is not a direct child of a Canvas node",P="Cannot move node into a non-Canvas parent",x="A top-level Node cannot be moved",N="Root Node cannot be moved",A="Cannot move node into a descendant",k="The component type specified for this node (%node_type%) does not exist in the resolver",L="The component specified in the <Canvas> `is` prop has additional Canvas specified in it's render template.",M="The node has specified a canDrag() rule that prevents it from being dragged",S="Invalid parameter Node Id specified",_="Attempting to delete a top-level Node",j="Resolver in <Editor /> has to be an object. For (de)serialization Craft.js needs a list of all the User Components. \n \nMore info: https://craft.js.org/r/docs/api/editor#props",U="An Error occurred while deserializing components: Cannot find component <%displayName% /> in resolver map. Please check your resolver in <Editor />\n\nAvailable components in resolver: %availableComponents%\n\nMore info: https://craft.js.org/r/docs/api/editor#props",q="You can only use useEditor in the context of <Editor />. \n\nPlease only use useEditor in components that are children of the <Editor /> component.",G="You can only use useNode in the context of <Editor />. \n\nPlease only use useNode in components that are children of the <Editor /> component.",Y=function(t,e){return(Y=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)},B=function(){return(B=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function W(){for(var t=0,e=0,n=arguments.length;e<n;e++)t+=arguments[e].length;var r=Array(t),i=0;for(e=0;e<n;e++)for(var o=arguments[e],a=0,s=o.length;a<s;a++,i++)r[i]=o[a];return r}var z={UNDO:"HISTORY_UNDO",REDO:"HISTORY_REDO",THROTTLE:"HISTORY_THROTTLE",IGNORE:"HISTORY_IGNORE",MERGE:"HISTORY_MERGE",CLEAR:"HISTORY_CLEAR"},F=function(){function t(){this.timeline=[],this.pointer=-1}return t.prototype.add=function(t,e){0===t.length&&0===e.length||(this.pointer=this.pointer+1,this.timeline.length=this.pointer,this.timeline[this.pointer]={patches:t,inversePatches:e,timestamp:Date.now()})},t.prototype.throttleAdd=function(t,e,n){if(void 0===n&&(n=500),0!==t.length||0!==e.length){if(this.timeline.length&&this.pointer>=0){var r=this.timeline[this.pointer],i=r.patches,o=r.inversePatches,a=r.timestamp;if((new Date).getTime()-a<n)return void(this.timeline[this.pointer]={timestamp:a,patches:W(i,t),inversePatches:W(e,o)})}this.add(t,e)}},t.prototype.merge=function(t,e){if(0!==t.length||0!==e.length)if(this.timeline.length&&this.pointer>=0){var n=this.timeline[this.pointer],r=n.inversePatches;this.timeline[this.pointer]={timestamp:n.timestamp,patches:W(n.patches,t),inversePatches:W(e,r)}}else this.add(t,e)},t.prototype.clear=function(){this.timeline=[],this.pointer=-1},t.prototype.canUndo=function(){return this.pointer>=0},t.prototype.canRedo=function(){return this.pointer<this.timeline.length-1},t.prototype.undo=function(t){if(this.canUndo()){var n=this.timeline[this.pointer].inversePatches;return this.pointer=this.pointer-1,e(t,n)}},t.prototype.redo=function(t){if(this.canRedo())return this.pointer=this.pointer+1,e(t,this.timeline[this.pointer].patches)},t}();function J(e,n,r,o){var a,p=s((function(){return new F}),[]),f=c([]),d=c();"function"==typeof e?a=e:(a=e.methods,f.current=e.ignoreHistoryForActions,d.current=e.normalizeHistory);var h=c(o);h.current=o;var y=c(n),g=s((function(){var e=d.current,n=f.current,o=h.current;return function(s,c){var u,l=r&&K(r,(function(){return s}),p),f=i(s,(function(t){var e,n;switch(c.type){case z.UNDO:return p.undo(t);case z.REDO:return p.redo(t);case z.CLEAR:return p.clear(),B({},t);case z.IGNORE:case z.MERGE:case z.THROTTLE:var r=c.payload,i=r[0],o=r.slice(1);(e=a(t,l))[i].apply(e,o);break;default:(n=a(t,l))[c.type].apply(n,c.payload)}})),d=f[0],h=f[1],y=f[2];return u=d,o&&o(d,s,{type:c.type,params:c.payload,patches:h},l,(function(t){var e=i(d,t);u=e[0],h=W(h,e[1]),y=W(e[2],y)})),[z.UNDO,z.REDO].includes(c.type)&&e&&(u=t(u,e)),W(n,[z.UNDO,z.REDO,z.IGNORE,z.CLEAR]).includes(c.type)||(c.type===z.THROTTLE?p.throttleAdd(h,y,c.config&&c.config.rate):c.type===z.MERGE?p.merge(h,y):p.add(h,y)),u}}),[p,a,r]),m=u((function(){return y.current}),[]),v=s((function(){return new Q(m)}),[m]),b=u((function(t){var e=g(y.current,t);y.current=e,v.notify()}),[g,v]);l((function(){v.notify()}),[v]);var E=s((function(){return r?K(r,(function(){return y.current}),p):[]}),[p,r]),w=s((function(){var t=Object.keys(a(null,null)),e=f.current;return B(B({},t.reduce((function(t,e){return t[e]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return b({type:e,payload:t})},t}),{})),{history:{undo:function(){return b({type:z.UNDO})},redo:function(){return b({type:z.REDO})},clear:function(){return b({type:z.CLEAR})},throttle:function(n){return B({},t.filter((function(t){return!e.includes(t)})).reduce((function(t,e){return t[e]=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return b({type:z.THROTTLE,payload:W([e],t),config:{rate:n}})},t}),{}))},ignore:function(){return B({},t.filter((function(t){return!e.includes(t)})).reduce((function(t,e){return t[e]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return b({type:z.IGNORE,payload:W([e],t)})},t}),{}))},merge:function(){return B({},t.filter((function(t){return!e.includes(t)})).reduce((function(t,e){return t[e]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return b({type:z.MERGE,payload:W([e],t)})},t}),{}))}}})}),[b,a]);return s((function(){return{getState:m,subscribe:function(t,e,n){return v.subscribe(t,e,n)},actions:w,query:E,history:p}}),[w,E,v,m,p])}function K(t,e,n){var r=Object.keys(t()).reduce((function(n,r){var i;return B(B({},n),((i={})[r]=function(){for(var n,i=[],o=0;o<arguments.length;o++)i[o]=arguments[o];return(n=t(e()))[r].apply(n,i)},i))}),{});return B(B({},r),{history:{canUndo:function(){return n.canUndo()},canRedo:function(){return n.canRedo()}}})}n(),r();var Q=function(){function t(t){this.subscribers=[],this.getState=t}return t.prototype.subscribe=function(t,e,n){var r=this,i=new V((function(){return t(r.getState())}),e,n);return this.subscribers.push(i),this.unsubscribe.bind(this,i)},t.prototype.unsubscribe=function(t){if(this.subscribers.length){var e=this.subscribers.indexOf(t);if(e>-1)return this.subscribers.splice(e,1)}},t.prototype.notify=function(){this.subscribers.forEach((function(t){return t.collect()}))},t}(),V=function(){function t(t,e,n){void 0===n&&(n=!1),this.collector=t,this.onChange=e,n&&this.collect()}return t.prototype.collect=function(){try{var t=this.collector();o(t,this.collected)||(this.collected=t,this.onChange&&this.onChange(this.collected))}catch(t){console.warn(t)}},t}(),X=function(t){var e=t.getBoundingClientRect(),n=e.x,r=e.y,i=e.top,o=e.left,a=e.bottom,s=e.right,c=e.width,u=e.height,l=window.getComputedStyle(t),p={left:parseInt(l.marginLeft),right:parseInt(l.marginRight),bottom:parseInt(l.marginBottom),top:parseInt(l.marginTop)},f={left:parseInt(l.paddingLeft),right:parseInt(l.paddingRight),bottom:parseInt(l.paddingBottom),top:parseInt(l.paddingTop)};return{x:n,y:r,top:i,left:o,bottom:a,right:s,width:c,height:u,outerWidth:Math.round(c+p.left+p.right),outerHeight:Math.round(u+p.top+p.bottom),margin:p,padding:f,inFlow:t.parentElement&&!!function(e){var n=getComputedStyle(e);if(!(l.overflow&&"visible"!==l.overflow||"none"!==n.float||"grid"===n.display||"flex"===n.display&&"column"!==n["flex-direction"])){switch(l.position){case"static":case"relative":break;default:return}switch(t.tagName){case"TR":case"TBODY":case"THEAD":case"TFOOT":return!0}switch(l.display){case"block":case"list-item":case"table":case"flex":case"grid":return!0}}}(t.parentElement)}};function Z(t,e){var n=t.subscribe,r=t.getState,i=t.actions,o=t.query,a=c(!0),s=c(null),f=c(e);f.current=e;var d=u((function(t){return B(B({},t),{actions:i,query:o})}),[i,o]);a.current&&e&&(s.current=e(r(),o),a.current=!1);var h=p(d(s.current)),y=h[0],g=h[1];return l((function(){var t;return f.current&&(t=n((function(t){return f.current(t,o)}),(function(t){g(d(t))}))),function(){t&&t()}}),[d,o,n]),y}var $,tt=function(t){return void 0===t&&(t=10),y(t)},et=function(){function t(){this.isEnabled=!0,this.elementIdMap=new WeakMap,this.registry=new Map}return t.prototype.getElementId=function(t){var e=this.elementIdMap.get(t);if(e)return e;var n=tt();return this.elementIdMap.set(t,n),n},t.prototype.getConnectorId=function(t,e){return e+"--"+this.getElementId(t)},t.prototype.register=function(t,e){var n=this,r=this.getByElement(t,e.name);if(r){if(h(e.required,r.required))return r;this.getByElement(t,e.name).disable()}var i=null,o=this.getConnectorId(t,e.name);return this.registry.set(o,{id:o,required:e.required,enable:function(){i&&i(),i=e.connector(t,e.required,e.options)},disable:function(){i&&i()},remove:function(){return n.remove(o)}}),this.isEnabled&&this.registry.get(o).enable(),this.registry.get(o)},t.prototype.get=function(t){return this.registry.get(t)},t.prototype.remove=function(t){var e=this.get(t);e&&(e.disable(),this.registry.delete(e.id))},t.prototype.enable=function(){this.isEnabled=!0,this.registry.forEach((function(t){t.enable()}))},t.prototype.disable=function(){this.isEnabled=!1,this.registry.forEach((function(t){t.disable()}))},t.prototype.getByElement=function(t,e){return this.get(this.getConnectorId(t,e))},t.prototype.removeByElement=function(t,e){return this.remove(this.getConnectorId(t,e))},t.prototype.clear=function(){this.disable(),this.elementIdMap=new WeakMap,this.registry=new Map},t}();!function(t){t[t.HandlerDisabled=0]="HandlerDisabled",t[t.HandlerEnabled=1]="HandlerEnabled"}($||($={}));var nt=function(){function t(t){this.registry=new et,this.subscribers=new Set,this.options=t}return t.prototype.listen=function(t){var e=this;return this.subscribers.add(t),function(){return e.subscribers.delete(t)}},t.prototype.disable=function(){this.onDisable&&this.onDisable(),this.registry.disable(),this.subscribers.forEach((function(t){t($.HandlerDisabled)}))},t.prototype.enable=function(){this.onEnable&&this.onEnable(),this.registry.enable(),this.subscribers.forEach((function(t){t($.HandlerEnabled)}))},t.prototype.cleanup=function(){this.disable(),this.subscribers.clear(),this.registry.clear()},t.prototype.addCraftEventListener=function(t,e,n,r){var i=function(r){(function(t,e,n){t.craft||(t.craft={stopPropagation:function(){},blockedEvents:{}});for(var r=t.craft&&t.craft.blockedEvents[e]||[],i=0;i<r.length;i++){var o=r[i];if(n!==o&&n.contains(o))return!0}return!1})(r,e,t)||(r.craft.stopPropagation=function(){r.craft.blockedEvents[e]||(r.craft.blockedEvents[e]=[]),r.craft.blockedEvents[e].push(t)},n(r))};return t.addEventListener(e,i,r),function(){return t.removeEventListener(e,i,r)}},t.prototype.createConnectorsUsage=function(){var t=this,e=this.handlers(),n=new Set,r=!1,i=new Map;return{connectors:Object.entries(e).reduce((function(e,o){var a,s=o[0],c=o[1];return B(B({},e),((a={})[s]=function(e,o,a){var u=function(){var r=t.registry.register(e,{required:o,name:s,options:a,connector:c});return n.add(r.id),r};return i.set(t.registry.getConnectorId(e,s),u),r&&u(),e},a))}),{}),register:function(){r=!0,i.forEach((function(t){t()}))},cleanup:function(){r=!1,n.forEach((function(e){return t.registry.remove(e)}))}}},t.prototype.derive=function(t,e){return new t(this,e)},t.prototype.createProxyHandlers=function(t,e){var n=[],r=t.handlers();return e(new Proxy(r,{get:function(t,e,i){return e in r==0?Reflect.get(t,e,i):function(t){for(var i=[],o=1;o<arguments.length;o++)i[o-1]=arguments[o];var a=r[e].apply(r,W([t],i));a&&n.push(a)}}})),function(){n.forEach((function(t){t()}))}},t.prototype.reflect=function(t){return this.createProxyHandlers(this,t)},t}(),rt=function(t){function e(e,n){var r=t.call(this,n)||this;return r.derived=e,r.options=n,r.unsubscribeParentHandlerListener=r.derived.listen((function(t){switch(t){case $.HandlerEnabled:return r.enable();case $.HandlerDisabled:return r.disable();default:return}})),r}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}Y(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(e,t),e.prototype.inherit=function(t){return this.createProxyHandlers(this.derived,t)},e.prototype.cleanup=function(){t.prototype.cleanup.call(this),this.unsubscribeParentHandlerListener()},e}(nt);function it(t,e){e&&("function"==typeof t?t(e):t.current=e)}function ot(t,e){var n=t.ref;return g("string"!=typeof n,"Cannot connect to an element with an existing string ref. Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. Read more: https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute"),f(t,n?{ref:function(t){it(n,t),it(e,t)}}:{ref:e})}function at(t){if("string"!=typeof t.type)throw new Error}function st(t){return function(e){void 0===e&&(e=null);for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];if(!d(e)){if(!e)return;var i=e;return i&&t.apply(void 0,W([i],n)),i}var o=e;return at(o),ot(o,t)}}function ct(t){return Object.keys(t).reduce((function(e,n){return e[n]=st((function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return t[n].apply(t,e)})),e}),{})}var ut=function(t){var e=t.parentDom,n=a.createElement("div",{style:B({position:"fixed",display:"block",opacity:1,borderStyle:"solid",borderWidth:"1px",borderColor:"transparent",zIndex:99999},t.style)});return e&&e.ownerDocument!==document?m.createPortal(n,e.ownerDocument.body):n},lt=function(t){l(t,[])},pt=function(t,e){var n="Deprecation warning: "+t+" will be deprecated in future relases.",r=e.suggest,i=e.doc;r&&(n+=" Please use "+r+" instead."),i&&(n+="("+i+")"),console.warn(n)},ft=function(){return"undefined"!=typeof window},dt=function(){return ft()&&/Linux/i.test(window.navigator.userAgent)},ht=function(){return ft()&&/Chrome/i.test(window.navigator.userAgent)};export{b as DEPRECATED_ROOT_NODE,rt as DerivedEventHandlers,M as ERROR_CANNOT_DRAG,_ as ERROR_DELETE_TOP_LEVEL_NODE,U as ERROR_DESERIALIZE_COMPONENT_NOT_IN_RESOLVER,w as ERROR_DUPLICATE_NODEID,L as ERROR_INFINITE_CANVAS,O as ERROR_INVALID_NODEID,S as ERROR_INVALID_NODE_ID,T as ERROR_MISSING_PLACEHOLDER_PLACEMENT,C as ERROR_MOVE_CANNOT_DROP,I as ERROR_MOVE_INCOMING_PARENT,D as ERROR_MOVE_NONCANVAS_CHILD,H as ERROR_MOVE_OUTGOING_PARENT,N as ERROR_MOVE_ROOT_NODE,x as ERROR_MOVE_TOP_LEVEL_NODE,A as ERROR_MOVE_TO_DESCENDANT,P as ERROR_MOVE_TO_NONCANVAS_PARENT,E as ERROR_NOPARENT,k as ERROR_NOT_IN_RESOLVER,j as ERROR_RESOLVER_NOT_AN_OBJECT,R as ERROR_TOP_LEVEL_ELEMENT_NO_ID,q as ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT,G as ERROR_USE_NODE_OUTSIDE_OF_EDITOR_CONTEXT,$ as EventHandlerUpdates,nt as EventHandlers,z as HISTORY_ACTIONS,F as History,v as ROOT_NODE,ut as RenderIndicator,ot as cloneWithRef,K as createQuery,pt as deprecationWarning,X as getDOMInfo,tt as getRandomId,ht as isChromium,ft as isClientSide,dt as isLinux,Z as useCollector,lt as useEffectOnce,J as useMethods,ct as wrapConnectorHooks,st as wrapHookToRecognizeElement}; |
{ | ||
"name": "@craftjs/utils", | ||
"description": "Utilities used internally across the craft.js monorepo", | ||
"version": "0.2.0-beta.7", | ||
"version": "0.2.0-beta.8", | ||
"author": "Prev Wong <prevwong@gmail.com>", | ||
@@ -33,3 +33,3 @@ "license": "MIT", | ||
}, | ||
"gitHead": "eb1c14c0c8e1d6072d15b6c21833972f00423ccd" | ||
"gitHead": "8fd5136051badffe75ba7de35f8f9279641b0b31" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
469556