我正试图从外部调用中应用一个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 }
)
}
} );
解决了。开始和结束选择实际上是对象:) 使用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 }
)
}
} );