Repository navigation
Expand file tree
/
Copy pathsheet-quick-edit.ts
More file actions
94 lines (88 loc) · 3.59 KB
/
Copy pathsheet-quick-edit.ts
File metadata and controls
94 lines (88 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { useCallback, useEffect, useRef } from "react";
import { flushSync } from "react-dom";
import type { SpreadsheetEditorHandle } from "@smartdoca/sheet";
import type { DocumentMode } from "@web/features/documents/document-mode.js";
/** No quick edit, one being opened, or one open. */
export type QuickEdit = "reading" | "starting" | "editing";
/** What the editor reports about cell editing means for a quick edit opened from view mode. */
export function quickEditAfter(
state: QuickEdit,
event: "start" | "end" | "abandoned",
): QuickEdit {
if (state === "starting") return event === "start" ? "editing" : "reading";
if (state === "editing") return event === "end" ? "reading" : state;
return state;
}
/** Whether the document still sits in the mode a quick edit put it in, so its end may return it to reading. */
export const quickEditReturns = (
opened: DocumentMode,
current: DocumentMode | undefined,
): boolean => current === opened;
/** The engine's own operation for opening the selected cell, which its double click runs. */
const SHOW_CELL_EDITOR = "sheet.operation.set-cell-edit-visible";
/** The engine names its input events; this one is a double click. */
const DOUBLE_CLICK = 3;
/**
* Opens the selected cell for editing in the engine, as a double click on that cell would.
* The answer is false when nothing could be opened.
*/
export async function startCellEditing(
handle: SpreadsheetEditorHandle,
): Promise<boolean> {
const api = handle.getRuntime()?.univerAPI;
const unitId = api
?.getActiveWorkbook()
?.getWorkbook?.()
?.getUnitId();
if (!api || !unitId) return false;
return api.executeCommand<object, boolean>(SHOW_CELL_EDITOR, {
visible: true,
eventType: DOUBLE_CLICK,
unitId,
});
}
/**
* Editing one cell from view mode: the document leaves reading for the cell's editing and goes back to
* reading when that edit ends, committed or cancelled. Returns what to call for a double tap on a cell.
* A reader whose mode is chosen by hand keeps it: only an edit this opened is closed here.
*/
export function useSheetQuickEdit(
handle: SpreadsheetEditorHandle | null,
mode: DocumentMode | undefined,
setMode: ((mode: DocumentMode) => void) | undefined,
): () => void {
const state = useRef<QuickEdit>("reading");
const openedWith = useRef<DocumentMode>("edit");
const live = useRef({ mode, setMode });
live.current = { mode, setMode };
const advance = useCallback((event: "start" | "end" | "abandoned") => {
const before = state.current;
state.current = quickEditAfter(before, event);
// Only an edit this hook opened returns the document to reading, and only while the document
// still sits in the mode the quick edit chose: a mode the reader picked by hand keeps standing.
if (
before !== "reading" &&
state.current === "reading" &&
quickEditReturns(openedWith.current, live.current.mode)
)
live.current.setMode?.("read");
}, []);
useEffect(() => {
state.current = "reading";
if (!handle) return;
return handle.onCellEditChange((event) => advance(event.phase));
}, [handle, advance]);
return useCallback(() => {
if (!handle || !live.current.setMode || state.current !== "reading") return;
state.current = "starting";
openedWith.current = "edit";
// The engine refuses to open a cell while the workbook is read only, so the document is made editable first.
flushSync(() => live.current.setMode?.("edit"));
startCellEditing(handle).then(
(opened) => {
if (!opened) advance("abandoned");
},
() => advance("abandoned"),
);
}, [handle, advance]);
}