UNPKG

@wordpress/block-editor

Version:
346 lines (344 loc) 12.8 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/list-view/index.js var list_view_exports = {}; __export(list_view_exports, { BLOCK_LIST_ITEM_HEIGHT: () => BLOCK_LIST_ITEM_HEIGHT, PrivateListView: () => PrivateListView, default: () => list_view_default }); module.exports = __toCommonJS(list_view_exports); var import_clsx = __toESM(require("clsx")); var import_compose = require("@wordpress/compose"); var import_components = require("@wordpress/components"); var import_data = require("@wordpress/data"); var import_deprecated = __toESM(require("@wordpress/deprecated")); var import_element = require("@wordpress/element"); var import_i18n = require("@wordpress/i18n"); var import_branch = __toESM(require("./branch")); var import_context = require("./context"); var import_drop_indicator = __toESM(require("./drop-indicator")); var import_use_block_selection = __toESM(require("./use-block-selection")); var import_use_list_view_block_indexes = __toESM(require("./use-list-view-block-indexes")); var import_use_list_view_client_ids = __toESM(require("./use-list-view-client-ids")); var import_use_list_view_collapse_items = __toESM(require("./use-list-view-collapse-items")); var import_use_list_view_drop_zone = __toESM(require("./use-list-view-drop-zone")); var import_use_list_view_expand_selected_item = __toESM(require("./use-list-view-expand-selected-item")); var import_store = require("../../store"); var import_block_settings_dropdown = require("../block-settings-menu/block-settings-dropdown"); var import_utils = require("./utils"); var import_use_clipboard_handler = __toESM(require("./use-clipboard-handler")); var import_jsx_runtime = require("react/jsx-runtime"); var expanded = (state, action) => { if (action.type === "clear") { return {}; } if (Array.isArray(action.clientIds)) { return { ...state, ...action.clientIds.reduce( (newState, id) => ({ ...newState, [id]: action.type === "expand" }), {} ) }; } return state; }; var BLOCK_LIST_ITEM_HEIGHT = 32; function ListViewComponent({ id, blocks, dropZoneElement, showBlockMovers = false, isExpanded = false, showAppender = false, blockSettingsMenu: BlockSettingsMenu = import_block_settings_dropdown.BlockSettingsDropdown, rootClientId, description, onSelect, additionalBlockContent: AdditionalBlockContent }, ref) { if (blocks) { (0, import_deprecated.default)( "`blocks` property in `wp.blockEditor.__experimentalListView`", { since: "6.3", alternative: "`rootClientId` property" } ); } const instanceId = (0, import_compose.useInstanceId)(ListViewComponent); const { clientIdsTree, draggedClientIds, selectedClientIds } = (0, import_use_list_view_client_ids.default)({ blocks, rootClientId }); const blockIndexes = (0, import_use_list_view_block_indexes.default)(clientIdsTree); const { getBlock } = (0, import_data.useSelect)(import_store.store); const { visibleBlockCount } = (0, import_data.useSelect)( (select) => { const { getGlobalBlockCount, getClientIdsOfDescendants } = select(import_store.store); const draggedBlockCount = draggedClientIds?.length > 0 ? getClientIdsOfDescendants(draggedClientIds).length + 1 : 0; return { visibleBlockCount: getGlobalBlockCount() - draggedBlockCount }; }, [draggedClientIds] ); const { updateBlockSelection } = (0, import_use_block_selection.default)(); const [expandedState, setExpandedState] = (0, import_element.useReducer)(expanded, {}); const [insertedBlock, setInsertedBlock] = (0, import_element.useState)(null); const { setSelectedTreeId } = (0, import_use_list_view_expand_selected_item.default)({ firstSelectedBlockClientId: selectedClientIds[0], setExpandedState }); const selectEditorBlock = (0, import_element.useCallback)( /** * @param {MouseEvent | KeyboardEvent | undefined} event * @param {string} blockClientId * @param {null | undefined | -1 | 1} focusPosition */ (event, blockClientId, focusPosition) => { updateBlockSelection(event, blockClientId, null, focusPosition); setSelectedTreeId(blockClientId); if (onSelect) { onSelect(getBlock(blockClientId)); } }, [setSelectedTreeId, updateBlockSelection, onSelect, getBlock] ); const { ref: dropZoneRef, target: blockDropTarget } = (0, import_use_list_view_drop_zone.default)({ dropZoneElement, expandedState, setExpandedState }); const elementRef = (0, import_element.useRef)(); const clipBoardRef = (0, import_use_clipboard_handler.default)({ selectBlock: selectEditorBlock }); const treeGridRef = (0, import_compose.useMergeRefs)([ clipBoardRef, elementRef, dropZoneRef, ref ]); (0, import_element.useEffect)(() => { if (selectedClientIds?.length) { (0, import_utils.focusListItem)(selectedClientIds[0], elementRef?.current); } }, []); const expand = (0, import_element.useCallback)( (clientId) => { if (!clientId) { return; } const clientIds = Array.isArray(clientId) ? clientId : [clientId]; setExpandedState({ type: "expand", clientIds }); }, [setExpandedState] ); const collapse = (0, import_element.useCallback)( (clientId) => { if (!clientId) { return; } setExpandedState({ type: "collapse", clientIds: [clientId] }); }, [setExpandedState] ); const collapseAll = (0, import_element.useCallback)(() => { setExpandedState({ type: "clear" }); }, [setExpandedState]); const expandRow = (0, import_element.useCallback)( (row) => { expand(row?.dataset?.block); }, [expand] ); const collapseRow = (0, import_element.useCallback)( (row) => { collapse(row?.dataset?.block); }, [collapse] ); const focusRow = (0, import_element.useCallback)( (event, startRow, endRow) => { if (event.shiftKey) { updateBlockSelection( event, startRow?.dataset?.block, endRow?.dataset?.block ); } }, [updateBlockSelection] ); (0, import_use_list_view_collapse_items.default)({ collapseAll, expand }); const firstDraggedBlockClientId = draggedClientIds?.[0]; const { blockDropTargetIndex, blockDropPosition, firstDraggedBlockIndex } = (0, import_element.useMemo)(() => { let _blockDropTargetIndex, _firstDraggedBlockIndex; if (blockDropTarget?.clientId) { const foundBlockIndex = blockIndexes[blockDropTarget.clientId]; _blockDropTargetIndex = foundBlockIndex === void 0 || blockDropTarget?.dropPosition === "top" ? foundBlockIndex : foundBlockIndex + 1; } else if (blockDropTarget === null) { _blockDropTargetIndex = null; } if (firstDraggedBlockClientId) { const foundBlockIndex = blockIndexes[firstDraggedBlockClientId]; _firstDraggedBlockIndex = foundBlockIndex === void 0 || blockDropTarget?.dropPosition === "top" ? foundBlockIndex : foundBlockIndex + 1; } return { blockDropTargetIndex: _blockDropTargetIndex, blockDropPosition: blockDropTarget?.dropPosition, firstDraggedBlockIndex: _firstDraggedBlockIndex }; }, [blockDropTarget, blockIndexes, firstDraggedBlockClientId]); const contextValue = (0, import_element.useMemo)( () => ({ blockDropPosition, blockDropTargetIndex, blockIndexes, draggedClientIds, expandedState, expand, firstDraggedBlockIndex, collapse, collapseAll, BlockSettingsMenu, listViewInstanceId: instanceId, AdditionalBlockContent, insertedBlock, setInsertedBlock, treeGridElementRef: elementRef, rootClientId }), [ blockDropPosition, blockDropTargetIndex, blockIndexes, draggedClientIds, expandedState, expand, firstDraggedBlockIndex, collapse, collapseAll, BlockSettingsMenu, instanceId, AdditionalBlockContent, insertedBlock, setInsertedBlock, rootClientId ] ); const [fixedListWindow] = (0, import_compose.__experimentalUseFixedWindowList)( elementRef, BLOCK_LIST_ITEM_HEIGHT, visibleBlockCount, { // Ensure that the windowing logic is recalculated when the expanded state changes. // This is necessary because expanding a collapsed block in a short list view can // switch the list view to a tall list view with a scrollbar, and vice versa. // When this happens, the windowing logic needs to be recalculated to ensure that // the correct number of blocks are rendered, by rechecking for a scroll container. expandedState, useWindowing: true, windowOverscan: 40 } ); if (!clientIdsTree.length && !showAppender) { return null; } const describedById = description && `block-editor-list-view-description-${instanceId}`; return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_data.AsyncModeProvider, { value: true, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_drop_indicator.default, { draggedBlockClientId: firstDraggedBlockClientId, listViewRef: elementRef, blockDropTarget } ), description && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.VisuallyHidden, { id: describedById, children: description }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalTreeGrid, { id, className: (0, import_clsx.default)("block-editor-list-view-tree", { "is-dragging": draggedClientIds?.length > 0 && blockDropTargetIndex !== void 0 }), "aria-label": (0, import_i18n.__)("Block navigation structure"), ref: treeGridRef, onCollapseRow: collapseRow, onExpandRow: expandRow, onFocusRow: focusRow, applicationAriaLabel: (0, import_i18n.__)("Block navigation structure"), "aria-describedby": describedById, style: { "--wp-admin--list-view-dragged-items-height": draggedClientIds?.length ? `${BLOCK_LIST_ITEM_HEIGHT * (draggedClientIds.length - 1)}px` : null }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_context.ListViewContext.Provider, { value: contextValue, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_branch.default, { blocks: clientIdsTree, parentId: rootClientId, selectBlock: selectEditorBlock, showBlockMovers, fixedListWindow, selectedClientIds, isExpanded, showAppender } ) }) } ) ] }); } var PrivateListView = (0, import_element.forwardRef)(ListViewComponent); var list_view_default = (0, import_element.forwardRef)((props, ref) => { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( PrivateListView, { ref, ...props, showAppender: false, rootClientId: null, onSelect: null, additionalBlockContent: null, blockSettingsMenu: void 0 } ); }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BLOCK_LIST_ITEM_HEIGHT, PrivateListView }); //# sourceMappingURL=index.js.map