diff --git a/src/transforms/at-range.js b/src/transforms/at-range.js index 9f0a54747..69ab12723 100644 --- a/src/transforms/at-range.js +++ b/src/transforms/at-range.js @@ -1,3 +1,5 @@ +/* eslint no-console: 0 */ + import Normalize from '../utils/normalize' /** @@ -66,12 +68,11 @@ export function deleteAtRange(transform, range, options = {}) { let ancestor = document.getCommonAncestor(startKey, endKey) let startChild = ancestor.getHighestChild(startKey) let endChild = ancestor.getHighestChild(endKey) - const startOff = (startChild.key === startKey ? 0 : startChild.getOffset(startKey)) + startOffset - const endOff = (endChild.key === endKey ? 0 : endChild.getOffset(endKey)) + endOffset + const startOff = (startChild.kind == 'text' ? 0 : startChild.getOffset(startKey)) + startOffset + const endOff = (endChild.kind == 'text' ? 0 : endChild.getOffset(endKey)) + endOffset - transform = transform - .splitNodeByKey(startChild.key, startOff, { normalize: false }) - .splitNodeByKey(endChild.key, endOff, { normalize: false }) + transform = transform.splitNodeByKey(startChild.key, startOff, { normalize: false }) + transform = transform.splitNodeByKey(endChild.key, endOff, { normalize: false }) state = transform.state document = state.document @@ -83,27 +84,27 @@ export function deleteAtRange(transform, range, options = {}) { const startIndex = ancestor.nodes.indexOf(startChild) const endIndex = ancestor.nodes.indexOf(endChild) - const middles = ancestor.nodes.slice(startIndex + 1, endIndex + 1) + const middles = ancestor.nodes.slice( + startIndex + 1, + endIndex + 1 + ) if (middles.size) { // remove first nodes directly so the document is not normalized - middles.butLast().forEach(child => { - transform.removeNodeOperation(transform.state.document.getPath(child.key)) + middles.forEach(child => { + transform.removeNodeByKey(child.key, { normalize: false }) }) - - // remove last node so the document is normalized - transform.removeNodeByKey(middles.last().key) } if (startBlock.key !== endBlock.key) { endBlock.nodes.forEach((child, i) => { const newKey = startBlock.key const newIndex = startBlock.nodes.size + i - transform.moveNodeByKey(child.key, newKey, newIndex) + transform.moveNodeByKey(child.key, newKey, newIndex, { normalize: false }) }) const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock - transform.removeNodeByKey(lonely.key) + transform.removeNodeByKey(lonely.key, { normalize: false }) } if (normalize) { diff --git a/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/index.js b/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/index.js new file mode 100644 index 000000000..c2a1b30f6 --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/index.js @@ -0,0 +1,30 @@ + +import assert from 'assert' + +export default function (state) { + const { document, selection } = state + const texts = document.getTexts() + const first = texts.first() + const last = texts.last() + const range = selection.merge({ + anchorKey: first.key, + anchorOffset: 0, + focusKey: last.key, + focusOffset: 1 + }) + + const next = state + .transform() + .moveTo(range) + .deleteForward() + .apply() + + /* const updated = next.document.getTexts().last() + + assert.deepEqual( + next.selection.toJS(), + range.collapseToEndOf(updated).toJS() + ) */ + + return next +} diff --git a/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/input.yaml b/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/input.yaml new file mode 100644 index 000000000..798f00a1b --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/input.yaml @@ -0,0 +1,12 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: one + - kind: inline + type: link + nodes: + - kind: text + text: two diff --git a/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/output.yaml b/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/output.yaml new file mode 100644 index 000000000..2e0dd1d5c --- /dev/null +++ b/test/transforms/fixtures/at-current-range/delete-forward/start-text-middle-inline/output.yaml @@ -0,0 +1,10 @@ + +nodes: + - kind: block + type: paragraph + nodes: + - kind: inline + type: link + nodes: + - kind: text + text: wo