waveterm/frontend/app/view/codeeditor/diffviewer.tsx
Mike Sawka b46d92ef24
upgrade monaco editor (remove monaco-editor/loader and monaco-editor/react) (#2743)
* Updates monaco-editor to new version.
* removes weird AMD stuff, to move to ESM via vite
* remove monaco-editor/loader
* remove monaco-editor/react
* implement the more native monaco wrappers ourselves
2026-01-05 16:34:36 -08:00

75 lines
2.4 KiB
TypeScript

// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
import { MonacoDiffViewer } from "@/app/monaco/monaco-react";
import { useOverrideConfigAtom } from "@/app/store/global";
import { boundNumber } from "@/util/util";
import type * as MonacoTypes from "monaco-editor";
import { useMemo, useRef } from "react";
interface DiffViewerProps {
blockId: string;
original: string;
modified: string;
language?: string;
fileName: string;
}
function defaultDiffEditorOptions(): MonacoTypes.editor.IDiffEditorOptions {
const opts: MonacoTypes.editor.IDiffEditorOptions = {
scrollBeyondLastLine: false,
fontSize: 12,
fontFamily: "Hack",
smoothScrolling: true,
scrollbar: {
useShadows: false,
verticalScrollbarSize: 5,
horizontalScrollbarSize: 5,
},
minimap: {
enabled: true,
},
readOnly: true,
renderSideBySide: true,
originalEditable: false,
};
return opts;
}
export function DiffViewer({ blockId, original, modified, language, fileName }: DiffViewerProps) {
const minimapEnabled = useOverrideConfigAtom(blockId, "editor:minimapenabled") ?? false;
const fontSize = boundNumber(useOverrideConfigAtom(blockId, "editor:fontsize"), 6, 64);
const inlineDiff = useOverrideConfigAtom(blockId, "editor:inlinediff");
const uuidRef = useRef(crypto.randomUUID()).current;
let editorPath: string;
if (fileName) {
const separator = fileName.startsWith("/") ? "" : "/";
editorPath = blockId + separator + fileName;
} else {
editorPath = uuidRef;
}
const editorOpts = useMemo(() => {
const opts = defaultDiffEditorOptions();
opts.minimap.enabled = minimapEnabled;
opts.fontSize = fontSize;
if (inlineDiff != null) {
opts.renderSideBySide = !inlineDiff;
}
return opts;
}, [minimapEnabled, fontSize, inlineDiff]);
return (
<div className="flex flex-col w-full h-full overflow-hidden items-center justify-center">
<div className="flex flex-col h-full w-full">
<MonacoDiffViewer
path={editorPath}
original={original}
modified={modified}
options={editorOpts}
language={language}
/>
</div>
</div>
);
}