Skip to content

Commit

Permalink
fix(editor): pressing backspace before a block object now deletes it
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhg committed Sep 10, 2024
1 parent 5f8866d commit 4c6474c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 98 deletions.
47 changes: 1 addition & 46 deletions packages/editor/e2e-tests/__tests__/block-objects.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,59 +51,14 @@ Feature: Block Objects
And "Delete" is pressed
Then the text is "foo,\n,bar"

# Mimics Google Docs' behaviour
@skip
Scenario: Pressing Delete before an image
Given the text "foo"
And an "image"
When "Enter" is pressed
And "bar" is typed
And the caret is put after "foo"
And "Delete" is pressed
Then the text is "foobar"

# Mimics Notion's behaviour
@skip
Scenario: Pressing Delete before an image
Given the text "foo"
And an "image"
When "Enter" is pressed
And "bar" is typed
And the caret is put after "foo"
And "Delete" is pressed
Then the text is "foobar,\n,image"

# Warning: Not consistent with Delete before an image
# Perhaps the image should be deleted instead
Scenario: Pressing Backspace after an image
Given the text "foo"
And an "image" "m1"
When "Enter" is pressed
And "bar" is typed
And the caret is put before "bar"
And "Backspace" is pressed with navigation intent
Then block "m1" is selected

# Mimics Google Docs' behaviour
@skip
Scenario: Pressing Backspace after an image
Given the text "foo"
And an "image"
When "Enter" is pressed
And "bar" is typed
And the caret is put before "bar"
Then the text is "foobar"

# Mimics Notion's behaviour
@skip
Scenario: Pressing Backspace after an image
Given the text "foo"
And an "image"
When "Enter" is pressed
And "bar" is typed
And the caret is put before "bar"
And "Backspace" is pressed
Then the text is "foobar,\n,image"
Then the text is "foo,\n,bar"

Scenario Outline: Deleting a lonely image
Given an "image"
Expand Down
52 changes: 0 additions & 52 deletions packages/editor/src/editor/plugins/createWithHotKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,59 +155,7 @@ export function createWithHotkeys(
return
}
}
// Disallow deleting void blocks by backspace from another line.
// Otherwise it's so easy to delete the void block above when trying to delete text on
// the line below or above
if (
isBackspace &&
editor.selection &&
editor.selection.focus.path[0] > 0 &&
Range.isCollapsed(editor.selection)
) {
const prevPath = Path.previous(editor.selection.focus.path.slice(0, 1))
const prevBlock = Node.descendant(editor, prevPath) as
| SlateTextBlock
| VoidElement
const focusBlock = Node.descendant(
editor,
editor.selection.focus.path.slice(0, 1),
)
if (
prevBlock &&
focusBlock &&
Editor.isVoid(editor, prevBlock) &&
editor.selection.focus.offset === 0
) {
debug('Preventing deleting void block above')
event.preventDefault()
event.stopPropagation()

const isTextBlock = isPortableTextTextBlock(focusBlock)
const isEmptyFocusBlock =
isTextBlock &&
focusBlock.children.length === 1 &&
focusBlock.children?.[0]?.text === ''

// If this is a not an text block or it is empty, simply remove it
if (!isTextBlock || isEmptyFocusBlock) {
Transforms.removeNodes(editor, {match: (n) => n === focusBlock})
Transforms.select(editor, prevPath)

editor.onChange()
return
}

// If the focused block is a text node but it isn't empty, focus on the previous block
if (isTextBlock && !isEmptyFocusBlock) {
Transforms.select(editor, prevPath)

editor.onChange()
return
}

return
}
}
if (
isDelete &&
editor.selection &&
Expand Down

0 comments on commit 4c6474c

Please sign in to comment.