@@ -7,17 +7,108 @@ import { BaseMarkdownEditor, MarkdownEditorInstance } from '../MarkdownEditor';
7
7
import { SendButton } from './SendButton' ;
8
8
import { useStyle } from './style' ;
9
9
10
+ /**
11
+ * Markdown 输入字段的属性接口
12
+ *
13
+ * @interface MarkdownInputFieldProps
14
+ * @property {string } [value] - 输入字段的当前文本值
15
+ * @property {function } [onChange] - 当输入值改变时触发的回调函数
16
+ * @property {string } [placeholder] - 输入字段的占位文本
17
+ * @property {React.CSSProperties } [style] - 应用于输入字段的内联样式
18
+ * @property {string } [className] - 应用于输入字段的 CSS 类名
19
+ * @property {boolean } [disabled] - 是否禁用输入字段
20
+ * @property {boolean } [typing] - 用户是否正在输入的状态标志
21
+ * @property {'Enter' | 'Mod+Enter' } [triggerSendKey] - 触发发送操作的键盘快捷键
22
+ * @property {function } [onSend] - 当内容发送时触发的异步回调函数
23
+ */
24
+
10
25
export type MarkdownInputFieldProps = {
26
+ /**
27
+ * 当前的 markdown 文本值。
28
+ * @example value="# Hello World"
29
+ */
11
30
value ?: string ;
31
+
32
+ /**
33
+ * 当输入值改变时触发的回调函数。
34
+ * @param value The new markdown text value
35
+ * @example onChange={(newValue) => setMarkdown(newValue)}
36
+ */
12
37
onChange ?: ( value : string ) => void ;
38
+
39
+ /**
40
+ * 输入字段的占位文本。
41
+ * @example placeholder="Type markdown here..."
42
+ */
13
43
placeholder ?: string ;
44
+
45
+ /**
46
+ * 应用于输入字段的内联样式。
47
+ * @example style={{ minHeight: '200px' }}
48
+ */
14
49
style ?: React . CSSProperties ;
50
+
51
+ /**
52
+ * 应用于输入字段的 CSS 类名。
53
+ * @example className="custom-markdown-input"
54
+ */
15
55
className ?: string ;
56
+
57
+ /**
58
+ * 是否禁用输入字段。
59
+ * @example disabled={isSubmitting}
60
+ */
16
61
disabled ?: boolean ;
62
+
63
+ /**
64
+ * 用户是否正在输入的状态标志。
65
+ * @example typing={isComposing}
66
+ */
17
67
typing ?: boolean ;
68
+
69
+ /**
70
+ * 触发发送操作的键盘快捷键。
71
+ * - 'Enter': 回车键触发发送
72
+ * - 'Mod+Enter': 按下 Ctrl/Command + Enter 触发发送
73
+ * @example triggerSendKey="Mod+Enter"
74
+ */
75
+ triggerSendKey ?: 'Enter' | 'Mod+Enter' ;
76
+
77
+ /**
78
+ * 当内容发送时触发的异步回调函数。
79
+ * 返回一个 Promise 对象,当发送成功后 resolve。
80
+ * @param value The current markdown text value
81
+ * @example onSend={async (text) => await submitMessage(text)}
82
+ */
18
83
onSend ?: ( value : string ) => Promise < void > ;
19
84
} ;
20
85
86
+ /**
87
+ * @component MarkdownInputField
88
+ * @description 带发送功能的Markdown输入字段组件。允许用户编辑Markdown内容并通过按钮或快捷键发送。
89
+ *
90
+ * @param {MarkdownInputFieldProps } props - 组件属性
91
+ * @param {string } [props.value] - 输入字段的Markdown内容值
92
+ * @param {function } [props.onChange] - 当输入内容改变时的回调函数
93
+ * @param {function } [props.onSend] - 当内容发送时的回调函数,应返回Promise
94
+ * @param {boolean } [props.disabled] - 是否禁用输入字段
95
+ * @param {string } [props.className] - 自定义CSS类名
96
+ * @param {React.CSSProperties } [props.style] - 自定义样式
97
+ * @param {string } [props.placeholder] - 输入字段的占位文本
98
+ * @param {string } [props.triggerSendKey='Enter'] - 触发发送的键盘快捷键,可以是'Enter'或'Mod+Enter'
99
+ * @param {boolean } [props.typing] - 是否显示正在输入状态
100
+ *
101
+ * @returns {JSX.Element } 带样式和功能的Markdown输入字段组件
102
+ *
103
+ * @example
104
+ * <MarkdownInputField
105
+ * value="# 标题"
106
+ * onChange={(value) => console.log(value)}
107
+ * onSend={(value) => Promise.resolve()}
108
+ * placeholder="请输入Markdown文本..."
109
+ * triggerSendKey="Mod+Enter"
110
+ * />
111
+ */
21
112
export const MarkdownInputField : React . FC < MarkdownInputFieldProps > = (
22
113
props ,
23
114
) => {
@@ -60,7 +151,20 @@ export const MarkdownInputField: React.FC<MarkdownInputFieldProps> = (
60
151
onMouseEnter = { ( ) => setHover ( true ) }
61
152
onMouseLeave = { ( ) => setHover ( false ) }
62
153
onKeyDown = { ( e ) => {
63
- if ( e . key === 'Enter' && ( e . ctrlKey || e . metaKey ) ) {
154
+ const { triggerSendKey = 'Enter' } = props ;
155
+ if ( triggerSendKey === 'Enter' && e . key === 'Enter' ) {
156
+ e . stopPropagation ( ) ;
157
+ e . preventDefault ( ) ;
158
+ if ( props . onSend ) {
159
+ sendMessage ( ) ;
160
+ }
161
+ return ;
162
+ }
163
+ if (
164
+ triggerSendKey === 'Mod+Enter' &&
165
+ ( e . ctrlKey || e . metaKey ) &&
166
+ e . key === 'Enter'
167
+ ) {
64
168
e . stopPropagation ( ) ;
65
169
e . preventDefault ( ) ;
66
170
if ( props . onSend ) {
0 commit comments