Skip to content

Commit

Permalink
refactor rich text charater limit example block
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiankaegy committed Aug 24, 2023
1 parent 94956d0 commit 7205b6e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
19 changes: 19 additions & 0 deletions example/src/blocks/rich-text-character-limit/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "example/rich-text-character-limit",
"apiVersion": 2,
"title": "Rich Text Character Limit",
"description": "Example Block to show the Rich Text Character Limit in usage",
"icon": "smiley",
"category": "common",
"example": {},
"supports": {
"html": false
},
"attributes": {
"title": {
"type": "string",
"default": ""
}
},
"editorScript": "file:./index.js"
}
29 changes: 29 additions & 0 deletions example/src/blocks/rich-text-character-limit/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { __ } from '@wordpress/i18n';
import { useBlockProps } from '@wordpress/block-editor';
import { RichTextCharacterLimit } from '@10up/block-components';

export const BlockEdit = (props) => {
const {
attributes: { title },
setAttributes
} = props;

const blockProps = useBlockProps();

return (
<div {...blockProps}>
<RichTextCharacterLimit
limit={30}
enforce={true}
tagName="h2"
value={title}
placeholder={__('Enter some text', 'example')}
onChange={(title) => setAttributes({ title })}
allowedFormats={[
'core/bold',
'core/link'
]}
/>
</div>
)
};
51 changes: 5 additions & 46 deletions example/src/blocks/rich-text-character-limit/index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
import { registerBlockType } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { useBlockProps } from '@wordpress/block-editor';
import { RichTextCharacterLimit } from '@10up/block-components';

const NAMESPACE = 'example';
import metadata from './block.json';
import { BlockEdit } from './edit';

registerBlockType( `${ NAMESPACE }/rich-text-character-limit`, {
apiVersion: 2,
title: __( 'Rich Text Character Limit', NAMESPACE ),
description: __( 'Example Block to show the Rich Text Character Limit in usage', NAMESPACE ),
icon: 'smiley',
category: 'common',
example: {},
supports: {
html: false
},
attributes: {
title: {
type: 'string',
default: ''
}
},
edit: (props) => {
const {
attributes: { title },
setAttributes
} = props;

const blockProps = useBlockProps();

return (
<div {...blockProps}>
<RichTextCharacterLimit
limit={30}
enforce={true}
tagName="h2"
value={title}
placeholder={ __( 'Enter some text', NAMESPACE ) }
onChange={(title) => setAttributes({title})}
allowedFormats={[
'core/bold',
'core/link'
]}
/>
</div>
)
},
registerBlockType(metadata, {
edit: BlockEdit,
save: () => null
} );
});

0 comments on commit 7205b6e

Please sign in to comment.