Gutenberg: Allow additional formatting in RichText
up vote
1
down vote
favorite
I have created a fairly simple accordion block, and it works great for basic text. The problem is that the control I am using for the accordion content is the RichText, which only allows for basic formatting such as bold.
What if I wanted to create an Unordered List as well as basic text? I am currently using multiline: "p"
, but how can I add additional elements so that I can also have UL elements in there as well?
The only two ideas I can think of, I cannot figure out how to implement. The first is to extend the block toolbar with BlockControls
to include additional formatters for UL, and the second is to use another element instead of RichText - such as Freeform (which might have been renamed to Classic Editor?) - but I cannot find any documentation on these.
Here is an example of my current code:
ATTRIBUTES
attributes: {
title: {
type: 'string',
selector: '.hd-accordion-title',
},
content: {
type: 'array',
source: 'children',
selector: '.hd-accordion-content',
}
},
EDIT
edit: function( props ) {
var title = props.attributes.title;
var content = props.attributes.content;
function onChangeTitle(newTitle) {
props.setAttributes({
title: newTitle
});
}
function onChangeContent(newContent) {
props.setAttributes({
content: newContent
});
}
return [
(
<div className={"hd-accordion"}>
<RichText
tagName="h3"
className= "hd-accordion-title"
value= { title }
onChange= { onChangeTitle }
placeholder = "Title"
keepPlaceholderOnFocus = { true }
multiline= { false }
/>
<RichText
tagName="div"
className="hd-accordion-content"
value={ content }
onChange= { onChangeContent }
placeholder = "content"
multiline="p"
/>
</div>
)
];
},
wordpress gutenberg wordpress-gutenberg gutenberg-blocks
add a comment |
up vote
1
down vote
favorite
I have created a fairly simple accordion block, and it works great for basic text. The problem is that the control I am using for the accordion content is the RichText, which only allows for basic formatting such as bold.
What if I wanted to create an Unordered List as well as basic text? I am currently using multiline: "p"
, but how can I add additional elements so that I can also have UL elements in there as well?
The only two ideas I can think of, I cannot figure out how to implement. The first is to extend the block toolbar with BlockControls
to include additional formatters for UL, and the second is to use another element instead of RichText - such as Freeform (which might have been renamed to Classic Editor?) - but I cannot find any documentation on these.
Here is an example of my current code:
ATTRIBUTES
attributes: {
title: {
type: 'string',
selector: '.hd-accordion-title',
},
content: {
type: 'array',
source: 'children',
selector: '.hd-accordion-content',
}
},
EDIT
edit: function( props ) {
var title = props.attributes.title;
var content = props.attributes.content;
function onChangeTitle(newTitle) {
props.setAttributes({
title: newTitle
});
}
function onChangeContent(newContent) {
props.setAttributes({
content: newContent
});
}
return [
(
<div className={"hd-accordion"}>
<RichText
tagName="h3"
className= "hd-accordion-title"
value= { title }
onChange= { onChangeTitle }
placeholder = "Title"
keepPlaceholderOnFocus = { true }
multiline= { false }
/>
<RichText
tagName="div"
className="hd-accordion-content"
value={ content }
onChange= { onChangeContent }
placeholder = "content"
multiline="p"
/>
</div>
)
];
},
wordpress gutenberg wordpress-gutenberg gutenberg-blocks
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have created a fairly simple accordion block, and it works great for basic text. The problem is that the control I am using for the accordion content is the RichText, which only allows for basic formatting such as bold.
What if I wanted to create an Unordered List as well as basic text? I am currently using multiline: "p"
, but how can I add additional elements so that I can also have UL elements in there as well?
The only two ideas I can think of, I cannot figure out how to implement. The first is to extend the block toolbar with BlockControls
to include additional formatters for UL, and the second is to use another element instead of RichText - such as Freeform (which might have been renamed to Classic Editor?) - but I cannot find any documentation on these.
Here is an example of my current code:
ATTRIBUTES
attributes: {
title: {
type: 'string',
selector: '.hd-accordion-title',
},
content: {
type: 'array',
source: 'children',
selector: '.hd-accordion-content',
}
},
EDIT
edit: function( props ) {
var title = props.attributes.title;
var content = props.attributes.content;
function onChangeTitle(newTitle) {
props.setAttributes({
title: newTitle
});
}
function onChangeContent(newContent) {
props.setAttributes({
content: newContent
});
}
return [
(
<div className={"hd-accordion"}>
<RichText
tagName="h3"
className= "hd-accordion-title"
value= { title }
onChange= { onChangeTitle }
placeholder = "Title"
keepPlaceholderOnFocus = { true }
multiline= { false }
/>
<RichText
tagName="div"
className="hd-accordion-content"
value={ content }
onChange= { onChangeContent }
placeholder = "content"
multiline="p"
/>
</div>
)
];
},
wordpress gutenberg wordpress-gutenberg gutenberg-blocks
I have created a fairly simple accordion block, and it works great for basic text. The problem is that the control I am using for the accordion content is the RichText, which only allows for basic formatting such as bold.
What if I wanted to create an Unordered List as well as basic text? I am currently using multiline: "p"
, but how can I add additional elements so that I can also have UL elements in there as well?
The only two ideas I can think of, I cannot figure out how to implement. The first is to extend the block toolbar with BlockControls
to include additional formatters for UL, and the second is to use another element instead of RichText - such as Freeform (which might have been renamed to Classic Editor?) - but I cannot find any documentation on these.
Here is an example of my current code:
ATTRIBUTES
attributes: {
title: {
type: 'string',
selector: '.hd-accordion-title',
},
content: {
type: 'array',
source: 'children',
selector: '.hd-accordion-content',
}
},
EDIT
edit: function( props ) {
var title = props.attributes.title;
var content = props.attributes.content;
function onChangeTitle(newTitle) {
props.setAttributes({
title: newTitle
});
}
function onChangeContent(newContent) {
props.setAttributes({
content: newContent
});
}
return [
(
<div className={"hd-accordion"}>
<RichText
tagName="h3"
className= "hd-accordion-title"
value= { title }
onChange= { onChangeTitle }
placeholder = "Title"
keepPlaceholderOnFocus = { true }
multiline= { false }
/>
<RichText
tagName="div"
className="hd-accordion-content"
value={ content }
onChange= { onChangeContent }
placeholder = "content"
multiline="p"
/>
</div>
)
];
},
wordpress gutenberg wordpress-gutenberg gutenberg-blocks
wordpress gutenberg wordpress-gutenberg gutenberg-blocks
asked Nov 16 at 21:32
Harmonic
213213
213213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You can register new formatting options like this-
Adding simple formatting button
registerFormat( 'bold', {
selector: 'strong',
edit( { isActive, toggleFormat } ) {
return (
<Fragment>
<Shortcut
type="primary"
key="b"
onUse={ () => toggleFormat() }
/>
<ToolbarControls>
<ToolbarButton
icon="editor-bold",
title={ __( 'Bold' ) }
isActive ={ isActive }
onClick={ () => toggleFormat() }
/>
</ToolbarControls>
</Fragment>
);
},
} );
Adding A Link Button
registerFormat( 'link', {
selector: 'a',
attributes: {
url: {
source: 'attribute',
attribute: 'href',
},
},
edit( { isActive, removeFormat } ) {
return (
<Fragment>
<Shortcut
type="access"
key="s"
onUse={ () => removeFormat() }
/>
<Shortcut
type="access"
key="a"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<Shortcut
type="primary"
key="k"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<ToolbarControls>
{ isActive && <ToolbarButton
icon="editor-unlink",
title={ __( 'Unlink' ) }
onClick={ () => removeFormat() }
/> }
{ ! isActive && <ToolbarButton
icon="admin-links",
title={ __( 'Link' ) }
onClick={ () => /* Set state and pass to LinkContainer */ }
/> }
</ToolbarControls>
<LinkContainer { ...props } />
</Fragment>
);
},
} );
Adding an Image button
registerFormat( 'image', {
selector: 'img',
attributes: {
url: {
source: 'attribute',
attribute: 'src',
},
},
edit: class ImageFormatEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
modal: false;
};
}
openModal() {
this.setState( { modal: true } )
}
closeModal() {
this.setState( { modal: false } )
}
render() {
const { insertObject } = this.props;
return (
<Fragment>
<InserterItems>
<InserterItem
icon="inline-image",
title={ __( 'Inline Image' ) }
onClick={ openModal }
/>
</InserterItems>
{ this.state.modal && <MediaUpload
type="image"
onSelect={ ( { id, url, alt, width } ) => {
this.closeModal()
insertObject( {
src: url,
alt,
class: `wp-image-${ id }`,
style: `width: ${ Math.min( width, 150 ) }px;`,
} );
} }
onClose={ this.closeModal }
render={ ( { open } ) => {
open();
return null;
} }
/> }
</Fragment>
);
}
},
} );
You might encounter few bugs here and there. Relevant ticket
Thanks! I already discovered this (it's apparently very new), but failed miserably at getting it to work with UL, but maybe using your examples I'll have better luck. For now, I just wrote a shortcode called[list]
to wrap around any eleents that automatically turns a list of paragraphs into UL
– Harmonic
21 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can register new formatting options like this-
Adding simple formatting button
registerFormat( 'bold', {
selector: 'strong',
edit( { isActive, toggleFormat } ) {
return (
<Fragment>
<Shortcut
type="primary"
key="b"
onUse={ () => toggleFormat() }
/>
<ToolbarControls>
<ToolbarButton
icon="editor-bold",
title={ __( 'Bold' ) }
isActive ={ isActive }
onClick={ () => toggleFormat() }
/>
</ToolbarControls>
</Fragment>
);
},
} );
Adding A Link Button
registerFormat( 'link', {
selector: 'a',
attributes: {
url: {
source: 'attribute',
attribute: 'href',
},
},
edit( { isActive, removeFormat } ) {
return (
<Fragment>
<Shortcut
type="access"
key="s"
onUse={ () => removeFormat() }
/>
<Shortcut
type="access"
key="a"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<Shortcut
type="primary"
key="k"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<ToolbarControls>
{ isActive && <ToolbarButton
icon="editor-unlink",
title={ __( 'Unlink' ) }
onClick={ () => removeFormat() }
/> }
{ ! isActive && <ToolbarButton
icon="admin-links",
title={ __( 'Link' ) }
onClick={ () => /* Set state and pass to LinkContainer */ }
/> }
</ToolbarControls>
<LinkContainer { ...props } />
</Fragment>
);
},
} );
Adding an Image button
registerFormat( 'image', {
selector: 'img',
attributes: {
url: {
source: 'attribute',
attribute: 'src',
},
},
edit: class ImageFormatEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
modal: false;
};
}
openModal() {
this.setState( { modal: true } )
}
closeModal() {
this.setState( { modal: false } )
}
render() {
const { insertObject } = this.props;
return (
<Fragment>
<InserterItems>
<InserterItem
icon="inline-image",
title={ __( 'Inline Image' ) }
onClick={ openModal }
/>
</InserterItems>
{ this.state.modal && <MediaUpload
type="image"
onSelect={ ( { id, url, alt, width } ) => {
this.closeModal()
insertObject( {
src: url,
alt,
class: `wp-image-${ id }`,
style: `width: ${ Math.min( width, 150 ) }px;`,
} );
} }
onClose={ this.closeModal }
render={ ( { open } ) => {
open();
return null;
} }
/> }
</Fragment>
);
}
},
} );
You might encounter few bugs here and there. Relevant ticket
Thanks! I already discovered this (it's apparently very new), but failed miserably at getting it to work with UL, but maybe using your examples I'll have better luck. For now, I just wrote a shortcode called[list]
to wrap around any eleents that automatically turns a list of paragraphs into UL
– Harmonic
21 hours ago
add a comment |
up vote
2
down vote
accepted
You can register new formatting options like this-
Adding simple formatting button
registerFormat( 'bold', {
selector: 'strong',
edit( { isActive, toggleFormat } ) {
return (
<Fragment>
<Shortcut
type="primary"
key="b"
onUse={ () => toggleFormat() }
/>
<ToolbarControls>
<ToolbarButton
icon="editor-bold",
title={ __( 'Bold' ) }
isActive ={ isActive }
onClick={ () => toggleFormat() }
/>
</ToolbarControls>
</Fragment>
);
},
} );
Adding A Link Button
registerFormat( 'link', {
selector: 'a',
attributes: {
url: {
source: 'attribute',
attribute: 'href',
},
},
edit( { isActive, removeFormat } ) {
return (
<Fragment>
<Shortcut
type="access"
key="s"
onUse={ () => removeFormat() }
/>
<Shortcut
type="access"
key="a"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<Shortcut
type="primary"
key="k"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<ToolbarControls>
{ isActive && <ToolbarButton
icon="editor-unlink",
title={ __( 'Unlink' ) }
onClick={ () => removeFormat() }
/> }
{ ! isActive && <ToolbarButton
icon="admin-links",
title={ __( 'Link' ) }
onClick={ () => /* Set state and pass to LinkContainer */ }
/> }
</ToolbarControls>
<LinkContainer { ...props } />
</Fragment>
);
},
} );
Adding an Image button
registerFormat( 'image', {
selector: 'img',
attributes: {
url: {
source: 'attribute',
attribute: 'src',
},
},
edit: class ImageFormatEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
modal: false;
};
}
openModal() {
this.setState( { modal: true } )
}
closeModal() {
this.setState( { modal: false } )
}
render() {
const { insertObject } = this.props;
return (
<Fragment>
<InserterItems>
<InserterItem
icon="inline-image",
title={ __( 'Inline Image' ) }
onClick={ openModal }
/>
</InserterItems>
{ this.state.modal && <MediaUpload
type="image"
onSelect={ ( { id, url, alt, width } ) => {
this.closeModal()
insertObject( {
src: url,
alt,
class: `wp-image-${ id }`,
style: `width: ${ Math.min( width, 150 ) }px;`,
} );
} }
onClose={ this.closeModal }
render={ ( { open } ) => {
open();
return null;
} }
/> }
</Fragment>
);
}
},
} );
You might encounter few bugs here and there. Relevant ticket
Thanks! I already discovered this (it's apparently very new), but failed miserably at getting it to work with UL, but maybe using your examples I'll have better luck. For now, I just wrote a shortcode called[list]
to wrap around any eleents that automatically turns a list of paragraphs into UL
– Harmonic
21 hours ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can register new formatting options like this-
Adding simple formatting button
registerFormat( 'bold', {
selector: 'strong',
edit( { isActive, toggleFormat } ) {
return (
<Fragment>
<Shortcut
type="primary"
key="b"
onUse={ () => toggleFormat() }
/>
<ToolbarControls>
<ToolbarButton
icon="editor-bold",
title={ __( 'Bold' ) }
isActive ={ isActive }
onClick={ () => toggleFormat() }
/>
</ToolbarControls>
</Fragment>
);
},
} );
Adding A Link Button
registerFormat( 'link', {
selector: 'a',
attributes: {
url: {
source: 'attribute',
attribute: 'href',
},
},
edit( { isActive, removeFormat } ) {
return (
<Fragment>
<Shortcut
type="access"
key="s"
onUse={ () => removeFormat() }
/>
<Shortcut
type="access"
key="a"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<Shortcut
type="primary"
key="k"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<ToolbarControls>
{ isActive && <ToolbarButton
icon="editor-unlink",
title={ __( 'Unlink' ) }
onClick={ () => removeFormat() }
/> }
{ ! isActive && <ToolbarButton
icon="admin-links",
title={ __( 'Link' ) }
onClick={ () => /* Set state and pass to LinkContainer */ }
/> }
</ToolbarControls>
<LinkContainer { ...props } />
</Fragment>
);
},
} );
Adding an Image button
registerFormat( 'image', {
selector: 'img',
attributes: {
url: {
source: 'attribute',
attribute: 'src',
},
},
edit: class ImageFormatEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
modal: false;
};
}
openModal() {
this.setState( { modal: true } )
}
closeModal() {
this.setState( { modal: false } )
}
render() {
const { insertObject } = this.props;
return (
<Fragment>
<InserterItems>
<InserterItem
icon="inline-image",
title={ __( 'Inline Image' ) }
onClick={ openModal }
/>
</InserterItems>
{ this.state.modal && <MediaUpload
type="image"
onSelect={ ( { id, url, alt, width } ) => {
this.closeModal()
insertObject( {
src: url,
alt,
class: `wp-image-${ id }`,
style: `width: ${ Math.min( width, 150 ) }px;`,
} );
} }
onClose={ this.closeModal }
render={ ( { open } ) => {
open();
return null;
} }
/> }
</Fragment>
);
}
},
} );
You might encounter few bugs here and there. Relevant ticket
You can register new formatting options like this-
Adding simple formatting button
registerFormat( 'bold', {
selector: 'strong',
edit( { isActive, toggleFormat } ) {
return (
<Fragment>
<Shortcut
type="primary"
key="b"
onUse={ () => toggleFormat() }
/>
<ToolbarControls>
<ToolbarButton
icon="editor-bold",
title={ __( 'Bold' ) }
isActive ={ isActive }
onClick={ () => toggleFormat() }
/>
</ToolbarControls>
</Fragment>
);
},
} );
Adding A Link Button
registerFormat( 'link', {
selector: 'a',
attributes: {
url: {
source: 'attribute',
attribute: 'href',
},
},
edit( { isActive, removeFormat } ) {
return (
<Fragment>
<Shortcut
type="access"
key="s"
onUse={ () => removeFormat() }
/>
<Shortcut
type="access"
key="a"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<Shortcut
type="primary"
key="k"
onUse={ /* Set state and pass to LinkContainer */ }
/>
<ToolbarControls>
{ isActive && <ToolbarButton
icon="editor-unlink",
title={ __( 'Unlink' ) }
onClick={ () => removeFormat() }
/> }
{ ! isActive && <ToolbarButton
icon="admin-links",
title={ __( 'Link' ) }
onClick={ () => /* Set state and pass to LinkContainer */ }
/> }
</ToolbarControls>
<LinkContainer { ...props } />
</Fragment>
);
},
} );
Adding an Image button
registerFormat( 'image', {
selector: 'img',
attributes: {
url: {
source: 'attribute',
attribute: 'src',
},
},
edit: class ImageFormatEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
modal: false;
};
}
openModal() {
this.setState( { modal: true } )
}
closeModal() {
this.setState( { modal: false } )
}
render() {
const { insertObject } = this.props;
return (
<Fragment>
<InserterItems>
<InserterItem
icon="inline-image",
title={ __( 'Inline Image' ) }
onClick={ openModal }
/>
</InserterItems>
{ this.state.modal && <MediaUpload
type="image"
onSelect={ ( { id, url, alt, width } ) => {
this.closeModal()
insertObject( {
src: url,
alt,
class: `wp-image-${ id }`,
style: `width: ${ Math.min( width, 150 ) }px;`,
} );
} }
onClose={ this.closeModal }
render={ ( { open } ) => {
open();
return null;
} }
/> }
</Fragment>
);
}
},
} );
You might encounter few bugs here and there. Relevant ticket
answered yesterday
Ashiquzzaman Kiron
270110
270110
Thanks! I already discovered this (it's apparently very new), but failed miserably at getting it to work with UL, but maybe using your examples I'll have better luck. For now, I just wrote a shortcode called[list]
to wrap around any eleents that automatically turns a list of paragraphs into UL
– Harmonic
21 hours ago
add a comment |
Thanks! I already discovered this (it's apparently very new), but failed miserably at getting it to work with UL, but maybe using your examples I'll have better luck. For now, I just wrote a shortcode called[list]
to wrap around any eleents that automatically turns a list of paragraphs into UL
– Harmonic
21 hours ago
Thanks! I already discovered this (it's apparently very new), but failed miserably at getting it to work with UL, but maybe using your examples I'll have better luck. For now, I just wrote a shortcode called
[list]
to wrap around any eleents that automatically turns a list of paragraphs into UL– Harmonic
21 hours ago
Thanks! I already discovered this (it's apparently very new), but failed miserably at getting it to work with UL, but maybe using your examples I'll have better luck. For now, I just wrote a shortcode called
[list]
to wrap around any eleents that automatically turns a list of paragraphs into UL– Harmonic
21 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345703%2fgutenberg-allow-additional-formatting-in-richtext%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown