如何在古腾堡的 save.js 函数中显示多个富文本块和按钮

发布于 2025-01-20 04:16:30 字数 5059 浏览 0 评论 0原文

我正在创建我的第一个Gutenberg街区。

我有一个< h1> block和一个< p> block, 以及一个自定义按钮和背景图像。

这是edit.js文件:

import { __ } from '@wordpress/i18n';
import {
    useBlockProps,
    RichText,
    BlockControls,
    MediaReplaceFlow,
    MediaPlaceholder,
} from '@wordpress/block-editor';
import { ToolbarButton, Button } from '@wordpress/components';
import './editor.scss';

export default function Edit(props) {
    const { attributes, setAttributes, noticeOperations, noticeUI } = props;
    const { url, alt, id, title, description } = attributes;

    const onSelectImage = (image) => {
        if (!image || !image.url) {
            setAttributes({ url: undefined, id: undefined, alt: '' });
            return;
        }
        setAttributes({ url: image.url, id: image.id, alt: image.alt });
    };

    const onSelectURL = (newURL) => {
        setAttributes({
            url: newURL,
            id: undefined,
            alt: '',
        });
    };

    const onUploadError = (message) => {
        noticeOperations.removeAllNotices();
        noticeOperations.createErrorNotice(message);
    };

    const removeImage = () => {
        setAttributes({
            url: undefined,
            alt: '',
            id: undefined,
        });
    };

    const onChangeTitle = (newTitle) => {
        setAttributes({ title: newTitle });
    };

    const onChangeDescription = (newDescription) => {
        setAttributes({ description: newDescription });
    };

    return (
        <>
            {url && (
                <BlockControls group="inline">
                    <MediaReplaceFlow
                        name={__('Replace Image', 'block-test/hero-block')}
                        onSelect={onSelectImage}
                        onSelectURL={onSelectURL}
                        onError={onUploadError}
                        accept="image/*"
                        allowedTypes={['image']}
                        mediaId={id}
                        mediaURL={url}
                    />
                    <ToolbarButton onClick={removeImage}>
                        {__('Remove Image', 'block-test/hero-block')}
                    </ToolbarButton>
                </BlockControls>
            )}
            <div {...useBlockProps()}>
                {url && (
                    <div className={'wp-block-test-hero-block-img'}>
                        <img src={url} alt={alt} />
                    </div>
                )}
                <MediaPlaceholder
                    icon="admin-users"
                    onSelect={onSelectImage}
                    onSelectURL={onSelectURL}
                    onError={onUploadError}
                    accept="image/*"
                    allowedTypes={['image']}
                    disableMediaButtons={url}
                    notices={noticeUI}
                />
                <RichText
                    className="hero-block-title"
                    onChange={onChangeTitle}
                    value={title}
                    placeholder={__('Title', 'block-test/hero-block')}
                    tagName="h1"
                    allowedFormats={[]}
                />
                <RichText
                    className="hero-block-description"
                    onChange={onChangeDescription}
                    value={description}
                    placeholder={__('Description', 'block-test/hero-block')}
                    tagName="p"
                    allowedFormats={[]}
                />
                <Button className="hero-btn">Learn More</Button>
            </div>
        </>
    );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

这是一个保存.js文件:

import { useBlockProps, RichText } from '@wordpress/block-editor';

export default function save({ attributes }) {
    const { title } = attributes;

    return (
        <RichText.Content
            {...useBlockProps.save()}
            tagName="h1"
            value={title}
        />
    );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

没有足够的想法将其显示在编辑器中,而且还可以在前端显示。

我认为这应该非常容易,但是现在对我来说并不容易。

我使用了Google,我找不到答案。

我会为任何提示感到高兴。

I am creating my first Gutenberg block.

I have one <h1> block and one <p> block,
as well as one custom button and a background image.

This is edit.js file:

import { __ } from '@wordpress/i18n';
import {
    useBlockProps,
    RichText,
    BlockControls,
    MediaReplaceFlow,
    MediaPlaceholder,
} from '@wordpress/block-editor';
import { ToolbarButton, Button } from '@wordpress/components';
import './editor.scss';

export default function Edit(props) {
    const { attributes, setAttributes, noticeOperations, noticeUI } = props;
    const { url, alt, id, title, description } = attributes;

    const onSelectImage = (image) => {
        if (!image || !image.url) {
            setAttributes({ url: undefined, id: undefined, alt: '' });
            return;
        }
        setAttributes({ url: image.url, id: image.id, alt: image.alt });
    };

    const onSelectURL = (newURL) => {
        setAttributes({
            url: newURL,
            id: undefined,
            alt: '',
        });
    };

    const onUploadError = (message) => {
        noticeOperations.removeAllNotices();
        noticeOperations.createErrorNotice(message);
    };

    const removeImage = () => {
        setAttributes({
            url: undefined,
            alt: '',
            id: undefined,
        });
    };

    const onChangeTitle = (newTitle) => {
        setAttributes({ title: newTitle });
    };

    const onChangeDescription = (newDescription) => {
        setAttributes({ description: newDescription });
    };

    return (
        <>
            {url && (
                <BlockControls group="inline">
                    <MediaReplaceFlow
                        name={__('Replace Image', 'block-test/hero-block')}
                        onSelect={onSelectImage}
                        onSelectURL={onSelectURL}
                        onError={onUploadError}
                        accept="image/*"
                        allowedTypes={['image']}
                        mediaId={id}
                        mediaURL={url}
                    />
                    <ToolbarButton onClick={removeImage}>
                        {__('Remove Image', 'block-test/hero-block')}
                    </ToolbarButton>
                </BlockControls>
            )}
            <div {...useBlockProps()}>
                {url && (
                    <div className={'wp-block-test-hero-block-img'}>
                        <img src={url} alt={alt} />
                    </div>
                )}
                <MediaPlaceholder
                    icon="admin-users"
                    onSelect={onSelectImage}
                    onSelectURL={onSelectURL}
                    onError={onUploadError}
                    accept="image/*"
                    allowedTypes={['image']}
                    disableMediaButtons={url}
                    notices={noticeUI}
                />
                <RichText
                    className="hero-block-title"
                    onChange={onChangeTitle}
                    value={title}
                    placeholder={__('Title', 'block-test/hero-block')}
                    tagName="h1"
                    allowedFormats={[]}
                />
                <RichText
                    className="hero-block-description"
                    onChange={onChangeDescription}
                    value={description}
                    placeholder={__('Description', 'block-test/hero-block')}
                    tagName="p"
                    allowedFormats={[]}
                />
                <Button className="hero-btn">Learn More</Button>
            </div>
        </>
    );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

And this is a save.js file:

import { useBlockProps, RichText } from '@wordpress/block-editor';

export default function save({ attributes }) {
    const { title } = attributes;

    return (
        <RichText.Content
            {...useBlockProps.save()}
            tagName="h1"
            value={title}
        />
    );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Not enough thoughts to display this not only in the editor but also on the front-end.

I think it should be very easy, but it's not easy for me right now.

I used Google, I couldn't find an answer for me.

I'll be glad for any tips.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文