|
|
@@ -1,323 +0,0 @@
|
|
1
|
|
-/**
|
|
2
|
|
- * TableStylePanel.tsx - 表格样式面板
|
|
3
|
|
- *
|
|
4
|
|
- * 用于设置单元格样式(字体、对齐、颜色等)
|
|
5
|
|
- *
|
|
6
|
|
- * @module components/Editor/blocks
|
|
7
|
|
- */
|
|
8
|
|
-
|
|
9
|
|
-import React, { useCallback, useMemo } from 'react';
|
|
10
|
|
-import { Button, Space, ColorPicker, InputNumber, Select, Divider } from 'antd';
|
|
11
|
|
-import type { Color } from 'antd/es/color-picker';
|
|
12
|
|
-import {
|
|
13
|
|
- BoldOutlined,
|
|
14
|
|
- ItalicOutlined,
|
|
15
|
|
- UnderlineOutlined,
|
|
16
|
|
- AlignLeftOutlined,
|
|
17
|
|
- AlignCenterOutlined,
|
|
18
|
|
- AlignRightOutlined,
|
|
19
|
|
- FontSizeOutlined,
|
|
20
|
|
- FontColorsOutlined,
|
|
21
|
|
- VerticalAlignTopOutlined,
|
|
22
|
|
- VerticalAlignMiddleOutlined,
|
|
23
|
|
- VerticalAlignBottomOutlined,
|
|
24
|
|
-} from '@ant-design/icons';
|
|
25
|
|
-import type { TableBlock, TableCell } from '../../../types/editor';
|
|
26
|
|
-import { useEditorStore } from '../../../stores/editorStore';
|
|
27
|
|
-import {
|
|
28
|
|
- getTableCellsForVisualBounds,
|
|
29
|
|
- getTableVisualCellPositions,
|
|
30
|
|
-} from '../../../utils/blockOperations';
|
|
31
|
|
-import { TableBorderControl } from './TableBorderControl';
|
|
32
|
|
-import './TableStylePanel.css';
|
|
33
|
|
-
|
|
34
|
|
-// ══════════════════════════════════════════════════════════════════════════════
|
|
35
|
|
-// Component Props
|
|
36
|
|
-// ══════════════════════════════════════════════════════════════════════════════
|
|
37
|
|
-
|
|
38
|
|
-export interface TableStylePanelProps {
|
|
39
|
|
- /** 表格块 */
|
|
40
|
|
- block: TableBlock;
|
|
41
|
|
- /** 选中的单元格 */
|
|
42
|
|
- selectedCell: { row: number; col: number };
|
|
43
|
|
- /** 选中的范围 */
|
|
44
|
|
- selectedRange?: {
|
|
45
|
|
- startRow: number;
|
|
46
|
|
- startCol: number;
|
|
47
|
|
- endRow: number;
|
|
48
|
|
- endCol: number;
|
|
49
|
|
- } | null;
|
|
50
|
|
- selectedVisualRange?: {
|
|
51
|
|
- rowStart: number;
|
|
52
|
|
- rowEnd: number;
|
|
53
|
|
- colStart: number;
|
|
54
|
|
- colEnd: number;
|
|
55
|
|
- } | null;
|
|
56
|
|
-}
|
|
57
|
|
-
|
|
58
|
|
-// ══════════════════════════════════════════════════════════════════════════════
|
|
59
|
|
-// Component
|
|
60
|
|
-// ══════════════════════════════════════════════════════════════════════════════
|
|
61
|
|
-
|
|
62
|
|
-/**
|
|
63
|
|
- * TableStylePanel - 表格样式面板
|
|
64
|
|
- *
|
|
65
|
|
- * 提供单元格样式设置功能:
|
|
66
|
|
- * - 字体样式(加粗、斜体、下划线)
|
|
67
|
|
- * - 文本对齐(左、中、右)
|
|
68
|
|
- * - 垂直对齐(上、中、下)
|
|
69
|
|
- * - 字号
|
|
70
|
|
- * - 字体颜色
|
|
71
|
|
- */
|
|
72
|
|
-export const TableStylePanel: React.FC<TableStylePanelProps> = ({
|
|
73
|
|
- block,
|
|
74
|
|
- selectedCell,
|
|
75
|
|
- selectedRange,
|
|
76
|
|
- selectedVisualRange,
|
|
77
|
|
-}) => {
|
|
78
|
|
- const updateBlock = useEditorStore((state) => state.updateBlock);
|
|
79
|
|
- const visualCellPositions = useMemo(
|
|
80
|
|
- () => getTableVisualCellPositions(block),
|
|
81
|
|
- [block],
|
|
82
|
|
- );
|
|
83
|
|
- const selectedPosition = visualCellPositions.get(`${selectedCell.row}-${selectedCell.col}`);
|
|
84
|
|
- const styleRange = selectedVisualRange || selectedPosition;
|
|
85
|
|
- const selectedCells = styleRange
|
|
86
|
|
- ? getTableCellsForVisualBounds(block, styleRange)
|
|
87
|
|
- : [];
|
|
88
|
|
- const getCommonStyleValue = <Key extends keyof TableCell['style']>(key: Key) => {
|
|
89
|
|
- if (selectedCells.length === 0) return undefined;
|
|
90
|
|
- const firstValue = selectedCells[0].style?.[key];
|
|
91
|
|
- return selectedCells.every((cell) => cell.style?.[key] === firstValue) ? firstValue : undefined;
|
|
92
|
|
- };
|
|
93
|
|
- const commonBold = selectedCells.length > 0 && selectedCells.every((cell) => cell.style?.bold === true);
|
|
94
|
|
- const commonItalic = selectedCells.length > 0 && selectedCells.every((cell) => cell.style?.italic === true);
|
|
95
|
|
- const commonUnderline = selectedCells.length > 0 && selectedCells.every((cell) => cell.style?.underline === true);
|
|
96
|
|
- const currentStyle = {
|
|
97
|
|
- bold: commonBold,
|
|
98
|
|
- italic: commonItalic,
|
|
99
|
|
- underline: commonUnderline,
|
|
100
|
|
- align: getCommonStyleValue('align'),
|
|
101
|
|
- valign: getCommonStyleValue('valign'),
|
|
102
|
|
- font_size: getCommonStyleValue('font_size'),
|
|
103
|
|
- font_name: getCommonStyleValue('font_name'),
|
|
104
|
|
- color: getCommonStyleValue('color'),
|
|
105
|
|
- };
|
|
106
|
|
-
|
|
107
|
|
- // 应用样式到选中的单元格或范围
|
|
108
|
|
- const applyStyle = useCallback((styleUpdates: Partial<TableCell['style']>) => {
|
|
109
|
|
- const newRows = block.content.rows.map((row, rowIdx) => {
|
|
110
|
|
- return {
|
|
111
|
|
- ...row,
|
|
112
|
|
- cells: row.cells.map((cell, colIdx) => {
|
|
113
|
|
- const cellPosition = visualCellPositions.get(`${rowIdx}-${colIdx}`);
|
|
114
|
|
- const isInRange = !!styleRange && !!cellPosition
|
|
115
|
|
- && cellPosition.rowStart <= styleRange.rowEnd
|
|
116
|
|
- && cellPosition.rowEnd >= styleRange.rowStart
|
|
117
|
|
- && cellPosition.colStart <= styleRange.colEnd
|
|
118
|
|
- && cellPosition.colEnd >= styleRange.colStart;
|
|
119
|
|
-
|
|
120
|
|
- if (!isInRange) return cell;
|
|
121
|
|
-
|
|
122
|
|
- return {
|
|
123
|
|
- ...cell,
|
|
124
|
|
- style: {
|
|
125
|
|
- ...cell.style,
|
|
126
|
|
- ...styleUpdates,
|
|
127
|
|
- },
|
|
128
|
|
- };
|
|
129
|
|
- }),
|
|
130
|
|
- };
|
|
131
|
|
- });
|
|
132
|
|
-
|
|
133
|
|
- updateBlock(block.id, {
|
|
134
|
|
- content: {
|
|
135
|
|
- ...block.content,
|
|
136
|
|
- rows: newRows,
|
|
137
|
|
- },
|
|
138
|
|
- });
|
|
139
|
|
- }, [block, styleRange, updateBlock, visualCellPositions]);
|
|
140
|
|
-
|
|
141
|
|
- // 字体样式切换
|
|
142
|
|
- const toggleBold = useCallback(() => {
|
|
143
|
|
- applyStyle({ bold: !currentStyle.bold });
|
|
144
|
|
- }, [currentStyle.bold, applyStyle]);
|
|
145
|
|
-
|
|
146
|
|
- const toggleItalic = useCallback(() => {
|
|
147
|
|
- applyStyle({ italic: !currentStyle.italic });
|
|
148
|
|
- }, [currentStyle.italic, applyStyle]);
|
|
149
|
|
-
|
|
150
|
|
- const toggleUnderline = useCallback(() => {
|
|
151
|
|
- applyStyle({ underline: !currentStyle.underline });
|
|
152
|
|
- }, [currentStyle.underline, applyStyle]);
|
|
153
|
|
-
|
|
154
|
|
- // 文本对齐
|
|
155
|
|
- const setTextAlign = useCallback((align: 'left' | 'center' | 'right' | 'justify') => {
|
|
156
|
|
- applyStyle({ align });
|
|
157
|
|
- }, [applyStyle]);
|
|
158
|
|
-
|
|
159
|
|
- // 垂直对齐
|
|
160
|
|
- const setVerticalAlign = useCallback((valign: 'top' | 'middle' | 'bottom') => {
|
|
161
|
|
- applyStyle({ valign });
|
|
162
|
|
- }, [applyStyle]);
|
|
163
|
|
-
|
|
164
|
|
- // 字号
|
|
165
|
|
- const setFontSize = useCallback((font_size: number | null) => {
|
|
166
|
|
- if (font_size && font_size > 0) {
|
|
167
|
|
- applyStyle({ font_size });
|
|
168
|
|
- }
|
|
169
|
|
- }, [applyStyle]);
|
|
170
|
|
-
|
|
171
|
|
- // 字体颜色
|
|
172
|
|
- const setFontColor = useCallback((color: Color) => {
|
|
173
|
|
- const hexColor = color.toHexString().replace(/^#/, '').toUpperCase();
|
|
174
|
|
- applyStyle({ color: hexColor });
|
|
175
|
|
- }, [applyStyle]);
|
|
176
|
|
-
|
|
177
|
|
- // 字体
|
|
178
|
|
- const setFontFamily = useCallback((font_name: string) => {
|
|
179
|
|
- applyStyle({ font_name });
|
|
180
|
|
- }, [applyStyle]);
|
|
181
|
|
-
|
|
182
|
|
- return (
|
|
183
|
|
- <div className="table-style-panel">
|
|
184
|
|
- <Space size="small" wrap>
|
|
185
|
|
- {/* 字体样式 */}
|
|
186
|
|
- <Space.Compact>
|
|
187
|
|
- <Button
|
|
188
|
|
- type={currentStyle.bold ? 'primary' : 'default'}
|
|
189
|
|
- size="small"
|
|
190
|
|
- icon={<BoldOutlined />}
|
|
191
|
|
- onClick={toggleBold}
|
|
192
|
|
- title="加粗"
|
|
193
|
|
- />
|
|
194
|
|
- <Button
|
|
195
|
|
- type={currentStyle.italic ? 'primary' : 'default'}
|
|
196
|
|
- size="small"
|
|
197
|
|
- icon={<ItalicOutlined />}
|
|
198
|
|
- onClick={toggleItalic}
|
|
199
|
|
- title="斜体"
|
|
200
|
|
- />
|
|
201
|
|
- <Button
|
|
202
|
|
- type={currentStyle.underline ? 'primary' : 'default'}
|
|
203
|
|
- size="small"
|
|
204
|
|
- icon={<UnderlineOutlined />}
|
|
205
|
|
- onClick={toggleUnderline}
|
|
206
|
|
- title="下划线"
|
|
207
|
|
- />
|
|
208
|
|
- </Space.Compact>
|
|
209
|
|
-
|
|
210
|
|
- <Divider type="vertical" style={{ margin: 0 }} />
|
|
211
|
|
-
|
|
212
|
|
- {/* 文本对齐 */}
|
|
213
|
|
- <Space.Compact>
|
|
214
|
|
- <Button
|
|
215
|
|
- type={currentStyle.align === 'left' ? 'primary' : 'default'}
|
|
216
|
|
- size="small"
|
|
217
|
|
- icon={<AlignLeftOutlined />}
|
|
218
|
|
- onClick={() => setTextAlign('left')}
|
|
219
|
|
- title="左对齐"
|
|
220
|
|
- />
|
|
221
|
|
- <Button
|
|
222
|
|
- type={currentStyle.align === 'center' || !currentStyle.align ? 'primary' : 'default'}
|
|
223
|
|
- size="small"
|
|
224
|
|
- icon={<AlignCenterOutlined />}
|
|
225
|
|
- onClick={() => setTextAlign('center')}
|
|
226
|
|
- title="居中对齐"
|
|
227
|
|
- />
|
|
228
|
|
- <Button
|
|
229
|
|
- type={currentStyle.align === 'right' ? 'primary' : 'default'}
|
|
230
|
|
- size="small"
|
|
231
|
|
- icon={<AlignRightOutlined />}
|
|
232
|
|
- onClick={() => setTextAlign('right')}
|
|
233
|
|
- title="右对齐"
|
|
234
|
|
- />
|
|
235
|
|
- </Space.Compact>
|
|
236
|
|
-
|
|
237
|
|
- <Divider type="vertical" style={{ margin: 0 }} />
|
|
238
|
|
-
|
|
239
|
|
- {/* 垂直对齐 */}
|
|
240
|
|
- <Space.Compact>
|
|
241
|
|
- <Button
|
|
242
|
|
- type={currentStyle.valign === 'top' ? 'primary' : 'default'}
|
|
243
|
|
- size="small"
|
|
244
|
|
- icon={<VerticalAlignTopOutlined />}
|
|
245
|
|
- onClick={() => setVerticalAlign('top')}
|
|
246
|
|
- title="顶部对齐"
|
|
247
|
|
- />
|
|
248
|
|
- <Button
|
|
249
|
|
- type={currentStyle.valign === 'middle' || !currentStyle.valign ? 'primary' : 'default'}
|
|
250
|
|
- size="small"
|
|
251
|
|
- icon={<VerticalAlignMiddleOutlined />}
|
|
252
|
|
- onClick={() => setVerticalAlign('middle')}
|
|
253
|
|
- title="垂直居中"
|
|
254
|
|
- />
|
|
255
|
|
- <Button
|
|
256
|
|
- type={currentStyle.valign === 'bottom' ? 'primary' : 'default'}
|
|
257
|
|
- size="small"
|
|
258
|
|
- icon={<VerticalAlignBottomOutlined />}
|
|
259
|
|
- onClick={() => setVerticalAlign('bottom')}
|
|
260
|
|
- title="底部对齐"
|
|
261
|
|
- />
|
|
262
|
|
- </Space.Compact>
|
|
263
|
|
-
|
|
264
|
|
- <Divider type="vertical" style={{ margin: 0 }} />
|
|
265
|
|
-
|
|
266
|
|
- {/* 字号 */}
|
|
267
|
|
- <Space.Compact>
|
|
268
|
|
- <Button size="small" icon={<FontSizeOutlined />} disabled title="字号" />
|
|
269
|
|
- <InputNumber
|
|
270
|
|
- size="small"
|
|
271
|
|
- min={8}
|
|
272
|
|
- max={72}
|
|
273
|
|
- value={currentStyle.font_size || 12}
|
|
274
|
|
- onChange={setFontSize}
|
|
275
|
|
- style={{ width: 60 }}
|
|
276
|
|
- />
|
|
277
|
|
- </Space.Compact>
|
|
278
|
|
-
|
|
279
|
|
- {/* 字体 */}
|
|
280
|
|
- <Select
|
|
281
|
|
- size="small"
|
|
282
|
|
- value={currentStyle.font_name || '宋体'}
|
|
283
|
|
- onChange={setFontFamily}
|
|
284
|
|
- style={{ width: 100 }}
|
|
285
|
|
- options={[
|
|
286
|
|
- { label: '宋体', value: '宋体' },
|
|
287
|
|
- { label: '黑体', value: '黑体' },
|
|
288
|
|
- { label: '楷体', value: '楷体' },
|
|
289
|
|
- { label: '仿宋', value: '仿宋' },
|
|
290
|
|
- { label: '微软雅黑', value: '微软雅黑' },
|
|
291
|
|
- { label: 'Arial', value: 'Arial' },
|
|
292
|
|
- { label: 'Times New Roman', value: 'Times New Roman' },
|
|
293
|
|
- ]}
|
|
294
|
|
- />
|
|
295
|
|
-
|
|
296
|
|
- {/* 字体颜色 */}
|
|
297
|
|
- <ColorPicker
|
|
298
|
|
- size="small"
|
|
299
|
|
- value={currentStyle.color ? `#${currentStyle.color}` : '#000000'}
|
|
300
|
|
- onChange={setFontColor}
|
|
301
|
|
- showText={() => (
|
|
302
|
|
- <Space size={4}>
|
|
303
|
|
- <FontColorsOutlined />
|
|
304
|
|
- 颜色
|
|
305
|
|
- </Space>
|
|
306
|
|
- )}
|
|
307
|
|
- />
|
|
308
|
|
-
|
|
309
|
|
- <Divider type="vertical" style={{ margin: 0 }} />
|
|
310
|
|
-
|
|
311
|
|
- {/* 边框控制 */}
|
|
312
|
|
- <TableBorderControl
|
|
313
|
|
- block={block}
|
|
314
|
|
- selectedCell={selectedCell}
|
|
315
|
|
- selectedRange={selectedRange}
|
|
316
|
|
- selectedVisualRange={selectedVisualRange}
|
|
317
|
|
- />
|
|
318
|
|
- </Space>
|
|
319
|
|
- </div>
|
|
320
|
|
- );
|
|
321
|
|
-};
|
|
322
|
|
-
|
|
323
|
|
-export default TableStylePanel;
|