@lyonbot/interactive-blocks
Version:
Make interactive selectable, drag-and-drop, copy-and-paste ready, Block and Slot components easily! Works with Vue, React and any MV* framework.
3 lines (2 loc) • 23.2 kB
JavaScript
function t(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function e(e,i,n){return i&&t(e.prototype,i),n&&t(e,n),e}function i(){return i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n])}return t},i.apply(this,arguments)}function n(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,o(t,e)}function o(t,e){return o=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},o(t,e)}function r(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var i=0,n=new Array(e);i<e;i++)n[i]=t[i];return n}function c(t,e){var i="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(i)return(i=i.call(t)).next.bind(i);if(Array.isArray(t)||(i=function(t,e){if(t){if("string"==typeof t)return s(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?s(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){i&&(t=i);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a=function(t,e,i){var n=t._listeners;n[e]?n[e].push(i):n[e]=[i]},l=function(){function t(){this._listeners={}}var e=t.prototype;return e.on=function(t,e){return a(this,t,{listener:e}),this},e.once=function(t,e){return a(this,t,{listener:e,once:!0}),this},e.off=function(t,e){var i=this._listeners[t];if(!i)return this;if(1===i.length&&e===i[0].listener)return this._listeners[t]=null,this;var n=i.findIndex(function(t){return t.listener===e});return-1!==n&&i.splice(n,1),this},e.emit=function(t){var e=arguments,i=this._listeners[t];if(i){var n=!1;i.forEach(function(t){t.listener([].slice.call(e,1)),t.once&&(n=!0)}),n&&(this._listeners[t]=i.filter(function(t){return!t.once}))}},e.removeAllListeners=function(){return this._listeners={},this},t}();function u(t,e){return!("object"!=typeof t||!t||!0!==t.isIBClipboardData||"string"==typeof e&&e!==t.ibContextBrand||"string"!=typeof t.ibContextBrand||"ibContextUUID"in t&&"string"!=typeof t.ibContextUUID||!Array.isArray(t.blocksData)||t.blocksData.some(function(t){return"object"!=typeof t||null===t}))}var h=function(){return function(t){var e=this;Object.assign(this,t),this.returnValue=!0,this.preventDefault=function(){e.returnValue=!1}}},v=h(),f=h(),d=h(),g=h(),p=h(),S=h(),k=function(t,e){return"function"==typeof t?t(e):t};function m(t,e){var i,n;function o(e){void 0===e&&(e=!1),i&&(e&&n&&t.apply(void 0,n),clearTimeout(i),i=null,n=void 0)}var r=function(){n=[].slice.call(arguments),i||(i=setTimeout(function(){return o(!0)},e))};return r.cancel=function(){return o(!1)},r.flush=function(){return o(!0)},r}function x(t,e){var i=e.map(function(e){return t[e]}).filter(function(t){return void 0!==t});return[].concat(e).sort(function(t,e){return e-t}).forEach(function(e){return t.splice(e,1)}),i}function B(t,e,i){var n=x(t,e);t.splice.apply(t,[i,0].concat(n))}function y(t,e,i,n){var o=x(t,e);i.splice.apply(i,[n,0].concat(o))}function b(t,e){if("camelCase"===e)return t;var i={};return Object.keys(t).forEach(function(n){var o,r=n;"react"===e?r="on"+(null==(o=r[0])?void 0:o.toUpperCase())+r.slice(1):"lowercase"!==e&&"vue"!==e&&e||(r=r.toLowerCase()),i[r]=t[n]}),i}var A=function(){var t=i.prototype;function i(t,e,i){var n=this;this.type="block",this.ctx=void 0,this.ownerSlot=void 0,this.slots=new Set,this.info=void 0,this._activeNumber=!1,this.handlePointerUp=function(t){n.ctx.handleBlockPointerUp(n,t)},this.ctx=t,this.ownerSlot=e,e&&e.items.add(this),this.info=i}return t._maybeUpdateActiveNumber=function(t){var e,i;return this._activeNumber!==t&&(this._activeNumber=t,null==(e=(i=this.info).onStatusChange)||e.call(i,this),!0)},t.select=function(t){this.ctx.addBlockToSelection(this,t)},t.unselect=function(){this.isActive&&(this.ctx.activeBlocks.delete(this),this.ctx.syncActiveElementStatus())},t.focus=function(t){this.isActive||this.select(t),this.ctx.focus()},t.createSlot=function(t){return this.ctx.createSlot(t,this)},t.getDOMEvents=function(t,e){void 0===e&&(e={});var i={pointerUp:this.handlePointerUp};return e.draggable&&Object.assign(i,this.ctx.dragging.getBlockDOMEventHandlers(this)),b(i,t)},t.dispose=function(){var t;this.ctx.activeBlocks.has(this)&&(this.ctx.activeBlocks.delete(this),this.ctx.syncActiveElementStatus()),null==(t=this.ownerSlot)||t.items.delete(this),this.slots.forEach(function(t){return t.dispose()}),this.slots.clear(),this.info=D},e(i,[{key:"activeNumber",get:function(){return this._activeNumber}},{key:"isActive",get:function(){return!1!==this._activeNumber}},{key:"hasFocus",get:function(){return this.isActive&&this.ctx.hasFocus}},{key:"ref",get:function(){return this.info.ref}},{key:"index",get:function(){return k(this.info.index,this)}},{key:"data",get:function(){return k(this.info.data,this)}}]),i}(),D={index:function(){throw new Error("Accessing a disposed block")},data:function(){throw new Error("Accessing a disposed block")}};function w(t,e){if(t)for(var i,n=0,o=c(t);!(i=o()).done;){var r=i.value;if(e(r,n))return r;n+=1}}function E(t){if(t)return t[Symbol.iterator]().next().value}var C="webkitRequestAnimationFrame"in window,T=document.createElement("div");T.style.cssText="position:fixed;pointer-events:none;left:0;top:0;background: #FFF; border: 1px solid #000; padding: 4px 8px;transform:translate(-50%, -50%)";var O=function(t){function e(e){var i;return(i=t.call(this)||this).ctx=void 0,i.draggingBlocks=void 0,i.slotOfDraggingBlocks=void 0,i.isHovering=!1,i.hoveringSlot=void 0,i.hoveringBlock=void 0,i.dropEffect=void 0,i.setHoveringSlot=m(i._originalSetHoveringSlot.bind(r(i)),50),i.ctx=e,i}n(e,t);var i=e.prototype;return i.dispose=function(){this.removeAllListeners(),this.setHoveringSlot.cancel()},i._originalSetHoveringSlot=function(t,e){var i;if(t&&"number"!=typeof e)throw new Error("Cannot setHoveringSlot with indexToDrop unknown");if(t!==this.hoveringSlot){var n=this.hoveringSlot;(null==(i=this.hoveringBlock)?void 0:i.ownerSlot)!==t&&(this.hoveringBlock=void 0),this.hoveringSlot=t,this.isHovering=!!t,n&&(n.isDragHovering=!1,n.indexToDrop=void 0,null==n.info.onDragHoverStatusChange||n.info.onDragHoverStatusChange(this.ctx)),t&&(t.isDragHovering=!0,t.indexToDrop=e,null==t.info.onDragHoverStatusChange||t.info.onDragHoverStatusChange(this.ctx)),this.emit("hoverChanged",this.ctx)}else t&&t.indexToDrop!==e&&(t.indexToDrop=e,null==t.info.onDragHoverStatusChange||t.info.onDragHoverStatusChange(this.ctx),this.emit("hoverChanged",this.ctx))},i.getBlockDOMEventHandlers=function(t){return{dragStart:this.handleBlockDragStart.bind(this,t),dragEnd:this.handleBlockDragEnd.bind(this,t),dragOver:this.handleBlockDragOver.bind(this,t),dragLeave:this.handleBlockDragLeave.bind(this,t)}},i.handleBlockDragStart=function(t,e){var i=e.dataTransfer;if(i){this.ctx.activeBlocks.has(t)||this.ctx.addBlockToSelection(t,"none");var n=this.ctx.options.serializeForClipboard(this.ctx.dumpSelectedData());if(n){var o=Array.from(this.ctx.activeBlocks),r=new p({type:"blockDragStart",text:n,blocks:o,ctx:this.ctx,currentBlock:t,dataTransfer:i,event:e});if(null==t.info.onDragStart||t.info.onDragStart(r),this.emit("blockDragStart",r),!1!==r.returnValue){e.stopPropagation(),i.setData("text/plain",r.text),i.setData("x-block-context/uuid",this.ctx.uuid),i.setData("x-block-context/brand-"+this.ctx.brand,"true"),o.length>1&&(document.body.appendChild(T),T.style.left=e.clientX+"px",T.style.top=e.clientY+"px",setTimeout(function(){return T.remove()},100),T.textContent=o.length+" items",i.setDragImage(T,T.offsetWidth/2,T.offsetHeight/2));var s=this.ctx.slotOfActiveBlocks;this.draggingBlocks=o,this.slotOfDraggingBlocks=s||void 0,s&&(this.setHoveringSlot(s,t.index),this.setHoveringSlot.flush())}}}},i.handleBlockDragEnd=function(){this.slotOfDraggingBlocks=void 0,this.draggingBlocks=void 0,this.setHoveringSlot(void 0)},i.handleBlockDragOver=function(t){this.hoveringBlock!==t&&t.ownerSlot==this.hoveringSlot&&(this.hoveringBlock=t,this.emit("hoverChanged",this.ctx))},i.handleBlockDragLeave=function(t){this.hoveringBlock===t&&(this.hoveringBlock=void 0,this.emit("hoverChanged",this.ctx))},i.getSlotDOMEventHandlers=function(t){var e=this;return{dragOver:function(i){e.handleSlotDragOver(t,i)&&(i.preventDefault(),i.stopPropagation())},dragLeave:function(i){e.handleSlotDragLeave(t),i.preventDefault(),i.stopPropagation()},drop:function(i){e.handleSlotDrop(t,i),i.preventDefault(),i.stopPropagation()}}},i.handleSlotDragOver=function(t,e){var i;if(null!=(i=e.dataTransfer)&&i.types.includes("x-block-context/uuid")&&!e.dataTransfer.types.includes("x-block-context/brand-"+this.ctx.brand))return!1;var n=this.computeIndexToDrop(t,e);return!1!==n&&(this.setHoveringSlot(t,n),!0)},i.handleSlotDrop=function(t,e){var i=this.computeIndexToDrop(t,e);if(!1!==i){var n=this.dropEffect,o=this.draggingBlocks,r=this.ctx,s=new S({type:"slotBeforeDrop",ctx:r,slot:t,dropEffect:n,dataTransfer:e.dataTransfer,event:e,indexToDrop:i,isDraggingFromCurrentCtx:!!o,draggingBlocks:o});if(this.emit("slotBeforeDrop",s),!1===s.returnValue)return this.setHoveringSlot(void 0),void this.setHoveringSlot.flush();if(this.setHoveringSlot(t,i),this.setHoveringSlot.flush(),o&&"copy"!==n)if(t===r.slotOfActiveBlocks){var c=i-o.filter(function(t){return t.index<i}).length,a=new d({type:"moveInSlot",blocks:o,ctx:r,slot:t,index:c});if(null==t.info.onMoveInSlot||t.info.onMoveInSlot(a),r.emit("moveInSlot",a),!a.returnValue)return;setTimeout(function(){r.activeSlot=t,r.activeBlocks.clear(),t.items.forEach(function(t){var e=t.index;e>=c&&e<c+o.length&&r.activeBlocks.add(t)}),r.syncActiveElementStatus(),r.focus()},100)}else{var l=i,u=new g({type:"moveBetweenSlots",blocks:o,ctx:r,fromSlot:r.slotOfActiveBlocks,toSlot:t,index:l});if(null==t.info.onMoveToThisSlot||t.info.onMoveToThisSlot(u),r.emit("moveBetweenSlots",u),!u.returnValue)return;setTimeout(function(){r.activeSlot=t,r.activeBlocks.clear(),t.items.forEach(function(t){var e=t.index;e>=l&&e<l+o.length&&r.activeBlocks.add(t)}),r.syncActiveElementStatus(),r.focus()},100)}else try{var h=this.ctx.options.unserializeForClipboard(e.dataTransfer.getData("text/plain"));r.activeSlot=t,r.activeBlocks.clear(),r.syncActiveElementStatus(),r.pasteWithData(h,i),r.focus()}catch(t){console.error("Failed to drop")}this.setHoveringSlot(void 0)}},i.handleSlotDragLeave=function(t){this.setHoveringSlot(void 0)},i.computeIndexToDrop=function(t,e){var i,n=e.dataTransfer.dropEffect;if("none"===n&&C&&(n=e.ctrlKey||e.altKey?"copy":"move"),this.dropEffect=n,"copy"!==this.dropEffect&&null!=(i=this.draggingBlocks)&&i.some(function(e){return t.isDescendantOfBlock(e)}))return!1;var o,r,s=null==t.info.computeIndexToDrop?void 0:t.info.computeIndexToDrop({ctx:this.ctx,slot:t,isDraggingFromCurrentCtx:!!this.draggingBlocks,draggingBlocks:this.draggingBlocks,dataTransfer:e.dataTransfer,dropEffect:this.dropEffect,currentTarget:e.currentTarget,clientX:e.clientX,clientY:e.clientY,offsetX:e.offsetX,offsetY:e.offsetY});return!1!==s&&(void 0===s?null!=(o=null==(r=this.hoveringBlock)?void 0:r.index)?o:function(t,e,i){if(!t)return 0;for(var n,o=0,r=c(t);!(n=r()).done;)o=Math.max(o,n.value.index+1);return o}(t.items):s)},e}(l),H=function(){function t(t,e,i){var n=this;this.type="slot",this.ctx=void 0,this.ownerBlock=void 0,this.items=new Set,this.info=void 0,this.handlePointerUp=function(t){n.ctx.handleSlotPointerUp(n,t)},this._isActive=!1,this.ctx=t,this.ownerBlock=e,e&&e.slots.add(this),this.info=i}var i=t.prototype;return i.createBlock=function(t){return this.ctx.createBlock(t,this)},i._maybeUpdateActive=function(t){var e,i;return this._isActive!==t&&(this._isActive=t,null==(e=(i=this.info).onStatusChange)||e.call(i,this),!0)},i.select=function(){this.ctx.activeSlot=this,this.ctx.activeBlocks=this.ctx.options.multipleSelect?new Set(this.items):this.items.size?new Set([E(this.items)]):new Set,this.ctx.syncActiveElementStatus()},i.focus=function(){this.isActive||(this.ctx.activeBlocks.clear(),this.ctx.activeSlot=this,this.ctx.syncActiveElementStatus()),this.ctx.focus()},i.isDescendantOfBlock=function(t){for(var e=this.ownerBlock;e;){var i;if(e===t)return!0;e=null==(i=e.ownerSlot)?void 0:i.ownerBlock}return!1},i.isDescendantOfSlot=function(t){for(var e,i=null==(e=this.ownerBlock)?void 0:e.ownerSlot;i;){var n;if(i===t)return!0;i=null==(n=i.ownerBlock)?void 0:n.ownerSlot}return!1},i.getDOMEvents=function(t,e){void 0===e&&(e={});var i={pointerUp:this.handlePointerUp};return e.draggable&&Object.assign(i,this.ctx.dragging.getSlotDOMEventHandlers(this)),b(i,t)},i.dispose=function(){var t,e=!1;this.ctx.slotOfActiveBlocks===this&&(this.ctx.activeBlocks.clear(),e=!0),this.ctx.activeSlot===this&&(this.ctx.activeSlot=null,e=!0),e&&this.ctx.syncActiveElementStatus(),null==(t=this.ownerBlock)||t.slots.delete(this),this.items.forEach(function(t){return t.dispose()}),this.items.clear(),this.info={}},e(t,[{key:"ref",get:function(){return this.info.ref}},{key:"isActive",get:function(){return this._isActive}},{key:"hasFocus",get:function(){return this.isActive&&this.ctx.hasFocus}}]),t}(),_={brand:"",deactivateHandlersWhenBlur:!0,navigateWithArrowKeys:!0,handleDeleteKey:!0,multipleSelect:!0,serializeForClipboard:function(t){return JSON.stringify(t)},unserializeForClipboard:function(t){return JSON.parse(t)}},I=function(t){function e(e){var n;void 0===e&&(e={}),(n=t.call(this)||this).hiddenInput=void 0,n._lastActiveElement=null,n.hasFocus=!1,n.options=void 0,n.brand="",n.uuid=Date.now().toString(36)+"-"+Math.random().toString(36),n.dragging=new O(r(n)),n.activeBlocks=new Set,n.slotOfActiveBlocks=null,n.activeSlot=null,n.lastActiveSlot=null,n.lastActiveBlocks=void 0,n.isFocusingBlock=void 0,n.isFocusingSlot=void 0,n.focusingElement=void 0,n.handleSlotPointerUp=function(t,e){(!e||e.eventPhase!==Event.CAPTURING_PHASE)&&n.isFocusingSlot||(n.isFocusingSlot=t,n.focusingElement||(n.focusingElement=null==e?void 0:e.currentTarget))},n.handleBlockPointerUp=function(t,e){(!e||e.eventPhase!==Event.CAPTURING_PHASE)&&n.isFocusingBlock||(n.isFocusingBlock=t,n.focusingElement||(n.focusingElement=null==e?void 0:e.currentTarget))},n.handleGlobalPointerUp=function(t){var e=n.isFocusingBlock,i=n.isFocusingSlot,o=n.focusingElement;if(n.isFocusingBlock=void 0,n.isFocusingSlot=void 0,n.focusingElement=void 0,e?(n.activeSlot=i||null,n.addBlockToSelection(e,t)):(n.activeSlot=i||null,n.options.deactivateHandlersWhenBlur&&n.activeBlocks.size>0&&n.activeBlocks.clear()),n.syncActiveElementStatus(),o){var r=null==o.getRootNode?void 0:o.getRootNode();(null==r?void 0:r.activeElement)===o&&n.focus()}},n.options=i({},_,e),n.brand=String(n.options.brand),document.addEventListener("pointerup",n.handleGlobalPointerUp,!1);var o=n.hiddenInput=document.createElement("textarea");o.style.cssText="opacity:0;left:0;top:0;position:fixed;width:2px;height:2px",o.tabIndex=-1,o.inputMode="none",o.ownerDocument.body.appendChild(o);var s=function(t){var e,i=n.options.serializeForClipboard(n.dumpSelectedData());i&&(t.preventDefault(),null==(e=t.clipboardData)||e.setData("text/plain",i))};return o.addEventListener("copy",function(t){s(t)},!1),o.addEventListener("cut",function(t){s(t),n.deleteActiveBlocks()},!1),o.addEventListener("paste",function(t){t.preventDefault();try{var e,i=null==(e=t.clipboardData)?void 0:e.getData("text/plain");if(!i)return;var o=n.options.unserializeForClipboard(i);n.pasteWithData(o)}catch(t){console.error("Failed to paste!",t)}},!1),o.addEventListener("focus",function(){var t;n.hasFocus=!0,n.emit("focus",r(n)),n.activeBlocks.forEach(function(t){return null==t.info.onStatusChange?void 0:t.info.onStatusChange(t)}),null==(t=n.activeSlot)||null==t.info.onStatusChange||t.info.onStatusChange(n.activeSlot)},!1),o.addEventListener("blur",function(){var t;n.hasFocus=!1,n._lastActiveElement=null,n.emit("blur",r(n)),n.activeBlocks.forEach(function(t){return null==t.info.onStatusChange?void 0:t.info.onStatusChange(t)}),null==(t=n.activeSlot)||null==t.info.onStatusChange||t.info.onStatusChange(n.activeSlot)},!1),o.addEventListener("keydown",function(t){var e=n.options;switch(t.code){case"KeyA":var i,o;e.multipleSelect&&(t.ctrlKey||t.metaKey)&&(n.activeBlocks=new Set(Array.from((null==(i=n.slotOfActiveBlocks)?void 0:i.items)||(null==(o=n.activeSlot)?void 0:o.items)||[]).sort(function(t,e){return t.index-e.index})),n.syncActiveElementStatus());break;case"ArrowUp":e.navigateWithArrowKeys&&n.activeNextBlock(-1,t.shiftKey||t.ctrlKey||t.metaKey);break;case"ArrowDown":e.navigateWithArrowKeys&&n.activeNextBlock(1,t.shiftKey||t.ctrlKey||t.metaKey);break;case"ArrowLeft":e.navigateWithArrowKeys&&n.activeParentBlock();break;case"ArrowRight":e.navigateWithArrowKeys&&n.activeChildrenBlocks();break;case"Delete":case"Backspace":e.handleDeleteKey&&n.deleteActiveBlocks();break;case"Tab":var r=n._lastActiveElement;if(r&&"focus"in r){var s=-1===r.tabIndex&&r.querySelector("[tabIndex], button, textarea, input, select, a, [contentEditable]");s&&"focus"in s?s.focus():r.focus()}t.preventDefault()}},!1),n}n(e,t);var o=e.prototype;return o.focus=function(){document.activeElement!==this.hiddenInput&&(this._lastActiveElement=document.activeElement,this.hiddenInput.focus())},o.dumpSelectedData=function(){var t={isIBClipboardData:!0,ibContextBrand:this.brand,ibContextUUID:this.uuid,blocksData:[]};if(this.activeBlocks.forEach(function(e){t.blocksData.push(e.data)}),0!==t.blocksData.length)return t},o.copy=function(){this.hiddenInput.focus(),document.execCommand("copy")},o.pasteWithData=function(t,e){var i=this;if(!u(t,this.brand))throw new Error("Need a valid IBClipboardData object");var n=this.activeSlot;if(n){var o=E(this.activeBlocks),r=null!=e?e:n===(null==o?void 0:o.ownerSlot)?o.index:Math.max.apply(Math,[0].concat(Array.from(n.items.values(),function(t){return 1+t.index}))),s=new v({type:"paste",ctx:this,data:t,slot:n,index:r});null==n.info.onPaste||n.info.onPaste(s),this.emit("paste",s),!1!==s.returnValue&&setTimeout(function(){var e=t.blocksData.length+r-1,o=Array.from(n.items).filter(function(t){return t.index>=r&&t.index<=e});o.length&&(i.activeSlot=n,i.activeBlocks.clear(),o.forEach(function(t){return i.activeBlocks.add(t)}),i.syncActiveElementStatus())},100)}},o.deleteActiveBlocks=function(){var t,e=Array.from(this.activeBlocks.values()),i=null==(t=e[0])?void 0:t.ownerSlot;if(!i)return!1;var n=e[0].index,o=Array.from(e,function(t){return t.index}),r=new f({type:"cut",blocks:e,indexes:o,indexesDescending:o.slice().sort(function(t,e){return e-t}),ctx:this,slot:i});if(null==i.info.onCut||i.info.onCut(r),this.emit("cut",r),r.returnValue){var s=w(i.items,function(t){return t.index===n});s&&this.addBlockToSelection(s)}return r.returnValue},o.activeNextBlock=function(t,e){var i=this;void 0===e&&(e=!1);var n=e&&this.options.multipleSelect;this.focus();var o,r=!0,s=Array.from(this.activeBlocks);if(s.length||(r=!1,(s=Array.from((null==(o=this.activeSlot)?void 0:o.items)||[])).length)){var c=s[0].ownerSlot,a=s[0].index,l=a,u=s[0],h=u;s.slice(1).forEach(function(t){if(t.ownerSlot===c){var e=t.index;e>a&&(a=e,u=t),e<l&&(l=e,h=t)}});var v=t>0?a+t:l+t,f=w(null==c?void 0:c.items,function(t){return t.index===v});f?(n||this.activeBlocks.clear(),this.activeBlocks.add(f),this.activeSlot=c,this.syncActiveElementStatus()):r?n||(this.activeBlocks.clear(),s.length>1&&this.activeBlocks.add(t>0?u:h),this.syncActiveElementStatus()):(this.activeBlocks.clear(),n?s.forEach(function(t){return i.activeBlocks.add(t)}):this.activeBlocks.add(t>0?h:u),this.syncActiveElementStatus())}},o.activeParentBlock=function(){var t,e=null==(t=this.activeSlot)?void 0:t.ownerBlock;this.activeSlot=(null==e?void 0:e.ownerSlot)||null,this.activeBlocks.clear(),e&&this.activeBlocks.add(e),this.syncActiveElementStatus()},o.activeChildrenBlocks=function(){var t=this,e=E(this.activeBlocks),i=E(null==e?void 0:e.slots);if(i){if(this.activeSlot=i,this.activeBlocks.clear(),this.options.multipleSelect)i.items.forEach(function(e){return t.activeBlocks.add(e)});else{var n=E(i.items);n&&this.activeBlocks.add(n)}this.syncActiveElementStatus()}},o.syncActiveElementStatus=function(){var t,e,i=!1,n=this.lastActiveBlocks,o=this.lastActiveSlot,r=Array.from(this.activeBlocks),s=(null==(t=r[0])?void 0:t.ownerSlot)||null;r.length>1&&s!==this.activeSlot&&(this.activeSlot=s),!this.activeSlot&&s&&(this.activeSlot=s),r.forEach(function(t,e){null==n||n.delete(t),i=t._maybeUpdateActiveNumber(e)||i}),null==n||n.forEach(function(t){i=t._maybeUpdateActiveNumber(!1)||i}),this.activeSlot!==o&&(null==o||o._maybeUpdateActive(!1),null==(e=this.activeSlot)||e._maybeUpdateActive(!0),this.lastActiveSlot=this.activeSlot,i=!0),this.lastActiveBlocks=new Set(this.activeBlocks),this.slotOfActiveBlocks=s,i&&this.emit("activeElementChanged",this)},o.clearSelection=function(){this.activeBlocks.clear(),this.activeSlot=null,this.syncActiveElementStatus()},o.addBlockToSelection=function(t,e){var i;if(e=(i=e)?!0===i?"ctrl":"object"==typeof i?i.ctrlKey||i.metaKey?"ctrl":i.shiftKey?"shift":"none":"ctrl"===i?"ctrl":"shift"===i?"shift":"none":"none",this.options.multipleSelect||(e="none"),"none"===e)this.activeBlocks.clear(),this.activeBlocks.add(t);else{var n=E(this.activeBlocks);if(n&&n.ownerSlot!==t.ownerSlot){for(var o=[],r=n;r&&r.ownerSlot;)o.push(r.ownerSlot),r=r.ownerSlot.ownerBlock;for(var s=-1,c=t;c&&c.ownerSlot;c=c.ownerSlot.ownerBlock)if(-1!==(s=o.indexOf(c.ownerSlot))){t=c;break}-1===s?this.activeBlocks.clear():s>0&&(this.activeBlocks=new Set(Array.from(this.activeBlocks,function(t){for(var e=s;e>0;e--,t=t.ownerSlot.ownerBlock);return t})))}this.activeSlot=t.ownerSlot||null,this.activeSlot||(e="ctrl")}if("ctrl"===e&&(this.activeBlocks.has(t)?this.activeBlocks.delete(t):this.activeBlocks.add(t)),"shift"===e){var a=this.activeSlot,l=t.index,u=l,h=l;this.activeBlocks.forEach(function(t){var e=t.index;u>e&&(u=e),h<e&&(h=e)}),this.activeBlocks=new Set(Array.from(a.items).filter(function(t){var e=t.index;return e>=u&&e<=h}).sort(function(t,e){return t.index-e.index}))}this.syncActiveElementStatus()},o.dispose=function(){var t;this.dragging.dispose(),null==(t=this.hiddenInput.parentElement)||t.removeChild(this.hiddenInput),document.removeEventListener("pointerup",this.handleGlobalPointerUp,!1)},o.createBlock=function(t,e){return void 0===e&&(e=null),new A(this,e,t)},o.createSlot=function(t,e){return void 0===e&&(e=null),new H(this,e,t)},e}(l);export{I as BlockContext,A as BlockHandler,l as EventEmitter,p as IBBlockDragStartAction,f as IBCutAction,g as IBMoveBetweenSlotsAction,d as IBMoveInSlotAction,v as IBPasteAction,S as IBSlotBeforeDropAction,H as SlotHandler,u as isIBClipboardData,y as moveItemsBetweenArrays,B as moveItemsInArray,x as removeItems,m as throttle};
//# sourceMappingURL=index.module.js.map