Skip to content

Commit

Permalink
Fix bug in normalize node: match() is never called
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksson-daniel committed Feb 5, 2025
1 parent cac3b9c commit 74d1222
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
34 changes: 12 additions & 22 deletions frontend/src/plate/plugins/normalize-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import { ELEMENT_MALTEKSTSEKSJON, ELEMENT_REDIGERBAR_MALTEKST } from '@app/plate
import { normalizeNodePlugin } from '@app/plate/plugins/normalize-node';
import { TemplateSections } from '@app/plate/template-sections';
import { createSimpleParagraph } from '@app/plate/templates/helpers';
import {
type BulletListElement,
type H1Element,
type KabalValue,
type ListItemContainerElement,
type ListItemElement,
type MaltekstseksjonElement,
type ParagraphElement,
type RedigerbarMaltekstElement,
TextAlign,
import type {
BulletListElement,
H1Element,
KabalValue,
ListItemContainerElement,
ListItemElement,
MaltekstseksjonElement,
ParagraphElement,
RedigerbarMaltekstElement,
} from '@app/plate/types';
import { BaseParagraphPlugin } from '@udecode/plate';
import { createPlateEditor } from '@udecode/plate-core/react';
import { BaseBulletedListPlugin, BaseListItemContentPlugin, BaseListItemPlugin } from '@udecode/plate-list';

Expand Down Expand Up @@ -44,11 +42,7 @@ describe('normalize node with missing type prop', () => {

editor.tf.replaceNodes(invalidNode, { at: [0] });

const defaultedNode: ParagraphElement = {
type: BaseParagraphPlugin.key,
align: TextAlign.LEFT,
children: [{ text: 'some text' }],
};
const defaultedNode = createSimpleParagraph('some text');

expect(editor.children).toEqual([defaultedNode]);
});
Expand All @@ -61,11 +55,7 @@ describe('normalize node with missing type prop', () => {

editor.tf.replaceNodes(invalidParent, { at: [0] });

const defaultedNode: ParagraphElement = {
type: BaseParagraphPlugin.key,
align: TextAlign.LEFT,
children: [{ text: 'some text' }],
};
const defaultedNode = createSimpleParagraph('some text');

expect(editor.children).toEqual([defaultedNode]);
});
Expand All @@ -88,7 +78,7 @@ describe('normalize node with missing type prop', () => {

editor.tf.replaceNodes(invalidMaltekstseksjon, { at: [0] });

const validParagraph: ParagraphElement = { children: [{ text: 'some text' }] } as ParagraphElement;
const validParagraph: ParagraphElement = createSimpleParagraph('some text');
const validRedigerbarMaltekst: RedigerbarMaltekstElement = {
type: ELEMENT_REDIGERBAR_MALTEKST,
section: TemplateSections.ANFOERSLER,
Expand Down
18 changes: 6 additions & 12 deletions frontend/src/plate/plugins/normalize-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,17 @@ export const nodeNormalize = (editor: PlateEditor, node: TElement, path: Path):
if (parentEntry === undefined) {
pushLog('Missing node type, but no parent. Setting type to paragraph.', options, LogLevel.WARN);

editor.tf.setNodes(
{ type: BaseParagraphPlugin.node.type, align: TextAlign.LEFT },
{ at: path, match: (n) => n === node },
);
editor.tf.setNodes({ type: BaseParagraphPlugin.node.type, align: TextAlign.LEFT }, { at: path, mode: 'highest' });

return true;
}

const [parentNode] = parentEntry;
const [parentNode, parentNodePath] = parentEntry;

if (isEditor(parentNode)) {
pushLog('Missing node type, element at top level in editor. Setting type to paragraph.', options, LogLevel.WARN);

editor.tf.setNodes(
{ type: BaseParagraphPlugin.node.type, align: TextAlign.LEFT },
{ at: [], match: (n) => n === node },
);
editor.tf.setNodes({ type: BaseParagraphPlugin.node.type, align: TextAlign.LEFT }, { at: path, mode: 'highest' });

return true;
}
Expand All @@ -106,7 +100,7 @@ export const nodeNormalize = (editor: PlateEditor, node: TElement, path: Path):
LogLevel.WARN,
);

editor.tf.setNodes({ type: BaseParagraphPlugin.node.type, align: TextAlign.LEFT }, { match: (n) => n === node });
editor.tf.setNodes({ type: BaseParagraphPlugin.node.type, align: TextAlign.LEFT }, { at: path, mode: 'highest' });

return true;
}
Expand All @@ -118,15 +112,15 @@ export const nodeNormalize = (editor: PlateEditor, node: TElement, path: Path):
LogLevel.WARN,
);

editor.tf.setNodes({ type: BaseParagraphPlugin.node.type }, { at: path, match: (n) => n === parentNode });
editor.tf.setNodes({ type: BaseParagraphPlugin.node.type }, { at: parentNodePath, mode: 'highest' });

return true;
}

if (parentNode.type === BaseListItemPlugin.node.type) {
pushLog('Normalized missing LIC', options);

editor.tf.setNodes({ type: BaseListItemContentPlugin.node.type }, { at: path, match: (n) => n === node });
editor.tf.setNodes({ type: BaseListItemContentPlugin.node.type }, { at: path, mode: 'highest' });

return true;
}
Expand Down

0 comments on commit 74d1222

Please sign in to comment.