@wordpress/block-editor
Version:
127 lines (126 loc) • 4.36 kB
JavaScript
// packages/block-editor/src/components/block-rename/modal.js
import {
__experimentalHStack as HStack,
__experimentalVStack as VStack,
Button,
TextControl,
Modal
} from "@wordpress/components";
import { __, sprintf } from "@wordpress/i18n";
import { useState } from "@wordpress/element";
import { speak } from "@wordpress/a11y";
import { useSelect, useDispatch } from "@wordpress/data";
import { store as blockEditorStore } from "../../store";
import { useBlockDisplayInformation } from "..";
import isEmptyString from "./is-empty-string";
import { cleanEmptyObject } from "../../hooks/utils";
import { jsx, jsxs } from "react/jsx-runtime";
function BlockRenameModal({ clientId, onClose }) {
const [editedBlockName, setEditedBlockName] = useState();
const blockInformation = useBlockDisplayInformation(clientId);
const { metadata } = useSelect(
(select) => {
const { getBlockAttributes } = select(blockEditorStore);
return {
metadata: getBlockAttributes(clientId)?.metadata
};
},
[clientId]
);
const { updateBlockAttributes } = useDispatch(blockEditorStore);
const blockName = metadata?.name || "";
const originalBlockName = blockInformation?.title;
const hasOverridesWarning = !!blockName && !!metadata?.bindings && Object.values(metadata.bindings).some(
(binding) => binding.source === "core/pattern-overrides"
);
const nameHasChanged = editedBlockName !== void 0 && editedBlockName !== blockName;
const nameIsOriginal = editedBlockName === originalBlockName;
const nameIsEmpty = isEmptyString(editedBlockName);
const isNameValid = nameHasChanged || nameIsOriginal;
const autoSelectInputText = (event) => event.target.select();
const handleSubmit = () => {
const newName = nameIsOriginal || nameIsEmpty ? void 0 : editedBlockName;
const message = nameIsOriginal || nameIsEmpty ? sprintf(
/* translators: %s: new name/label for the block */
__('Block name reset to: "%s".'),
editedBlockName
) : sprintf(
/* translators: %s: new name/label for the block */
__('Block name changed to: "%s".'),
editedBlockName
);
speak(message, "assertive");
updateBlockAttributes([clientId], {
metadata: cleanEmptyObject({
...metadata,
name: newName
})
});
onClose();
};
return /* @__PURE__ */ jsx(
Modal,
{
title: __("Rename"),
onRequestClose: onClose,
overlayClassName: "block-editor-block-rename-modal",
focusOnMount: "firstContentElement",
size: "small",
children: /* @__PURE__ */ jsx(
"form",
{
onSubmit: (e) => {
e.preventDefault();
if (!isNameValid) {
return;
}
handleSubmit();
},
children: /* @__PURE__ */ jsxs(VStack, { spacing: "3", children: [
/* @__PURE__ */ jsx(
TextControl,
{
__nextHasNoMarginBottom: true,
__next40pxDefaultSize: true,
value: editedBlockName ?? blockName,
label: __("Name"),
help: hasOverridesWarning ? __(
"This block allows overrides. Changing the name can cause problems with content entered into instances of this pattern."
) : void 0,
placeholder: originalBlockName,
onChange: setEditedBlockName,
onFocus: autoSelectInputText
}
),
/* @__PURE__ */ jsxs(HStack, { justify: "right", children: [
/* @__PURE__ */ jsx(
Button,
{
__next40pxDefaultSize: true,
variant: "tertiary",
onClick: onClose,
children: __("Cancel")
}
),
/* @__PURE__ */ jsx(
Button,
{
__next40pxDefaultSize: true,
accessibleWhenDisabled: true,
disabled: !isNameValid,
variant: "primary",
type: "submit",
children: __("Save")
}
)
] })
] })
}
)
}
);
}
export {
BlockRenameModal as default
};
//# sourceMappingURL=modal.js.map