@wordpress/block-library
Version:
Block library for the WordPress editor.
101 lines (98 loc) • 3.27 kB
JavaScript
import { createElement, Fragment } from "@wordpress/element";
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import { RichText, AlignmentControl, InspectorControls, BlockControls, useBlockProps } from '@wordpress/block-editor';
import { ToggleControl, PanelBody } from '@wordpress/components';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import LevelControl from './level-toolbar';
export default function SiteTitleEdit(_ref) {
let {
attributes,
setAttributes,
insertBlocksAfter
} = _ref;
const {
level,
textAlign,
isLink,
linkTarget
} = attributes;
const [title, setTitle] = useEntityProp('root', 'site', 'title');
const {
canUserEdit,
readOnlyTitle
} = useSelect(select => {
const {
canUser,
getEntityRecord
} = select(coreStore);
const siteData = getEntityRecord('root', '__unstableBase');
return {
canUserEdit: canUser('update', 'settings'),
readOnlyTitle: decodeEntities(siteData === null || siteData === void 0 ? void 0 : siteData.name)
};
}, []);
const TagName = level === 0 ? 'p' : `h${level}`;
const blockProps = useBlockProps({
className: classnames({
[`has-text-align-${textAlign}`]: textAlign,
'wp-block-site-title__placeholder': !canUserEdit && !readOnlyTitle
})
});
const siteTitleContent = canUserEdit ? createElement(TagName, blockProps, createElement(RichText, {
tagName: isLink ? 'a' : 'span',
href: isLink ? '#site-title-pseudo-link' : undefined,
"aria-label": __('Site title text'),
placeholder: __('Write site title…'),
value: title,
onChange: setTitle,
allowedFormats: [],
disableLineBreaks: true,
__unstableOnSplitAtEnd: () => insertBlocksAfter(createBlock(getDefaultBlockName()))
})) : createElement(TagName, blockProps, isLink ? createElement("a", {
href: "#site-title-pseudo-link",
onClick: event => event.preventDefault()
}, readOnlyTitle || __('Site Title placeholder')) : createElement("span", null, title || readOnlyTitle));
return createElement(Fragment, null, createElement(BlockControls, {
group: "block"
}, createElement(LevelControl, {
level: level,
onChange: newLevel => setAttributes({
level: newLevel
})
}), createElement(AlignmentControl, {
value: textAlign,
onChange: nextAlign => {
setAttributes({
textAlign: nextAlign
});
}
})), createElement(InspectorControls, null, createElement(PanelBody, {
title: __('Link settings')
}, createElement(ToggleControl, {
label: __('Make title link to home'),
onChange: () => setAttributes({
isLink: !isLink
}),
checked: isLink
}), isLink && createElement(ToggleControl, {
label: __('Open in new tab'),
onChange: value => setAttributes({
linkTarget: value ? '_blank' : '_self'
}),
checked: linkTarget === '_blank'
}))), siteTitleContent);
}
//# sourceMappingURL=index.js.map