@wordpress/block-editor
Version:
103 lines (102 loc) • 3.59 kB
JavaScript
// packages/block-editor/src/components/inner-blocks/use-nested-settings-update.js
import { useLayoutEffect, useState } from "@wordpress/element";
import { useRegistry } from "@wordpress/data";
import deprecated from "@wordpress/deprecated";
import isShallowEqual from "@wordpress/is-shallow-equal";
import { store as blockEditorStore } from "../../store";
import { getLayoutType } from "../../layouts";
var pendingSettingsUpdates = /* @__PURE__ */ new WeakMap();
function createShallowMemo() {
let value;
return (newValue) => {
if (value === void 0 || !isShallowEqual(value, newValue)) {
value = newValue;
}
return value;
};
}
function useShallowMemo(value) {
const [memo] = useState(createShallowMemo);
return memo(value);
}
function useNestedSettingsUpdate(clientId, parentLock, allowedBlocks, prioritizedInserterBlocks, defaultBlock, directInsert, __experimentalDefaultBlock, __experimentalDirectInsert, templateLock, captureToolbars, orientation, layout) {
const registry = useRegistry();
const _allowedBlocks = useShallowMemo(allowedBlocks);
const _prioritizedInserterBlocks = useShallowMemo(
prioritizedInserterBlocks
);
const _templateLock = templateLock === void 0 || parentLock === "contentOnly" ? parentLock : templateLock;
useLayoutEffect(() => {
const newSettings = {
allowedBlocks: _allowedBlocks,
prioritizedInserterBlocks: _prioritizedInserterBlocks,
templateLock: _templateLock
};
if (captureToolbars !== void 0) {
newSettings.__experimentalCaptureToolbars = captureToolbars;
}
if (orientation !== void 0) {
newSettings.orientation = orientation;
} else {
const layoutType = getLayoutType(layout?.type);
newSettings.orientation = layoutType.getOrientation(layout);
}
if (__experimentalDefaultBlock !== void 0) {
deprecated("__experimentalDefaultBlock", {
alternative: "defaultBlock",
since: "6.3",
version: "6.4"
});
newSettings.defaultBlock = __experimentalDefaultBlock;
}
if (defaultBlock !== void 0) {
newSettings.defaultBlock = defaultBlock;
}
if (__experimentalDirectInsert !== void 0) {
deprecated("__experimentalDirectInsert", {
alternative: "directInsert",
since: "6.3",
version: "6.4"
});
newSettings.directInsert = __experimentalDirectInsert;
}
if (directInsert !== void 0) {
newSettings.directInsert = directInsert;
}
if (newSettings.directInsert !== void 0 && typeof newSettings.directInsert !== "boolean") {
deprecated("Using `Function` as a `directInsert` argument", {
alternative: "`boolean` values",
since: "6.5"
});
}
if (!pendingSettingsUpdates.get(registry)) {
pendingSettingsUpdates.set(registry, {});
}
pendingSettingsUpdates.get(registry)[clientId] = newSettings;
window.queueMicrotask(() => {
const settings = pendingSettingsUpdates.get(registry);
if (Object.keys(settings).length) {
const { updateBlockListSettings } = registry.dispatch(blockEditorStore);
updateBlockListSettings(settings);
pendingSettingsUpdates.set(registry, {});
}
});
}, [
clientId,
_allowedBlocks,
_prioritizedInserterBlocks,
_templateLock,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
captureToolbars,
orientation,
layout,
registry
]);
}
export {
useNestedSettingsUpdate as default
};
//# sourceMappingURL=use-nested-settings-update.js.map