From 1aba4b9f94ebf51430632216008adde3b44d6d14 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Thu, 22 Sep 2016 16:08:52 -0700 Subject: [PATCH] fix deleting across nested blocks, closes #341 --- src/transforms/at-range.js | 3 +- .../join-across-nested-blocks/index.js | 18 ++++++++++++ .../join-across-nested-blocks/input.yaml | 28 +++++++++++++++++++ .../join-across-nested-blocks/output.yaml | 23 +++++++++++++++ .../index.js | 0 .../input.yaml | 0 .../output.yaml | 0 7 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/index.js create mode 100644 test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/input.yaml create mode 100644 test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/output.yaml rename test/transforms/fixtures/at-range/delete-at-range/{across-nested-blocks => join-nested-blocks}/index.js (100%) rename test/transforms/fixtures/at-range/delete-at-range/{across-nested-blocks => join-nested-blocks}/input.yaml (100%) rename test/transforms/fixtures/at-range/delete-at-range/{across-nested-blocks => join-nested-blocks}/output.yaml (100%) diff --git a/src/transforms/at-range.js b/src/transforms/at-range.js index 29b38fca0..a20eb46af 100644 --- a/src/transforms/at-range.js +++ b/src/transforms/at-range.js @@ -92,7 +92,8 @@ export function deleteAtRange(transform, range) { transform.moveNodeByKey(child.key, newKey, newIndex) }) - transform.removeNodeByKey(endChild.key) + const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock + transform.removeNodeByKey(lonely.key) transform.normalizeDocument() return transform } diff --git a/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/index.js b/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/index.js new file mode 100644 index 000000000..3e1780eca --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/index.js @@ -0,0 +1,18 @@ + +export default function (state) { + const { document, selection } = state + const texts = document.getTexts() + const second = texts.get(1) + const third = texts.get(2) + const range = selection.merge({ + anchorKey: second.key, + anchorOffset: second.length, + focusKey: third.key, + focusOffset: 0 + }) + + return state + .transform() + .deleteAtRange(range) + .apply() +} diff --git a/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/input.yaml b/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/input.yaml new file mode 100644 index 000000000..188259ec6 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/input.yaml @@ -0,0 +1,28 @@ + +nodes: + - kind: block + type: list + nodes: + - kind: block + type: item + nodes: + - kind: text + text: one + - kind: block + type: item + nodes: + - kind: text + text: two + - kind: block + type: list + nodes: + - kind: block + type: item + nodes: + - kind: text + text: three + - kind: block + type: item + nodes: + - kind: text + text: four diff --git a/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/output.yaml b/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/output.yaml new file mode 100644 index 000000000..eb8f589c8 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-at-range/join-across-nested-blocks/output.yaml @@ -0,0 +1,23 @@ + +nodes: + - kind: block + type: list + nodes: + - kind: block + type: item + nodes: + - kind: text + text: one + - kind: block + type: item + nodes: + - kind: text + text: twothree + - kind: block + type: list + nodes: + - kind: block + type: item + nodes: + - kind: text + text: four diff --git a/test/transforms/fixtures/at-range/delete-at-range/across-nested-blocks/index.js b/test/transforms/fixtures/at-range/delete-at-range/join-nested-blocks/index.js similarity index 100% rename from test/transforms/fixtures/at-range/delete-at-range/across-nested-blocks/index.js rename to test/transforms/fixtures/at-range/delete-at-range/join-nested-blocks/index.js diff --git a/test/transforms/fixtures/at-range/delete-at-range/across-nested-blocks/input.yaml b/test/transforms/fixtures/at-range/delete-at-range/join-nested-blocks/input.yaml similarity index 100% rename from test/transforms/fixtures/at-range/delete-at-range/across-nested-blocks/input.yaml rename to test/transforms/fixtures/at-range/delete-at-range/join-nested-blocks/input.yaml diff --git a/test/transforms/fixtures/at-range/delete-at-range/across-nested-blocks/output.yaml b/test/transforms/fixtures/at-range/delete-at-range/join-nested-blocks/output.yaml similarity index 100% rename from test/transforms/fixtures/at-range/delete-at-range/across-nested-blocks/output.yaml rename to test/transforms/fixtures/at-range/delete-at-range/join-nested-blocks/output.yaml