Cut and paste does not restore the document

It is really hard to describe my questions.

I made a demo based on the basic example at https://glitch.com/edit/#!/vanilla-minotaurasaurus?path=index.js:26:17

As you see, I just added a title NodeSpec

title: {
                content: "inline*",
                defining: true,
                parseDOM: [{
                    tag: "div.mytitle"
                }],
                toDOM(_node) {
                    return ["div", {
                        class: 'mytitle'
                    }, 0]
                }
}

and set the doc content expression as title block+.

Now just select the whole document, and cut it. Since the expression requires a title, so the document is not empty. And the cursor is not in title but in the next paragraph.

Please try out both the operations below and see the final result:

  • Paste it back immediately
  • Or move the cursor to title, and then paste it back

They do NOT bring the original document content back.

What I want is that paste will bring back the original content if the cursor is in title (the beginning of the document), while throw away title but bring all the block+ back if the cursor is not in title.

Part of this was a bug in the replace code (fixed here) which would cause it to drop too much content because it ran out of ideas on where to place it. When pasting into the title itself, the problem is that the end of the title node (which is after the cursor) could not meaningfully be placed after the content. I’ve pushed this patch to work around it, but it’s probably not the ideal solution. I don’t have time to dig into it more deeply right now.

Nice!

Now I changed the demo a bit https://glitch.com/edit/#!/vanilla-minotaurasaurus?path=index.js:37:29, added a new NodeSpec value.

Now if the cursor is at title or value node and paste the document back, everything becomes title or value.

Dear @marijn,

At nextjournal, we’re incurring in a similar problem for which the mentioned commits doesn’t seem to solve (we’re on release 1.2.2 of prosemirror-transform).

We have a distinguished title node in schema

{doc: {content: "title{1} subtitle? authors? block*", ... },
 title: {content: "inline*", ... },
 ...
}

and it turns out that pasting certain document slices the title node fails, pasting just don’t have any effect and no exception is thrown.

I reproduced and described the situation in a fork of prosemirror website repo with minimal additions onto the basic schema. The example along with instruction for reproducing issues will be available at http://localhost:8080/examples/copypaste/ once the dev server is running.

I’ve also setup a glitch project if that might help.

I suspect that the code around replace fails to fit the (right end of the) slice into the target selection. fitting the copied slice into the target selection since this behaviour occurs only in presence of specific slice openStart/End depths (a.o. [1, 0] and [1, 3]).

Are there any properties that I can add to title schema in order to solve this?

best, Andrea

To work around :point_up_2: we’re currently trying to “fix” the pasted slice in our handlePaste handler when the target selection is in the title node by either:

  • changing the openEnd depth to 1 or 0 depending on its contents
  • adding an empty paragraph at the end of the slice’s fragment

but both solution feel definitely hacky…

I can reproduce this. The problem is that the replace-fitting code doesn’t know what to do with the piece of title left after the replace, because that doesn’t fit anywhere. This is unfortunately extremely tricky code. I’ll continue looking at this tomorrow.

1 Like

So this is turning out to be harder to address than I hoped. Still working on it, not sure when I’ll get actual results.

I think prosemirror-transform 1.2.5-beta1 should help with this. But there’s a lot of code rewritten in that version, so I’m holding off releasing it as a normal version for now (see this thread).

1 Like