|
|
@@ -13,6 +13,60 @@ import { richTextToHtml, htmlToRichText } from '../../../utils/richTextConverter
|
|
13
|
13
|
import { RichTextToolbar } from './RichTextToolbar';
|
|
14
|
14
|
import './RichTextEditor.css';
|
|
15
|
15
|
|
|
|
16
|
+export function getEditorTextWithLineBreaks(node: Node): string {
|
|
|
17
|
+ if (node.nodeType === 3) {
|
|
|
18
|
+ return node.textContent || '';
|
|
|
19
|
+ }
|
|
|
20
|
+
|
|
|
21
|
+ if (node.nodeType !== 1 && node.nodeType !== 11) {
|
|
|
22
|
+ return '';
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ const element = node as HTMLElement;
|
|
|
26
|
+ if (element.tagName === 'BR') {
|
|
|
27
|
+ return '\n';
|
|
|
28
|
+ }
|
|
|
29
|
+
|
|
|
30
|
+ const text = Array.from(node.childNodes).map(getEditorTextWithLineBreaks).join('');
|
|
|
31
|
+ const blockTags = new Set(['DIV', 'P', 'LI']);
|
|
|
32
|
+ return element.tagName && blockTags.has(element.tagName) ? `${text}\n` : text;
|
|
|
33
|
+}
|
|
|
34
|
+
|
|
|
35
|
+export function getNextOrderedListNumber(text: string): number {
|
|
|
36
|
+ const itemNumbers = Array.from(text.matchAll(/(?:^|\n)\s*(\d+)\.(?=\s|$)/g))
|
|
|
37
|
+ .map((match) => Number(match[1]))
|
|
|
38
|
+ .filter(Number.isFinite);
|
|
|
39
|
+
|
|
|
40
|
+ return itemNumbers.length > 0 ? Math.max(...itemNumbers) + 1 : 1;
|
|
|
41
|
+}
|
|
|
42
|
+
|
|
|
43
|
+export function normalizeOrderedListNumbers(root: HTMLElement): boolean {
|
|
|
44
|
+ const walker = root.ownerDocument.createTreeWalker(root, 4);
|
|
|
45
|
+ let itemNumber = 0;
|
|
|
46
|
+ let changed = false;
|
|
|
47
|
+ let textNode = walker.nextNode();
|
|
|
48
|
+
|
|
|
49
|
+ while (textNode) {
|
|
|
50
|
+ const originalText = textNode.textContent || '';
|
|
|
51
|
+ const normalizedText = originalText.replace(
|
|
|
52
|
+ /(^|\n)(\s*)\d+\.(?=\s|$)/g,
|
|
|
53
|
+ (_match, lineStart: string, indent: string) => {
|
|
|
54
|
+ itemNumber += 1;
|
|
|
55
|
+ return `${lineStart}${indent}${itemNumber}.`;
|
|
|
56
|
+ }
|
|
|
57
|
+ );
|
|
|
58
|
+
|
|
|
59
|
+ if (normalizedText !== originalText) {
|
|
|
60
|
+ textNode.textContent = normalizedText;
|
|
|
61
|
+ changed = true;
|
|
|
62
|
+ }
|
|
|
63
|
+
|
|
|
64
|
+ textNode = walker.nextNode();
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ return changed;
|
|
|
68
|
+}
|
|
|
69
|
+
|
|
16
|
70
|
// ══════════════════════════════════════════════════════════════════════════════
|
|
17
|
71
|
// Component Props
|
|
18
|
72
|
// ══════════════════════════════════════════════════════════════════════════════
|
|
|
@@ -34,6 +88,10 @@ export interface RichTextEditorProps {
|
|
34
|
88
|
autoFocus?: boolean;
|
|
35
|
89
|
/** 失焦回调 */
|
|
36
|
90
|
onBlur?: () => void;
|
|
|
91
|
+ /** 当前 block 内容格式 */
|
|
|
92
|
+ currentContentFormat?: 'paragraph' | 'ordered-list' | `heading-${1 | 2 | 3 | 4 | 5 | 6}`;
|
|
|
93
|
+ /** block 内容格式变更回调 */
|
|
|
94
|
+ onContentFormatChange?: (format: 'paragraph' | 'ordered-list' | `heading-${1 | 2 | 3 | 4 | 5 | 6}`) => void;
|
|
37
|
95
|
/** 对齐方式变更回调(用于段落块) */
|
|
38
|
96
|
onAlignChange?: (align: 'left' | 'center' | 'right' | 'justify') => void;
|
|
39
|
97
|
/** 当前对齐方式(用于初始化工具栏) */
|
|
|
@@ -83,6 +141,8 @@ export const RichTextEditor: React.FC<RichTextEditorProps> = ({
|
|
83
|
141
|
baseStyle = {},
|
|
84
|
142
|
autoFocus = false,
|
|
85
|
143
|
onBlur,
|
|
|
144
|
+ currentContentFormat,
|
|
|
145
|
+ onContentFormatChange,
|
|
86
|
146
|
onAlignChange,
|
|
87
|
147
|
currentAlign,
|
|
88
|
148
|
tableContext,
|
|
|
@@ -118,6 +178,15 @@ export const RichTextEditor: React.FC<RichTextEditorProps> = ({
|
|
118
|
178
|
}
|
|
119
|
179
|
}, [value, baseStyle.fontSize, baseStyle.fontFamily]);
|
|
120
|
180
|
|
|
|
181
|
+ // ── 修正已有有序列表编号 ───────────────────────────────────────────────────
|
|
|
182
|
+ useEffect(() => {
|
|
|
183
|
+ if (currentContentFormat !== 'ordered-list' || !editorRef.current || !onChange) return;
|
|
|
184
|
+
|
|
|
185
|
+ if (normalizeOrderedListNumbers(editorRef.current)) {
|
|
|
186
|
+ onChange(htmlToRichText(editorRef.current.innerHTML));
|
|
|
187
|
+ }
|
|
|
188
|
+ }, [currentContentFormat, onChange, value]);
|
|
|
189
|
+
|
|
121
|
190
|
// ── 自动聚焦 ───────────────────────────────────────────────────────────────
|
|
122
|
191
|
useEffect(() => {
|
|
123
|
192
|
if (autoFocus && editorRef.current && !readOnly) {
|
|
|
@@ -132,10 +201,14 @@ export const RichTextEditor: React.FC<RichTextEditorProps> = ({
|
|
132
|
201
|
// 标记正在输入
|
|
133
|
202
|
isFormattingRef.current = true;
|
|
134
|
203
|
|
|
|
204
|
+ if (currentContentFormat === 'ordered-list') {
|
|
|
205
|
+ normalizeOrderedListNumbers(editorRef.current);
|
|
|
206
|
+ }
|
|
|
207
|
+
|
|
135
|
208
|
const html = editorRef.current.innerHTML;
|
|
136
|
209
|
const richText = htmlToRichText(html);
|
|
137
|
210
|
onChange(richText);
|
|
138
|
|
- }, [onChange]);
|
|
|
211
|
+ }, [currentContentFormat, onChange]);
|
|
139
|
212
|
|
|
140
|
213
|
// ── 处理格式变更(工具栏修改) ──────────────────────────────────────────────
|
|
141
|
214
|
const handleFormatChange = useCallback(() => {
|
|
|
@@ -168,6 +241,28 @@ export const RichTextEditor: React.FC<RichTextEditorProps> = ({
|
|
168
|
241
|
|
|
169
|
242
|
// ── 处理键盘快捷键 ─────────────────────────────────────────────────────────
|
|
170
|
243
|
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
|
|
|
244
|
+ if (e.key === 'Enter' && currentContentFormat === 'ordered-list' && !e.nativeEvent.isComposing) {
|
|
|
245
|
+ e.preventDefault();
|
|
|
246
|
+
|
|
|
247
|
+ const selection = window.getSelection();
|
|
|
248
|
+ if (!editorRef.current || !selection || selection.rangeCount === 0) return;
|
|
|
249
|
+
|
|
|
250
|
+ const editorText = getEditorTextWithLineBreaks(editorRef.current);
|
|
|
251
|
+ const nextNumber = getNextOrderedListNumber(editorText);
|
|
|
252
|
+
|
|
|
253
|
+ document.execCommand('insertText', false, `\n${nextNumber}. `);
|
|
|
254
|
+ normalizeOrderedListNumbers(editorRef.current);
|
|
|
255
|
+
|
|
|
256
|
+ const endRange = document.createRange();
|
|
|
257
|
+ endRange.selectNodeContents(editorRef.current);
|
|
|
258
|
+ endRange.collapse(false);
|
|
|
259
|
+ selection.removeAllRanges();
|
|
|
260
|
+ selection.addRange(endRange);
|
|
|
261
|
+
|
|
|
262
|
+ handleInput();
|
|
|
263
|
+ return;
|
|
|
264
|
+ }
|
|
|
265
|
+
|
|
171
|
266
|
// 单行模式禁止换行
|
|
172
|
267
|
if (singleLine && e.key === 'Enter') {
|
|
173
|
268
|
e.preventDefault();
|
|
|
@@ -194,7 +289,7 @@ export const RichTextEditor: React.FC<RichTextEditorProps> = ({
|
|
194
|
289
|
break;
|
|
195
|
290
|
}
|
|
196
|
291
|
}
|
|
197
|
|
- }, [singleLine, handleInput]);
|
|
|
292
|
+ }, [singleLine, currentContentFormat, handleInput]);
|
|
198
|
293
|
|
|
199
|
294
|
// ── 处理选中文本(显示工具栏) ───────────────────────────────────────────────
|
|
200
|
295
|
const handleMouseUp = useCallback(() => {
|
|
|
@@ -298,6 +393,8 @@ export const RichTextEditor: React.FC<RichTextEditorProps> = ({
|
|
298
|
393
|
position={toolbarPosition}
|
|
299
|
394
|
onClose={() => setShowToolbar(false)}
|
|
300
|
395
|
onFormat={handleFormatChange}
|
|
|
396
|
+ currentContentFormat={currentContentFormat}
|
|
|
397
|
+ onContentFormatChange={onContentFormatChange}
|
|
301
|
398
|
onAlignChange={onAlignChange}
|
|
302
|
399
|
currentAlign={currentAlign}
|
|
303
|
400
|
tableContext={tableContext}
|