@wordpress/block-editor
Version:
148 lines (146 loc) • 5.96 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-editor/src/components/block-list/use-in-between-inserter.js
var use_in_between_inserter_exports = {};
__export(use_in_between_inserter_exports, {
useInBetweenInserter: () => useInBetweenInserter
});
module.exports = __toCommonJS(use_in_between_inserter_exports);
var import_compose = require("@wordpress/compose");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_store = require("../../store/index.cjs");
var import_insertion_point = require("../block-tools/insertion-point.cjs");
var import_lock_unlock = require("../../lock-unlock.cjs");
function useInBetweenInserter() {
const openRef = (0, import_element.useContext)(import_insertion_point.InsertionPointOpenRef);
const isInBetweenInserterDisabled = (0, import_data.useSelect)(
(select) => select(import_store.store).getSettings().isDistractionFree || (0, import_lock_unlock.unlock)(select(import_store.store)).isZoomOut(),
[]
);
const {
getBlockListSettings,
getBlockIndex,
isMultiSelecting,
getSelectedBlockClientIds,
getSettings,
getTemplateLock,
__unstableIsWithinBlockOverlay,
getBlockEditingMode,
getBlockName,
getBlockAttributes,
getParentSectionBlock
} = (0, import_lock_unlock.unlock)((0, import_data.useSelect)(import_store.store));
const { showInsertionPoint, hideInsertionPoint } = (0, import_data.useDispatch)(import_store.store);
return (0, import_compose.useRefEffect)(
(node) => {
if (isInBetweenInserterDisabled) {
return;
}
function onMouseMove(event) {
if (openRef === void 0 || openRef.current) {
return;
}
if (event.target.nodeType === event.target.TEXT_NODE) {
return;
}
if (isMultiSelecting()) {
return;
}
if (!event.target.classList.contains(
"block-editor-block-list__layout"
)) {
hideInsertionPoint();
return;
}
let rootClientId;
if (!event.target.classList.contains("is-root-container")) {
const blockElement = !!event.target.getAttribute(
"data-block"
) ? event.target : event.target.closest("[data-block]");
rootClientId = blockElement.getAttribute("data-block");
}
if (getTemplateLock(rootClientId) || getBlockEditingMode(rootClientId) === "disabled" || getBlockName(rootClientId) === "core/block" || rootClientId && getBlockAttributes(rootClientId).layout?.isManualPlacement) {
return;
}
const blockListSettings = getBlockListSettings(rootClientId);
const orientation = blockListSettings?.orientation || "vertical";
const captureToolbars = !!blockListSettings?.__experimentalCaptureToolbars;
const offsetTop = event.clientY;
const offsetLeft = event.clientX;
const children = Array.from(event.target.children);
let element = children.find((blockEl) => {
const blockElRect = blockEl.getBoundingClientRect();
return blockEl.classList.contains("wp-block") && orientation === "vertical" && blockElRect.top > offsetTop || blockEl.classList.contains("wp-block") && orientation === "horizontal" && ((0, import_i18n.isRTL)() ? blockElRect.right < offsetLeft : blockElRect.left > offsetLeft);
});
if (!element) {
hideInsertionPoint();
return;
}
if (!element.id) {
element = element.firstElementChild;
if (!element) {
hideInsertionPoint();
return;
}
}
const clientId = element.id.slice("block-".length);
if (!clientId || __unstableIsWithinBlockOverlay(clientId) || !!getParentSectionBlock(clientId)) {
return;
}
if (getSelectedBlockClientIds().includes(clientId) && orientation === "vertical" && !captureToolbars && !getSettings().hasFixedToolbar) {
return;
}
const elementRect = element.getBoundingClientRect();
if (orientation === "horizontal" && (event.clientY > elementRect.bottom || event.clientY < elementRect.top) || orientation === "vertical" && (event.clientX > elementRect.right || event.clientX < elementRect.left)) {
hideInsertionPoint();
return;
}
const index = getBlockIndex(clientId);
if (index === 0) {
hideInsertionPoint();
return;
}
showInsertionPoint(rootClientId, index, {
__unstableWithInserter: true
});
}
node.addEventListener("mousemove", onMouseMove);
return () => {
node.removeEventListener("mousemove", onMouseMove);
};
},
[
openRef,
getBlockListSettings,
getBlockIndex,
isMultiSelecting,
showInsertionPoint,
hideInsertionPoint,
getSelectedBlockClientIds,
isInBetweenInserterDisabled
]
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useInBetweenInserter
});
//# sourceMappingURL=use-in-between-inserter.cjs.map