Skip to content

Commit 6bcf65d

Browse files
authored
feat: add read only marker and read only mode (#1899)
1 parent d7a9006 commit 6bcf65d

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

frontend/app/view/codeeditor/codeeditor.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ interface CodeEditorProps {
112112
blockId: string;
113113
text: string;
114114
filename: string;
115+
fileinfo: FileInfo;
115116
language?: string;
116117
meta?: MetaType;
117118
onChange?: (text: string) => void;
118119
onMount?: (monacoPtr: MonacoTypes.editor.IStandaloneCodeEditor, monaco: Monaco) => () => void;
119120
}
120121

121-
export function CodeEditor({ blockId, text, language, filename, meta, onChange, onMount }: CodeEditorProps) {
122+
export function CodeEditor({ blockId, text, language, filename, fileinfo, meta, onChange, onMount }: CodeEditorProps) {
122123
const divRef = useRef<HTMLDivElement>(null);
123124
const unmountRef = useRef<() => void>(null);
124125
const minimapEnabled = useOverrideConfigAtom(blockId, "editor:minimapenabled") ?? false;
@@ -169,12 +170,13 @@ export function CodeEditor({ blockId, text, language, filename, meta, onChange,
169170

170171
const editorOpts = useMemo(() => {
171172
const opts = defaultEditorOptions();
173+
opts.readOnly = fileinfo.readonly;
172174
opts.minimap.enabled = minimapEnabled;
173175
opts.stickyScroll.enabled = stickyScrollEnabled;
174176
opts.wordWrap = wordWrap ? "on" : "off";
175177
opts.fontSize = fontSize;
176178
return opts;
177-
}, [minimapEnabled, stickyScrollEnabled, wordWrap, fontSize]);
179+
}, [minimapEnabled, stickyScrollEnabled, wordWrap, fontSize, fileinfo.readonly]);
178180

179181
return (
180182
<div className="code-editor-wrapper">

frontend/app/view/preview/preview.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,35 @@ export class PreviewModel implements ViewModel {
259259
saveClassName = "green";
260260
}
261261
if (isCeView) {
262-
viewTextChildren.push({
263-
elemtype: "textbutton",
264-
text: "Save",
265-
className: clsx(
266-
`${saveClassName} warning border-radius-4 vertical-padding-2 horizontal-padding-10 font-size-11 font-weight-500`
267-
),
268-
onClick: () => fireAndForget(this.handleFileSave.bind(this)),
269-
});
262+
const fileInfo = globalStore.get(this.loadableFileInfo);
263+
if (fileInfo.state != "hasData") {
264+
viewTextChildren.push({
265+
elemtype: "textbutton",
266+
text: "Loading ...",
267+
className: clsx(
268+
`grey warning border-radius-4 vertical-padding-2 horizontal-padding-10 font-size-11 font-weight-500`
269+
),
270+
onClick: () => {},
271+
});
272+
} else if (fileInfo.data.readonly) {
273+
viewTextChildren.push({
274+
elemtype: "textbutton",
275+
text: "Read Only",
276+
className: clsx(
277+
`yellow warning border-radius-4 vertical-padding-2 horizontal-padding-10 font-size-11 font-weight-500`
278+
),
279+
onClick: () => {},
280+
});
281+
} else {
282+
viewTextChildren.push({
283+
elemtype: "textbutton",
284+
text: "Save",
285+
className: clsx(
286+
`${saveClassName} warning border-radius-4 vertical-padding-2 horizontal-padding-10 font-size-11 font-weight-500`
287+
),
288+
onClick: () => fireAndForget(this.handleFileSave.bind(this)),
289+
});
290+
}
270291
if (get(this.canPreview)) {
271292
viewTextChildren.push({
272293
elemtype: "textbutton",
@@ -934,6 +955,7 @@ function CodeEditPreview({ model }: SpecializedViewProps) {
934955
const fileContent = useAtomValue(model.fileContent);
935956
const setNewFileContent = useSetAtom(model.newFileContent);
936957
const fileName = useAtomValue(model.statFilePath);
958+
const fileInfo = useAtomValue(model.statFile);
937959
const blockMeta = useAtomValue(model.blockAtom)?.meta;
938960

939961
function codeEditKeyDownHandler(e: WaveKeyboardEvent): boolean {
@@ -985,6 +1007,7 @@ function CodeEditPreview({ model }: SpecializedViewProps) {
9851007
blockId={model.blockId}
9861008
text={fileContent}
9871009
filename={fileName}
1010+
fileinfo={fileInfo}
9881011
meta={blockMeta}
9891012
onChange={(text) => setNewFileContent(text)}
9901013
onMount={onMount}

0 commit comments

Comments
 (0)