[英]VUE-COMPOSITION-API: why does ref not feel reactive on contenteditable
After I insert an image to the contenteditable
, I notice the ref()
can't "catch up" the inserted image to the text.将图像插入contenteditable
后,我注意到ref()
无法将插入的图像“赶上”到文本中。
ref()
below the button注意按钮下方显示的ref()
const insertImgTag = () => {
const image =
'<img class="separator" src="/text-image.png" width="27px" height="19px">';
let sel, range, node;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = window.getSelection().getRangeAt(0);
node = range.createContextualFragment(image);
range.insertNode(node);
}
} else if (document.selection && document.selection.createRange) {
document.selection.createRange().pasteHTML(image);
}
};
notice on the image above, the shown ref()
still counts the <img>
as one instead of two like the text注意上图,显示的ref()
仍然将<img>
计为一个,而不是像文本那样的两个
The problem is somehow, I don't get the event in the component.问题是不知何故,我没有在组件中得到事件。 This is definitively not a ref() problem.这绝对不是 ref() 问题。 But anyway, universal event forwarding is completely "hacky" with vue2 (you'll notice in contenteditable source code I have to manually forward all the events...)但无论如何,通用事件转发对于 vue2 来说完全是“hacky”(您会在 contenteditable 源代码中注意到我必须手动转发所有事件......)
So, Two options:所以,有两个选择:
I invite you to try with Vue3 (I stopped further developpment of vue-contenteditable for Vue2, only the Vue3 version is maintained)我邀请您尝试使用 Vue3(我停止为 Vue2 进一步开发 vue-contenteditable,仅维护 Vue3 版本)
If you really can't move to vue 3, then you can use this hack to "patch" your problem:如果你真的无法迁移到 vue 3,那么你可以使用这个 hack 来“修补”你的问题:
<div id="app">
<contenteditable
+++ ref="ce"
v-model="string"
tag="p"
class="text-data"
[...]
--- setup() {
+++ setup(props, {refs}) {
[...]
const insertImgTag = () => {
const image =
'<img class="separator" src="/text-image.png" width="27px" height="19px">';
let sel, range, node;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = window.getSelection().getRangeAt(0);
node = range.createContextualFragment(image);
range.insertNode(node);
}
} else if (document.selection && document.selection.createRange) {
document.selection.createRange().pasteHTML(image);
}
+++ string.value = refs.ce.current_content();
};
https://codesandbox.io/s/lucid-glitter-8c4qp?file=/src/App.vue (note:the {refs} in setup is a vue-compositionapi hack because the vue3 way of doing it can't be implemented with vue2) https://codesandbox.io/s/lucid-glitter-8c4qp?file=/src/App.vue (注意:设置中的 {refs} 是 vue-compositionapi hack,因为 vue3 的执行方式不能用 vue2 实现)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.