Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete behaviour when selection ends in void element #3990

Merged
merged 3 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
9 changes: 8 additions & 1 deletion packages/slate/src/interfaces/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,13 @@ export const Editor = {
at: end,
match: n => Editor.isBlock(editor, n),
})

// If last element is a void node, unhang range by including void node in range
if (Editor.isVoid(editor, endBlock[0])) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this the best solution. Should we just simply not unhang the range in Transforms.delete if the selection end is inside a void node? I think maybe we should handle this in Transforms.delete vs. changing the default behavior of unhangRange? Let's pair on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought there were some path issues before, but I didn't realize that they're handled further down in Transforms.delete. I've moved the end void node handling to Transforms.delete so that it now skips unhangRange. If you think there are still changes that could be made I'm happy to pair. Thanks for the feedback!

end = { path: end.path, offset: 1 }
return { anchor: start, focus: end }
}

const blockPath = endBlock ? endBlock[1] : []
const first = Editor.start(editor, [])
const before = { anchor: first, focus: end }
Expand All @@ -1274,7 +1281,7 @@ export const Editor = {
at: before,
match: Text.isText,
reverse: true,
voids,
voids: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to leave this as a variable and perhaps change how Transforms.delete is called in slate-react instead.

})) {
if (skip) {
skip = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/** @jsx jsx */
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = (
<editor>
<block>
<text>
<anchor />
This is a first paragraph
</text>
</block>
<block>
<text>This is the second paragraph</text>
</block>
<block void>
<text />
</block>
<block>
<text>
<focus />
</text>
</block>
</editor>
)
export const run = editor => {
editor.deleteFragment(editor)
}
export const output = (
<editor>
<block>
<text>
<cursor />
</text>
</block>
<block>
<text />
</block>
</editor>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/** @jsx jsx */
import { Editor, Transforms } from 'slate'
import { jsx } from '../../..'

export const input = (
<editor>
<block>
<text>
<anchor />
This is a first paragraph
</text>
</block>
<block>
<text>This is the second paragraph</text>
</block>
<block void>
<text>
<focus />
</text>
</block>
</editor>
)
export const run = editor => {
editor.deleteFragment(editor)
}
export const output = (
<editor>
<block>
<text>
<cursor />
</text>
</block>
</editor>
)
Binary file added site/.DS_Store
Binary file not shown.