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:
@@ -212,7 +212,8 @@ export const GeneralTransforms: GeneralTransforms = {
|
||||
|
||||
if (newProperties == null) {
|
||||
selection = newProperties
|
||||
} else if (selection == null) {
|
||||
} else {
|
||||
if (selection == null) {
|
||||
if (!Range.isRange(newProperties)) {
|
||||
throw new Error(
|
||||
`Cannot apply an incomplete "set_selection" operation properties ${JSON.stringify(
|
||||
@@ -221,9 +222,22 @@ export const GeneralTransforms: GeneralTransforms = {
|
||||
)
|
||||
}
|
||||
|
||||
selection = newProperties
|
||||
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 {
|
||||
Object.assign(selection, newProperties)
|
||||
selection[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break
|
||||
|
@@ -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 })
|
29
packages/slate/test/operations/set_selection/remove.tsx
Normal file
29
packages/slate/test/operations/set_selection/remove.tsx
Normal 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>
|
||||
)
|
Reference in New Issue
Block a user