@gechiui/block-editor
Version:
186 lines (131 loc) • 5.2 kB
Markdown
Render a rich [`contenteditable` input](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content), providing users with the option to format the content.
_Required._ HTML string to make editable. The HTML should be valid, and valid inside the `tagName`, if provided.
_Required._ Called when the value changes.
_Default: `div`._ The [tag name](https://www.w3.org/TR/html51/syntax.html#tag-name) of the editable element.
_Optional._ Placeholder text to show when the field is empty, similar to the
[`input` and `textarea` attribute of the same name](https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/HTML5_updates#The_placeholder_attribute).
_Optional._ By default, a line break will be inserted on <kbd>Enter</kbd>. If the editable field can contain multiple paragraphs, this property can be set to create new paragraphs on <kbd>Enter</kbd>.
_Optional._ Called when the content can be split, where `value` is a piece of content being split off. Here you should create a new block with that content and return it. Note that you also need to provide `onReplace` in order for this to take any effect.
_Optional._ Called when the `RichText` instance can be replaced with the given blocks.
_Optional._ Called when blocks can be merged. `forward` is true when merging with the next block, false when merging with the previous block.
_Optional._ Called when the block can be removed. `forward` is true when the selection is expected to move to the next block, false to the previous block.
_Optional._ By default, all registered formats are allowed. This setting can be used to fine-tune the allowed formats. Example: `[ 'core/bold', 'core/link' ]`.
_Optional._ By default, all formatting controls are present. This setting can be used to remove formatting controls that would make content [interactive](https://html.spec.whatwg.org/multipage/dom.html#interactive-content). This is useful if you want to make content that is already interactive editable.
_Optional._ Whether to show the input is selected or not in order to show the formatting controls. By default it renders the controls when the block is selected.
_Optional._ A list of autocompleters to use instead of the default.
_Optional._ Whether or not to preserve white space characters in the `value`. Normally tab, newline and space characters are collapsed to a single space. If turned on, soft line breaks will be saved as newline characters, not as line break elements.
`RichText.Content` should be used in the `save` function of your block to correctly save rich text content.
{% codetabs %}
{% ES5 %}
```js
gc.blocks.registerBlockType( /* ... */, {
// ...
attributes: {
content: {
source: 'html',
selector: 'h2',
},
},
edit: function( props ) {
return gc.element.createElement( gc.editor.RichText, {
tagName: 'h2',
className: props.className,
value: props.attributes.content,
onChange: function( content ) {
props.setAttributes( { content: content } );
}
} );
},
save: function( props ) {
return gc.element.createElement( gc.editor.RichText.Content, {
tagName: 'h2', value: props.attributes.content
} );
}
} );
```
{% ESNext %}
```js
const { registerBlockType } = gc.blocks;
const { RichText } = gc.editor;
registerBlockType( /* ... */, {
// ...
attributes: {
content: {
source: 'html',
selector: 'h2',
},
},
edit( { className, attributes, setAttributes } ) {
return (
<RichText
tagName="h2"
className={ className }
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
/>
);
},
save( { attributes } ) {
return <RichText.Content tagName="h2" value={ attributes.content } />;
}
} );
```
{% end %}
Slot to extend the format toolbar. Use it in the edit function of a `registerFormatType` call to surface the format to the UI.
{% codetabs %}
{% ES5 %}
```js
gc.richText.registerFormatType( /* ... */, {
/* ... */
edit: function( props ) {
return gc.element.createElement(
gc.editor.RichTextToolbarButton, {
icon: 'editor-code',
title: 'My formatting button',
onClick: function() { /* ... */ }
isActive: props.isActive,
} );
},
/* ... */
} );
```
{% ESNext %}
```js
import { registerFormatType } from 'gc-rich-text';
import { richTextToolbarButton } from 'gc-editor';
registerFormatType( /* ... */, {
/* ... */
edit( { isActive } ) {
return (
<RichTextToolbarButton
icon={ 'editor-code' }
title={ 'My formatting button' }
onClick={ /* ... */ }
isActive={ isActive }
/>
);
},
/* ... */
} );
```
{% end %}