@@ -17,7 +17,8 @@ import type {
1717 ListEditorNode ,
1818 ParagraphEditorNode ,
1919 EditorState ,
20- ListItemInfo
20+ ListItemInfo ,
21+ ChildEditorNode
2122} from './type' ;
2223
2324export function registerLexicalTextEntity < T extends TextNode | VariableLabelNode | VariableNode > (
@@ -472,6 +473,47 @@ export const editorStateToText = (editor: LexicalEditor) => {
472473 const editorState = editor . getEditorState ( ) . toJSON ( ) as EditorState ;
473474 const paragraphs = editorState . root . children ;
474475
476+ const extractText = ( node : ChildEditorNode ) : string => {
477+ if ( ! node ) return '' ;
478+
479+ // 处理换行符节点
480+ if ( node . type === 'linebreak' ) {
481+ return '\n' ;
482+ }
483+
484+ // 处理 tab 节点
485+ if ( node . type === 'tab' ) {
486+ return ' ' ;
487+ }
488+
489+ // 处理文本节点
490+ if ( node . type === 'text' ) {
491+ return node . text ;
492+ }
493+
494+ // 处理自定义变量节点
495+ if ( node . type === 'variableLabel' || node . type === 'Variable' ) {
496+ return node . variableKey ;
497+ }
498+
499+ // 处理段落节点 - 递归处理其 children
500+ if ( node . type === 'paragraph' ) {
501+ return node . children . map ( extractText ) . join ( '' ) ;
502+ }
503+
504+ // 处理列表项节点 - 递归处理其 children
505+ if ( node . type === 'listitem' ) {
506+ return node . children . map ( extractText ) . join ( '' ) ;
507+ }
508+
509+ // 处理列表节点 - 递归处理其 children
510+ if ( node . type === 'list' ) {
511+ return node . children . map ( extractText ) . join ( '' ) ;
512+ }
513+
514+ return '' ;
515+ } ;
516+
475517 paragraphs . forEach ( ( paragraph ) => {
476518 if ( paragraph . type === 'list' ) {
477519 const listResults = processList ( { list : paragraph } ) ;
@@ -483,43 +525,15 @@ export const editorStateToText = (editor: LexicalEditor) => {
483525 const indentSpaces = ' ' . repeat ( paragraph . indent || 0 ) ;
484526
485527 children . forEach ( ( child ) => {
486- if ( child . type === 'linebreak' ) {
487- paragraphText . push ( '\n' ) ;
488- } else if ( child . type === 'text' ) {
489- paragraphText . push ( child . text ) ;
490- } else if ( child . type === 'tab' ) {
491- paragraphText . push ( ' ' ) ;
492- } else if ( child . type === 'variableLabel' || child . type === 'Variable' ) {
493- paragraphText . push ( child . variableKey ) ;
528+ const val = extractText ( child ) ;
529+ if ( val ) {
530+ paragraphText . push ( val ) ;
494531 }
495532 } ) ;
496533
497534 const finalText = paragraphText . join ( '' ) ;
498535 editorStateTextString . push ( indentSpaces + finalText ) ;
499536 } else {
500- // 处理其他未知类型节点(heading、quote、code 等)
501- // 递归提取所有子节点的文本内容
502- const extractText = ( node : any ) : string => {
503- if ( ! node ) return '' ;
504-
505- // 如果有 text 属性,直接返回
506- if ( node . text !== undefined ) {
507- return node . text ;
508- }
509-
510- // 如果有 variableKey 属性(自定义变量节点)
511- if ( node . variableKey ) {
512- return node . variableKey ;
513- }
514-
515- // 如果有 children,递归处理
516- if ( Array . isArray ( node . children ) ) {
517- return node . children . map ( extractText ) . join ( '' ) ;
518- }
519-
520- return '' ;
521- } ;
522-
523537 const text = extractText ( paragraph ) ;
524538 if ( text ) {
525539 editorStateTextString . push ( text ) ;
0 commit comments