1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-11 09:43:58 +02:00

added support for deleting selection properties by setting them to null (#4109)

added tests for setting and deleting custom selection props
This commit is contained in:
Johan Sörlin
2021-04-01 00:32:51 +02:00
committed by GitHub
parent 1b77385848
commit e344f47f03
3 changed files with 82 additions and 10 deletions

View File

@@ -212,18 +212,32 @@ export const GeneralTransforms: GeneralTransforms = {
if (newProperties == null) {
selection = newProperties
} else if (selection == null) {
if (!Range.isRange(newProperties)) {
throw new Error(
`Cannot apply an incomplete "set_selection" operation properties ${JSON.stringify(
newProperties
)} when there is no current selection.`
)
} else {
if (selection == null) {
if (!Range.isRange(newProperties)) {
throw new Error(
`Cannot apply an incomplete "set_selection" operation properties ${JSON.stringify(
newProperties
)} when there is no current selection.`
)
}
selection = { ...newProperties }
}
selection = newProperties
} else {
Object.assign(selection, newProperties)
for (const key in newProperties) {
const value = newProperties[key]
if (value == null) {
if (key === 'anchor' || key === 'focus') {
throw new Error(`Cannot remove the "${key}" selection property`)
}
delete selection[key]
} else {
selection[key] = value
}
}
}
break

View File

@@ -0,0 +1,29 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'
import { Transforms, Editor } from 'slate'
export const input = (
<editor>
<element>
a<cursor />
</element>
</editor>
)
export const operations = [
{
type: 'set_selection',
oldProperties: {},
newProperties: { custom: 123 },
},
]
export const output = (
<editor>
<element>
a<cursor />
</element>
</editor>
)
Transforms.setSelection(output, { custom: 123 })

View File

@@ -0,0 +1,29 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'
import { Transforms, Editor } from 'slate'
export const input = (
<editor>
<element>
a<cursor />
</element>
</editor>
)
Transforms.setSelection(input, { custom: 123 })
export const operations = [
{
type: 'set_selection',
oldProperties: {},
newProperties: { custom: null },
},
]
export const output = (
<editor>
<element>
a<cursor />
</element>
</editor>
)