How to get Cursor Position from q-editor?



  • I need access to the current cursor position in the q-editor.
    I also looked at this thread:
    https://forum.quasar-framework.org/topic/4702/how-to-get-the-cursor-position-form-q-input/5

    But the above link only works for q-input

    The following code returns cursorPos: undefined on the console.

    <q-editor v-model="text" ref="editorval" @input="changeInput"/>
    

    and

    changeInput(value) {
       let editor = this.$refs.editorval
       if (editor) {
         this.cursorPosition = editor.selectionStart;
         console.log('cursorPos:', this.cursorPosition)
       }
       this.cursorPosition = 0;
    }
    

    What is the correct way to do this?



  • try this

    changeInput (value) {
          console.log(this.$refs.editorval.$refs.content)
          const content = this.$refs.editorval.$refs.content
          if (content) {
            if (window.getSelection) { // ie11 10 9 ff safari
              var range = window.getSelection()
              console.log(range)
              this.cursorPosition = range.focusOffset
              console.log('cursorPos:', this.cursorPosition)
            }
            this.cursorPosition = 0
          }
        }
    

Log in to reply