Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-void-ranges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate': patch
---

Keep a valid insertion point when replacing a selection whose end node is removed: a void node for insertNodes or a read-only node for insertFragment.
12 changes: 9 additions & 3 deletions packages/slate/src/transforms-node/insert-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ export const insertNodes: NodeTransforms['insertNodes'] = (
if (Range.isCollapsed(at)) {
at = at.anchor
} else {
const [, end] = Range.edges(at)
const pointRef = Editor.pointRef(editor, end)
const [start, end] = Range.edges(at)
const startRef = Editor.pointRef(editor, start)
const endRef = Editor.pointRef(editor, end)
const before = Editor.before(editor, start, { voids })
const beforeRef = before && Editor.pointRef(editor, before)
Transforms.delete(editor, { at })
at = pointRef.unref()!
const startPoint = startRef.unref()
const endPoint = endRef.unref()
const beforePoint = beforeRef ? beforeRef.unref() : null
at = endPoint ?? startPoint ?? beforePoint ?? Editor.start(editor, [])
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/slate/src/transforms-text/insert-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ export const insertFragment: TextTransforms['insertFragment'] = (
if (Range.isCollapsed(at)) {
at = at.anchor
} else {
const [, end] = Range.edges(at)
const [start, end] = Range.edges(at)

if (!voids && Editor.void(editor, { at: end })) {
return
}

const pointRef = Editor.pointRef(editor, end)
const startRef = Editor.pointRef(editor, start)
const endRef = Editor.pointRef(editor, end)
Transforms.delete(editor, { at })
at = pointRef.unref()!
const startPoint = startRef.unref()
const endPoint = endRef.unref()
at = endPoint ?? startPoint!
}
} else if (Location.isPath(at)) {
at = Editor.start(editor, at)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import assert from 'assert'
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = jsx(
'editor',
{},
jsx('block', {}, 'one', jsx('anchor', {}), 'two'),
jsx('block', { readOnly: true }, jsx('focus', {})),
jsx('block', {}, 'three')
)

export const run = (editor, options = {}) => {
const refs = Editor.pointRefs(editor).size
Transforms.insertFragment(editor, [jsx('block', {}, 'inserted')], options)
assert.equal(Editor.pointRefs(editor).size, refs)
}

export const output = jsx(
'editor',
{},
jsx('block', {}, 'oneinserted', jsx('cursor', {})),
jsx('block', {}, 'three')
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import assert from 'assert'
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = jsx(
'editor',
{},
jsx(
'block',
{},
'one',
jsx('anchor', {}),
'two',
jsx('inline', { readOnly: true }, jsx('focus', {})),
'three'
)
)

export const run = (editor, options = {}) => {
const refs = Editor.pointRefs(editor).size
Transforms.insertFragment(editor, [jsx('block', {}, 'inserted')], options)
assert.equal(Editor.pointRefs(editor).size, refs)
}

export const output = jsx(
'editor',
{},
jsx('block', {}, 'oneinserted', jsx('cursor', {}), 'three')
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import assert from 'assert'
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = jsx(
'editor',
{},
jsx('block', {}, 'one', jsx('anchor', {}), 'two'),
jsx('block', { void: true }, jsx('focus', {})),
jsx('block', {}, 'three')
)

export const run = (editor, options = {}) => {
const refs = Editor.pointRefs(editor).size
Transforms.insertFragment(editor, [jsx('block', {}, 'inserted')], options)
assert.equal(Editor.pointRefs(editor).size, refs)
}

export const output = jsx(
'editor',
{},
jsx('block', {}, 'one', jsx('anchor', {}), 'two'),
jsx('block', { void: true }, jsx('focus', {})),
jsx('block', {}, 'three')
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import assert from 'assert'
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = jsx(
'editor',
{},
jsx(
'block',
{},
'one',
jsx('anchor', {}),
'two',
jsx('inline', { void: true }, jsx('focus', {})),
'three'
)
)

export const run = (editor, options = {}) => {
const refs = Editor.pointRefs(editor).size
Transforms.insertFragment(editor, [jsx('block', {}, 'inserted')], options)
assert.equal(Editor.pointRefs(editor).size, refs)
}

export const output = jsx(
'editor',
{},
jsx(
'block',
{},
'one',
jsx('anchor', {}),
'two',
jsx('inline', { void: true }, jsx('focus', {})),
'three'
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import assert from 'assert'
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = jsx(
'editor',
{},
jsx('block', {}, 'one', jsx('anchor', {}), 'two'),
jsx('block', { void: true }, jsx('focus', {})),
jsx('block', {}, 'three')
)

export const run = (editor, options = {}) => {
const refs = Editor.pointRefs(editor).size
Transforms.insertNodes(editor, { text: 'inserted' }, options)
assert.equal(Editor.pointRefs(editor).size, refs)
}

export const output = jsx(
'editor',
{},
jsx('block', {}, 'oneinserted', jsx('cursor', {})),
jsx('block', {}, 'three')
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import assert from 'assert'
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = jsx(
'editor',
{},
jsx(
'block',
{},
'one',
jsx('anchor', {}),
'two',
jsx('inline', { void: true }, jsx('focus', {})),
'three'
)
)

export const run = (editor, options = {}) => {
const refs = Editor.pointRefs(editor).size
Transforms.insertNodes(editor, { text: 'inserted' }, options)
assert.equal(Editor.pointRefs(editor).size, refs)
}

export const output = jsx(
'editor',
{},
jsx('block', {}, 'oneinserted', jsx('cursor', {}), 'three')
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import assert from 'assert'
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = jsx(
'editor',
{},
jsx(
'block',
{},
'one',
jsx('inline', { void: true }, jsx('anchor', {})),
'two',
jsx('inline', { void: true }, jsx('focus', {})),
'three'
)
)

export const run = (editor, options = {}) => {
const refs = Editor.pointRefs(editor).size
Transforms.insertNodes(editor, { text: 'inserted' }, options)
assert.equal(Editor.pointRefs(editor).size, refs)
}

export const output = jsx(
'editor',
{},
jsx('block', {}, 'oneinserted', jsx('cursor', {}), 'three')
)
Loading