-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed error when deleting nested elements 🐛
- Loading branch information
1 parent
89f8748
commit 2b7c992
Showing
7 changed files
with
80 additions
and
94 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
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,51 @@ | ||
import { Editor, Element, Node, NodeEntry } from 'slate'; | ||
import { warn } from '../logger'; | ||
|
||
/** | ||
* Delete child of type other than received in argument. | ||
*/ | ||
export const nestedElementNormalizeNode = ( | ||
editor: Editor, | ||
entry: NodeEntry, | ||
allowType: string | string[] | ||
) => { | ||
let isRestricted: boolean = false; | ||
const [node, path] = entry; | ||
allowType = Array.isArray(allowType) ? allowType : [allowType]; | ||
const childEntryList = Array.from(Node.children(editor, path)); | ||
const childNodes = childEntryList.map((entry) => { | ||
const [node] = entry; | ||
return node; | ||
}); | ||
const hasCurrentTypeChild = !!childNodes.find((child) => { | ||
if (!Element.isElement(child)) { | ||
return false; | ||
} | ||
return allowType.includes(child.type); | ||
}); | ||
if (!hasCurrentTypeChild) { | ||
editor.removeNodes({ at: path }); | ||
return true; | ||
} | ||
|
||
childEntryList.forEach((childEntry) => { | ||
const [child, childPath] = childEntry; | ||
if (!Element.isElement(child) || !allowType.includes(child.type)) { | ||
warn({ | ||
messages: [ | ||
'Element removed.', | ||
{ | ||
from: [node, path], | ||
target: [child, childPath], | ||
}, | ||
], | ||
}); | ||
editor.removeNodes({ | ||
at: childPath, | ||
}); | ||
isRestricted = true; | ||
} | ||
}); | ||
|
||
return isRestricted; | ||
}; |
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
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
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