-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add shouldMergeNodesRemovePrevNode (#5621)
* chore: add shouldMergeNodesRemovePrevNode * fix: typo
- Loading branch information
1 parent
4470f37
commit d271c4b
Showing
6 changed files
with
42 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'slate': minor | ||
--- | ||
|
||
Add a `shouldMergeNodesRemovePrevNode` editor method to control when `Transforms.mergeNodes` should remove the previous node rather than carrying out a merge operation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/slate/src/editor/should-merge-nodes-remove-prev-node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { EditorInterface, Element, Editor, Text } from '../interfaces' | ||
|
||
export const shouldMergeNodesRemovePrevNode: EditorInterface['shouldMergeNodesRemovePrevNode'] = | ||
(editor, [prevNode, prevPath], [curNode, curNodePath]) => { | ||
// If the target node that we're merging with is empty, remove it instead | ||
// of merging the two. This is a common rich text editor behavior to | ||
// prevent losing formatting when deleting entire nodes when you have a | ||
// hanging selection. | ||
// if prevNode is first child in parent,don't remove it. | ||
|
||
return ( | ||
(Element.isElement(prevNode) && Editor.isEmpty(editor, prevNode)) || | ||
(Text.isText(prevNode) && | ||
prevNode.text === '' && | ||
prevPath[prevPath.length - 1] !== 0) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters