@gechiui/block-editor
Version:
141 lines (117 loc) • 4.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.addAttribute = addAttribute;
exports.addSaveProps = addSaveProps;
exports.withInspectorControl = void 0;
var _element = require("@gechiui/element");
var _lodash = require("lodash");
var _hooks = require("@gechiui/hooks");
var _components = require("@gechiui/components");
var _i18n = require("@gechiui/i18n");
var _blocks = require("@gechiui/blocks");
var _compose = require("@gechiui/compose");
var _components2 = require("../components");
/**
* External dependencies
*/
/**
* GeChiUI dependencies
*/
/**
* Internal dependencies
*/
/**
* Regular expression matching invalid anchor characters for replacement.
*
* @type {RegExp}
*/
const ANCHOR_REGEX = /[\s#]/g;
/**
* 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) {
// allow blocks to specify their own attribute definition with default values if needed.
if ((0, _lodash.has)(settings.attributes, ['anchor', 'type'])) {
return settings;
}
if ((0, _blocks.hasBlockSupport)(settings, 'anchor')) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = { ...settings.attributes,
anchor: {
type: 'string',
source: 'attribute',
attribute: 'id',
selector: '*'
}
};
}
return settings;
}
/**
* Override the default edit UI to include a new block inspector control for
* assigning the anchor ID, if block supports anchor.
*
* @param {GCComponent} BlockEdit Original component.
*
* @return {GCComponent} Wrapped component.
*/
const withInspectorControl = (0, _compose.createHigherOrderComponent)(BlockEdit => {
return props => {
const hasAnchor = (0, _blocks.hasBlockSupport)(props.name, 'anchor');
if (hasAnchor && props.isSelected) {
const isWeb = _element.Platform.OS === 'web';
const textControl = (0, _element.createElement)(_components.TextControl, {
className: "html-anchor-control",
label: (0, _i18n.__)('HTML锚点'),
help: (0, _element.createElement)(_element.Fragment, null, (0, _i18n.__)('输入一两个词(无需空格)即可为此区块创建一个唯一网址,称为“锚点”。之后,您就可以直接链接至页面的此部分。'), isWeb && (0, _element.createElement)(_components.ExternalLink, {
href: (0, _i18n.__)('https://www.gechiui.com/support/article/page-jumps/')
}, (0, _i18n.__)('了解关于锚点的细节'))),
value: props.attributes.anchor || '',
placeholder: !isWeb ? (0, _i18n.__)('添加锚点') : null,
onChange: nextValue => {
nextValue = nextValue.replace(ANCHOR_REGEX, '-');
props.setAttributes({
anchor: nextValue
});
},
autoCapitalize: "none",
autoComplete: "off"
});
return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(BlockEdit, props), isWeb && (0, _element.createElement)(_components2.InspectorControls, {
__experimentalGroup: "advanced"
}, textControl), !isWeb && props.name === 'core/heading' && (0, _element.createElement)(_components2.InspectorControls, null, (0, _element.createElement)(_components.PanelBody, {
title: (0, _i18n.__)('标题设置')
}, textControl)));
}
return (0, _element.createElement)(BlockEdit, props);
};
}, 'withInspectorControl');
/**
* 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.
*/
exports.withInspectorControl = withInspectorControl;
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);
(0, _hooks.addFilter)('editor.BlockEdit', 'core/editor/anchor/with-inspector-control', withInspectorControl);
(0, _hooks.addFilter)('blocks.getSaveContent.extraProps', 'core/anchor/save-props', addSaveProps);
//# sourceMappingURL=anchor.js.map