@wordpress/block-editor
Version:
225 lines (223 loc) • 8.19 kB
JavaScript
;
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/branch.js
var branch_exports = {};
__export(branch_exports, {
default: () => branch_default
});
module.exports = __toCommonJS(branch_exports);
var import_components = require("@wordpress/components");
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_appender = require("./appender");
var import_block = __toESM(require("./block"));
var import_context = require("./context");
var import_utils = require("./utils");
var import_store = require("../../store");
var import_use_block_display_information = __toESM(require("../use-block-display-information"));
var import_jsx_runtime = require("react/jsx-runtime");
function countBlocks(block, expandedState, draggedClientIds, isExpandedByDefault) {
const isDragged = draggedClientIds?.includes(block.clientId);
if (isDragged) {
return 0;
}
const isExpanded = expandedState[block.clientId] ?? isExpandedByDefault;
if (isExpanded) {
return 1 + block.innerBlocks.reduce(
countReducer(
expandedState,
draggedClientIds,
isExpandedByDefault
),
0
);
}
return 1;
}
var countReducer = (expandedState, draggedClientIds, isExpandedByDefault) => (count, block) => {
const isDragged = draggedClientIds?.includes(block.clientId);
if (isDragged) {
return count;
}
const isExpanded = expandedState[block.clientId] ?? isExpandedByDefault;
if (isExpanded && block.innerBlocks.length > 0) {
return count + countBlocks(
block,
expandedState,
draggedClientIds,
isExpandedByDefault
);
}
return count + 1;
};
var noop = () => {
};
function ListViewBranch(props) {
const {
blocks,
selectBlock = noop,
showBlockMovers,
selectedClientIds,
level = 1,
path = "",
isBranchSelected = false,
listPosition = 0,
fixedListWindow,
isExpanded,
parentId,
shouldShowInnerBlocks = true,
isSyncedBranch = false,
showAppender: showAppenderProp = true
} = props;
const parentBlockInformation = (0, import_use_block_display_information.default)(parentId);
const syncedBranch = isSyncedBranch || !!parentBlockInformation?.isSynced;
const canParentExpand = (0, import_data.useSelect)(
(select) => {
if (!parentId) {
return true;
}
return select(import_store.store).canEditBlock(parentId);
},
[parentId]
);
const {
blockDropPosition,
blockDropTargetIndex,
firstDraggedBlockIndex,
blockIndexes,
expandedState,
draggedClientIds
} = (0, import_context.useListViewContext)();
const nextPositionRef = (0, import_element.useRef)();
if (!canParentExpand) {
return null;
}
const showAppender = showAppenderProp && level === 1;
const filteredBlocks = blocks.filter(Boolean);
const blockCount = filteredBlocks.length;
const rowCount = showAppender ? blockCount + 1 : blockCount;
nextPositionRef.current = listPosition;
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
filteredBlocks.map((block, index) => {
const { clientId, innerBlocks } = block;
if (index > 0) {
nextPositionRef.current += countBlocks(
filteredBlocks[index - 1],
expandedState,
draggedClientIds,
isExpanded
);
}
const isDragged = !!draggedClientIds?.includes(clientId);
const { displacement, isAfterDraggedBlocks, isNesting } = (0, import_utils.getDragDisplacementValues)({
blockIndexes,
blockDropTargetIndex,
blockDropPosition,
clientId,
firstDraggedBlockIndex,
isDragged
});
const { itemInView } = fixedListWindow;
const blockInView = itemInView(nextPositionRef.current);
const position = index + 1;
const updatedPath = path.length > 0 ? `${path}_${position}` : `${position}`;
const hasNestedBlocks = !!innerBlocks?.length;
const shouldExpand = hasNestedBlocks && shouldShowInnerBlocks ? expandedState[clientId] ?? isExpanded : void 0;
const isSelected = (0, import_utils.isClientIdSelected)(
clientId,
selectedClientIds
);
const isSelectedBranch = isBranchSelected || isSelected && hasNestedBlocks;
const showBlock = isDragged || blockInView || isSelected && clientId === selectedClientIds[0] || index === 0 || index === blockCount - 1;
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_data.AsyncModeProvider, { value: !isSelected, children: [
showBlock && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_block.default,
{
block,
selectBlock,
isSelected,
isBranchSelected: isSelectedBranch,
isDragged,
level,
position,
rowCount,
siblingBlockCount: blockCount,
showBlockMovers,
path: updatedPath,
isExpanded: isDragged ? false : shouldExpand,
listPosition: nextPositionRef.current,
selectedClientIds,
isSyncedBranch: syncedBranch,
displacement,
isAfterDraggedBlocks,
isNesting
}
),
!showBlock && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("tr", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("td", { className: "block-editor-list-view-placeholder" }) }),
hasNestedBlocks && shouldExpand && !isDragged && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
ListViewBranch,
{
parentId: clientId,
blocks: innerBlocks,
selectBlock,
showBlockMovers,
level: level + 1,
path: updatedPath,
listPosition: nextPositionRef.current + 1,
fixedListWindow,
isBranchSelected: isSelectedBranch,
selectedClientIds,
isExpanded,
isSyncedBranch: syncedBranch
}
)
] }, clientId);
}),
showAppender && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.__experimentalTreeGridRow,
{
level,
setSize: rowCount,
positionInSet: rowCount,
isExpanded: true,
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.__experimentalTreeGridCell, { children: (treeGridCellProps) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_appender.Appender,
{
clientId: parentId,
nestingLevel: level,
blockCount,
...treeGridCellProps
}
) })
}
)
] });
}
var branch_default = (0, import_element.memo)(ListViewBranch);
//# sourceMappingURL=branch.js.map