Skip to content

Commit 57b26c0

Browse files
committed
fix: editor
1 parent 819206b commit 57b26c0

File tree

2 files changed

+77
-52
lines changed

2 files changed

+77
-52
lines changed

packages/web/components/common/Textarea/PromptEditor/type.d.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,11 @@ export type TextEditorNode = BaseEditorNode & {
4242
export type LineBreakEditorNode = BaseEditorNode & {
4343
type: 'linebreak';
4444
};
45-
46-
export type VariableLabelEditorNode = BaseEditorNode & {
47-
type: 'variableLabel';
48-
variableKey: string;
49-
};
50-
51-
export type VariableEditorNode = BaseEditorNode & {
52-
type: 'Variable';
53-
variableKey: string;
54-
};
55-
5645
export type TabEditorNode = BaseEditorNode & {
5746
type: 'tab';
5847
};
5948

60-
export type ChildEditorNode =
61-
| TextEditorNode
62-
| LineBreakEditorNode
63-
| VariableLabelEditorNode
64-
| VariableEditorNode
65-
| TabEditorNode;
66-
49+
// Rich text
6750
export type ParagraphEditorNode = BaseEditorNode & {
6851
type: 'paragraph';
6952
children: ChildEditorNode[];
@@ -72,15 +55,33 @@ export type ParagraphEditorNode = BaseEditorNode & {
7255
indent: number;
7356
};
7457

58+
// ListItem 节点的 children 可以包含嵌套的 list 节点
59+
export type ListItemChildEditorNode =
60+
| TextEditorNode
61+
| LineBreakEditorNode
62+
| TabEditorNode
63+
| VariableLabelEditorNode
64+
| VariableEditorNode;
65+
7566
export type ListItemEditorNode = BaseEditorNode & {
7667
type: 'listitem';
77-
children: Array<ChildEditorNode | ListEditorNode>;
68+
children: (ListItemChildEditorNode | ListEditorNode)[];
7869
direction: string | null;
7970
format: string;
8071
indent: number;
8172
value: number;
8273
};
8374

75+
// Custom variable node types
76+
export type VariableLabelEditorNode = BaseEditorNode & {
77+
type: 'variableLabel';
78+
variableKey: string;
79+
};
80+
export type VariableEditorNode = BaseEditorNode & {
81+
type: 'Variable';
82+
variableKey: string;
83+
};
84+
8485
export type ListEditorNode = BaseEditorNode & {
8586
type: 'list';
8687
children: ListItemEditorNode[];
@@ -92,10 +93,20 @@ export type ListEditorNode = BaseEditorNode & {
9293
tag: 'ul' | 'ol';
9394
};
9495

96+
export type ChildEditorNode =
97+
| TextEditorNode
98+
| LineBreakEditorNode
99+
| TabEditorNode
100+
| ParagraphEditorNode
101+
| ListEditorNode
102+
| ListItemEditorNode
103+
| VariableLabelEditorNode
104+
| VariableEditorNode;
105+
95106
export type EditorState = {
96107
root: {
97108
type: 'root';
98-
children: Array<ParagraphEditorNode | ListEditorNode>;
109+
children: ChildEditorNode[];
99110
direction: string;
100111
format: string;
101112
indent: number;

packages/web/components/common/Textarea/PromptEditor/utils.ts

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import type {
1717
ListEditorNode,
1818
ParagraphEditorNode,
1919
EditorState,
20-
ListItemInfo
20+
ListItemInfo,
21+
ChildEditorNode
2122
} from './type';
2223

2324
export 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

Comments
 (0)