grapesjs-click
Version:
GrapesJS click plugin (no more drag-and-drop)
2 lines (1 loc) • 5.37 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const S="grapesjs-click",c="[GrapesJS::ClickPlugin]",y="click",E=`${y}:grab-block`,f=`${y}:drop-block`,B=`${y}:grab-component`,u=`${y}:drop-component`;function m(o,n){try{const t=o.Canvas.getBody();if(!t)throw new Error('"bodyElement" is empty');t.style.cursor=n}catch(e){const{message:t}=e;console.warn(`${c} changeBodyCursor - ${t}`)}}function M(o,n){var e;try{const r=o.Components.getWrapper();if(!r)throw new Error('"wrapperComponent" is empty');const s=(e=r.find(`#${n}`))==null?void 0:e[0];if(!s)throw new Error('"component" is empty');return s}catch(t){const{message:r}=t;console.warn(`${c} getComponentById - ${r}`)}}function G(o){try{const{droppable:n}=o.props();if(n!==!1)return o;const e=o.parent();return e?G(e):void 0}catch(n){const{message:e}=n;console.warn(`${c} getDroppableComponent - ${e}`)}}function D(o,n,e=500){const t=window.setTimeout(()=>{o.select(n),window.clearTimeout(t)},e)}const h=`${S}-block`;function T(){return window.sessionStorage.getItem(h)}function $(o,n){window.sessionStorage.removeItem(h),o.trigger("block:drag:stop",n),m(o,"auto")}function L(o,n,e){try{const{isDebugging:t,id:r}=e;if(t&&console.log(`${c} onGrabBlock`,{editor:o,sender:n,options:e}),!r)throw new Error('"blockId" is required');const i=o.Blocks.get(r);if(!i)throw new Error('"block" is empty');const{disable:a}=i.attributes;if(a)throw new Error('"block" is disable');window.sessionStorage.setItem(h,r),o.trigger(E,i),m(o,"grabbing");const p=o.getSelected();if(!p)return;o.selectRemove(p)}catch(t){const{message:r}=t;console.warn(`${c} onGrabBlock - ${r}`),$(o)}}function W(o,n,e){var t;try{const{isDebugging:r,id:s}=e;r&&console.log(`${c} onDropBlock`,{editor:o,sender:n,options:e});const a=window.sessionStorage.getItem(h)||s;if(!a)throw new Error('"blockId" is required');const l=o.Blocks.get(a);if(!l)throw new Error('"block" is empty');const b=o.getSelected()||o.getWrapper();if(!b)throw new Error('"selectedComponent" is empty');const w=G(b);if(!w)throw new Error('"droppableComponent" is empty');const d=l.getContent();if(!d)throw new Error('"component" is empty');const g=(t=w.append(d))==null?void 0:t[0];g&&D(o,g),o.trigger(f,l,g),$(o,l)}catch(r){const{message:s}=r;console.warn(`${c} onDropBlock - ${s}`),$(o)}}const C=`${S}-component`;function j(){return window.sessionStorage.getItem(C)}function k(o,n){window.sessionStorage.removeItem(C),o.trigger("component:drag:stop",n),m(o,"auto")}function z(o,n,e){try{const{isDebugging:t,id:r}=e;t&&console.log(`${c} onGrabComponent`,{editor:o,sender:n,options:e});const s=o.getSelected(),i=r||(s==null?void 0:s.getId());if(!i)throw new Error('"componentId" is required');const a=M(o,i);if(!a)throw new Error('"component" is empty');const{draggable:p}=a.props();if(p===!1)throw new Error('"component" is not draggable');window.sessionStorage.setItem(C,i),o.trigger(B,a),m(o,"grabbing")}catch(t){const{message:r}=t;console.warn(`${c} onGrabComponent - ${r}`),k(o)}}function A(o,n,e){var t;try{const{isDebugging:r,id:s}=e;r&&console.log(`${c} onDropComponent`,{editor:o,sender:n,options:e});const a=window.sessionStorage.getItem(C)||s;if(!a)throw new Error('"componentId" is required');const p=M(o,a);if(!p)throw new Error('"component" is empty');const{draggable:l,copyable:b,removable:w}=p.props();if(l===!1||b===!1||w===!1)throw new Error('"component" is not draggable, copyable or removable');const d=o.getSelected()||o.getWrapper();if(!d)throw new Error('"selectedComponent" is empty');const g=G(d);if(!g)throw new Error('"droppableComponent" is empty');const H=p.clone(),I=(t=g.append(H))==null?void 0:t[0];I&&(p.remove(),D(o,I)),o.trigger(u,I),k(o,p)}catch(r){const{message:s}=r;console.warn(`${c} onDropComponent - ${s}`),k(o)}}function v(o){return(o==null?void 0:o.isConnected)===!0}const R=24;function x(o,n){return n.isTrusted?{x:n.clientX,y:n.clientY}:o.Canvas.getMouseRelativeCanvas(n,{})}function P(o,n,e=R){return t=>{try{if(!o)throw new Error("No editor");if(!n||!v(n))throw new Error("No element");const r=x(o,t),s=n.clientHeight/1.3,i=r.x+e,a=r.y+s;n.style.top=`${a}px`,n.style.left=`${i}px`}catch(r){const{message:s}=r;console.warn(`${c} onMouseMove - ${s}`)}}}function N(o,n){try{if(!o||!v(o))throw new Error("No element");const e=window.getComputedStyle(o);e.display==="none"&&(o.style.display="block"),e.position!=="fixed"&&(o.style.position="fixed"),["0","auto"].includes(e.zIndex)&&(o.style.zIndex="99999")}catch(e){const{message:t}=e;console.warn(`${c} showGrabbedInfo - ${t}`)}finally{if(!n)return;window.addEventListener("mousemove",n)}}function q(o,n){try{if(!o||!v(o))throw new Error("No element");o.style.display!=="none"&&(o.style.display="none")}catch(e){const{message:t}=e;console.warn(`${c} hideGrabbedInfo - ${t}`)}finally{if(!n)return;window.removeEventListener("mousemove",n)}}function _(o,n){const e=P(o,n),t=()=>N(n,e),r=()=>q(n,e);o.on(`run:${E}`,t),o.on(`run:${f}`,r),o.on(`run:${B}`,t),o.on(`run:${u}`,r)}const J=(o,n={})=>{const{hasAutoDropped:e=!0}=n,t=o.Commands;t.add(E,L),t.add(f,W),t.add(B,z),t.add(u,A),e&&o.on("component:selected",()=>{if(T()){t.run(f);return}j()&&t.run(u)})};exports.changeBodyCursor=m;exports.default=J;exports.getMouseListener=P;exports.getMousePosition=x;exports.hideGrabbedInfo=q;exports.initGrabbedInfo=_;exports.showGrabbedInfo=N;