From e344f47f03c939b393d02edd37ae4bfda117c14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20S=C3=B6rlin?= Date: Thu, 1 Apr 2021 00:32:51 +0200 Subject: [PATCH] added support for deleting selection properties by setting them to null (#4109) added tests for setting and deleting custom selection props --- packages/slate/src/transforms/general.ts | 34 +++++++++++++------ .../operations/set_selection/custom-props.tsx | 29 ++++++++++++++++ .../test/operations/set_selection/remove.tsx | 29 ++++++++++++++++ 3 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 packages/slate/test/operations/set_selection/custom-props.tsx create mode 100644 packages/slate/test/operations/set_selection/remove.tsx diff --git a/packages/slate/src/transforms/general.ts b/packages/slate/src/transforms/general.ts index 9e77a759b..a3d17a4ec 100755 --- a/packages/slate/src/transforms/general.ts +++ b/packages/slate/src/transforms/general.ts @@ -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 diff --git a/packages/slate/test/operations/set_selection/custom-props.tsx b/packages/slate/test/operations/set_selection/custom-props.tsx new file mode 100644 index 000000000..eba804c35 --- /dev/null +++ b/packages/slate/test/operations/set_selection/custom-props.tsx @@ -0,0 +1,29 @@ +/** @jsx jsx */ +import { jsx } from 'slate-hyperscript' +import { Transforms, Editor } from 'slate' + +export const input = ( + + + a + + +) + +export const operations = [ + { + type: 'set_selection', + oldProperties: {}, + newProperties: { custom: 123 }, + }, +] + +export const output = ( + + + a + + +) + +Transforms.setSelection(output, { custom: 123 }) diff --git a/packages/slate/test/operations/set_selection/remove.tsx b/packages/slate/test/operations/set_selection/remove.tsx new file mode 100644 index 000000000..f0b65d06c --- /dev/null +++ b/packages/slate/test/operations/set_selection/remove.tsx @@ -0,0 +1,29 @@ +/** @jsx jsx */ +import { jsx } from 'slate-hyperscript' +import { Transforms, Editor } from 'slate' + +export const input = ( + + + a + + +) + +Transforms.setSelection(input, { custom: 123 }) + +export const operations = [ + { + type: 'set_selection', + oldProperties: {}, + newProperties: { custom: null }, + }, +] + +export const output = ( + + + a + + +)