handleCut Property

I am wondering if there is something akin to a handleCut editor view property. I couldn’t find it in the docs or the source code. In my case I do not want to delete the selection being cut but add a new mark to it. Does anyone know a way to do this?

I’m just a newbie at this… so take my suggestion with a grain of salt.

You would probably remap the keymap to your own function. https://prosemirror.net/docs/ref/#keymap

Your function would probably use https://prosemirror.net/docs/ref/#transform.Transform.addMark in a transaction.

I do not think a keymap handler will be able to catch all the dom events around the clipboard. I could instead listen to copy/cut dom events to catch all the ways a cut can occur (keyboard shortcuts, context menus, other menus, etc). Prosemirror already has code built in for listening to those events and parsing the cut/copy data. I’d rather not have to duplicate that.

Handling the dom "cut" event through handleDOMEvents is probably the best way to do this.

I was just going to make this thread. Handling the cut event for our use case requires adjusting deletion range which would require going in, copying the code for this, pasting it in our code, to override the handler, just to change the way it deletes selection. Or, dispatch a selection change then let the default handler go. Which to me feels wrong to dispatch a transaction just to handle the event properly. But, I guess theres no intention to provide more access to parts of this like pasteHandler.

EDIT: Just so thats not confusing by what I mean by copying and pasting stuff from input.js, I mean I have to copy in all the functions like captureCopy, serializeForClipboard, edithandlers.cut just to change that if statement at the bottom. And override the event with that.

Thanks for the advice @marijn. Any chance a handleCut property will be added in the future?

I’m pretty much going to copy the editHandlers.cut function exactly except for the deleteSelection line. I’ll keep an eye on changes to that function in prosemirror-view updates to keep mine up to date.

Have you tried doing this in your DOM event handler and then letting the default handling take effect afterwards? (I.e. don’t return true or prevent the event’s default.)

I’m sure thats a particularly valid solution but, because your code is written as

  if (cut) view.dispatch(view.state.tr.deleteSelection().scrollIntoView())

it requires a special dispatch before letting the default go, and since we’re in a collab environment, and I don’t want to even think about what I would break with that.

Possibly nothing, since selection changes aren’t even sent to other users in the regular collab setup.

I was able to duplicate the editHandlers.cut function in my own handleDOMEvents.cut handler. It took a little more work than expected though. editHandlers.cut makes use of a browser and clipboard module which are not exported in prosemirror-view so I had to change my build process to import those modules. I also had to copy the captureCopy method in prosemirror-view/src/input.js since it is not exported. I don’t like just copying code but it works :slight_smile:

@marijn Either way, Even after your deleteSelection() fires, we need to apply attributes with nodeMarkup and that cannot be done without listening to the changes with appendTransaction and we just dont have access to that singular case. To fix that we would have to listen to all the changes/insertions because we need to ensure new nodes have properties for this one case.

EDIT: It looks like I will be doing it the same way as jordandh.

Okay, that’s bad. If either of you wants to open an RFC for a handleCut prop, and include a full description of the use case, we can discuss such a feature.

I’ll write up an RFC today.

I wrote up one just now and tried to submit it, but, I am not familiar with that process and I imagine that I have spammed someone to death. I apologize in advance I wrecked up the place. Not even sure if it went through.

I just finished writing mine but have not yet submitted a pull request.

I would suggest you do it, I have no idea how to do it properly.

The pull request has been made https://github.com/ProseMirror/rfcs/pull/3

Just curious, what is the status on this? I found this thread after needing different logic on cut.

1 Like