2

Problem

I want to wrap some plain text in a Vue component dynamically, using mouseUp after selecting the text.

For example: <div> Hello World! </div> => <div> <Greeting/> World! </div>

Aprox solution

Right now, I'm wrapping it using document.createElement("span") and surrounding the range of the selection with it.

I have found similar questions like the next one, but I'm trying to avoid render level:

How can I dynamically wrap a substring with a component in Vue.js?

1 Answer 1

2

Well, I finally managed to solve it. (Before submitting, I spend a lot of time figuring out. I don't know how I solved it so fast)

Solution

 var ComponentClass = Vue.extend(Annotation);
            var instance = new ComponentClass();
            instance.$mount();
            this.range.deleteContents();
            this.range.insertNode(instance.$el);

Where: Annotation is a Vue component and the Range is the selection range obtained previously.

I hope this help someone else!

2
  • Well, if you solved it, you might as well accept your own answer.
    – Ken Kinder
    Commented Jun 16, 2020 at 18:01
  • @Ken Kinder I have to wait till tomorrow haha
    – pxstrana
    Commented Jun 16, 2020 at 18:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.