UNPKG

@wordpress/block-editor

Version:
222 lines (220 loc) 7.75 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/block-draggable/index.js var block_draggable_exports = {}; __export(block_draggable_exports, { default: () => block_draggable_default }); module.exports = __toCommonJS(block_draggable_exports); var import_blocks = require("@wordpress/blocks"); var import_components = require("@wordpress/components"); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_compose = require("@wordpress/compose"); var import_draggable_chip = __toESM(require("./draggable-chip")); var import_use_scroll_when_dragging = __toESM(require("./use-scroll-when-dragging")); var import_store = require("../../store"); var import_use_block_refs = require("../block-list/use-block-props/use-block-refs"); var import_use_block_drop_zone = require("../use-block-drop-zone"); var import_jsx_runtime = require("react/jsx-runtime"); var BlockDraggable = ({ appendToOwnerDocument, children, clientIds, cloneClassname, elementId, onDragStart, onDragEnd, fadeWhenDisabled = false, dragComponent }) => { const { srcRootClientId, isDraggable, icon, visibleInserter, getBlockType } = (0, import_data.useSelect)( (select) => { const { canMoveBlocks, getBlockRootClientId: getBlockRootClientId2, getBlockName, getBlockAttributes, isBlockInsertionPointVisible } = select(import_store.store); const { getBlockType: _getBlockType, getActiveBlockVariation } = select(import_blocks.store); const rootClientId = getBlockRootClientId2(clientIds[0]); const blockName = getBlockName(clientIds[0]); const variation = getActiveBlockVariation( blockName, getBlockAttributes(clientIds[0]) ); return { srcRootClientId: rootClientId, isDraggable: canMoveBlocks(clientIds), icon: variation?.icon || _getBlockType(blockName)?.icon, visibleInserter: isBlockInsertionPointVisible(), getBlockType: _getBlockType }; }, [clientIds] ); const isDraggingRef = (0, import_element.useRef)(false); const [startScrolling, scrollOnDragOver, stopScrolling] = (0, import_use_scroll_when_dragging.default)(); const { getAllowedBlocks, getBlockNamesByClientId, getBlockRootClientId } = (0, import_data.useSelect)(import_store.store); const { startDraggingBlocks, stopDraggingBlocks } = (0, import_data.useDispatch)(import_store.store); (0, import_element.useEffect)(() => { return () => { if (isDraggingRef.current) { stopDraggingBlocks(); } }; }, []); const blockEl = (0, import_use_block_refs.useBlockElement)(clientIds[0]); const editorRoot = blockEl?.closest("body"); (0, import_element.useEffect)(() => { if (!editorRoot || !fadeWhenDisabled) { return; } const onDragOver = (event) => { if (!event.target.closest("[data-block]")) { return; } const draggedBlockNames = getBlockNamesByClientId(clientIds); const targetClientId = event.target.closest("[data-block]").getAttribute("data-block"); const allowedBlocks = getAllowedBlocks(targetClientId); const targetBlockName = getBlockNamesByClientId([ targetClientId ])[0]; let dropTargetValid; if (allowedBlocks?.length === 0) { const targetRootClientId = getBlockRootClientId(targetClientId); const targetRootBlockName = getBlockNamesByClientId([ targetRootClientId ])[0]; const rootAllowedBlocks = getAllowedBlocks(targetRootClientId); dropTargetValid = (0, import_use_block_drop_zone.isDropTargetValid)( getBlockType, rootAllowedBlocks, draggedBlockNames, targetRootBlockName ); } else { dropTargetValid = (0, import_use_block_drop_zone.isDropTargetValid)( getBlockType, allowedBlocks, draggedBlockNames, targetBlockName ); } if (!dropTargetValid && !visibleInserter) { window?.document?.body?.classList?.add( "block-draggable-invalid-drag-token" ); } else { window?.document?.body?.classList?.remove( "block-draggable-invalid-drag-token" ); } }; const throttledOnDragOver = (0, import_compose.throttle)(onDragOver, 200); editorRoot.addEventListener("dragover", throttledOnDragOver); return () => { editorRoot.removeEventListener("dragover", throttledOnDragOver); }; }, [ clientIds, editorRoot, fadeWhenDisabled, getAllowedBlocks, getBlockNamesByClientId, getBlockRootClientId, getBlockType, visibleInserter ]); if (!isDraggable) { return children({ draggable: false }); } const transferData = { type: "block", srcClientIds: clientIds, srcRootClientId }; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Draggable, { appendToOwnerDocument, cloneClassname, __experimentalTransferDataType: "wp-blocks", transferData, onDragStart: (event) => { window.requestAnimationFrame(() => { startDraggingBlocks(clientIds); isDraggingRef.current = true; startScrolling(event); if (onDragStart) { onDragStart(); } }); }, onDragOver: scrollOnDragOver, onDragEnd: () => { stopDraggingBlocks(); isDraggingRef.current = false; stopScrolling(); if (onDragEnd) { onDragEnd(); } }, __experimentalDragComponent: ( // Check against `undefined` so that `null` can be used to disable // the default drag component. dragComponent !== void 0 ? dragComponent : /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_draggable_chip.default, { count: clientIds.length, icon, fadeWhenDisabled: true } ) ), elementId, children: ({ onDraggableStart, onDraggableEnd }) => { return children({ draggable: true, onDragStart: onDraggableStart, onDragEnd: onDraggableEnd }); } } ); }; var block_draggable_default = BlockDraggable; //# sourceMappingURL=index.js.map