From bcb2fc39e5252aad97d09c40afef8c7a4c165136 Mon Sep 17 00:00:00 2001 From: Nicolas Gaborit Date: Wed, 21 Sep 2016 20:43:03 +0200 Subject: [PATCH] Focus and blur should not clear marks on selection (#338) * Do not clear mark on focus/blur * Add tests --- src/transforms/operations.js | 15 ++++++++--- .../fixtures/on-selection/blur/marks/index.js | 26 +++++++++++++++++++ .../on-selection/blur/marks/input.yaml | 17 ++++++++++++ .../on-selection/focus/marks/index.js | 26 +++++++++++++++++++ .../on-selection/focus/marks/input.yaml | 17 ++++++++++++ 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 test/transforms/fixtures/on-selection/blur/marks/index.js create mode 100644 test/transforms/fixtures/on-selection/blur/marks/input.yaml create mode 100644 test/transforms/fixtures/on-selection/focus/marks/index.js create mode 100644 test/transforms/fixtures/on-selection/focus/marks/input.yaml diff --git a/src/transforms/operations.js b/src/transforms/operations.js index 5880d8e68..0a46aac04 100644 --- a/src/transforms/operations.js +++ b/src/transforms/operations.js @@ -371,9 +371,18 @@ export function setSelectionOperation(transform, properties) { prevProps[k] = selection[k] } - // If the current selection has marks, and the new selection doesn't change - // them in some way, they are old and should be removed. - if (selection.marks && properties.marks == selection.marks) { + // If the selection moves, clear any marks, unless the new selection + // does change the marks in some way + const moved = [ + 'anchorKey', + 'anchorOffset', + 'focusKey', + 'focusOffset', + ].some(p => props.hasOwnProperty(p)) + + if (selection.marks + && properties.marks == selection.marks + && moved) { props.marks = null } diff --git a/test/transforms/fixtures/on-selection/blur/marks/index.js b/test/transforms/fixtures/on-selection/blur/marks/index.js new file mode 100644 index 000000000..53e5ae576 --- /dev/null +++ b/test/transforms/fixtures/on-selection/blur/marks/index.js @@ -0,0 +1,26 @@ + +import assert from 'assert' +import { Mark } from '../../../../../..' + +export default function (state) { + const { startText, selection } = state + const sel = selection.merge({ + marks: Mark.createSet([ + Mark.create({ + type: 'bold' + }) + ]) + }) + + const next = state + .transform() + .addMark('bold') + .focus() + .blur() + .apply() + + assert.deepEqual( + next.selection.toJS(), + sel.toJS() + ) +} diff --git a/test/transforms/fixtures/on-selection/blur/marks/input.yaml b/test/transforms/fixtures/on-selection/blur/marks/input.yaml new file mode 100644 index 000000000..2a7e1afb1 --- /dev/null +++ b/test/transforms/fixtures/on-selection/blur/marks/input.yaml @@ -0,0 +1,17 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: block + type: paragraph + nodes: + - kind: text + text: two + - kind: block + type: paragraph + nodes: + - kind: text + text: three diff --git a/test/transforms/fixtures/on-selection/focus/marks/index.js b/test/transforms/fixtures/on-selection/focus/marks/index.js new file mode 100644 index 000000000..650c23fc0 --- /dev/null +++ b/test/transforms/fixtures/on-selection/focus/marks/index.js @@ -0,0 +1,26 @@ + +import assert from 'assert' +import { Mark } from '../../../../../..' + +export default function (state) { + const { startText, selection } = state + const sel = selection.merge({ + isFocused: true, + marks: Mark.createSet([ + Mark.create({ + type: 'bold' + }) + ]) + }) + + const next = state + .transform() + .addMark('bold') + .focus() + .apply() + + assert.deepEqual( + next.selection.toJS(), + sel.toJS() + ) +} diff --git a/test/transforms/fixtures/on-selection/focus/marks/input.yaml b/test/transforms/fixtures/on-selection/focus/marks/input.yaml new file mode 100644 index 000000000..2a7e1afb1 --- /dev/null +++ b/test/transforms/fixtures/on-selection/focus/marks/input.yaml @@ -0,0 +1,17 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: block + type: paragraph + nodes: + - kind: text + text: two + - kind: block + type: paragraph + nodes: + - kind: text + text: three