emojiTool/src/renderer/components/TableInsertModal.tsx
hackrobot 826f0bcf22
Some checks are pending
Test / test (macos-latest) (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
Test / test (windows-latest) (push) Waiting to run
feat: three-pane editor with rich preview, tables, images, and scroll sync
Add emoji + rich Markdown dual preview, MarkText-style tables, local/S3 images, settings UI, Electron 44, and update README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-03 02:49:44 +08:00

112 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useEffect, useState } from 'react';
import { Button, InputNumber, Modal } from 'antd';
import {
clampDim,
createEmptyTable,
resizeTable,
serializeMarkdownTable,
type MdTable,
} from '../lib/mdTable';
import TableEditorPanel from './TableEditorPanel';
import styles from './TableModals.module.scss';
type Props = {
open: boolean;
onCancel: () => void;
onConfirm: (markdown: string) => void;
};
const DEFAULT_ROWS = 4;
const DEFAULT_COLS = 3;
export default function TableInsertModal({ open, onCancel, onConfirm }: Props) {
const [rows, setRows] = useState(DEFAULT_ROWS);
const [cols, setCols] = useState(DEFAULT_COLS);
const [table, setTable] = useState<MdTable>(() =>
createEmptyTable(DEFAULT_ROWS, DEFAULT_COLS),
);
useEffect(() => {
if (open) {
setRows(DEFAULT_ROWS);
setCols(DEFAULT_COLS);
setTable(createEmptyTable(DEFAULT_ROWS, DEFAULT_COLS));
}
}, [open]);
const applyRows = (nextRows: number) => {
const clamped = clampDim(nextRows, 2, 20);
setRows(clamped);
setTable((prev) => resizeTable(prev, clamped, prev.header.length));
};
const applyCols = (nextCols: number) => {
const clamped = clampDim(nextCols, 1, 20);
setCols(clamped);
setTable((prev) => resizeTable(prev, prev.rows.length + 1, clamped));
};
const syncDimsFromTable = (next: MdTable) => {
setTable(next);
setRows(next.rows.length + 1);
setCols(next.header.length);
};
const handleOk = () => {
onConfirm(serializeMarkdownTable(table));
};
const colCount = table.header.length;
return (
<Modal
title="插入表格"
open={open}
onCancel={onCancel}
destroyOnClose
centered
width={Math.min(920, 160 + colCount * 140)}
footer={[
<Button key="cancel" onClick={onCancel}>
取消
</Button>,
<Button key="ok" type="primary" onClick={handleOk}>
确定
</Button>,
]}
className={styles.insertModal}
>
<div className={styles.insertBody}>
<div className={styles.dimRow}>
<label className={styles.dimField}>
<span>行</span>
<InputNumber
min={2}
max={20}
value={rows}
onChange={(v) => applyRows(clampDim(Number(v ?? DEFAULT_ROWS), 2, 20))}
controls
/>
</label>
<label className={styles.dimField}>
<span>列</span>
<InputNumber
min={1}
max={20}
value={cols}
onChange={(v) => applyCols(clampDim(Number(v ?? DEFAULT_COLS), 1, 20))}
controls
/>
</label>
</div>
<TableEditorPanel
table={table}
onChange={syncDimsFromTable}
hint={`${rows} 行 × ${cols} 列(含表头)· 在下方直接填写内容后确定 · Ctrl+Shift+T`}
/>
</div>
</Modal>
);
}