/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ // @generated by scripts/generate-node-json.mjs from each class's `json` // schema. Run `pnpm run generate-node-json` to regenerate; do not edit. // Keys are emitted in the order the schema-driven walk writes them, so the two // produce byte-identical JSON. That order is the schema's, not alphabetical. /* eslint-disable sort-keys-fix/sort-keys-fix */ import type {LexicalNode} from './LexicalNode'; import type {CompactDefaultTest} from './LexicalUtils'; import type {ElementNode} from './nodes/LexicalElementNode'; import type {LineBreakNode} from './nodes/LexicalLineBreakNode'; import type {ParagraphNode} from './nodes/LexicalParagraphNode'; import type {TabNode} from './nodes/LexicalTabNode'; import type {TextNode} from './nodes/LexicalTextNode'; import { aliasTableOf, type ComposedSchemaFields, getterTableOf, setterDefaultOf, setterTableOf, } from './LexicalSchema'; /** * The generated implementations for one node class. * * Each is handed to the class it was generated from through that class's * `$config`, so nothing has to match code to class at runtime. * * @internal */ export interface GeneratedJSON { // Method syntax, so TypeScript checks the parameter bivariantly and a // function generated for one class is assignable here — the same reason // SerializationSchema.isEqual is declared this way. The alternative is to // widen the parameter to 'never', which no generated function actually // accepts and every call site then has to cast back. exportJSON(node: LexicalNode): {[key: string]: unknown}; // `isCompactDefault` is the running class's own omission test, supplied by // the dispatch. Generated code states each property's comparison as source // where the default has a literal a value could be `===`; where it does not, // the comparison is this call, so the two forms omit the same properties // without the generated module holding a value import of anything. Most // exporters declare the parameter and never call it, and one generated // before it existed simply declares one parameter. exportCompactJSON?( node: LexicalNode, isCompactDefault: CompactDefaultTest, ): {[key: string]: unknown}; // Returns the node the properties were applied to, which is the node passed // in: it is writable by construction and every setter writes it in place. // The return is kept so a caller reads the applied node from one place, // whether or not the class has a generated parser. updateFromJSON?( node: LexicalNode, json: {readonly [key: string]: unknown}, ): LexicalNode; // Copies the fields this class's own `$config` declared, for the // `afterCloneFrom` synthesized from the same schema. The superclass's half is // that method's `super` call, so this covers one class's own fields and // nothing above it. afterCloneFrom?(node: LexicalNode, prevNode: LexicalNode): void; } /** * Builds the generated implementations for one class from that class's * composed schema, which the registration hands it: the lookup tables the * code reads are the schema's own objects, read from it here, so a generated * module holds no copy of a table and nothing about one is written into it * at build time. * * @internal */ export type GeneratedJSONFactory = ( fields: ComposedSchemaFields, ) => GeneratedJSON; // The JSON number grammar, anchored, matching numberValue: `Number()` alone // reads '0x10' as 16 and '' as 0, and neither is a shape a JSON encoder // produces. Emitted from the same source the codegen verified against, so the // two cannot be different functions. const JSON_NUMBER = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/; function num(v: unknown, d: number): number { if (typeof v === 'number') { return Number.isFinite(v) ? v : d; } if (typeof v !== 'string' || !JSON_NUMBER.test(v)) { return d; } const n = Number(v); return Number.isFinite(n) ? n : d; } function numC( v: unknown, d: number, min: number, max: number, integer: boolean, ): number { const n = num(v, d); return n >= min && n <= max && (!integer || Number.isInteger(n)) ? n : d; } /** * ElementNode's schema-declared fields, for a clone. Generated from that * schema; do not edit by hand. * * @internal */ export function afterCloneElementNode( node: ElementNode, prevNode: ElementNode, ): void { node.__dir = prevNode.__dir; node.__format = prevNode.__format; node.__indent = prevNode.__indent; node.__textFormat = prevNode.__textFormat; node.__textStyle = prevNode.__textStyle; } /** ElementNode's generated implementations, for its `$config`. @internal */ export const GENERATED_ELEMENT: GeneratedJSONFactory = fields => { const ELEMENT_FORMAT_GETTER = getterTableOf(fields, 'format') as { readonly [key: string]: | '' | 'center' | 'end' | 'justify' | 'left' | 'right' | 'start'; }; const ELEMENT_FORMAT_SETTER = setterTableOf(fields, 'format') as { readonly [key: string]: 0 | 1 | 2 | 3 | 4 | 5 | 6; }; const ELEMENT_FORMAT_SETTER_DEFAULT = setterDefaultOf(fields, 'format') as | 0 | 1 | 2 | 3 | 4 | 5 | 6; /** Generated from ElementNode's serialization schema. Do not edit by hand. */ function exportElementNode(node: ElementNode): {[key: string]: unknown} { const textFormat = node.__textFormat; const textStyle = node.__textStyle; const shouldSerializeTextStyles = (textFormat !== 0 || textStyle !== '') && node.shouldSerializeTextStyles(); return { children: [], direction: node.__dir, format: ELEMENT_FORMAT_GETTER[node.__format], indent: node.__indent, textFormat: textFormat !== 0 && shouldSerializeTextStyles ? textFormat : undefined, textStyle: textStyle !== '' && shouldSerializeTextStyles ? textStyle : undefined, type: node.__type, version: 1, }; } /** Generated from ElementNode's serialization schema. Do not edit by hand. */ function exportCompactElementNode(node: ElementNode): { [key: string]: unknown; } { const textFormat = node.__textFormat; const textStyle = node.__textStyle; const shouldSerializeTextStyles = (textFormat !== 0 || textStyle !== '') && node.shouldSerializeTextStyles(); const json: {[key: string]: unknown} = {type: node.__type, children: []}; const direction = node.__dir; if (direction != null) { json.direction = direction; } const format = ELEMENT_FORMAT_GETTER[node.__format]; if (format !== undefined && format !== '') { json.format = format; } const indent = node.__indent; if (indent !== undefined && indent !== 0) { json.indent = indent; } if ( textFormat !== undefined && textFormat !== 0 && shouldSerializeTextStyles ) { json.textFormat = textFormat; } if ( textStyle !== undefined && textStyle !== '' && shouldSerializeTextStyles ) { json.textStyle = textStyle; } return json; } /** Generated from ElementNode's serialization schema. Do not edit by hand. */ function updateElementNode( node: ElementNode, json: {readonly [key: string]: unknown}, ): ElementNode { const direction = json.direction; node.__dir = direction === null || direction === 'ltr' || direction === 'rtl' ? direction : null; const format = json.format; node.__format = typeof format === 'string' && format in ELEMENT_FORMAT_SETTER ? ELEMENT_FORMAT_SETTER[format] : ELEMENT_FORMAT_SETTER_DEFAULT; node.__indent = numC(json.indent, 0, 0, Infinity, true); node.__textFormat = num(json.textFormat, 0); const textStyle = json.textStyle; node.__textStyle = typeof textStyle === 'string' ? textStyle : ''; return node; } return { exportJSON: exportElementNode, exportCompactJSON: exportCompactElementNode, updateFromJSON: updateElementNode, }; }; /** * TextNode's schema-declared fields, for a clone. Generated from that * schema; do not edit by hand. * * @internal */ export function afterCloneTextNode(node: TextNode, prevNode: TextNode): void { node.__detail = prevNode.__detail; node.__format = prevNode.__format; node.__mode = prevNode.__mode; node.__style = prevNode.__style; node.__text = prevNode.__text; } /** TextNode's generated implementations, for its `$config`. @internal */ export const GENERATED_TEXT: GeneratedJSONFactory = fields => { const TEXT_MODE_GETTER = getterTableOf(fields, 'mode') as { readonly [key: string]: 'normal' | 'segmented' | 'token'; }; const TEXT_DETAIL_ALIAS = aliasTableOf(fields, 'detail', 0) as { readonly [key: string]: 1 | 2; }; const TEXT_FORMAT_ALIAS = aliasTableOf(fields, 'format', 0) as { readonly [key: string]: | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024; }; const TEXT_MODE_SETTER = setterTableOf(fields, 'mode') as { readonly [key: string]: 0 | 1 | 2; }; const TEXT_MODE_SETTER_DEFAULT = setterDefaultOf(fields, 'mode') as 0 | 1 | 2; /** Generated from TextNode's serialization schema. Do not edit by hand. */ function exportTextNode(node: TextNode): {[key: string]: unknown} { return { detail: node.__detail, format: node.__format, mode: TEXT_MODE_GETTER[node.__mode], style: node.__style, text: node.__text, type: node.__type, version: 1, }; } /** Generated from TextNode's serialization schema. Do not edit by hand. */ function exportCompactTextNode(node: TextNode): {[key: string]: unknown} { const json: {[key: string]: unknown} = {type: node.__type}; const detail = node.__detail; if (detail !== undefined && detail !== 0) { json.detail = detail; } const format = node.__format; if (format !== undefined && format !== 0) { json.format = format; } const mode = TEXT_MODE_GETTER[node.__mode]; if (mode !== undefined && mode !== 'normal') { json.mode = mode; } const style = node.__style; if (style !== undefined && style !== '') { json.style = style; } const text = node.__text; if (text !== undefined && text !== '') { json.text = text; } return json; } /** Generated from TextNode's serialization schema. Do not edit by hand. */ function updateTextNode( node: TextNode, json: {readonly [key: string]: unknown}, ): TextNode { const detail = json.detail; node.__detail = typeof detail === 'string' && detail in TEXT_DETAIL_ALIAS ? TEXT_DETAIL_ALIAS[detail] : num(detail, 0); const format = json.format; node.__format = typeof format === 'string' && format in TEXT_FORMAT_ALIAS ? TEXT_FORMAT_ALIAS[format] : num(format, 0); const mode = json.mode; node.__mode = typeof mode === 'string' && mode in TEXT_MODE_SETTER ? TEXT_MODE_SETTER[mode] : TEXT_MODE_SETTER_DEFAULT; const style = json.style; node.__style = typeof style === 'string' ? style : ''; const text = json.text; node.__text = typeof text === 'string' ? text : ''; return node; } return { exportJSON: exportTextNode, exportCompactJSON: exportCompactTextNode, updateFromJSON: updateTextNode, afterCloneFrom: afterCloneTextNode, }; }; /** ParagraphNode's generated implementations, for its `$config`. @internal */ export const GENERATED_PARAGRAPH: GeneratedJSONFactory = fields => { const PARAGRAPH_FORMAT_GETTER = getterTableOf(fields, 'format') as { readonly [key: string]: | '' | 'center' | 'end' | 'justify' | 'left' | 'right' | 'start'; }; const PARAGRAPH_FORMAT_SETTER = setterTableOf(fields, 'format') as { readonly [key: string]: 0 | 1 | 2 | 3 | 4 | 5 | 6; }; const PARAGRAPH_FORMAT_SETTER_DEFAULT = setterDefaultOf(fields, 'format') as | 0 | 1 | 2 | 3 | 4 | 5 | 6; /** Generated from ParagraphNode's serialization schema. Do not edit by hand. */ function exportParagraphNode(node: ParagraphNode): {[key: string]: unknown} { const textFormat = node.__textFormat; const textStyle = node.__textStyle; const shouldSerializeTextStyles = (textFormat !== 0 || textStyle !== '') && node.shouldSerializeTextStyles(); return { children: [], direction: node.__dir, format: PARAGRAPH_FORMAT_GETTER[node.__format], indent: node.__indent, textFormat: textFormat !== 0 && shouldSerializeTextStyles ? textFormat : undefined, textStyle: textStyle !== '' && shouldSerializeTextStyles ? textStyle : undefined, type: node.__type, version: 1, }; } /** Generated from ParagraphNode's serialization schema. Do not edit by hand. */ function exportCompactParagraphNode(node: ParagraphNode): { [key: string]: unknown; } { const textFormat = node.__textFormat; const textStyle = node.__textStyle; const shouldSerializeTextStyles = (textFormat !== 0 || textStyle !== '') && node.shouldSerializeTextStyles(); const json: {[key: string]: unknown} = {type: node.__type, children: []}; const direction = node.__dir; if (direction != null) { json.direction = direction; } const format = PARAGRAPH_FORMAT_GETTER[node.__format]; if (format !== undefined && format !== '') { json.format = format; } const indent = node.__indent; if (indent !== undefined && indent !== 0) { json.indent = indent; } if ( textFormat !== undefined && textFormat !== 0 && shouldSerializeTextStyles ) { json.textFormat = textFormat; } if ( textStyle !== undefined && textStyle !== '' && shouldSerializeTextStyles ) { json.textStyle = textStyle; } return json; } /** Generated from ParagraphNode's serialization schema. Do not edit by hand. */ function updateParagraphNode( node: ParagraphNode, json: {readonly [key: string]: unknown}, ): ParagraphNode { const direction = json.direction; node.__dir = direction === null || direction === 'ltr' || direction === 'rtl' ? direction : null; const format = json.format; node.__format = typeof format === 'string' && format in PARAGRAPH_FORMAT_SETTER ? PARAGRAPH_FORMAT_SETTER[format] : PARAGRAPH_FORMAT_SETTER_DEFAULT; node.__indent = numC(json.indent, 0, 0, Infinity, true); node.__textFormat = num(json.textFormat, 0); const textStyle = json.textStyle; node.__textStyle = typeof textStyle === 'string' ? textStyle : ''; return node; } return { exportJSON: exportParagraphNode, exportCompactJSON: exportCompactParagraphNode, updateFromJSON: updateParagraphNode, }; }; /** LineBreakNode's generated implementations, for its `$config`. @internal */ export const GENERATED_LINEBREAK: GeneratedJSONFactory = () => { /** Generated from LineBreakNode's serialization schema. Do not edit by hand. */ function exportLineBreakNode(node: LineBreakNode): {[key: string]: unknown} { return { type: node.__type, version: 1, }; } /** Generated from LineBreakNode's serialization schema. Do not edit by hand. */ function exportCompactLineBreakNode(node: LineBreakNode): { [key: string]: unknown; } { return {type: node.__type}; } return { exportJSON: exportLineBreakNode, exportCompactJSON: exportCompactLineBreakNode, }; }; /** TabNode's generated implementations, for its `$config`. @internal */ export const GENERATED_TAB: GeneratedJSONFactory = fields => { const TAB_MODE_GETTER = getterTableOf(fields, 'mode') as { readonly [key: string]: 'normal'; }; const TAB_FORMAT_ALIAS = aliasTableOf(fields, 'format', 0) as { readonly [key: string]: | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024; }; /** Generated from TabNode's serialization schema. Do not edit by hand. */ function exportTabNode(node: TabNode): {[key: string]: unknown} { return { detail: node.__detail, mode: TAB_MODE_GETTER[node.__mode], text: node.__text, format: node.__format, style: node.__style, type: node.__type, version: 1, }; } /** Generated from TabNode's serialization schema. Do not edit by hand. */ function exportCompactTabNode(node: TabNode): {[key: string]: unknown} { const json: {[key: string]: unknown} = {type: node.__type}; const format = node.__format; if (format !== undefined && format !== 0) { json.format = format; } const style = node.__style; if (style !== undefined && style !== '') { json.style = style; } return json; } /** Generated from TabNode's serialization schema. Do not edit by hand. */ function updateTabNode( node: TabNode, json: {readonly [key: string]: unknown}, ): TabNode { const format = json.format; node.__format = typeof format === 'string' && format in TAB_FORMAT_ALIAS ? TAB_FORMAT_ALIAS[format] : num(format, 0); const style = json.style; node.__style = typeof style === 'string' ? style : ''; return node; } return { exportJSON: exportTabNode, exportCompactJSON: exportCompactTabNode, updateFromJSON: updateTabNode, }; };