Change transformPasted behaviour when shift key is pressed

I have a Plugin that implements transformPasted so that URLs get turned into link or image nodes.

I want to be able able to say if(metaKeyIsPressed) return so that, for example, CMD+SHIFT+V on Mac would just insert the URL as plain text.

Is this possible with the way pasting works right now?

1 Like

Browser paste events unfortunately don’t come with modifier key info, so the only way that I’m aware of for doing this is to use key events to track the status of the meta key (which is hard to do reliably, but corner cases like switching focus while holding a modifier key tend to be rare, so might be good enough).

What a coincidence, we also need to do the same thing. I was about to ask how to do this.

1 Like

That’s kind of weird. I’m assuming that must be how Google Docs and other editors do it, so hopefully it will work okay. Will report back once I’ve tried it out.

1 Like

The following setup seems to be working pretty well for me.

export function links(schema) {
  let shiftKey = false
  return new Plugin({
    props: {
      handleKeyDown(_, event) {
        shiftKey = event.shiftKey
        return false
      },
      transformPasted(slice) {

        if (shiftKey) {
          return slice
        }

        return new Slice(
          linkify(slice.content), 
          slice.openStart, 
          slice.openEnd
        )
      }
    }
  })
}
1 Like