admin管理员组文章数量:1122846
In the specific case where you're using "Convert to Blocks," the selection might be happening inside a custom RichText component or other block structures that Gutenberg uses. These components don't directly expose their selected text the way regular HTML elements do. Potential Reasons Why It Doesn't Work:
Gutenberg Virtual DOM: Gutenberg uses a virtual DOM through React, so selections may not be happening in the plain DOM but within a component's managed state. This can prevent window.getSelection() from behaving as expected.
Custom Block Structures: After converting to blocks, the editor is likely rendering text inside a RichText component or other block wrappers, and window.getSelection() won't properly capture selections within these components.
Reactivity of Gutenberg: Gutenberg manages its blocks in a reactive way (using React), meaning changes in selection and content may not be reflected in the DOM in the same way they would in a simple HTML environment
In the specific case where you're using "Convert to Blocks," the selection might be happening inside a custom RichText component or other block structures that Gutenberg uses. These components don't directly expose their selected text the way regular HTML elements do. Potential Reasons Why It Doesn't Work:
Gutenberg Virtual DOM: Gutenberg uses a virtual DOM through React, so selections may not be happening in the plain DOM but within a component's managed state. This can prevent window.getSelection() from behaving as expected.
Custom Block Structures: After converting to blocks, the editor is likely rendering text inside a RichText component or other block wrappers, and window.getSelection() won't properly capture selections within these components.
Reactivity of Gutenberg: Gutenberg manages its blocks in a reactive way (using React), meaning changes in selection and content may not be reflected in the DOM in the same way they would in a simple HTML environment
Share
Improve this question
edited Sep 29, 2024 at 17:02
user250199
asked Sep 29, 2024 at 10:14
user250199user250199
112 bronze badges
1 Answer
Reset to default 1We have to select text from the Gutenberg editor. The purpose is to store and replace text. Please use this code to help us with you
const { select } = wp.data;
const selectedBlock = select('core/block-editor').getSelectedBlock();
if (selectedBlock) {
const selectedText = selectedBlock.attributes.content;
console.log("Selected Text:", selectedText);
// Store the selectedText as needed.
}
Replacing Text within a Block
const { dispatch } = wp.data;
const newText = "New Text to Replace";
dispatch('core/block-editor').updateBlockAttributes(selectedBlock.clientId, {
content: newText
});
I am saving and Reusing Selected Text using the console.log()
localStorage.setItem('selectedText', selectedText);
const storedText = localStorage.getItem('selectedText');
console.log("Stored Text:", storedText);
const { registerPlugin } = wp.plugins;
const { PluginBlockSettingsMenuItem } = wp.editPost;
const MyReplaceTextPlugin = () => (
<PluginBlockSettingsMenuItem
label="Replace Text"
onClick={() => {
const newText = "Replaced Text!";
dispatch('core/block-editor').updateBlockAttributes(selectedBlock.clientId, {
content: newText
});
}}
/>
);
registerPlugin('my-replace-text-plugin', {
render: MyReplaceTextPlugin
});
本文标签: pluginsI have to select text from gutenberg editor Purpose is to store and replace text
版权声明:本文标题:plugins - I have to select text from gutenberg editor. Purpose is to store and replace text 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287044a1927797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论