Tiptap: Setting the selection programmatically

Created on 21 Nov 2018  路  4Comments  路  Source: ueberdosis/tiptap

Is there a way to set the selection programmatically? Looking at the Editor.js interface I did not spot something related to setting the selection. I need to be able to set the caret position on a double click event (not the default single click). Would I have to resort to the underlying ProseMirror?

Thank you!

Most helpful comment

There is a method now.

editor.setSelection(from, to)
editor.focus()

All 4 comments

Hey,

working with selection could definitely a bit easier with prosemirror 馃槄
A few days ago I've changed how links works. So if you click on a link, the full link is selected so the bubble menu gets triggered and you could edit or delete your link. this looks something like that:

nov-22-2018 22-26-16

I've added a prosemirror plugin for doing this:

import { Mark, Plugin, TextSelection } from 'tiptap'
import { getMarkRange } from 'tiptap-utils'

export default class Link extends Mark {

  // ...

  get plugins() {
    return [
      new Plugin({
        props: {
          handleClick(view, pos) {
            const { schema, doc, tr } = view.state
            const range = getMarkRange(doc.resolve(pos), schema.marks.link)

            if (!range) {
              return
            }

            const $start = doc.resolve(range.from)
            const $end = doc.resolve(range.to)
            const transaction = tr.setSelection(new TextSelection($start, $end))

            view.dispatch(transaction)
          },
        },
      }),
    ]
  }

}

Maybe that helps you a bit. Instead of handleClick there is also a handleDoubleClick event (docs).

Yes this seems quite relevant to what I am trying to do. When I manage to make it work correctly I shall post my code here.

There is a method now.

editor.setSelection(from, to)
editor.focus()

Hey,

working with selection could definitely a bit easier with prosemirror 馃槄
A few days ago I've changed how links works. So if you click on a link, the full link is selected so the bubble menu gets triggered and you could edit or delete your link. this looks something like that:

nov-22-2018 22-26-16

I've added a prosemirror plugin for doing this:

import { Mark, Plugin, TextSelection } from 'tiptap'
import { getMarkRange } from 'tiptap-utils'

export default class Link extends Mark {

  // ...

  get plugins() {
    return [
      new Plugin({
        props: {
          handleClick(view, pos) {
            const { schema, doc, tr } = view.state
            const range = getMarkRange(doc.resolve(pos), schema.marks.link)

            if (!range) {
              return
            }

            const $start = doc.resolve(range.from)
            const $end = doc.resolve(range.to)
            const transaction = tr.setSelection(new TextSelection($start, $end))

            view.dispatch(transaction)
          },
        },
      }),
    ]
  }

}

Maybe that helps you a bit. Instead of handleClick there is also a handleDoubleClick event (docs).

you save me. thank you ! But why it's not the actual behavior of link extension?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

unikitty37 picture unikitty37  路  3Comments

Auxxxxlx picture Auxxxxlx  路  3Comments

connecteev picture connecteev  路  3Comments

pk-pressf1 picture pk-pressf1  路  3Comments

asseti6 picture asseti6  路  3Comments