From 7c187452f9e68e3762ca1d222f68cb8fbad561ca Mon Sep 17 00:00:00 2001 From: Khairul Hidayat Date: Tue, 12 Mar 2024 07:46:07 +0700 Subject: [PATCH] fix: add readonly mode to editor --- components/ui/code-editor.tsx | 51 ++++++++++++------- .../project/@slug/components/file-listing.tsx | 6 +-- .../project/@slug/components/file-viewer.tsx | 5 ++ pages/project/@slug/context/project.tsx | 2 +- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/components/ui/code-editor.tsx b/components/ui/code-editor.tsx index 69a3e30..66a81fd 100644 --- a/components/ui/code-editor.tsx +++ b/components/ui/code-editor.tsx @@ -18,13 +18,13 @@ type Props = { wordWrap?: boolean; onChange: (val: string) => void; formatOnSave?: boolean; + isReadOnly?: boolean; }; const CodeEditor = (props: Props) => { - const { filename, path, value, formatOnSave, wordWrap, onChange } = props; + const { filename, path, value, formatOnSave, isReadOnly, onChange } = props; const editorRef = useRef(null); const [isMounted, setMounted] = useState(false); - const [data, setData] = useState(value); const [debounceChange, resetDebounceChange] = useDebounce(onChange, 3000); const language = useMemo(() => getLanguage(filename), [filename]); @@ -54,6 +54,10 @@ const CodeEditor = (props: Props) => { }, []); const onSave = useCallback(async () => { + if (isReadOnly) { + return; + } + const editor = editorRef.current; if (!editor) { return; @@ -101,24 +105,28 @@ const CodeEditor = (props: Props) => { } setTimeout(() => resetDebounceChange(), 100); - }, [language.formatter, formatOnSave, onChange, resetDebounceChange]); + }, [ + language.formatter, + formatOnSave, + onChange, + resetDebounceChange, + isReadOnly, + ]); - useEffect(() => { - setData(value); + // useEffect(() => { + // const editor = editorRef.current; + // const model = editor?.getModel(); - const editor = editorRef.current; - const model = editor?.getModel(); - - if (editor && model?.uri.path.substring(1) === path) { - editor.executeEdits("", [ - { - range: editor.getModel()!.getFullModelRange(), - text: value, - forceMoveMarkers: true, - }, - ]); - } - }, [value]); + // if (editor && model?.uri.path.substring(1) === path) { + // editor.executeEdits("", [ + // { + // range: editor.getModel()!.getFullModelRange(), + // text: value, + // forceMoveMarkers: true, + // }, + // ]); + // } + // }, [value]); useCommandKey("s", onSave); @@ -131,7 +139,12 @@ const CodeEditor = (props: Props) => { defaultValue={value} height="100%" onMount={onMount} - onChange={(e: string) => debounceChange(e)} + options={{ readOnly: isReadOnly }} + onChange={(e: string) => { + if (!isReadOnly) { + debounceChange(e); + } + }} /> ); diff --git a/pages/project/@slug/components/file-listing.tsx b/pages/project/@slug/components/file-listing.tsx index 9a8ceb3..f794a74 100644 --- a/pages/project/@slug/components/file-listing.tsx +++ b/pages/project/@slug/components/file-listing.tsx @@ -58,12 +58,12 @@ const FileListing = () => { - Upload File - + {/* Upload File */} + {/* */} settingsDialog.setState(true)}> Project Settings - Download Project + {/* Download Project */} diff --git a/pages/project/@slug/components/file-viewer.tsx b/pages/project/@slug/components/file-viewer.tsx index 3caf1cd..95bd435 100644 --- a/pages/project/@slug/components/file-viewer.tsx +++ b/pages/project/@slug/components/file-viewer.tsx @@ -47,8 +47,13 @@ const FileViewer = ({ id }: Props) => { filename={data.filename} path={data.path} value={data.content || ""} + isReadOnly={!project.isMutable} formatOnSave onChange={(val) => { + if (!project.isMutable) { + return; + } + updateFileContent.mutate({ projectId: project.id, id, diff --git a/pages/project/@slug/context/project.tsx b/pages/project/@slug/context/project.tsx index 5eae37f..5718f27 100644 --- a/pages/project/@slug/context/project.tsx +++ b/pages/project/@slug/context/project.tsx @@ -2,7 +2,7 @@ import { createContext, useContext } from "react"; import type { ProjectSchema } from "~/server/db/schema/project"; type TProjectContext = { - project: ProjectSchema; + project: ProjectSchema & { isMutable: boolean }; isCompact?: boolean; isEmbed?: boolean; };