$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>block editor - Using applyFormat (from @wordpressrich-text) to make persistent changes to Gutenberg's rich text value|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

block editor - Using applyFormat (from @wordpressrich-text) to make persistent changes to Gutenberg's rich text value

matteradmin9PV0评论

I am working on a Gutenberg sidebar plugin which does some text analysis and based on that, it needs to annotate text in the paragraph blocks. This is the easier part and is achieved using the Annotations API by iterating over each block like this:

wp.data.dispatch( 'core/annotations' ).__experimentalAddAnnotation( {
    source: "my-annotations-plugin",
    blockClientId: wp.data.select( 'core/editor' ).getBlockOrder()[0],
    richTextIdentifier: "content",
    range: {
        start: 50,
        end: 100,
    },
} );

Now, the challenge that I am facing is persisting these annotations (as that's the requirement of the plugin). I figured out that Annotations API internally uses applyFormat method of @wordpress/rich-text package but I am not able to figure out how to use applyFormat directly. The documentation is in-adequate and lacks code examples.

If you have worked with this it would help to have sample working (ES5 or ES6) code to use applyFormat in the right way.

I am working on a Gutenberg sidebar plugin which does some text analysis and based on that, it needs to annotate text in the paragraph blocks. This is the easier part and is achieved using the Annotations API by iterating over each block like this:

wp.data.dispatch( 'core/annotations' ).__experimentalAddAnnotation( {
    source: "my-annotations-plugin",
    blockClientId: wp.data.select( 'core/editor' ).getBlockOrder()[0],
    richTextIdentifier: "content",
    range: {
        start: 50,
        end: 100,
    },
} );

Now, the challenge that I am facing is persisting these annotations (as that's the requirement of the plugin). I figured out that Annotations API internally uses applyFormat method of @wordpress/rich-text package but I am not able to figure out how to use applyFormat directly. The documentation is in-adequate and lacks code examples.

If you have worked with this it would help to have sample working (ES5 or ES6) code to use applyFormat in the right way.

Share Improve this question asked Jan 14, 2019 at 11:18 Akshay RajeAkshay Raje 1311 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I finally figured it out. Just posting it here if anyone needs to do this as the Gutenberg documentation lacks code examples.

With reference to the below code, blockIndex is the block you are dealing with; and startIndex and endIndex are ranges to annotate in context of the block:

// Get latest modified content of the block
let html = wp.data.select( "core/editor" ).getBlocks()[blockIndex].attributes.content;
// Get uuid of the block
let blockUid = wp.data.select( "core/editor" ).getBlocks()[blockIndex].clientId;
// Create a RichText value from HTML string of block content
let value = wp.richText.create({
  html
});
// Apply a format object to a Rich Text value from the given startIndex to the given endIndex
value = wp.richText.applyFormat(value, { type: 'strong' }, startIndex, endIndex);
// Update the block with new content
wp.data.dispatch( 'core/editor' ).updateBlock( blockUid, {
    attributes: {
      content: wp.richText.toHTMLString({
        value
      })
    }
  } );
Post a comment

comment list (0)

  1. No comments so far