Skip to content

Commit

Permalink
Fix normalization for typesless nodes inside toplevel elements
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksson-daniel committed Nov 29, 2024
1 parent ec5c734 commit fb4b2ec
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
42 changes: 40 additions & 2 deletions frontend/src/plate/plugins/normalize-node.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { describe, expect, it } from 'bun:test';
import { saksbehandlerPlugins } from '@app/plate/plugins/plugin-sets/saksbehandler';
import { ELEMENT_MALTEKSTSEKSJON, ELEMENT_REDIGERBAR_MALTEKST } from '@app/plate/plugins/element-types';
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,
} from '@app/plate/types';
import { BaseParagraphPlugin, replaceNode } from '@udecode/plate-common';
import { createPlateEditor } from '@udecode/plate-core/react';
import { BaseBulletedListPlugin, BaseListItemContentPlugin, BaseListItemPlugin } from '@udecode/plate-list';

const createEditor = (value: KabalValue) => createPlateEditor({ plugins: saksbehandlerPlugins, value });
const createEditor = (value: KabalValue) => createPlateEditor({ plugins: [normalizeNodePlugin], value });

describe('normalize node with missing type prop', () => {
it('should fix LICs', () => {
Expand Down Expand Up @@ -65,4 +69,38 @@ describe('normalize node with missing type prop', () => {

expect(editor.children).toEqual([defaultedNode]);
});

it('should fix descendants inside redigerbar maltekst (top level element)', () => {
const invalidParagraph: ParagraphElement = { children: [{ text: 'some text' }] } as ParagraphElement;
const invalidRedigerbarMaltekst: RedigerbarMaltekstElement = {
type: ELEMENT_REDIGERBAR_MALTEKST,
section: TemplateSections.ANFOERSLER,
children: [invalidParagraph],
};
const invalidMaltekstseksjon: MaltekstseksjonElement = {
type: ELEMENT_MALTEKSTSEKSJON,
section: TemplateSections.REGELVERK_TITLE,
textIdList: [],
children: [invalidRedigerbarMaltekst],
};

const editor = createEditor([createSimpleParagraph()]);

replaceNode(editor, { at: [0], nodes: invalidMaltekstseksjon });

const validParagraph: ParagraphElement = { children: [{ text: 'some text' }] } as ParagraphElement;
const validRedigerbarMaltekst: RedigerbarMaltekstElement = {
type: ELEMENT_REDIGERBAR_MALTEKST,
section: TemplateSections.ANFOERSLER,
children: [validParagraph],
};
const validMaltekstseksjon: MaltekstseksjonElement = {
type: ELEMENT_MALTEKSTSEKSJON,
section: TemplateSections.REGELVERK_TITLE,
textIdList: [],
children: [validRedigerbarMaltekst],
};

expect(editor.children).toEqual([validMaltekstseksjon]);
});
});
20 changes: 17 additions & 3 deletions frontend/src/plate/plugins/normalize-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,33 @@ export const normalizeNodePlugin = createPlatePlugin({

const [parentNode] = parentEntry;

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

return setNodes(
editor,
{ type: BaseParagraphPlugin.node.type, align: TextAlign.LEFT },
{ at: [], match: (n) => n === node },
);
}

const isParentTopLevelElement = isTopLevelElement(parentNode);

if (isParentTopLevelElement || isEditor(parentNode)) {
if (isParentTopLevelElement) {
pushLog(
`Missing node type, element at top level in "${isParentTopLevelElement ? parentNode.type : 'editor'}". Setting type to paragraph.`,
`Missing node type, element at top level in "${parentNode.type}". Setting type to paragraph.`,
options,
LogLevel.WARN,
);

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

Expand Down

0 comments on commit fb4b2ec

Please sign in to comment.