An open source design system that's fully customizable and agent ready
TypeScript
fix(richtext): RichTextExtensionNode is constructible with no arguments (#9690)
Sherry626845fcc773
diff --git a/.changeset/richtext-extension-node-no-args.md b/.changeset/richtext-extension-node-no-args.mdnew file mode 100644index 0000000000..05975bfaea--- /dev/null+++ b/.changeset/richtext-extension-node-no-args.md@@ -0,0 +1,7 @@+---+'@astryxdesign/richtext': patch+---++[fix] RichTextExtensionNode is constructible with no arguments, as a registered node class must be. A Yjs collaboration binding builds one instance of every registered node class, with no arguments, to read its default properties, so creating a binding over an editor with this package's default nodes threw `TypeError: Cannot read properties of undefined (reading 'display')` before any document was loaded. Its `facts` now default to the same empty values the class already reads for damaged stored state, so a no-argument node reports an empty inline extension instead of throwing. A node built with facts is unchanged.++@potatowagondiff --git a/packages/richtext/src/markdownExtensionNode.test.ts b/packages/richtext/src/markdownExtensionNode.test.tsnew file mode 100644index 0000000000..1a717f817c--- /dev/null+++ b/packages/richtext/src/markdownExtensionNode.test.ts@@ -0,0 +1,79 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file markdownExtensionNode.test.ts+ * @input Uses @lexical/headless, lexical, ./editorNodes,+ * ./markdownExtensionNode+ * @output Pins the registered-node-class contract: RichTextExtensionNode is+ * constructible with no arguments, and such a node reads as the empty+ * values the class already gives damaged stored state.+ * @position Unit test for the extension node's construction contract.+ */++import {describe, expect, it} from 'vitest';+import {createHeadlessEditor} from '@lexical/headless';++import {DEFAULT_NODES} from './editorNodes';+import {+ RichTextExtensionNode,+ factsFrom,+ type RichTextExtensionNodeFacts,+} from './markdownExtensionNode';++/** Runs `work` in an editor that registers this package's default nodes. */+function inEditor<T>(work: () => T): T {+ const editor = createHeadlessEditor({+ nodes: [...DEFAULT_NODES],+ onError(error) {+ throw error;+ },+ });+ let result!: T;+ editor.update(+ () => {+ result = work();+ },+ {discrete: true},+ );+ return result;+}++describe('RichTextExtensionNode construction', () => {+ // A Yjs binding builds one instance of every registered node class, with no+ // arguments, to read its default properties. A throw here stops collaboration+ // for every editor that registers this node, not only a document that holds+ // one.+ it('is constructible with no arguments', () => {+ expect(() => inEditor(() => new RichTextExtensionNode())).not.toThrow();+ });++ it('reads as empty facts, not as undefined', () => {+ const {facts, format} = inEditor(() => {+ const node = new RichTextExtensionNode();+ return {facts: node.getFacts(), format: node.getFormat()};+ });++ expect(facts).toEqual<RichTextExtensionNodeFacts>(factsFrom({}));+ // `display` decides whether the node carries text formats; reading it off+ // an undefined object is what used to throw.+ expect(facts.display).toBe('inline');+ expect(format).toBe(0);+ });++ it('still takes the facts it is given', () => {+ const given = factsFrom({+ display: 'block',+ name: 'chart',+ plugin: 'demo',+ source: '```chart\n```',+ });+ const {facts, format} = inEditor(() => {+ const node = new RichTextExtensionNode(given, 1);+ return {facts: node.getFacts(), format: node.getFormat()};+ });++ expect(facts.name).toBe('chart');+ // A block node carries no text format, whatever it is passed.+ expect(format).toBe(0);+ });+});diff --git a/packages/richtext/src/markdownExtensionNode.ts b/packages/richtext/src/markdownExtensionNode.tsindex 922da75089..6b3329dbdb 100644--- a/packages/richtext/src/markdownExtensionNode.ts+++ b/packages/richtext/src/markdownExtensionNode.ts@@ -169,7 +169,14 @@ export class RichTextExtensionNode extends DecoratorNode<unknown> {return {span: importExtensionElement, div: importExtensionElement};}- constructor(facts: RichTextExtensionNodeFacts, format = 0, key?: NodeKey) {+ // The Yjs binding and `$create` call the constructor with no arguments.+ // The binding reads each class's default properties from that node.+ // With no facts, the node gets the values that damaged stored facts get.+ constructor(+ facts: RichTextExtensionNodeFacts = factsFrom({}),+ format = 0,+ key?: NodeKey,+ ) {super(key);this.__facts = facts;this.__format = facts.display === 'inline' ? format : 0;