如何从外部调用中在古腾堡块内进行applyFormat?

问题描述 投票:1回答:1

我正试图从外部调用中应用一个Richtext格式。目前唯一失败的是applyFormat。我没有收到任何错误,但格式也没有被应用。传递给调度器的值持有新的activeFormat,但它从未被应用。有什么解决方法吗?

   var html = wp.data.select('core/block-editor').getSelectedBlock().attributes.content;
   var blockUid = wp.data.select('core/block-editor').getSelectedBlock().clientId;
   var value = wp.richText.create({
                  html 
                });
   value = wp.richText.applyFormat(value, { type: 'core/bold' }, 
           wp.data.select('core/block-editor').getSelectionStart(), 
           wp.data.select('core/block-editor').getSelectionEnd()
           );

    wp.data.dispatch( 'core/block-editor' ).updateBlock( blockUid, {
           attributes: {
              content: wp.richText.toHTMLString(
                       {value } 
                        )
           }
    } );
javascript wordpress api wordpress-gutenberg richtext
1个回答
0
投票

解决了。开始和结束选择实际上是对象:) 使用offset就可以了! 下面是工作解决方案。

   var html = wp.data.select('core/block-editor').getSelectedBlock().attributes.content;
   var blockUid = wp.data.select('core/block-editor').getSelectedBlock().clientId;
   var value = wp.richText.create({
                  html 
                });
   value = wp.richText.applyFormat(value, { type: 'core/bold' }, 
           wp.data.select('core/block-editor').getSelectionStart().offset, 
           wp.data.select('core/block-editor').getSelectionEnd().offset
           );

    wp.data.dispatch( 'core/block-editor' ).updateBlock( blockUid, {
           attributes: {
              content: wp.richText.toHTMLString(
                       {value } 
                        )
           }
    } );
© www.soinside.com 2019 - 2024. All rights reserved.