/** * 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 {CodeHighlightNode} from './CodeHighlightNode'; import type {CodeNode} from './CodeNode'; import { aliasTableOf, type GeneratedJSONFactory, getterTableOf, setterDefaultOf, setterTableOf, } from 'lexical'; // 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; } /** * CodeNode's schema-declared fields, for a clone. Generated from that * schema; do not edit by hand. * * @internal */ export function afterCloneCodeNode(node: CodeNode, prevNode: CodeNode): void { node.__language = prevNode.__language; node.__theme = prevNode.__theme; } /** CodeNode's generated implementations, for its `$config`. @internal */ export const GENERATED_CODE: GeneratedJSONFactory = fields => { const CODE_FORMAT_GETTER = getterTableOf(fields, 'format') as { readonly [key: string]: | '' | 'center' | 'end' | 'justify' | 'left' | 'right' | 'start'; }; const CODE_FORMAT_SETTER = setterTableOf(fields, 'format') as { readonly [key: string]: 0 | 1 | 2 | 3 | 4 | 5 | 6; }; const CODE_FORMAT_SETTER_DEFAULT = setterDefaultOf(fields, 'format') as | 0 | 1 | 2 | 3 | 4 | 5 | 6; /** Generated from CodeNode's serialization schema. Do not edit by hand. */ function exportCodeNode(node: CodeNode): {[key: string]: unknown} { const textFormat = node.__textFormat; const textStyle = node.__textStyle; const shouldSerializeTextStyles = (textFormat !== 0 || textStyle !== '') && node.shouldSerializeTextStyles(); return { children: [], language: node.__language, theme: node.__theme, direction: node.__dir, format: CODE_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 CodeNode's serialization schema. Do not edit by hand. */ function exportCompactCodeNode(node: CodeNode): {[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 language = node.__language; if (language !== undefined) { json.language = language; } const theme = node.__theme; if (theme !== undefined) { json.theme = theme; } const direction = node.__dir; if (direction != null) { json.direction = direction; } const format = CODE_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 CodeNode's serialization schema. Do not edit by hand. */ function updateCodeNode( node: CodeNode, json: {readonly [key: string]: unknown}, ): CodeNode { 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 CODE_FORMAT_SETTER ? CODE_FORMAT_SETTER[format] : CODE_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 : ''; const language = json.language; node.setLanguage( language == null ? language : typeof language === 'string' ? language : '', ); const theme = json.theme; node.setTheme( theme === undefined ? undefined : typeof theme === 'string' ? theme : '', ); return node; } return { exportJSON: exportCodeNode, exportCompactJSON: exportCompactCodeNode, updateFromJSON: updateCodeNode, }; }; /** * CodeHighlightNode's schema-declared fields, for a clone. Generated from that * schema; do not edit by hand. * * @internal */ export function afterCloneCodeHighlightNode( node: CodeHighlightNode, prevNode: CodeHighlightNode, ): void { node.__highlightType = prevNode.__highlightType; } /** CodeHighlightNode's generated implementations, for its `$config`. @internal */ export const GENERATED_CODEHIGHLIGHT: GeneratedJSONFactory = fields => { const CODEHIGHLIGHT_MODE_GETTER = getterTableOf(fields, 'mode') as { readonly [key: string]: 'normal' | 'segmented' | 'token'; }; const CODEHIGHLIGHT_DETAIL_ALIAS = aliasTableOf(fields, 'detail', 0) as { readonly [key: string]: 1 | 2; }; const CODEHIGHLIGHT_FORMAT_ALIAS = aliasTableOf(fields, 'format', 0) as { readonly [key: string]: | 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024; }; const CODEHIGHLIGHT_MODE_SETTER = setterTableOf(fields, 'mode') as { readonly [key: string]: 0 | 1 | 2; }; const CODEHIGHLIGHT_MODE_SETTER_DEFAULT = setterDefaultOf(fields, 'mode') as | 0 | 1 | 2; /** Generated from CodeHighlightNode's serialization schema. Do not edit by hand. */ function exportCodeHighlightNode(node: CodeHighlightNode): { [key: string]: unknown; } { return { highlightType: node.__highlightType, detail: node.__detail, format: node.__format, mode: CODEHIGHLIGHT_MODE_GETTER[node.__mode], style: node.__style, text: node.__text, type: node.__type, version: 1, }; } /** Generated from CodeHighlightNode's serialization schema. Do not edit by hand. */ function exportCompactCodeHighlightNode(node: CodeHighlightNode): { [key: string]: unknown; } { const json: {[key: string]: unknown} = {type: node.__type}; const highlightType = node.__highlightType; if (highlightType !== undefined) { json.highlightType = highlightType; } 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 = CODEHIGHLIGHT_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 CodeHighlightNode's serialization schema. Do not edit by hand. */ function updateCodeHighlightNode( node: CodeHighlightNode, json: {readonly [key: string]: unknown}, ): CodeHighlightNode { const detail = json.detail; node.__detail = typeof detail === 'string' && detail in CODEHIGHLIGHT_DETAIL_ALIAS ? CODEHIGHLIGHT_DETAIL_ALIAS[detail] : num(detail, 0); const format = json.format; node.setFormat( typeof format === 'string' && format in CODEHIGHLIGHT_FORMAT_ALIAS ? CODEHIGHLIGHT_FORMAT_ALIAS[format] : num(format, 0), ); const mode = json.mode; node.__mode = typeof mode === 'string' && mode in CODEHIGHLIGHT_MODE_SETTER ? CODEHIGHLIGHT_MODE_SETTER[mode] : CODEHIGHLIGHT_MODE_SETTER_DEFAULT; const style = json.style; node.__style = typeof style === 'string' ? style : ''; const text = json.text; node.__text = typeof text === 'string' ? text : ''; const highlightType = json.highlightType; node.setHighlightType( highlightType == null ? highlightType : typeof highlightType === 'string' ? highlightType : '', ); return node; } return { exportJSON: exportCodeHighlightNode, exportCompactJSON: exportCompactCodeHighlightNode, updateFromJSON: updateCodeHighlightNode, afterCloneFrom: afterCloneCodeHighlightNode, }; };