From 6241df42ffe3d83b1298d6acda6e775f8a033517 Mon Sep 17 00:00:00 2001 From: zhujinxuan Date: Fri, 30 Mar 2018 11:41:59 -0400 Subject: [PATCH 1/2] Fix splitBlock bug with hanging selection --- packages/slate/src/changes/at-range.js | 21 ++++++++---- .../with-delete-hanging-selection.js | 33 +++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 packages/slate/test/changes/at-current-range/split-block/with-delete-hanging-selection.js diff --git a/packages/slate/src/changes/at-range.js b/packages/slate/src/changes/at-range.js index 28f7c85e1c..6c4effc56b 100644 --- a/packages/slate/src/changes/at-range.js +++ b/packages/slate/src/changes/at-range.js @@ -988,12 +988,7 @@ Changes.setInlineAtRange = (...args) => { Changes.splitBlockAtRange = (change, range, height = 1, options = {}) => { const normalize = change.getFlag('normalize', options) - if (range.isExpanded) { - change.deleteAtRange(range, { normalize }) - range = range.collapseToStart() - } - - const { startKey, startOffset } = range + const { startKey, startOffset, endOffset, endKey } = range const { value } = change const { document } = value let node = document.assertDescendant(startKey) @@ -1006,7 +1001,19 @@ Changes.splitBlockAtRange = (change, range, height = 1, options = {}) => { h++ } - change.splitDescendantsByKey(node.key, startKey, startOffset, { normalize }) + change.splitDescendantsByKey(node.key, startKey, startOffset, { + normalize: normalize && range.isCollapsed, + }) + + if (range.isExpanded) { + if (range.isBackward) range = range.flip() + const nextBlock = change.value.document.getNextBlock(node.key) + range = range.moveAnchorToStartOf(nextBlock) + if (startKey === endKey) { + range = range.moveFocusTo(range.anchorKey, endOffset - startOffset) + } + change.deleteAtRange(range, { normalize }) + } } /** diff --git a/packages/slate/test/changes/at-current-range/split-block/with-delete-hanging-selection.js b/packages/slate/test/changes/at-current-range/split-block/with-delete-hanging-selection.js new file mode 100644 index 0000000000..96f269c1d2 --- /dev/null +++ b/packages/slate/test/changes/at-current-range/split-block/with-delete-hanging-selection.js @@ -0,0 +1,33 @@ +/** @jsx h */ + +import h from '../../../helpers/h' + +export default function(change) { + change.splitBlock() +} + +export const input = ( + + + zero + + word + + + cat is cute + + + +) + +export const output = ( + + + zero + + + cat is cute + + + +) From 9d6cd92d18dcf6e6b5dd31a98dae2bf36db71094 Mon Sep 17 00:00:00 2001 From: zhujinxuan Date: Wed, 11 Apr 2018 10:23:21 -0400 Subject: [PATCH 2/2] Restore marks --- .../slate/src/changes/at-current-range.js | 6 ++- .../split-block/with-marks.js | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 packages/slate/test/changes/at-current-range/split-block/with-marks.js diff --git a/packages/slate/src/changes/at-current-range.js b/packages/slate/src/changes/at-current-range.js index 7107b244d9..9570904821 100644 --- a/packages/slate/src/changes/at-current-range.js +++ b/packages/slate/src/changes/at-current-range.js @@ -217,8 +217,12 @@ Changes.insertText = (change, text, marks) => { Changes.splitBlock = (change, depth = 1) => { const { value } = change - const { selection } = value + const { selection, document } = value + const marks = selection.marks || document.getInsertMarksAtRange(selection) change.splitBlockAtRange(selection, depth).collapseToEnd() + if (marks && marks.size !== 0) { + change.select({ marks }) + } } /** diff --git a/packages/slate/test/changes/at-current-range/split-block/with-marks.js b/packages/slate/test/changes/at-current-range/split-block/with-marks.js new file mode 100644 index 0000000000..e9f8085912 --- /dev/null +++ b/packages/slate/test/changes/at-current-range/split-block/with-marks.js @@ -0,0 +1,38 @@ +/** @jsx h */ + +import h from '../../../helpers/h' + +export default function(change) { + change + .addMark('italic') + .splitBlock() + .insertText('cat is cute') +} + +export const input = ( + + + + word + + + + +) + +export const output = ( + + + + word + + + + + cat is cute + + + + + +)