Skip to content
Permalink
Browse files

Unwrap closed nodes one by one when creating a replace step

FIX: Fix an issue where a replace could needlessly drop content when
the first node of the slice didn't fit the target context.

See https://discuss.prosemirror.net/t/cut-and-paste-does-not-restore-the-document/2166
  • Loading branch information...
marijnh committed Aug 26, 2019
1 parent cbf247e commit b183da4a3523a57a7a149b0ab9191f8aac57c831
Showing with 8 additions and 9 deletions.
  1. +8 −9 src/replace.js
@@ -224,8 +224,11 @@ function nodeRight(content, depth) {
// : (ResolvedPos, Slice) → [{content: Fragment, openEnd: number, depth: number}]
function placeSlice($from, slice) {
let frontier = new Frontier($from)
for (let pass = 1; slice.size && pass <= 3; pass++)
slice = frontier.placeSlice(slice.content, slice.openStart, slice.openEnd, pass)
for (let pass = 1; slice.size && pass <= 3; pass++) {
let value = frontier.placeSlice(slice.content, slice.openStart, slice.openEnd, pass)
if (pass == 3 && value != slice && value.size) pass = 0 // Restart if the 3rd pass made progress but left content
slice = value
}
while (frontier.open.length) frontier.closeNode()
return frontier.placed
}
@@ -270,13 +273,9 @@ class Frontier {
}
let result = this.placeContent(fragment, openStart, openEnd, pass, parent)
if (pass > 2 && result.size && openStart == 0) {
for (let i = 0; i < result.content.childCount; i++) {
let child = result.content.child(i)
this.placeContent(child.content, 0,
openEnd && i == result.content.childCount - 1 ? openEnd - 1 : 0,
pass, child)
}
result = Fragment.empty
let child = result.content.firstChild, single = result.content.childCount == 1
this.placeContent(child.content, 0, openEnd && single ? openEnd - 1 : 0, pass, child)
result = single ? Fragment.empty : new Slice(result.content.cutByIndex(1), 0, openEnd)
}
return result
}

0 comments on commit b183da4

Please sign in to comment.
You can’t perform that action at this time.