vue-trix icon indicating copy to clipboard operation
vue-trix copied to clipboard

How can I get the Trix editor so I can insert text?

Open tkoop opened this issue 4 years ago • 4 comments

According to https://github.com/basecamp/trix#readme I can do something like this:

// Insert “Hello” at the beginning of the document element.editor.setSelectedRange([0, 0]) element.editor.insertString("Hello")

How can I do that with this Vue component?

tkoop avatar Dec 31 '20 23:12 tkoop

@tkoop You may try to use $refs to reference Trix in the Vue component.

hanhdt avatar Jan 02 '21 10:01 hanhdt

@tkoop You may try to use $refs to reference Trix in the Vue component.

I've tried this, but it's not working for me.

https://codesandbox.io/s/romantic-wildflower-dv5x0?fontsize=14&hidenavigation=1&theme=dark

When I click the "Insert" button, it should insert text into the editor, but it doesn't. Look in App.vue lines 25 and 26.

tkoop avatar Jan 02 '21 21:01 tkoop

Change your insertText function to

    insertText: function () {
      console.log("Button clicked");
      this.$refs.editor.$refs.trix.editor.setSelectedRange([0, 0]);
      this.$refs.editor.$refs.trix.editor.insertString("Hello");
    },

You need to access the trix editor inside the component. this.$refs.editor in your example is only the VueTrix instance.

Znarkus avatar Feb 19 '21 08:02 Znarkus

@Znarkus, this seems to work for inserting at a given position, but I cannot select a range of text: setSelectedRange([0,3]). It simply puts the cursor in the first position before all of the text no matter which arguments I provide to setSelectedRange. I cannot figure out how to make it work...

Arns avatar Jul 23 '21 19:07 Arns