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