Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tkoop opened this issue Dec 31, 2020 · 4 comments
Open

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

tkoop opened this issue Dec 31, 2020 · 4 comments
Labels
question Further information is requested

Comments

@tkoop
Copy link

tkoop commented Dec 31, 2020

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?

@hanhdt hanhdt added the question Further information is requested label Jan 2, 2021
@hanhdt
Copy link
Owner

hanhdt commented Jan 2, 2021

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

@tkoop
Copy link
Author

tkoop commented Jan 2, 2021

@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.

@Znarkus
Copy link

Znarkus commented Feb 19, 2021

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.

@Arns
Copy link

Arns commented Jul 23, 2021

@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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants