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,18 +212,32 @@ export const GeneralTransforms: GeneralTransforms = {
|
|||||||
|
|
||||||
if (newProperties == null) {
|
if (newProperties == null) {
|
||||||
selection = newProperties
|
selection = newProperties
|
||||||
} else if (selection == null) {
|
} else {
|
||||||
if (!Range.isRange(newProperties)) {
|
if (selection == null) {
|
||||||
throw new Error(
|
if (!Range.isRange(newProperties)) {
|
||||||
`Cannot apply an incomplete "set_selection" operation properties ${JSON.stringify(
|
throw new Error(
|
||||||
newProperties
|
`Cannot apply an incomplete "set_selection" operation properties ${JSON.stringify(
|
||||||
)} when there is no current selection.`
|
newProperties
|
||||||
)
|
)} when there is no current selection.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
selection = { ...newProperties }
|
||||||
}
|
}
|
||||||
|
|
||||||
selection = newProperties
|
for (const key in newProperties) {
|
||||||
} else {
|
const value = newProperties[key]
|
||||||
Object.assign(selection, newProperties)
|
|
||||||
|
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
|
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