From f27ccd5140345507827c3e57645488d04ef04a4a Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Thu, 15 Sep 2016 14:49:43 -0700 Subject: [PATCH] fix deleting backward/forward multiple characters, fixes #326 --- src/transforms/at-range.js | 4 +-- .../multiple-characters/index.js | 27 +++++++++++++++++++ .../multiple-characters/input.yaml | 7 +++++ .../multiple-characters/output.yaml | 7 +++++ .../multiple-characters/index.js | 27 +++++++++++++++++++ .../multiple-characters/input.yaml | 7 +++++ .../multiple-characters/output.yaml | 7 +++++ .../multiple-characters/index.js | 17 ++++++++++++ .../multiple-characters/input.yaml | 7 +++++ .../multiple-characters/output.yaml | 7 +++++ .../multiple-characters/index.js | 17 ++++++++++++ .../multiple-characters/input.yaml | 7 +++++ .../multiple-characters/output.yaml | 7 +++++ 13 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/index.js create mode 100644 test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/input.yaml create mode 100644 test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/output.yaml create mode 100644 test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/index.js create mode 100644 test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/input.yaml create mode 100644 test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/output.yaml create mode 100644 test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/index.js create mode 100644 test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/input.yaml create mode 100644 test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/output.yaml create mode 100644 test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/index.js create mode 100644 test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/input.yaml create mode 100644 test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/output.yaml diff --git a/src/transforms/at-range.js b/src/transforms/at-range.js index 95eb82b21..fb81b3d01 100644 --- a/src/transforms/at-range.js +++ b/src/transforms/at-range.js @@ -152,7 +152,7 @@ export function deleteBackwardAtRange(transform, range, n = 1) { } range = range.merge({ - focusOffset: focusOffset - 1, + focusOffset: focusOffset - n, isBackward: true, }) @@ -204,7 +204,7 @@ export function deleteForwardAtRange(transform, range, n = 1) { } range = range.merge({ - focusOffset: focusOffset + 1 + focusOffset: focusOffset + n }) return transform.deleteAtRange(range) diff --git a/test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/index.js b/test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/index.js new file mode 100644 index 000000000..65894e2b8 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/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) + .deleteBackward(3) + .apply() + + assert.deepEqual( + next.selection.toJS(), + range.moveBackward(3).toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/input.yaml b/test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/input.yaml new file mode 100644 index 000000000..27f668fe2 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/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-backward/multiple-characters/output.yaml b/test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/output.yaml new file mode 100644 index 000000000..79fc29996 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-backward/multiple-characters/output.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: w diff --git a/test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/index.js b/test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/index.js new file mode 100644 index 000000000..dbc0f2167 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/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: 0, + focusKey: first.key, + focusOffset: 0 + }) + + const next = state + .transform() + .moveTo(range) + .deleteForward(3) + .apply() + + assert.deepEqual( + next.selection.toJS(), + range.collapseToStart().toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/input.yaml b/test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/input.yaml new file mode 100644 index 000000000..27f668fe2 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/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-forward/multiple-characters/output.yaml b/test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/output.yaml new file mode 100644 index 000000000..ce6b3bd72 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-forward/multiple-characters/output.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: d diff --git a/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/index.js b/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/index.js new file mode 100644 index 000000000..8a3cd8780 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/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() + .deleteBackwardAtRange(range, 3) + .apply() +} diff --git a/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/input.yaml b/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/input.yaml new file mode 100644 index 000000000..27f668fe2 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/input.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: word diff --git a/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/output.yaml b/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/output.yaml new file mode 100644 index 000000000..79fc29996 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-backward-at-range/multiple-characters/output.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: w diff --git a/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/index.js b/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/index.js new file mode 100644 index 000000000..6f9917444 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/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: 0 + }) + + return state + .transform() + .deleteForwardAtRange(range, 3) + .apply() +} diff --git a/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/input.yaml b/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/input.yaml new file mode 100644 index 000000000..27f668fe2 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/input.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: word diff --git a/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/output.yaml b/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/output.yaml new file mode 100644 index 000000000..ce6b3bd72 --- /dev/null +++ b/test/transforms/fixtures/at-range/delete-forward-at-range/multiple-characters/output.yaml @@ -0,0 +1,7 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: d