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 30, 2023
1 parent 89f8748 commit 2b7c992
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 94 deletions.
8 changes: 5 additions & 3 deletions packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { style } from '@vanilla-extract/css';
import * as childHelpers from './child';
import Element, * as elements from './element';
import * as logger from './logger';
import * as error from './error';
import * as logger from './logger';
import Node from './node';
import * as normalizeNode from './normlizeNode';
import Path, * as pathHelpers from './path';
import ReactEditor from './reactEditor';
import * as transforms from './transform';
import Text, * as textHelpers from './text';
import * as childHelpers from './child';
import * as transforms from './transform';

export const helpers = {
ReactEditor,
Element,
logger,
error,
Node,
normalizeNode,
Path,
...transforms,
Text,
Expand Down
51 changes: 51 additions & 0 deletions packages/core/src/helpers/normlizeNode/index.ts
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;
};
26 changes: 5 additions & 21 deletions packages/element-list/src/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@ 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.normalizeNode.nestedElementNormalizeNode(
editor,
entry,
'list-item'
);
},
};
export default element;
26 changes: 5 additions & 21 deletions packages/element-list/src/ordered/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@ 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.normalizeNode.nestedElementNormalizeNode(
editor,
entry,
'list-item'
);
},
};

Expand Down
29 changes: 5 additions & 24 deletions packages/element-list/src/todo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,11 @@ 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.normalizeNode.nestedElementNormalizeNode(
editor,
entry,
'todo-list-item'
);
},
};

Expand Down
6 changes: 5 additions & 1 deletion packages/element-note/src/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ const element: Element<Attributes> = {
editable,
toolbox,
normalizeNode: (editor, entry) => {
return helpers.childHelpers.restrictChild(editor, entry, 'note-body');
return helpers.normalizeNode.nestedElementNormalizeNode(
editor,
entry,
'note-body'
);
},
};

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.normalizeNode.nestedElementNormalizeNode(editor, entry, [
'toggle-head',
'toggle-body',
]);
},
};
export default element;

0 comments on commit 2b7c992

Please sign in to comment.