UNPKG

@wordpress/block-editor

Version:
123 lines (119 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addAttribute = addAttribute; exports.addSaveProps = addSaveProps; exports.default = void 0; var _hooks = require("@wordpress/hooks"); var _components = require("@wordpress/components"); var _i18n = require("@wordpress/i18n"); var _blocks = require("@wordpress/blocks"); var _element = require("@wordpress/element"); var _components2 = require("../components"); var _blockEditingMode = require("../components/block-editing-mode"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Regular expression matching invalid anchor characters for replacement. * * @type {RegExp} */const ANCHOR_REGEX = /[\s#]/g; const ANCHOR_SCHEMA = { type: 'string', source: 'attribute', attribute: 'id', selector: '*' }; /** * Filters registered block settings, extending attributes with anchor using ID * of the first node. * * @param {Object} settings Original block settings. * * @return {Object} Filtered block settings. */ function addAttribute(settings) { var _settings$attributes$; // Allow blocks to specify their own attribute definition with default values if needed. if ('type' in ((_settings$attributes$ = settings.attributes?.anchor) !== null && _settings$attributes$ !== void 0 ? _settings$attributes$ : {})) { return settings; } if ((0, _blocks.hasBlockSupport)(settings, 'anchor')) { // Gracefully handle if settings.attributes is undefined. settings.attributes = { ...settings.attributes, anchor: ANCHOR_SCHEMA }; } return settings; } function BlockEditAnchorControlPure({ anchor, setAttributes }) { const blockEditingMode = (0, _blockEditingMode.useBlockEditingMode)(); if (blockEditingMode !== 'default') { return null; } const isWeb = _element.Platform.OS === 'web'; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components2.InspectorControls, { group: "advanced", children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, { __nextHasNoMarginBottom: true, __next40pxDefaultSize: true, className: "html-anchor-control", label: (0, _i18n.__)('HTML anchor'), help: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [(0, _i18n.__)('Enter a word or two — without spaces — to make a unique web address just for this block, called an “anchor”. Then, you’ll be able to link directly to this section of your page.'), isWeb && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [' ', /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ExternalLink, { href: (0, _i18n.__)('https://wordpress.org/documentation/article/page-jumps/'), children: (0, _i18n.__)('Learn more about anchors') })] })] }), value: anchor || '', placeholder: !isWeb ? (0, _i18n.__)('Add an anchor') : null, onChange: nextValue => { nextValue = nextValue.replace(ANCHOR_REGEX, '-'); setAttributes({ anchor: nextValue }); }, autoCapitalize: "none", autoComplete: "off" }) }); } var _default = exports.default = { addSaveProps, edit: BlockEditAnchorControlPure, attributeKeys: ['anchor'], hasSupport(name) { return (0, _blocks.hasBlockSupport)(name, 'anchor'); } }; /** * Override props assigned to save component to inject anchor ID, if block * supports anchor. This is only applied if the block's save result is an * element and not a markup string. * * @param {Object} extraProps Additional props applied to save element. * @param {Object} blockType Block type. * @param {Object} attributes Current block attributes. * * @return {Object} Filtered props applied to save element. */ function addSaveProps(extraProps, blockType, attributes) { if ((0, _blocks.hasBlockSupport)(blockType, 'anchor')) { extraProps.id = attributes.anchor === '' ? null : attributes.anchor; } return extraProps; } (0, _hooks.addFilter)('blocks.registerBlockType', 'core/anchor/attribute', addAttribute); //# sourceMappingURL=anchor.js.map