diff --git a/lib/models/transforms.js b/lib/models/transforms.js index 7a6cdc03b..a7c72904f 100644 --- a/lib/models/transforms.js +++ b/lib/models/transforms.js @@ -108,23 +108,23 @@ const Transforms = { ancestor = ancestor.merge({ nodes }) - // Take the end edge's split text and move it to the start edge. + // Take the end edge's inline nodes and move them to the start edge. let startBlock = ancestor.getClosestBlock(startText) - let endChild = ancestor.getFurthestInline(endText) || endText + let endBlock = ancestor.getClosestBlock(endText) - const startNodes = startBlock.nodes.push(endChild) + const startNodes = startBlock.nodes.concat(endBlock.nodes) startBlock = startBlock.merge({ nodes: startNodes }) ancestor = ancestor.updateDescendant(startBlock) // While the end child is an only child, remove the block it's in. - let endParent = ancestor.getClosestBlock(endChild) + let endParent = ancestor.getClosestBlock(endBlock) while (endParent && endParent.nodes.size == 1) { - endChild = endParent + endBlock = endParent endParent = ancestor.getClosestBlock(endParent) } - ancestor = ancestor.removeDescendant(endChild) + ancestor = ancestor.removeDescendant(endBlock) // Update the node. node = isAncestor diff --git a/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/index.js b/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/index.js new file mode 100644 index 000000000..a49da01bc --- /dev/null +++ b/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/index.js @@ -0,0 +1,18 @@ + +export default function (state) { + const { document, selection } = state + const texts = document.getTexts() + const first = texts.get(0) + const second = texts.get(1) + const range = selection.merge({ + anchorKey: first.key, + anchorOffset: first.length, + focusKey: second.key, + focusOffset: 0 + }) + + return state + .transform() + .deleteAtRange(range) + .apply() +} diff --git a/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/input.yaml b/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/input.yaml new file mode 100644 index 000000000..efe5831c8 --- /dev/null +++ b/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/input.yaml @@ -0,0 +1,19 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: block + type: paragraph + nodes: + - kind: text + text: two + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/output.yaml b/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/output.yaml new file mode 100644 index 000000000..35754ebb8 --- /dev/null +++ b/test/transforms/fixtures/delete-at-range/join-blocks-with-inlines/output.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: onetwo + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/index.js b/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/index.js new file mode 100644 index 000000000..959faf7ee --- /dev/null +++ b/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/index.js @@ -0,0 +1,17 @@ + +export default function (state) { + const { document, selection } = state + const texts = document.getTexts() + const second = texts.get(1) + const range = selection.merge({ + anchorKey: second.key, + anchorOffset: 0, + focusKey: second.key, + focusOffset: 0 + }) + + return state + .transform() + .deleteBackwardAtRange(range) + .apply() +} diff --git a/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/input.yaml b/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/input.yaml new file mode 100644 index 000000000..efe5831c8 --- /dev/null +++ b/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/input.yaml @@ -0,0 +1,19 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: block + type: paragraph + nodes: + - kind: text + text: two + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/output.yaml b/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/output.yaml new file mode 100644 index 000000000..35754ebb8 --- /dev/null +++ b/test/transforms/fixtures/delete-backward-at-range/join-blocks-with-inlines/output.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: onetwo + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/index.js b/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/index.js new file mode 100644 index 000000000..0e7ec1b3d --- /dev/null +++ b/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/index.js @@ -0,0 +1,33 @@ + +import assert from 'assert' + +export default function (state) { + const { document, selection } = state + const texts = document.getTexts() + const first = texts.get(0) + const second = texts.get(1) + const range = selection.merge({ + anchorKey: second.key, + anchorOffset: 0, + focusKey: second.key, + focusOffset: 0 + }) + + const next = state + .transform() + .moveTo(range) + .deleteBackward() + .apply() + + assert.deepEqual( + next.selection.toJS(), + range.merge({ + anchorKey: first.key, + anchorOffset: first.length, + focusKey: first.key, + focusOffset: first.length + }).toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/input.yaml b/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/input.yaml new file mode 100644 index 000000000..efe5831c8 --- /dev/null +++ b/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/input.yaml @@ -0,0 +1,19 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: block + type: paragraph + nodes: + - kind: text + text: two + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/output.yaml b/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/output.yaml new file mode 100644 index 000000000..35754ebb8 --- /dev/null +++ b/test/transforms/fixtures/delete-backward/join-blocks-with-inlines/output.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: onetwo + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/index.js b/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/index.js new file mode 100644 index 000000000..5cfc23e3c --- /dev/null +++ b/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/index.js @@ -0,0 +1,17 @@ + +export default function (state) { + const { document, selection } = state + const texts = document.getTexts() + const first = texts.first() + const range = selection.merge({ + anchorKey: first.key, + anchorOffset: first.length, + focusKey: first.key, + focusOffset: first.length + }) + + return state + .transform() + .deleteForwardAtRange(range) + .apply() +} diff --git a/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/input.yaml b/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/input.yaml new file mode 100644 index 000000000..efe5831c8 --- /dev/null +++ b/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/input.yaml @@ -0,0 +1,19 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: block + type: paragraph + nodes: + - kind: text + text: two + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/output.yaml b/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/output.yaml new file mode 100644 index 000000000..35754ebb8 --- /dev/null +++ b/test/transforms/fixtures/delete-forward-at-range/join-blocks-with-inlines/output.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: onetwo + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/index.js b/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/index.js new file mode 100644 index 000000000..6e2cf6f52 --- /dev/null +++ b/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/index.js @@ -0,0 +1,27 @@ + +import assert from 'assert' + +export default function (state) { + const { document, selection } = state + const texts = document.getTexts() + const first = texts.first() + const range = selection.merge({ + anchorKey: first.key, + anchorOffset: first.length, + focusKey: first.key, + focusOffset: first.length + }) + + const next = state + .transform() + .moveTo(range) + .deleteForward() + .apply() + + assert.deepEqual( + next.selection.toJS(), + range.collapseToStart().toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/input.yaml b/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/input.yaml new file mode 100644 index 000000000..efe5831c8 --- /dev/null +++ b/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/input.yaml @@ -0,0 +1,19 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: block + type: paragraph + nodes: + - kind: text + text: two + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/output.yaml b/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/output.yaml new file mode 100644 index 000000000..35754ebb8 --- /dev/null +++ b/test/transforms/fixtures/delete-forward/join-blocks-with-inlines/output.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: onetwo + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete/join-blocks-with-inlines/index.js b/test/transforms/fixtures/delete/join-blocks-with-inlines/index.js new file mode 100644 index 000000000..84875ef8f --- /dev/null +++ b/test/transforms/fixtures/delete/join-blocks-with-inlines/index.js @@ -0,0 +1,28 @@ + +import assert from 'assert' + +export default function (state) { + const { document, selection } = state + const texts = document.getTexts() + const first = texts.get(0) + const second = texts.get(1) + const range = selection.merge({ + anchorKey: first.key, + anchorOffset: first.length, + focusKey: second.key, + focusOffset: 0 + }) + + const next = state + .transform() + .moveTo(range) + .delete() + .apply() + + assert.deepEqual( + next.selection.toJS(), + range.collapseToStart().toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/delete/join-blocks-with-inlines/input.yaml b/test/transforms/fixtures/delete/join-blocks-with-inlines/input.yaml new file mode 100644 index 000000000..efe5831c8 --- /dev/null +++ b/test/transforms/fixtures/delete/join-blocks-with-inlines/input.yaml @@ -0,0 +1,19 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: block + type: paragraph + nodes: + - kind: text + text: two + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four diff --git a/test/transforms/fixtures/delete/join-blocks-with-inlines/output.yaml b/test/transforms/fixtures/delete/join-blocks-with-inlines/output.yaml new file mode 100644 index 000000000..35754ebb8 --- /dev/null +++ b/test/transforms/fixtures/delete/join-blocks-with-inlines/output.yaml @@ -0,0 +1,14 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: onetwo + - kind: inline + type: link + nodes: + - kind: text + text: three + - kind: text + text: four