admin管理员组文章数量:1291044
My requirement: I am trying to create a simple gutenberg block which will have a textarea
and a dropdown
. Once a user has provided the details I want to save it as a shortcode in DB, but when the user come to edit the block it should show the content in textarea
and selected item in dropdown
.
The issue that I am getting, on create everything is working as expected, but when I come to edit screen the content is getting messed up. Also the dropdown is not showing for existing block.
Here is the code that I have written with the help of this & this article.
const {__} = wp.i18n;
const {RawHTML} = wp.element;
const {registerBlockType} = wp.blocks;
const {PanelBody, SelectControl, TextareaControl} = wpponents;
const {InspectorControls} = wp.editor;
const blockStyle = {
backgroundColor: '#900',
color: '#fff',
padding: '20px',
};
const countries = [
{label: __('Select a countries', 'txtdomain'), value: ''},
{label: 'India', value: 'IND'},
{label: 'United States of America', value: 'USA'},
{label: 'Sri Lanka', value: 'LKA'}
];
registerBlockType('myplugin/example-01-block', {
title: 'My Plugin',
icon: 'editor-code',
category: 'common',
attributes: {
content: {
type: "string",
source: "text",
default: '// Place your text here!',
},
country: {
type: "string",
default: ""
},
},
example: {},
edit: (props) => {
const {
attributes: {content},
setAttributes,
className,
country
} = props;
return (
<div className={ className }>
<InspectorControls>
<PanelBody
title={ __('My Plugin Settings', 'txtdomain') }
>
<SelectControl
label={ __('Country', 'txtdomain') }
value={ country }
options={ countries }
onChange={ value => setAttributes({country: value}) }
/>
</PanelBody>
</InspectorControls>
<TextareaControl
value={ content }
onChange={ value => setAttributes({content: value}) }
/>
</div>
);
},
save: (props) => {
const {
attributes: {content, country}
} = props;
var myShortcode = '[myshortcode country="' + country + '", text="' + content + '"]';
return (
<div>
<RawHTML>{ myShortcode }</RawHTML>
</div>
);
}
})
On Create for first time
When editing the created block in next time.
Thank you in advance!
My requirement: I am trying to create a simple gutenberg block which will have a textarea
and a dropdown
. Once a user has provided the details I want to save it as a shortcode in DB, but when the user come to edit the block it should show the content in textarea
and selected item in dropdown
.
The issue that I am getting, on create everything is working as expected, but when I come to edit screen the content is getting messed up. Also the dropdown is not showing for existing block.
Here is the code that I have written with the help of this & this article.
const {__} = wp.i18n;
const {RawHTML} = wp.element;
const {registerBlockType} = wp.blocks;
const {PanelBody, SelectControl, TextareaControl} = wpponents;
const {InspectorControls} = wp.editor;
const blockStyle = {
backgroundColor: '#900',
color: '#fff',
padding: '20px',
};
const countries = [
{label: __('Select a countries', 'txtdomain'), value: ''},
{label: 'India', value: 'IND'},
{label: 'United States of America', value: 'USA'},
{label: 'Sri Lanka', value: 'LKA'}
];
registerBlockType('myplugin/example-01-block', {
title: 'My Plugin',
icon: 'editor-code',
category: 'common',
attributes: {
content: {
type: "string",
source: "text",
default: '// Place your text here!',
},
country: {
type: "string",
default: ""
},
},
example: {},
edit: (props) => {
const {
attributes: {content},
setAttributes,
className,
country
} = props;
return (
<div className={ className }>
<InspectorControls>
<PanelBody
title={ __('My Plugin Settings', 'txtdomain') }
>
<SelectControl
label={ __('Country', 'txtdomain') }
value={ country }
options={ countries }
onChange={ value => setAttributes({country: value}) }
/>
</PanelBody>
</InspectorControls>
<TextareaControl
value={ content }
onChange={ value => setAttributes({content: value}) }
/>
</div>
);
},
save: (props) => {
const {
attributes: {content, country}
} = props;
var myShortcode = '[myshortcode country="' + country + '", text="' + content + '"]';
return (
<div>
<RawHTML>{ myShortcode }</RawHTML>
</div>
);
}
})
On Create for first time
When editing the created block in next time.
Thank you in advance!
Share Improve this question asked Jun 15, 2021 at 7:19 Raunak GuptaRaunak Gupta 2593 silver badges14 bronze badges1 Answer
Reset to default 1The main issue in your code is the following part, whereby you set the attribute source to text
which means (after the block/post is saved) the editor will read the value from the inner text of the div returned by your save
function:
attributes: {
content: {
type: "string",
source: "text", // this shouldn't be "text"
default: '// Place your text here!',
},
So if the save
function returned an element with the HTML <div>[myshortcode country="IND", text="Hello World!"]</div>
, then with the above attribute settings, the content
value would be [myshortcode country="IND", text="Hello World!"]
which explains why this happened:
So you should just omit the source
property there, i.e. do not specify the attribute source, and just let the editor stores the attribute value in the block comment delimiter.
Other Issues in your code
wp.editor.InspectorControls
is deprecated and usewp.blockEditor.InspectorControls
instead. So useconst { InspectorControls } = wp.blockEditor;
.In your
edit
function,country
should be part of theattributes
like so:const { attributes: {content, country}, // "country" belongs here setAttributes, className, country // NOT HERE, so remove this. } = props;
There's an unwanted
,
(comma) in your shortcode ([myshortcode country="' + country + '", text=...
), so you should remove that comma.
本文标签: plugin developmentGutenberg Block showing invalid content on edit
版权声明:本文标题:plugin development - Gutenberg Block showing invalid content on edit 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503478a2382183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论