https://sonarly.com/issue/24449?type=bug When a user navigates to `/objects/notes` and opens a note whose stored `bodyV2.blocknote` JSON contains blocks without `id` fields, BlockNote 0.47.x throws "Error: Block doesn't have id" during ProseMirror EditorView initialization. The error originates in BlockNote's internal node view factory (`yv` function) which reads `node.attrs.id` from each `blockContainer` ProseMirror node and throws if it's falsy. The `parseInitialBlocknote` utility parses stored JSON into `PartialBlock[]` without ensuring blocks have IDs, and while `useCreateBlockNote` is designed to auto-generate IDs for PartialBlocks, certain block structures (nested children, specific block types, or data written under the previous BlockNote 0.31.x format) fail to receive IDs before the ProseMirror view mounts.
32 lines
918 B
TypeScript
32 lines
918 B
TypeScript
import type { PartialBlock } from '@blocknote/core';
|
|
import { isArray, isNonEmptyString } from '@sniptt/guards';
|
|
|
|
import { ensureBlockIds } from '@/blocknote-editor/utils/ensureBlockIds';
|
|
|
|
export const parseInitialBlocknote = (
|
|
blocknote?: string | null,
|
|
logContext?: string,
|
|
): PartialBlock[] | undefined => {
|
|
if (isNonEmptyString(blocknote) && blocknote !== '{}') {
|
|
let parsedBody: PartialBlock[] | undefined = undefined;
|
|
|
|
// TODO: Remove this once we have removed the old rich text
|
|
try {
|
|
parsedBody = JSON.parse(blocknote);
|
|
} catch {
|
|
// oxlint-disable-next-line no-console
|
|
console.warn(logContext ?? `Failed to parse blocknote body`);
|
|
// oxlint-disable-next-line no-console
|
|
console.warn(blocknote);
|
|
}
|
|
|
|
if (!isArray(parsedBody) || parsedBody.length === 0) {
|
|
return undefined;
|
|
}
|
|
|
|
return ensureBlockIds(parsedBody);
|
|
}
|
|
|
|
return undefined;
|
|
};
|