Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

カバーブロック拡張のリファクタリング #2451

Merged
merged 5 commits into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 43 additions & 53 deletions src/extensions/core/cover/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,69 +46,59 @@ const enhanceCoverBlock = createHigherOrderComponent((BlockEdit) => {
};
}, 'enhanceCoverBlock');

const addLinkAttributesToCoverBlock = (settings, name) => {
const extendCoverBlock = (settings, name) => {
if (!isCoverBlock(name)) {
return settings;
}

settings.attributes = {
...settings.attributes,
linkUrl: {
type: 'string',
default: '',
return {
...settings,
attributes: {
...settings.attributes,
linkUrl: {
type: 'string',
default: '',
},
linkTarget: {
type: 'string',
default: '_self',
},
},
linkTarget: {
type: 'string',
default: '_self',
},
};
edit: enhanceCoverBlock(settings.edit),
save: (props) => {
const { attributes } = props;
const { linkUrl, linkTarget } = attributes;
const saveElement = settings.save(props);

return settings;
};
if (!linkUrl) {
return saveElement;
}

const insertLinkIntoCoverBlock = (element, blockType, attributes) => {
if (blockType.name !== 'core/cover') {
return element;
}

const { linkUrl, linkTarget } = attributes;

if (!linkUrl) {
return element;
}
const existingClassName = saveElement.props.className || '';
const classNameWithLink =
`${existingClassName} ${linkUrl ? 'has-link' : ''}`.trim();
const existingStyle = saveElement.props.style || {};
const relAttribute =
linkTarget === '_blank' ? 'noopener noreferrer' : 'noopener';

// `element` から既存のクラスを取得し、リンクがある場合にのみ `has-link` を追加
const existingClassName = element.props.className || '';
const classNameWithLink =
`${existingClassName} ${linkUrl ? 'has-link' : ''}`.trim();
const existingStyle = element.props.style || {};

// rel 属性の設定
const relAttribute =
linkTarget === '_blank' ? 'noopener noreferrer' : 'noopener';

return (
<div className={classNameWithLink} style={existingStyle}>
{element.props.children}
<a
href={linkUrl}
target={linkTarget}
rel={relAttribute}
aria-label={__('Cover link', 'vk-blocks-pro')}
className="wp-block-cover-vk-link"
></a>
</div>
);
return (
<div className={classNameWithLink} style={existingStyle}>
{saveElement.props.children}
<a
href={linkUrl}
target={linkTarget}
rel={relAttribute}
aria-label={__('Cover link', 'vk-blocks-pro')}
className="wp-block-cover-vk-link"
></a>
</div>
);
},
};
};

addFilter('editor.BlockEdit', 'custom/enhance-cover-block', enhanceCoverBlock);
addFilter(
'blocks.registerBlockType',
'custom/add-link-attributes',
addLinkAttributesToCoverBlock
);
addFilter(
'blocks.getSaveElement',
'custom/insert-link-into-cover-block',
insertLinkIntoCoverBlock
'custom/extend-cover-block',
extendCoverBlock
);
Loading