nandi/oripublic Fork 0
3017563d783a895a85f8f8ad162b58bb5d944946
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

EditorPane.tsx · 147 lines · 3.4 KBTypeScript Blame HistoryRaw
  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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * EditorPane edits a workspace file with Monaco: dirty indicator, Save
 * button and Ctrl/Cmd+S, saving through the backend file API.
 *
 * @example
 * <EditorPane path="src/main.go" />
 */

import { lazy, Suspense, useCallback, useEffect, useState } from "react";
import { readFile, writeFile } from "../api";
import { languageForPath } from "../lang";

const MonacoViewer = lazy(() => import("./MonacoViewer"));

type SaveStatus = "idle" | "saving" | "saved";

export function EditorPane({ path }: { path: string | null }) {
	const [original, setOriginal] = useState<string | null>(null);
	const [text, setText] = useState("");
	const [error, setError] = useState<string | null>(null);
	const [status, setStatus] = useState<SaveStatus>("idle");

	useEffect(() => {
		if (!path) {
			return;
		}
		setOriginal(null);
		setError(null);
		setStatus("idle");
		readFile(path)
			.then((file) => {
				setOriginal(file.content);
				setText(file.content);
			})
			.catch((cause: Error) => setError(cause.message));
	}, [path]);

	const dirty = original !== null && text !== original;

	const save = useCallback(async () => {
		if (!path || original === null || text === original) {
			return;
		}
		setStatus("saving");
		setError(null);
		try {
			await writeFile(path, text);
			setOriginal(text);
			setStatus("saved");
		} catch (cause) {
			setError((cause as Error).message);
			setStatus("idle");
		}
	}, [path, text, original]);

	const placeholder = editorPlaceholder(path, original, error);
	if (placeholder) {
		return placeholder;
	}

	return (
		// The shortcut is handled on the wrapper so it works wherever the
		// focus sits inside the pane, Monaco included.
		<div
			className="editor"
			onKeyDown={(event) => {
				if ((event.ctrlKey || event.metaKey) && event.key === "s") {
					event.preventDefault();
					void save();
				}
			}}
		>
			<EditorHeader
				path={path as string}
				dirty={dirty}
				status={status}
				error={error}
				onSave={save}
			/>
			<div className="editor-body">
				<Suspense fallback={<p className="pane-empty">loading editor</p>}>
					<MonacoViewer
						content={text}
						language={languageForPath(path as string)}
						onChange={(value) => setText(value ?? "")}
					/>
				</Suspense>
			</div>
		</div>
	);
}

/** editorPlaceholder covers the no-file / error / loading states. */
function editorPlaceholder(
	path: string | null,
	original: string | null,
	error: string | null,
) {
	if (!path) {
		return (
			<p className="pane-empty">Double-click a file in the tree to edit it.</p>
		);
	}
	if (error && original === null) {
		return <p className="pane-error">{error}</p>;
	}
	if (original === null) {
		return <p className="pane-empty">loading</p>;
	}
	return null;
}

/** EditorHeader shows the file name, dirty marker, save state and button. */
function EditorHeader({
	path,
	dirty,
	status,
	error,
	onSave,
}: {
	path: string;
	dirty: boolean;
	status: SaveStatus;
	error: string | null;
	onSave: () => Promise<void>;
}) {
	return (
		<div className="pane-header">
			<span className="pane-path" title={path}>
				{path.split("/").pop()}
				{dirty ? " ●" : ""}
			</span>
			{error && <span className="pane-error-inline">{error}</span>}
			{status === "saved" && !dirty && (
				<span className="pane-saved">saved</span>
			)}
			<button
				type="button"
				className="pane-action"
				disabled={!dirty || status === "saving"}
				onClick={() => void onSave()}
			>
				{status === "saving" ? "Saving…" : "Save"}
			</button>
		</div>
	);
}