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(() => 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 ( 取消 , , ]} className={styles.insertModal} >
); }