Skip to content

Commit

Permalink
fix: fixed error when deleting nested elements 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
MasanobuRyuman committed Jun 22, 2023
1 parent 89f8748 commit 5835a92
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 92 deletions.
10 changes: 8 additions & 2 deletions packages/core/src/helpers/child/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export const restrictChild = (
let isRestricted: boolean = false;
const [node, path] = entry;
allowType = Array.isArray(allowType) ? allowType : [allowType];
for (const [child, childPath] of Node.children(editor, path)) {
const childEntryList = Array.from(Node.children(editor, path));
childEntryList.forEach((childEntry, index) => {
const [child, childPath] = childEntry;
if (index === 0) {
return;
}
if (!Element.isElement(child) || !allowType.includes(child.type)) {
warn({
messages: [
Expand All @@ -25,6 +30,7 @@ export const restrictChild = (
});
isRestricted = true;
}
}
});

return isRestricted;
};
22 changes: 1 addition & 21 deletions packages/element-list/src/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,7 @@ const element: Element<Attributes> = {
editable,
toolbox,
normalizeNode: (editor, entry) => {
// Allow only elements of type `list-item`.
let isNormalized: boolean = false;
const [node, path] = entry;
for (const [child, childPath] of helpers.Node.children(editor, path)) {
if (!helpers.Element.isElement(child) || child.type !== 'list-item') {
helpers.logger.warn({
messages: [
'Element removed.',
{
from: [node, path],
target: [child, childPath],
},
],
});
editor.removeNodes({
at: childPath,
});
isNormalized = true;
}
}
return isNormalized;
return helpers.childHelpers.restrictChild(editor, entry, 'list-item');
},
};
export default element;
22 changes: 1 addition & 21 deletions packages/element-list/src/ordered/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,7 @@ const element: Element<Attributes> = {
editable,
toolbox,
normalizeNode: (editor, entry) => {
// Allow only elements of type `list-item`.
let isNormalized: boolean = false;
const [node, path] = entry;
for (const [child, childPath] of helpers.Node.children(editor, path)) {
if (!helpers.Element.isElement(child) || child.type !== 'list-item') {
helpers.logger.warn({
messages: [
'Element removed.',
{
from: [node, path],
target: [child, childPath],
},
],
});
editor.removeNodes({
at: childPath,
});
isNormalized = true;
}
}
return isNormalized;
return helpers.childHelpers.restrictChild(editor, entry, 'list-item');
},
};

Expand Down
25 changes: 1 addition & 24 deletions packages/element-list/src/todo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,7 @@ const element: Element<Attributes> = {
editable,
toolbox,
normalizeNode: (editor, entry) => {
// Allow only elements of type `todo-list-item`.
let isNormalized: boolean = false;
const [node, path] = entry;
for (const [child, childPath] of helpers.Node.children(editor, path)) {
if (
!helpers.Element.isElement(child) ||
child.type !== 'todo-list-item'
) {
helpers.logger.warn({
messages: [
'Element removed.',
{
from: [node, path],
target: [child, childPath],
},
],
});
editor.removeNodes({
at: childPath,
});
isNormalized = true;
}
}
return isNormalized;
return helpers.childHelpers.restrictChild(editor, entry, 'todo-list-item');
},
};

Expand Down
28 changes: 4 additions & 24 deletions packages/element-toggle/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,10 @@ const element: Element<Attributes> = {
editable,
toolbox,
normalizeNode: (editor, entry) => {
// Allow only elements of type `list-item`.
let isNormalized: boolean = false;
const [node, path] = entry;
for (const [child, childPath] of helpers.Node.children(editor, path)) {
if (
!helpers.Element.isElement(child) ||
(child.type !== 'toggle-head' && child.type !== 'toggle-body')
) {
helpers.logger.warn({
messages: [
'Element removed.',
{
from: [node, path],
target: [child, childPath],
},
],
});
editor.removeNodes({
at: childPath,
});
isNormalized = true;
}
}
return isNormalized;
return helpers.childHelpers.restrictChild(editor, entry, [
'toggle-head',
'toggle-body',
]);
},
};
export default element;

0 comments on commit 5835a92

Please sign in to comment.