From da85c5ac8744ea871b38427ffca9f83a55ce2873 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Tue, 22 Nov 2016 12:03:47 -0800 Subject: [PATCH] refactor delete at current range --- src/transforms/at-current-range.js | 46 +++++++++++++------ .../delete/whole-inline/index.js | 29 ++++++++++++ .../delete/whole-inline/input.yaml | 7 +++ .../delete/whole-inline/output.yaml | 7 +++ .../delete-at-range/whole-inline/index.js | 17 +++++++ .../delete-at-range/whole-inline/input.yaml | 10 ++++ .../delete-at-range/whole-inline/output.yaml | 7 +++ 7 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 test/transforms/fixtures/at-current-range/delete/whole-inline/index.js create mode 100644 test/transforms/fixtures/at-current-range/delete/whole-inline/input.yaml create mode 100644 test/transforms/fixtures/at-current-range/delete/whole-inline/output.yaml create mode 100644 test/transforms/fixtures/at-range/delete-at-range/whole-inline/index.js create mode 100644 test/transforms/fixtures/at-range/delete-at-range/whole-inline/input.yaml create mode 100644 test/transforms/fixtures/at-range/delete-at-range/whole-inline/output.yaml diff --git a/src/transforms/at-current-range.js b/src/transforms/at-current-range.js index c442b1e06..78cac2ded 100644 --- a/src/transforms/at-current-range.js +++ b/src/transforms/at-current-range.js @@ -42,39 +42,55 @@ export function addMark(transform, mark) { export function _delete(transform) { const { state } = transform const { document, selection } = state - let after + // If the selection is collapsed, there's nothing to delete. if (selection.isCollapsed) return transform const { startText } = state const { startKey, startOffset, endKey, endOffset } = selection - const block = document.getClosestBlock(startText.key) - const highest = block.getHighestChild(startText.key) + const block = document.getClosestBlock(startKey) + const highest = block.getHighestChild(startKey) const previous = block.getPreviousSibling(highest.key) const next = block.getNextSibling(highest.key) + let after + // If there's a previous node, and we're at the start of the current node, + // and the selection encompasses the entire current node, it won't exist after + // deleting, so we need to update the selection's keys. if ( previous && startOffset == 0 && (endKey != startKey || endOffset == startText.length) ) { - if (previous.kind == 'text') { - if (next && next.kind == 'text') { - after = selection.merge({ - anchorKey: previous.key, - anchorOffset: previous.length, - focusKey: previous.key, - focusOffset: previous.length - }) - } else { - after = selection.collapseToEndOf(previous) - } - } else { + + // If the nodes on either sides are text nodes, they will end up being + // combined, so we need to set the selection to right in between them. + if (previous.kind == 'text' && next && next.kind == 'text') { + after = selection.merge({ + anchorKey: previous.key, + anchorOffset: previous.length, + focusKey: previous.key, + focusOffset: previous.length + }) + } + + // Otherwise, if only the previous node is a text node, it won't be merged, + // so collapse to the end of it. + else if (previous.kind == 'text') { + after = selection.collapseToEndOf(previous) + } + + // Otherwise, if the previous node isn't a text node, we need to get the + // last text node inside of it and collapse to the end of that. + else { const last = previous.getLastText() after = selection.collapseToEndOf(last) } } + // Otherwise, if the inline is an online child + + // Otherwise simply collapse the selection. else { after = selection.collapseToStart() } diff --git a/test/transforms/fixtures/at-current-range/delete/whole-inline/index.js b/test/transforms/fixtures/at-current-range/delete/whole-inline/index.js new file mode 100644 index 000000000..25911b7a0 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete/whole-inline/index.js @@ -0,0 +1,29 @@ + +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: 0, + focusKey: first.key, + focusOffset: first.length + }) + + const next = state + .transform() + .moveTo(range) + .delete() + .apply() + + const updated = state.document.getFirstText() + + assert.deepEqual( + next.selection.toJS(), + range.collapseToStartOf(updated).toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/at-current-range/delete/whole-inline/input.yaml b/test/transforms/fixtures/at-current-range/delete/whole-inline/input.yaml new file mode 100644 index 000000000..27f668fe2 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete/whole-inline/input.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: word diff --git a/test/transforms/fixtures/at-current-range/delete/whole-inline/output.yaml b/test/transforms/fixtures/at-current-range/delete/whole-inline/output.yaml new file mode 100644 index 000000000..f6de4d08a --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete/whole-inline/output.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: "" diff --git a/test/transforms/fixtures/at-range/delete-at-range/whole-inline/index.js b/test/transforms/fixtures/at-range/delete-at-range/whole-inline/index.js new file mode 100644 index 000000000..f86c52492 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-at-range/whole-inline/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: 0, + focusKey: first.key, + focusOffset: first.length + }) + + return state + .transform() + .deleteAtRange(range) + .apply() +} diff --git a/test/transforms/fixtures/at-range/delete-at-range/whole-inline/input.yaml b/test/transforms/fixtures/at-range/delete-at-range/whole-inline/input.yaml new file mode 100644 index 000000000..f752cee89 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-at-range/whole-inline/input.yaml @@ -0,0 +1,10 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + nodes: + - kind: text + text: word diff --git a/test/transforms/fixtures/at-range/delete-at-range/whole-inline/output.yaml b/test/transforms/fixtures/at-range/delete-at-range/whole-inline/output.yaml new file mode 100644 index 000000000..f6de4d08a --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-at-range/whole-inline/output.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: ""