• Resolved Andry

    (@blackstar1991)


    Hi, I am creating a gutenberg block that gives the ability to assign a class to selected text in Block. Here is my code. This is my code in edit.js

    const { __ } = wp.i18n;
    const { toggleFormat } = wp.richText;
    const { RichTextToolbarButton, RichTextShortcut } = wp.editor;
    const { registerFormatType } = wp.richText;
    
    const name = 'block-fields/add-class';
    
    const add_class = {
    	name,
    	title: __('Add Class'),
    	tagName: 'span',
    	className: 'wp-block-custom-red',
    	edit({ isActive, value, onChange }) {
    		const onToggle = () => {
    			onChange(toggleFormat(value, { type: name }), { tagName: 'span', className: isActive ? 'wp-block-custom-red' : null });
    		};
    		return (
    			<>
    				<RichTextShortcut
    					type="primary"
    					character="u"
    					onUse={onToggle}
    				/>
    				<RichTextToolbarButton
    					icon="editor-paste-word"
    					title={__('Add Class')}
    					onClick={onToggle}
    					isActive={isActive}
    				/>
    			</>
    		);
    	},
    };
    
    function registerFormats() {
    	registerFormatType(name, add_class);
    }
    
    registerFormats();
    
    export default add_class;

    This code works, but I’m seeing errors in the developer console. Can you tell me what causes them and how to get rid of them ?
    Also, I would be grateful if you could explain how to move this functionality to quick buttons ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • What are the errors in the console?

    Thread Starter Andry

    (@blackstar1991)

    Unfortunately I don’t understand how to solve this.

    blocks.min.js:19 The "edit" property must be a valid component.
    VM3190:3 Symbol.observable as defined by Redux and Redux DevTools do not match. This could cause your app to behave differently if the DevTools are not loaded. Consider polyfilling Symbol.observable before Redux is imported or avoid polyfilling Symbol.observable altogether.
    blocks.min.js:19 The "edit" property must be a valid component.
    VM3190:3 Symbol.observable as defined by Redux and Redux DevTools do not match. This could cause your app to behave differently if the DevTools are not loaded. Consider polyfilling Symbol.observable before Redux is imported or avoid polyfilling Symbol.observable altogether.

    You are calling edit incorrectly within add_class. You are using the registerFormatType() function to call add_class(). Have a look at the documentation, there is an example of the use of edit in the constellation: https://developer.www.ads-software.com/block-editor/how-to-guides/format-api/

    Thread Starter Andry

    (@blackstar1991)

    Hmmm, I rewrote it as in the example, but now it doesn’t work.

    const { __ } = wp.i18n;
    const { registerFormatType, toggleFormat } = wp.richText;
    //const { RichTextToolbarButton, RichTextShortcut } = wp.editor;
    const { ToolbarGroup, ToolbarButton } = wp.components;
    const { BlockControls } = wp.block-editor;
    
    const name = 'block-fields/add-class';
    const add_class = {
    	name,
    	title: __('Add Class'),
    	tagName: 'span',
    	className: null,
    	edit({ isActive, value, onChange }) {
    		const onToggle = () => {
    			onChange(
    				toggleFormat(value, { type: name }),
    				applyFormat(value, {
    					type: name,
    					attributes: { tagName: 'span', className: isActive ? 'wp-block-custom-red' : null }
    				})
    			);
    		};
    		return (
    			<BlockControls>
    				<ToolbarGroup>
    					<ToolbarButton
    						icon="editor-paste-word"
    						title={__('Add Class')}
    						onClick={onToggle}
    						isActive={ isActive }
    					/>
    				</ToolbarGroup>
    			</BlockControls>
    		);
    	},
    };
    
    function registerFormats() {
    	registerFormatType(name, add_class);
    }
    
    registerFormats();
    
    export default add_class;

    I got a problem – “Uncaught ReferenceError: editor is not defined”

    Can you give any other tips ? *I instaled

    npm install @wordpress/editor --save
    Thread Starter Andry

    (@blackstar1991)

    I find the problem in bad instaletion of component.

    const { __ } = wp.i18n;
    const { registerFormatType, toggleFormat, applyFormat } = wp.richText;
    const { ToolbarGroup, ToolbarButton } = wp.components;
    import { BlockControls } from '@wordpress/block-editor';
    import { registerShortcut } from '@wordpress/keycodes';
    
    
    
    const name = 'block-fields/add-class';
    const className = 'wp-block-custom-red';
    
    const add_class = {
    	name,
    	title: __('Add Class'),
    	tagName: 'span',
    	className,
    	edit({ isActive, value, onChange }) {
    		const onToggle = () => {
    			onChange(
    				toggleFormat(value, { type: name }),
    				applyFormat(value, {
    					type: name,
    					attributes: { tagName: 'span', className: isActive ? className : null }
    				})
    			);
    		};
    
    		return (
    			<BlockControls>
    				<ToolbarGroup>
    					<ToolbarButton
    						icon="editor-paste-word"
    						title={__('Add Class')}
    						onClick={onToggle}
    						isActive={isActive}
    					/>
    				</ToolbarGroup>
    			</BlockControls>
    		);
    	},
    };
    
    function registerFormats() {
    	registerFormatType(name, add_class);
    }
    
    registerFormats();
    
    export default add_class;
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Guttenberg block that add class for selected element.’ is closed to new replies.