@wordpress/block-editor
Version:
119 lines (117 loc) • 4.39 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/hooks/anchor.js
var anchor_exports = {};
__export(anchor_exports, {
addAttribute: () => addAttribute,
addSaveProps: () => addSaveProps,
default: () => anchor_default
});
module.exports = __toCommonJS(anchor_exports);
var import_hooks = require("@wordpress/hooks");
var import_components = require("@wordpress/components");
var import_i18n = require("@wordpress/i18n");
var import_blocks = require("@wordpress/blocks");
var import_element = require("@wordpress/element");
var import_components2 = require("../components");
var import_block_editing_mode = require("../components/block-editing-mode");
var import_jsx_runtime = require("react/jsx-runtime");
var ANCHOR_REGEX = /[\s#]/g;
var ANCHOR_SCHEMA = {
type: "string",
source: "attribute",
attribute: "id",
selector: "*"
};
function addAttribute(settings) {
if ("type" in (settings.attributes?.anchor ?? {})) {
return settings;
}
if ((0, import_blocks.hasBlockSupport)(settings, "anchor")) {
settings.attributes = {
...settings.attributes,
anchor: ANCHOR_SCHEMA
};
}
return settings;
}
function BlockEditAnchorControlPure({ anchor, setAttributes }) {
const blockEditingMode = (0, import_block_editing_mode.useBlockEditingMode)();
if (blockEditingMode !== "default") {
return null;
}
const isWeb = import_element.Platform.OS === "web";
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components2.InspectorControls, { group: "advanced", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.TextControl,
{
__nextHasNoMarginBottom: true,
__next40pxDefaultSize: true,
className: "html-anchor-control",
label: (0, import_i18n.__)("HTML anchor"),
help: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
(0, import_i18n.__)(
"Enter a word or two \u2014 without spaces \u2014 to make a unique web address just for this block, called an \u201Canchor\u201D. Then, you\u2019ll be able to link directly to this section of your page."
),
isWeb && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
" ",
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.ExternalLink,
{
href: (0, import_i18n.__)(
"https://wordpress.org/documentation/article/page-jumps/"
),
children: (0, import_i18n.__)("Learn more about anchors")
}
)
] })
] }),
value: anchor || "",
placeholder: !isWeb ? (0, import_i18n.__)("Add an anchor") : null,
onChange: (nextValue) => {
nextValue = nextValue.replace(ANCHOR_REGEX, "-");
setAttributes({
anchor: nextValue
});
},
autoCapitalize: "none",
autoComplete: "off"
}
) });
}
var anchor_default = {
addSaveProps,
edit: BlockEditAnchorControlPure,
attributeKeys: ["anchor"],
hasSupport(name) {
return (0, import_blocks.hasBlockSupport)(name, "anchor");
}
};
function addSaveProps(extraProps, blockType, attributes) {
if ((0, import_blocks.hasBlockSupport)(blockType, "anchor")) {
extraProps.id = attributes.anchor === "" ? null : attributes.anchor;
}
return extraProps;
}
(0, import_hooks.addFilter)("blocks.registerBlockType", "core/anchor/attribute", addAttribute);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
addAttribute,
addSaveProps
});
//# sourceMappingURL=anchor.js.map