feat(spreadsheet): basic read-only support

This commit is contained in:
Elian Doran
2026-03-08 21:09:11 +02:00
parent 4960c49cb2
commit 90796fc4fa
2 changed files with 42 additions and 3 deletions

View File

@@ -189,7 +189,7 @@ function SwitchSplitOrientationButton({ note, isReadOnly, isDefaultViewMode }: N
export function ToggleReadOnlyButton({ note, isDefaultViewMode }: NoteActionsCustomInnerProps) {
const [ isReadOnly, setReadOnly ] = useNoteLabelBoolean(note, "readOnly");
const isSavedSqlite = note.isTriliumSqlite() && !note.isHiddenCompletely();
const isEnabled = ([ "mermaid", "mindMap", "canvas" ].includes(note.type) || isSavedSqlite)
const isEnabled = ([ "mermaid", "mindMap", "canvas", "spreadsheet" ].includes(note.type) || isSavedSqlite)
&& note.isContentAvailable() && isDefaultViewMode;
return isEnabled && <NoteAction

View File

@@ -6,11 +6,12 @@ import sheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US';
import { UniverSheetsNotePreset } from '@univerjs/preset-sheets-note';
import sheetsNoteEnUS from '@univerjs/preset-sheets-note/locales/en-US';
import { CommandType, createUniver, FUniver, IDisposable, IWorkbookData, LocaleType, mergeLocales } from '@univerjs/presets';
import { MutableRef, useEffect, useRef } from "preact/hooks";
import { note } from "mermaid/dist/rendering-util/rendering-elements/shapes/note.js";
import { MutableRef, useCallback, useEffect, useRef } from "preact/hooks";
import NoteContext from "../../components/note_context";
import FNote from "../../entities/fnote";
import { SavedData, useColorScheme, useEditorSpacedUpdate, useTriliumEvent } from "../react/hooks";
import { SavedData, useColorScheme, useEditorSpacedUpdate, useIsNoteReadOnly, useNoteLabel, useNoteLabelBoolean, useTriliumEvent } from "../react/hooks";
import { TypeWidgetProps } from "./type_widget";
interface PersistedData {
@@ -25,6 +26,7 @@ export default function Spreadsheet({ note, noteContext }: TypeWidgetProps) {
useInitializeSpreadsheet(containerRef, apiRef);
useDarkMode(apiRef);
usePersistence(note, noteContext, apiRef, containerRef);
useReadOnly(note, apiRef);
// Focus the spreadsheet when the note is focused.
useTriliumEvent("focusOnDetail", () => {
@@ -152,3 +154,40 @@ function usePersistence(note: FNote, noteContext: NoteContext | null | undefined
};
}, []);
}
function useReadOnly(note: FNote, apiRef: MutableRef<FUniver | undefined>) {
const [ readOnly ] = useNoteLabelBoolean(note, "readOnly");
useEffect(() => {
const univerAPI = apiRef.current;
if (!univerAPI) return;
function apply() {
const workbook = univerAPI?.getActiveWorkbook();
if (!workbook) return;
const permission = workbook.getPermission();
if (readOnly) {
workbook.disableSelection();
} else {
workbook.enableSelection();
}
permission.setWorkbookEditPermission(workbook.getId(), !readOnly);
permission.setPermissionDialogVisible(false);
}
// Try immediately (covers post-init changes).
apply();
// Also listen for lifecycle in case Univer isn't ready yet.
const disposable = univerAPI.addEvent(
univerAPI.Event.LifeCycleChanged,
({ stage }) => {
if (stage === univerAPI.Enum.LifecycleStages.Rendered) {
apply();
}
}
);
return () => disposable.dispose();
}, [ readOnly, apiRef ]);
}