UNPKG

@wordpress/block-editor

Version:
231 lines (230 loc) 8.76 kB
// packages/block-editor/src/components/list-view/drop-indicator.js import clsx from "clsx"; import { __experimentalHStack as HStack, __experimentalTruncate as Truncate, Popover } from "@wordpress/components"; import { getScrollContainer } from "@wordpress/dom"; import { useCallback, useMemo } from "@wordpress/element"; import { isRTL } from "@wordpress/i18n"; import BlockIcon from "../block-icon"; import useBlockDisplayInformation from "../use-block-display-information"; import useBlockDisplayTitle from "../block-title/use-block-display-title"; import ListViewExpander from "./expander"; import { jsx, jsxs } from "react/jsx-runtime"; function ListViewDropIndicatorPreview({ draggedBlockClientId, listViewRef, blockDropTarget }) { const blockInformation = useBlockDisplayInformation(draggedBlockClientId); const blockTitle = useBlockDisplayTitle({ clientId: draggedBlockClientId, context: "list-view" }); const { rootClientId, clientId, dropPosition } = blockDropTarget || {}; const [rootBlockElement, blockElement] = useMemo(() => { if (!listViewRef.current) { return []; } const _rootBlockElement = rootClientId ? listViewRef.current.querySelector( `[data-block="${rootClientId}"]` ) : void 0; const _blockElement = clientId ? listViewRef.current.querySelector( `[data-block="${clientId}"]` ) : void 0; return [_rootBlockElement, _blockElement]; }, [listViewRef, rootClientId, clientId]); const targetElement = blockElement || rootBlockElement; const rtl = isRTL(); const getDropIndicatorWidth = useCallback( (targetElementRect, indent) => { if (!targetElement) { return 0; } let width = targetElement.offsetWidth; const scrollContainer = getScrollContainer( targetElement, "horizontal" ); const ownerDocument = targetElement.ownerDocument; const windowScroll = scrollContainer === ownerDocument.body || scrollContainer === ownerDocument.documentElement; if (scrollContainer && !windowScroll) { const scrollContainerRect = scrollContainer.getBoundingClientRect(); const distanceBetweenContainerAndTarget = isRTL() ? scrollContainerRect.right - targetElementRect.right : targetElementRect.left - scrollContainerRect.left; const scrollContainerWidth = scrollContainer.clientWidth; if (scrollContainerWidth < width + distanceBetweenContainerAndTarget) { width = scrollContainerWidth - distanceBetweenContainerAndTarget; } if (!rtl && targetElementRect.left + indent < scrollContainerRect.left) { width -= scrollContainerRect.left - targetElementRect.left; return width; } if (rtl && targetElementRect.right - indent > scrollContainerRect.right) { width -= targetElementRect.right - scrollContainerRect.right; return width; } } return width - indent; }, [rtl, targetElement] ); const style = useMemo(() => { if (!targetElement) { return {}; } const targetElementRect = targetElement.getBoundingClientRect(); return { width: getDropIndicatorWidth(targetElementRect, 0) }; }, [getDropIndicatorWidth, targetElement]); const horizontalScrollOffsetStyle = useMemo(() => { if (!targetElement) { return {}; } const scrollContainer = getScrollContainer(targetElement); const ownerDocument = targetElement.ownerDocument; const windowScroll = scrollContainer === ownerDocument.body || scrollContainer === ownerDocument.documentElement; if (scrollContainer && !windowScroll) { const scrollContainerRect = scrollContainer.getBoundingClientRect(); const targetElementRect = targetElement.getBoundingClientRect(); const distanceBetweenContainerAndTarget = rtl ? scrollContainerRect.right - targetElementRect.right : targetElementRect.left - scrollContainerRect.left; if (!rtl && scrollContainerRect.left > targetElementRect.left) { return { transform: `translateX( ${distanceBetweenContainerAndTarget}px )` }; } if (rtl && scrollContainerRect.right < targetElementRect.right) { return { transform: `translateX( ${distanceBetweenContainerAndTarget * -1}px )` }; } } return {}; }, [rtl, targetElement]); const ariaLevel = useMemo(() => { if (!rootBlockElement) { return 1; } const _ariaLevel = parseInt( rootBlockElement.getAttribute("aria-level"), 10 ); return _ariaLevel ? _ariaLevel + 1 : 1; }, [rootBlockElement]); const hasAdjacentSelectedBranch = useMemo(() => { if (!targetElement) { return false; } return targetElement.classList.contains("is-branch-selected"); }, [targetElement]); const popoverAnchor = useMemo(() => { const isValidDropPosition = dropPosition === "top" || dropPosition === "bottom" || dropPosition === "inside"; if (!targetElement || !isValidDropPosition) { return void 0; } return { contextElement: targetElement, getBoundingClientRect() { const rect = targetElement.getBoundingClientRect(); let left = rect.left; let top = 0; const scrollContainer = getScrollContainer( targetElement, "horizontal" ); const doc = targetElement.ownerDocument; const windowScroll = scrollContainer === doc.body || scrollContainer === doc.documentElement; if (scrollContainer && !windowScroll) { const scrollContainerRect = scrollContainer.getBoundingClientRect(); const scrollbarWidth = rtl ? scrollContainer.offsetWidth - scrollContainer.clientWidth : 0; if (left < scrollContainerRect.left + scrollbarWidth) { left = scrollContainerRect.left + scrollbarWidth; } } if (dropPosition === "top") { top = rect.top - rect.height * 2; } else { top = rect.top; } const width = getDropIndicatorWidth(rect, 0); const height = rect.height; return new window.DOMRect(left, top, width, height); } }; }, [targetElement, dropPosition, getDropIndicatorWidth, rtl]); if (!targetElement) { return null; } return /* @__PURE__ */ jsx( Popover, { animate: false, anchor: popoverAnchor, focusOnMount: false, className: "block-editor-list-view-drop-indicator--preview", variant: "unstyled", flip: false, resize: true, children: /* @__PURE__ */ jsx( "div", { style, className: clsx( "block-editor-list-view-drop-indicator__line", { "block-editor-list-view-drop-indicator__line--darker": hasAdjacentSelectedBranch } ), children: /* @__PURE__ */ jsxs( "div", { className: "block-editor-list-view-leaf", "aria-level": ariaLevel, children: [ /* @__PURE__ */ jsxs( "div", { className: clsx( "block-editor-list-view-block-select-button", "block-editor-list-view-block-contents" ), style: horizontalScrollOffsetStyle, children: [ /* @__PURE__ */ jsx(ListViewExpander, { onClick: () => { } }), /* @__PURE__ */ jsx( BlockIcon, { icon: blockInformation?.icon, showColors: true, context: "list-view" } ), /* @__PURE__ */ jsx( HStack, { alignment: "center", className: "block-editor-list-view-block-select-button__label-wrapper", justify: "flex-start", spacing: 1, children: /* @__PURE__ */ jsx("span", { className: "block-editor-list-view-block-select-button__title", children: /* @__PURE__ */ jsx(Truncate, { ellipsizeMode: "auto", children: blockTitle }) }) } ) ] } ), /* @__PURE__ */ jsx("div", { className: "block-editor-list-view-block__menu-cell" }) ] } ) } ) } ); } export { ListViewDropIndicatorPreview as default }; //# sourceMappingURL=drop-indicator.js.map