From 7e5f0b477932b4a49fc2e68774fc7be18b93d14c Mon Sep 17 00:00:00 2001 From: Soreine Date: Wed, 26 Oct 2016 14:26:22 +0200 Subject: [PATCH 1/4] Update selection in `joinNode` operation --- src/models/node.js | 34 +++++++++++++++---------------- src/transforms/apply-operation.js | 29 +++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/models/node.js b/src/models/node.js index cba45b4e7..9fc71ab71 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -975,38 +975,38 @@ const Node = { }, /** - * Join a node by `key` with another `withKey`. - * It brings Node after Node + * Join a children node `first` with another children node `second`. + * `first` and `second` will be concatenated in that order. + * `first` and `second` must be two Nodes or two Text. * - * @param {String} key - * @param {String} withKey + * @param {Node} first + * @param {Node} second * @return {Node} */ - joinNode(key, withKey) { + joinNode(first, second) { let node = this - let target = node.assertPath(key) - let withTarget = node.assertPath(withKey) - let parent = node.getParent(target) + let parent = node.getParent(second) const isParent = node == parent - const index = parent.nodes.indexOf(target) + const index = parent.nodes.indexOf(second) - if (target.kind == 'text') { - let { characters } = withTarget - characters = characters.concat(target.characters) - withTarget = withTarget.merge({ characters }) + if (second.kind == 'text') { + let { characters } = first + characters = characters.concat(second.characters) + first = first.merge({ characters }) } else { - const size = withTarget.nodes.size - target.nodes.forEach((child, i) => { - withTarget = withTarget.insertNode(size + i, child) + const size = first.nodes.size + second.nodes.forEach((child, i) => { + first = first.insertNode(size + i, child) }) } parent = parent.removeNode(index) node = isParent ? parent : node.updateDescendant(parent) - node = node.updateDescendant(withTarget) + console.log(node) + node = node.updateDescendant(first) return node }, diff --git a/src/transforms/apply-operation.js b/src/transforms/apply-operation.js index 6d99f6e0c..32720c0c4 100644 --- a/src/transforms/apply-operation.js +++ b/src/transforms/apply-operation.js @@ -136,9 +136,32 @@ function insertText(state, operation) { function joinNode(state, operation) { const { path, withPath } = operation - let { document } = state - document = document.joinNode(path, withPath) - state = state.merge({ document }) + let { document, selection } = state + const first = document.assertPath(withPath) + const second = document.assertPath(path) + + // Update doc + document = document.joinNode(first, second) + + // Update selection + // When merging two texts together + if (second.kind == 'text') { + // The final key is the `first` key + if (selection.anchorKey == second.key) { + selection = selection.merge({ + anchorKey: first.key, + anchorOffset: selection.anchorOffset + first.characters.size + }) + } + if (selection.focusKey == second.key) { + selection = selection.merge({ + focusKey: first.key, + focusOffset: selection.focusOffset + first.characters.size + }) + } + } + + state = state.merge({ document, selection }) return state } From 947513fc5852b45c521f0cdc1bc3066a46a9456e Mon Sep 17 00:00:00 2001 From: Soreine Date: Wed, 26 Oct 2016 14:26:55 +0200 Subject: [PATCH 2/4] Add test to `joinNodeByKey` --- .../by-key/join-node-by-key/block/index.js | 29 +++++++++++++++++++ .../by-key/join-node-by-key/block/input.yaml | 16 ++++++++++ .../by-key/join-node-by-key/block/output.yaml | 8 +++++ .../by-key/join-node-by-key/text/index.js | 29 +++++++++++++++++++ .../by-key/join-node-by-key/text/input.yaml | 16 ++++++++++ .../by-key/join-node-by-key/text/output.yaml | 13 +++++++++ 6 files changed, 111 insertions(+) create mode 100644 test/transforms/fixtures/by-key/join-node-by-key/block/index.js create mode 100644 test/transforms/fixtures/by-key/join-node-by-key/block/input.yaml create mode 100644 test/transforms/fixtures/by-key/join-node-by-key/block/output.yaml create mode 100644 test/transforms/fixtures/by-key/join-node-by-key/text/index.js create mode 100644 test/transforms/fixtures/by-key/join-node-by-key/text/input.yaml create mode 100644 test/transforms/fixtures/by-key/join-node-by-key/text/output.yaml diff --git a/test/transforms/fixtures/by-key/join-node-by-key/block/index.js b/test/transforms/fixtures/by-key/join-node-by-key/block/index.js new file mode 100644 index 000000000..f991fd9c7 --- /dev/null +++ b/test/transforms/fixtures/by-key/join-node-by-key/block/index.js @@ -0,0 +1,29 @@ + +import assert from 'assert' + +export default function (state) { + const { selection } = state + + const range = selection.merge({ + anchorKey: 'anchor', + anchorOffset: 1, + focusKey: 'focus', + focusOffset: 2 + }) + + const next = state + .transform() + .moveTo(range) + .joinNodeByKey('key2', 'key1') + .apply() + + assert.deepEqual( + next.selection.toJS(), + range.merge({ + focusKey: 'anchor', + focusOffset: 5 + }).toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/by-key/join-node-by-key/block/input.yaml b/test/transforms/fixtures/by-key/join-node-by-key/block/input.yaml new file mode 100644 index 000000000..66df88e7a --- /dev/null +++ b/test/transforms/fixtures/by-key/join-node-by-key/block/input.yaml @@ -0,0 +1,16 @@ + +nodes: + - kind: block + key: 'key1' + type: paragraph + nodes: + - kind: text + key: 'anchor' + text: The + - kind: block + key: 'key2' + type: paragraph + nodes: + - kind: text + key: 'focus' + text: text diff --git a/test/transforms/fixtures/by-key/join-node-by-key/block/output.yaml b/test/transforms/fixtures/by-key/join-node-by-key/block/output.yaml new file mode 100644 index 000000000..f965e4ab2 --- /dev/null +++ b/test/transforms/fixtures/by-key/join-node-by-key/block/output.yaml @@ -0,0 +1,8 @@ + +nodes: + - kind: block + key: key1 + type: paragraph + nodes: + - kind: text + text: Thetext diff --git a/test/transforms/fixtures/by-key/join-node-by-key/text/index.js b/test/transforms/fixtures/by-key/join-node-by-key/text/index.js new file mode 100644 index 000000000..797f72907 --- /dev/null +++ b/test/transforms/fixtures/by-key/join-node-by-key/text/index.js @@ -0,0 +1,29 @@ + +import assert from 'assert' + +export default function (state) { + const { selection } = state + + const range = selection.merge({ + anchorKey: 'anchor', + anchorOffset: 1, + focusKey: 'focus', + focusOffset: 2 + }) + + const next = state + .transform() + .moveTo(range) + .joinNodeByKey('focus', 'anchor') + .apply() + + assert.deepEqual( + next.selection.toJS(), + range.merge({ + focusKey: 'anchor', + focusOffset: 5 + }).toJS() + ) + + return next +} diff --git a/test/transforms/fixtures/by-key/join-node-by-key/text/input.yaml b/test/transforms/fixtures/by-key/join-node-by-key/text/input.yaml new file mode 100644 index 000000000..1d54b7ee6 --- /dev/null +++ b/test/transforms/fixtures/by-key/join-node-by-key/text/input.yaml @@ -0,0 +1,16 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + key: 'anchor' + text: one + - kind: inline + type: link + nodes: + - kind: text + text: two + - kind: text + key: 'focus' + text: three diff --git a/test/transforms/fixtures/by-key/join-node-by-key/text/output.yaml b/test/transforms/fixtures/by-key/join-node-by-key/text/output.yaml new file mode 100644 index 000000000..434082dfa --- /dev/null +++ b/test/transforms/fixtures/by-key/join-node-by-key/text/output.yaml @@ -0,0 +1,13 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + key: 'anchor' + text: onethree + - kind: inline + type: link + nodes: + - kind: text + text: two From a50d381d8eba1c86521877f25e538d73b47ac9ca Mon Sep 17 00:00:00 2001 From: Soreine Date: Wed, 26 Oct 2016 14:35:40 +0200 Subject: [PATCH 3/4] Remove console.log --- src/models/node.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/models/node.js b/src/models/node.js index 9fc71ab71..21b653efd 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -1005,7 +1005,6 @@ const Node = { parent = parent.removeNode(index) node = isParent ? parent : node.updateDescendant(parent) - console.log(node) node = node.updateDescendant(first) return node }, From b3e1b3ad08b937cd179bc21d9573c36f1a500d3f Mon Sep 17 00:00:00 2001 From: Soreine Date: Wed, 26 Oct 2016 14:48:24 +0200 Subject: [PATCH 4/4] Adapt existing tests --- .../unwrap-inline/across-blocks/index.js | 24 +++++++++---------- .../unwrap-inline/across-blocks/input.yaml | 2 ++ .../unwrap-inline/across-blocks/output.yaml | 2 ++ .../unwrap-inline/nested-block/index.js | 14 +++++------ .../unwrap-inline/nested-block/input.yaml | 4 ++++ .../unwrap-inline/only-one/index.js | 14 +++++------ .../unwrap-inline/only-one/input.yaml | 5 ++++ .../unwrap-inline/only-one/output.yaml | 2 ++ .../unwrap-inline/single-block/index.js | 14 +++++------ .../unwrap-inline/single-block/input.yaml | 5 ++++ 10 files changed, 53 insertions(+), 33 deletions(-) diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/index.js b/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/index.js index 49c6255e8..fa7f133bb 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/index.js +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/index.js @@ -2,14 +2,11 @@ import assert from 'assert' export default function (state) { - const { document, selection } = state - const texts = document.getTexts() - let first = texts.first() - const second = texts.last() + const { selection } = state const range = selection.merge({ - anchorKey: first.key, - anchorOffset: 2, - focusKey: second.key, + anchorKey: 'anchor', + anchorOffset: 1, + focusKey: 'focus', focusOffset: 2 }) @@ -19,15 +16,18 @@ export default function (state) { .unwrapInline('hashtag') .apply() - // Selection is reset, in theory it should me on the image - first = next.document.getTexts().first() + // Test selection + const { document } = next + const first = document.getTexts().first() + const last = document.getTexts().last() + assert.deepEqual( next.selection.toJS(), range.merge({ anchorKey: first.key, - anchorOffset: 0, - focusKey: first.key, - focusOffset: 0 + anchorOffset: 1, + focusKey: last.key, + focusOffset: 4 }).toJS() ) diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/input.yaml b/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/input.yaml index 57d48f74f..efe76eccf 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/input.yaml +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/input.yaml @@ -4,6 +4,7 @@ nodes: type: paragraph nodes: - kind: text + key: 'anchor' # w[o text: wo - kind: inline type: hashtag @@ -19,4 +20,5 @@ nodes: - kind: text text: an - kind: text + key: 'focus' # ot]her text: other diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/output.yaml b/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/output.yaml index 881deb1d9..612d61ee1 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/output.yaml +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/across-blocks/output.yaml @@ -10,3 +10,5 @@ nodes: nodes: - kind: text text: another +# Selection +# w[ordanot]her \ No newline at end of file diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/nested-block/index.js b/test/transforms/fixtures/at-current-range/unwrap-inline/nested-block/index.js index 80ede57bf..6b8cc9d94 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/nested-block/index.js +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/nested-block/index.js @@ -2,14 +2,11 @@ import assert from 'assert' export default function (state) { - const { document, selection } = state - const texts = document.getTexts() - const first = texts.first() - const last = texts.get(1) + const { selection } = state const range = selection.merge({ - anchorKey: first.key, + anchorKey: 'anchor', anchorOffset: 1, - focusKey: last.key, + focusKey: 'focus', focusOffset: 2 }) @@ -21,7 +18,10 @@ export default function (state) { assert.deepEqual( next.selection.toJS(), - range.collapseToStartOf(next.document).toJS() + range.merge({ + focusKey: 'anchor', + focusOffset: 3 + }).toJS() ) return next diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/nested-block/input.yaml b/test/transforms/fixtures/at-current-range/unwrap-inline/nested-block/input.yaml index dda085eb1..bace31d67 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/nested-block/input.yaml +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/nested-block/input.yaml @@ -7,11 +7,15 @@ nodes: type: paragraph nodes: - kind: text + key: 'anchor' text: w - kind: inline type: hashtag nodes: - kind: text + key: 'focus' text: or - kind: text text: d +# Selection +# w[or]d \ No newline at end of file diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/index.js b/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/index.js index 2c11e5ddd..221ab5fed 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/index.js +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/index.js @@ -2,14 +2,11 @@ import assert from 'assert' export default function (state) { - const { document, selection } = state - const texts = document.getTexts() - const first = texts.get(2) - const last = texts.get(3) + const { selection } = state const range = selection.merge({ - anchorKey: first.key, + anchorKey: 'anchor', anchorOffset: 2, - focusKey: last.key, + focusKey: 'focus', focusOffset: 2 }) @@ -21,7 +18,10 @@ export default function (state) { assert.deepEqual( next.selection.toJS(), - range.collapseToStartOf(next.document).toJS() + range.merge({ + focusKey: 'anchor', + focusOffset: 5 + }).toJS() ) return next diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/input.yaml b/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/input.yaml index d93ed90ad..a21510e72 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/input.yaml +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/input.yaml @@ -11,11 +11,16 @@ nodes: - kind: text text: ll - kind: text + key: 'anchor' text: "o w" - kind: inline type: hashtag nodes: - kind: text + key: 'focus' text: or - kind: text text: d + +# Selection +# hello [wor]d \ No newline at end of file diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/output.yaml b/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/output.yaml index 80cea6855..046a57fc0 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/output.yaml +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/only-one/output.yaml @@ -12,3 +12,5 @@ nodes: text: ll - kind: text text: "o word" +# Selection +# hello [wor]d \ No newline at end of file diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/single-block/index.js b/test/transforms/fixtures/at-current-range/unwrap-inline/single-block/index.js index 80ede57bf..6b8cc9d94 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/single-block/index.js +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/single-block/index.js @@ -2,14 +2,11 @@ import assert from 'assert' export default function (state) { - const { document, selection } = state - const texts = document.getTexts() - const first = texts.first() - const last = texts.get(1) + const { selection } = state const range = selection.merge({ - anchorKey: first.key, + anchorKey: 'anchor', anchorOffset: 1, - focusKey: last.key, + focusKey: 'focus', focusOffset: 2 }) @@ -21,7 +18,10 @@ export default function (state) { assert.deepEqual( next.selection.toJS(), - range.collapseToStartOf(next.document).toJS() + range.merge({ + focusKey: 'anchor', + focusOffset: 3 + }).toJS() ) return next diff --git a/test/transforms/fixtures/at-current-range/unwrap-inline/single-block/input.yaml b/test/transforms/fixtures/at-current-range/unwrap-inline/single-block/input.yaml index 7bfbe8b4b..1e1a75bbb 100644 --- a/test/transforms/fixtures/at-current-range/unwrap-inline/single-block/input.yaml +++ b/test/transforms/fixtures/at-current-range/unwrap-inline/single-block/input.yaml @@ -4,11 +4,16 @@ nodes: type: paragraph nodes: - kind: text + key: 'anchor' text: w - kind: inline type: hashtag nodes: - kind: text + key: 'focus' text: or - kind: text text: d + +# selection: +# w[or]d \ No newline at end of file