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: fixed error when deleting nested elements 🐛 #94

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
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, * as nodeHelpers from './node';
import * as normalizeNode from './normlizeNode';
import Path, * as pathHelpers from './path';
import * as rangeHelpers from './range';
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'
);
},
insertBreak: (editor, nodeEntry) => {
const [node, path] = nodeEntry;
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;