Files
mnote/wolai-frontend/src/components/online-table/FullScreenTableEditor.tsx
T

600 lines
20 KiB
TypeScript
Raw Normal View History

"use client";
2025-11-23 20:04:29 +08:00
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Loader2, Table, X, Zap } from "lucide-react";
import type { DocumentTable, TableRowData } from "@/types/online-table";
import {
DEFAULT_TABLE_COLUMNS,
DEFAULT_TABLE_ROWS,
DEFAULT_TABLE_SCHEMA,
createDefaultTableSnapshot,
saveOnlineTable,
} from "@/lib/online-table";
import { useDebouncedCallback } from "@/hooks/use-debounced-callback";
2025-11-29 05:16:23 +08:00
import { useLuckysheetLoader } from "@/components/online-table/useLuckysheetLoader";
import { extractRowsForPreview } from "@/components/online-table/utils";
interface FullScreenTableEditorProps {
tableId: string;
onClose: () => void;
}
// Luckysheet 容器的 ID
const LUCKY_SHEET_CONTAINER_ID = "luckysheet-editor-container";
2025-11-29 05:16:23 +08:00
type LuckysheetSelection =
| {
row?: [number, number];
column?: [number, number];
row_focus?: number;
column_focus?: number;
}
| undefined;
const ENABLE_SINGLE_CLICK_EDIT = true;
const isPrintableKey = (event: KeyboardEvent) => {
if (event.defaultPrevented) return false;
if (event.metaKey || event.ctrlKey || event.altKey) return false;
if (event.key === "Enter" || event.key === "Tab" || event.key === "Escape") return false;
if (event.key.length === 1) return true;
return event.key === "Process" || event.key === "Unidentified";
};
2025-11-29 09:35:36 +08:00
const isInlineEditorVisible = () => {
const inputBox = document.getElementById("luckysheet-input-box");
if (!inputBox) {
return false;
}
const style = window.getComputedStyle(inputBox);
return style.top !== "-10000px" && style.display !== "none";
};
2025-11-29 05:16:23 +08:00
const isElementInsideEditorToolbar = (element: HTMLElement | null) => {
if (!element) return false;
if (element.closest(".luckysheet-wa-editor")) {
return true;
2025-11-23 20:04:29 +08:00
}
2025-11-29 05:16:23 +08:00
if (element.closest(".luckysheet-modal-dialog")) {
return true;
}
return false;
2025-11-23 20:04:29 +08:00
};
const FullScreenTableEditor: React.FC<FullScreenTableEditorProps> = ({ tableId, onClose }) => {
const containerRef = useRef<HTMLDivElement>(null);
2025-11-23 20:04:29 +08:00
const isApplyingSnapshotRef = useRef(false);
const hasInitializedRef = useRef(false);
const lastTableIdRef = useRef<string | null>(null);
2025-11-29 05:16:23 +08:00
const isLuckysheetReady = useLuckysheetLoader();
const [tableData, setTableData] = useState<DocumentTable | null>(null);
const [isTableLoading, setIsTableLoading] = useState(true);
const [tableError, setTableError] = useState<string | null>(null);
2025-11-23 20:04:29 +08:00
const [isSaving, setIsSaving] = useState(false);
const [hasPendingChanges, setHasPendingChanges] = useState(false);
const [saveError, setSaveError] = useState<string | null>(null);
const [lastSyncedAt, setLastSyncedAt] = useState<number | null>(null);
const persistSnapshotRef = useRef<((reason: "auto" | "close") => Promise<void>) | null>(null);
2025-11-29 05:16:23 +08:00
const lastPointerDownInGridRef = useRef(false);
2025-11-29 10:46:40 +08:00
const savingHintTimerRef = useRef<number | null>(null);
const [showSavingHint, setShowSavingHint] = useState(false);
const startSavingHint = useCallback(() => {
if (savingHintTimerRef.current !== null) return;
savingHintTimerRef.current = window.setTimeout(() => {
setShowSavingHint(true);
}, 700);
}, []);
const stopSavingHint = useCallback(() => {
if (savingHintTimerRef.current !== null) {
clearTimeout(savingHintTimerRef.current);
savingHintTimerRef.current = null;
}
setShowSavingHint(false);
}, []);
2025-11-29 05:16:23 +08:00
useEffect(() => {
if (typeof window !== "undefined") {
(window as unknown as { __wolaiFullScreenState?: { isLoaded: boolean; tableReady: boolean } }).__wolaiFullScreenState = {
isLoaded: isLuckysheetReady,
tableReady: !!tableData,
};
}
}, [isLuckysheetReady, tableData]);
2025-11-23 20:04:29 +08:00
const fetchTable = useCallback((id: string) => {
setIsTableLoading(true);
setTableError(null);
setTableData(null);
fetch(`/api/tables/${id}`)
.then((response) => {
if (!response.ok) {
throw new Error(`Failed to load table ${id}`);
}
return response.json() as Promise<DocumentTable>;
})
.then((data) => {
setTableData(data);
})
.catch((error) => {
console.error(error);
setTableError("无法加载表格数据,请稍后重试。");
setTableData(null);
})
.finally(() => setIsTableLoading(false));
2025-11-23 20:04:29 +08:00
}, []);
useEffect(() => {
fetchTable(tableId);
2025-11-23 20:04:29 +08:00
hasInitializedRef.current = false;
lastTableIdRef.current = tableId;
}, [fetchTable, tableId]);
useEffect(() => {
2025-11-29 05:16:23 +08:00
if (tableData) {
setIsTableLoading(false);
}
2025-11-29 05:16:23 +08:00
}, [tableData]);
2025-11-23 20:04:29 +08:00
const luckysheetSheets = useMemo(() => {
if (tableData?.snapshot?.luckysheet && Array.isArray(tableData.snapshot.luckysheet) && tableData.snapshot.luckysheet.length > 0) {
return tableData.snapshot.luckysheet;
}
const snapshot = createDefaultTableSnapshot(tableData?.schema ?? DEFAULT_TABLE_SCHEMA);
return snapshot.luckysheet ?? [];
2025-11-23 20:04:29 +08:00
}, [tableData]);
2025-11-29 05:16:23 +08:00
const normalizedSheets = useMemo(() => {
return luckysheetSheets.map((sheet) => ({
...sheet,
celldata: Array.isArray(sheet.celldata) ? sheet.celldata : [],
config: {
...(sheet.config ?? {}),
rowlen: { ...(sheet.config?.rowlen ?? {}) },
columnlen: { ...(sheet.config?.columnlen ?? {}) },
merge: { ...(sheet.config?.merge ?? {}) },
},
}));
}, [luckysheetSheets]);
2025-11-23 20:04:29 +08:00
const persistSnapshot = useCallback(async (reason: "auto" | "close") => {
if (!tableData || !window.luckysheet || typeof window.luckysheet.getluckysheetfile !== "function") {
return;
}
2025-11-29 10:46:40 +08:00
startSavingHint();
2025-11-23 20:04:29 +08:00
setIsSaving(true);
setSaveError(null);
try {
const luckysheetData = window.luckysheet.getluckysheetfile?.() ?? luckysheetSheets;
const rows = extractRowsForPreview(
luckysheetData,
(tableData.schema?.columns ?? []).map((item) => ({ id: item.id })),
).filter((row) => row && typeof row === "object" && Object.keys(row).length > 0);
const snapshot = {
...(tableData.snapshot ?? {}),
rows,
luckysheet: Array.isArray(luckysheetData) ? luckysheetData : luckysheetSheets,
};
2025-11-29 05:16:23 +08:00
await saveOnlineTable(tableId, {
2025-11-23 20:04:29 +08:00
snapshot,
rows,
schema: tableData.schema,
});
setHasPendingChanges(false);
setLastSyncedAt(Date.now());
window.dispatchEvent(new CustomEvent("online-table-saved", { detail: { tableId } }));
} catch (error) {
console.error("保存 Luckysheet 数据失败", error);
setSaveError(reason === "close" ? "关闭前保存失败,请重试" : "自动保存失败");
} finally {
2025-11-29 10:46:40 +08:00
stopSavingHint();
2025-11-23 20:04:29 +08:00
setIsSaving(false);
}
2025-11-29 10:46:40 +08:00
}, [startSavingHint, stopSavingHint, tableData, tableId]);
2025-11-23 20:04:29 +08:00
const debouncedPersist = useDebouncedCallback(() => {
void persistSnapshot("auto");
}, 1200);
useEffect(() => {
persistSnapshotRef.current = persistSnapshot;
}, [persistSnapshot]);
useEffect(() => {
return () => {
debouncedPersist.cancel();
void persistSnapshotRef.current?.("close");
2025-11-29 10:46:40 +08:00
stopSavingHint();
2025-11-23 20:04:29 +08:00
};
2025-11-29 10:46:40 +08:00
}, [debouncedPersist, stopSavingHint]);
2025-11-29 05:16:23 +08:00
useEffect(() => {
if (!isLuckysheetReady) {
lastPointerDownInGridRef.current = false;
return;
}
const container = document.getElementById(LUCKY_SHEET_CONTAINER_ID);
if (!container) {
return;
}
const handlePointerDownInside = () => {
lastPointerDownInGridRef.current = true;
};
const handlePointerDownDocument = (event: PointerEvent) => {
if (!(event.target instanceof Node)) {
return;
}
if (!container.contains(event.target)) {
lastPointerDownInGridRef.current = false;
}
};
container.addEventListener("pointerdown", handlePointerDownInside);
document.addEventListener("pointerdown", handlePointerDownDocument);
return () => {
container.removeEventListener("pointerdown", handlePointerDownInside);
document.removeEventListener("pointerdown", handlePointerDownDocument);
};
}, [isLuckysheetReady]);
const focusLuckysheetEditor = useCallback(() => {
2025-11-29 09:35:36 +08:00
const applyFocus = () => {
2025-11-29 05:16:23 +08:00
const editor = document.getElementById("luckysheet-rich-text-editor");
if (editor && typeof editor.focus === "function") {
editor.focus();
2025-11-29 09:35:36 +08:00
const selection = window.getSelection();
if (selection && editor.childNodes.length > 0) {
const range = document.createRange();
range.selectNodeContents(editor);
range.collapse(false);
selection.removeAllRanges();
selection.addRange(range);
}
2025-11-29 05:16:23 +08:00
}
2025-11-29 09:35:36 +08:00
};
requestAnimationFrame(() => {
applyFocus();
setTimeout(applyFocus, 0);
2025-11-29 05:16:23 +08:00
});
}, []);
const shouldAutoFocusEditor = useCallback(
(target: EventTarget | null) => {
if (!isLuckysheetReady || !tableData) {
return false;
}
const container = document.getElementById(LUCKY_SHEET_CONTAINER_ID);
if (!container) {
return false;
}
const editor = document.getElementById("luckysheet-rich-text-editor");
if (!editor || document.activeElement === editor) {
return false;
}
const targetElement = target instanceof HTMLElement ? target : null;
if (isElementInsideEditorToolbar(targetElement)) {
return false;
}
const activeElement = document.activeElement as HTMLElement | null;
if (isElementInsideEditorToolbar(activeElement)) {
return false;
}
if (targetElement && container.contains(targetElement)) {
return true;
}
if (activeElement && container.contains(activeElement)) {
return true;
}
return lastPointerDownInGridRef.current;
},
[isLuckysheetReady, tableData],
);
const ensureInlineEditor = useCallback(
(target: EventTarget | null) => {
if (!shouldAutoFocusEditor(target)) {
return false;
}
if (isApplyingSnapshotRef.current) {
return false;
}
if (!window.luckysheet || typeof window.luckysheet.enterEditMode !== "function") {
return false;
}
window.luckysheet.enterEditMode();
focusLuckysheetEditor();
return true;
},
[focusLuckysheetEditor, shouldAutoFocusEditor],
);
useEffect(() => {
if (!isLuckysheetReady) {
return;
}
const handleKeydown = (event: KeyboardEvent) => {
if (!isPrintableKey(event)) {
return;
}
ensureInlineEditor(event.target);
};
const handleCompositionStart = (event: CompositionEvent) => {
ensureInlineEditor(event.target);
};
window.addEventListener("keydown", handleKeydown, true);
window.addEventListener("compositionstart", handleCompositionStart, true);
return () => {
window.removeEventListener("keydown", handleKeydown, true);
window.removeEventListener("compositionstart", handleCompositionStart, true);
};
}, [ensureInlineEditor, isLuckysheetReady]);
const isSingleCellSelection = useCallback((range: LuckysheetSelection[] | undefined) => {
if (!Array.isArray(range) || range.length !== 1) {
return false;
}
const target = range[0];
if (!target) {
return false;
}
const rowRange = target.row ?? (typeof target.row_focus === "number" ? [target.row_focus, target.row_focus] : undefined);
const columnRange = target.column ?? (typeof target.column_focus === "number" ? [target.column_focus, target.column_focus] : undefined);
if (!rowRange || !columnRange) {
return false;
}
return rowRange[0] === rowRange[1] && columnRange[0] === columnRange[1];
}, []);
const tryEnterSingleClickEdit = useCallback(
(range: LuckysheetSelection[] | undefined) => {
if (!ENABLE_SINGLE_CLICK_EDIT) {
return;
}
if (isApplyingSnapshotRef.current) {
return;
}
if (typeof window === "undefined") {
return;
}
const luckysheetInstance = window.luckysheet;
if (!luckysheetInstance || typeof luckysheetInstance.enterEditMode !== "function") {
return;
}
if (!isSingleCellSelection(range)) {
return;
}
2025-11-29 09:35:36 +08:00
setTimeout(() => {
const editor = document.getElementById("luckysheet-rich-text-editor");
if (editor && document.activeElement === editor && isInlineEditorVisible()) {
return;
}
luckysheetInstance.enterEditMode();
focusLuckysheetEditor();
}, 0);
2025-11-29 05:16:23 +08:00
},
[focusLuckysheetEditor, isSingleCellSelection],
);
2025-11-29 09:35:36 +08:00
useEffect(() => {
if (!isLuckysheetReady) {
return;
}
const container = document.getElementById(LUCKY_SHEET_CONTAINER_ID);
if (!container) {
return;
}
const handlePointerUp = (event: PointerEvent | MouseEvent | TouchEvent) => {
const target = event.target instanceof Node ? event.target : null;
if (target && !container.contains(target)) {
return;
}
2025-11-29 10:46:40 +08:00
containerRef.current?.focus();
2025-11-29 09:35:36 +08:00
requestAnimationFrame(() => {
const selection = window.luckysheet?.getluckysheet_select_save?.();
if (!selection) {
return;
}
const normalized = Array.isArray(selection)
? (selection as LuckysheetSelection[])
: [selection as LuckysheetSelection];
tryEnterSingleClickEdit(normalized);
});
};
const events: Array<keyof DocumentEventMap> = ["pointerup", "mouseup", "touchend"];
events.forEach((eventName) => {
container.addEventListener(eventName, handlePointerUp, true);
});
return () => {
events.forEach((eventName) => {
container.removeEventListener(eventName, handlePointerUp, true);
});
};
}, [isLuckysheetReady, tryEnterSingleClickEdit]);
// Luckysheet 初始化和清理
useEffect(() => {
2025-11-29 05:16:23 +08:00
if (!isLuckysheetReady || !tableData || !containerRef.current || !window.luckysheet) {
return;
}
2025-11-23 20:04:29 +08:00
if (hasInitializedRef.current && lastTableIdRef.current === tableId) {
return;
}
hasInitializedRef.current = true;
isApplyingSnapshotRef.current = true;
if (containerRef.current.children.length > 0) {
window.luckysheet.destroy(LUCKY_SHEET_CONTAINER_ID);
containerRef.current.innerHTML = "";
}
2025-11-29 05:16:23 +08:00
const gridKey = tableData?.grid_key ?? tableId;
const loadUrl = gridKey ? `/api/luckysheet/load?gridKey=${gridKey}` : "";
const options = {
container: LUCKY_SHEET_CONTAINER_ID,
title: tableData.title ?? tableId,
lang: "zh",
showinfobar: false,
showtoolbar: true,
showsheetbar: true,
showstatisticBar: true,
allowEdit: true,
row: DEFAULT_TABLE_ROWS,
column: DEFAULT_TABLE_COLUMNS,
2025-11-29 05:16:23 +08:00
data: normalizedSheets,
allowUpdate: false,
gridKey,
loadUrl,
2025-11-29 09:35:36 +08:00
pointEdit: true,
pointEditZoom: typeof window !== "undefined" && window.devicePixelRatio ? window.devicePixelRatio : 1,
2025-11-29 05:16:23 +08:00
uploadImage: async (file: File) => {
const formData = new FormData();
formData.append("image", file);
const response = await fetch("/api/luckysheet/upload-image", {
method: "POST",
body: formData,
});
const payload = await response.json().catch(() => null);
if (!response.ok || !payload?.url) {
throw new Error(payload?.msg ?? "上传图片失败");
}
return payload.url as string;
},
imageUrlHandle: (url: string) => url,
2025-11-23 20:04:29 +08:00
hook: {
2025-11-29 05:16:23 +08:00
workbookCreateAfter: () => {
setIsTableLoading(false);
},
2025-11-23 20:04:29 +08:00
updated: () => {
if (isApplyingSnapshotRef.current) return;
setHasPendingChanges(true);
debouncedPersist();
},
2025-11-29 05:16:23 +08:00
cellEditBefore: () => {
focusLuckysheetEditor();
},
rangeSelect: (_sheet: unknown, selectedRange: LuckysheetSelection[] | LuckysheetSelection | undefined) => {
const normalizedRange = Array.isArray(selectedRange)
? (selectedRange as LuckysheetSelection[])
: selectedRange
? [selectedRange]
: undefined;
focusLuckysheetEditor();
tryEnterSingleClickEdit(normalizedRange);
},
rangeMoveAfter: (_oldRange: LuckysheetSelection[] | undefined, newRange: LuckysheetSelection[] | undefined) => {
focusLuckysheetEditor();
tryEnterSingleClickEdit(newRange);
},
2025-11-23 20:04:29 +08:00
},
};
2025-11-29 05:16:23 +08:00
if (process.env.NODE_ENV !== "production") {
// eslint-disable-next-line no-console
console.log("[FullScreenTableEditor] init luckysheet", { tableId, sheets: options.data });
}
try {
window.luckysheet.create(options);
} catch (error) {
console.error("Luckysheet 初始化失败", error);
setTableError("Luckysheet 初始化失败,请重试");
isApplyingSnapshotRef.current = false;
return;
}
2025-11-23 20:04:29 +08:00
// 等待首帧渲染完成再开放 updated 事件
setTimeout(() => {
isApplyingSnapshotRef.current = false;
}, 0);
return () => {
if (window.luckysheet) {
window.luckysheet.destroy(LUCKY_SHEET_CONTAINER_ID);
}
2025-11-29 05:16:23 +08:00
if (containerRef.current) {
containerRef.current.innerHTML = "";
}
hasInitializedRef.current = false;
};
2025-11-29 05:16:23 +08:00
}, [debouncedPersist, normalizedSheets, isLuckysheetReady, tableData, tableId, focusLuckysheetEditor, tryEnterSingleClickEdit]);
2025-11-23 20:04:29 +08:00
const handleClose = async () => {
await persistSnapshot("close");
onClose();
};
2025-11-29 05:16:23 +08:00
const showLoadingOverlay = !isLuckysheetReady || isTableLoading;
const loadingMessage = !isLuckysheetReady ? "正在加载 Luckysheet 资源..." : "正在加载表格数据...";
2025-11-23 20:04:29 +08:00
const statusText = saveError
? saveError
2025-11-29 10:46:40 +08:00
: isSaving && showSavingHint
2025-11-23 20:04:29 +08:00
? "同步中..."
: hasPendingChanges
? "有未保存变更"
: lastSyncedAt
? `已同步 ${new Date(lastSyncedAt).toLocaleTimeString()}`
: "准备就绪";
return (
<div className="fixed inset-0 z-50 bg-white dark:bg-gray-900 flex flex-col">
{/* 顶部工具栏 */}
<header className="flex justify-between items-center p-3 border-b border-gray-200 shadow-sm">
<div className="flex items-center space-x-3">
<Table className="h-5 w-5 text-blue-500" />
<h1 className="text-lg font-bold">
2025-11-23 20:04:29 +08:00
在线表格编辑 - {(tableData?.title ?? tableId).slice(0, 24)}
</h1>
<Zap className="h-4 w-4 text-yellow-500" />
<span className="text-xs text-yellow-600 font-medium" title="协同模式">协同模式 (单用户模式)</span>
</div>
2025-11-23 20:04:29 +08:00
<div className="flex items-center space-x-3">
<span className="text-xs text-gray-500">{statusText}</span>
<button
onClick={handleClose}
className="p-2 rounded-full hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors"
title="退出并保存"
>
{isSaving ? <Loader2 className="h-5 w-5 animate-spin" /> : <X className="h-5 w-5" />}
</button>
</div>
</header>
{/* Luckysheet 容器 */}
<div
id={LUCKY_SHEET_CONTAINER_ID}
ref={containerRef}
className="flex-grow w-full h-full"
2025-11-29 10:46:40 +08:00
tabIndex={-1}
aria-label="在线表格全屏编辑区域"
2025-11-29 05:16:23 +08:00
style={{ display: isLuckysheetReady && !isTableLoading && !tableError ? "block" : "none" }}
2025-11-23 20:04:29 +08:00
/>
{showLoadingOverlay && (
<div className="flex justify-center items-center h-full text-gray-500 border border-dashed">
2025-11-23 20:04:29 +08:00
<p>{loadingMessage}</p>
</div>
)}
{tableError && !showLoadingOverlay && (
<div className="flex flex-col justify-center items-center h-full text-red-500 border border-dashed space-y-2">
<p>{tableError}</p>
<button
type="button"
className="px-3 py-1 rounded-md border border-red-300 text-sm"
onClick={() => fetchTable(tableId)}
>
重试
</button>
</div>
)}
</div>
);
};
export default FullScreenTableEditor;