Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export function ChangesContent() {
<DiffViewer
contents={contents}
viewMode={viewMode}
filePath={selectedFile.path}
editable={isEditable}
onSave={handleSave}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
import { DiffEditor, type DiffOnMount } from "@monaco-editor/react";
import type * as Monaco from "monaco-editor";
import { useCallback, useRef } from "react";
import { monaco, SUPERSET_THEME } from "renderer/contexts/MonacoProvider";
import { SUPERSET_THEME } from "renderer/contexts/MonacoProvider";
import type { DiffViewMode, FileContents } from "shared/changes-types";
import {
registerCopyPathLineAction,
registerSaveCommand,
} from "./editor-actions";

interface DiffViewerProps {
contents: FileContents;
viewMode: DiffViewMode;
filePath: string;
editable?: boolean;
onSave?: (content: string) => void;
}

export function DiffViewer({
contents,
viewMode,
filePath,
editable = false,
onSave,
}: DiffViewerProps) {
const modifiedEditorRef = useRef<ReturnType<
NonNullable<Parameters<DiffOnMount>[0]["getModifiedEditor"]>
> | null>(null);
const modifiedEditorRef = useRef<Monaco.editor.IStandaloneCodeEditor | null>(
null,
);

const handleSave = useCallback(() => {
if (!editable || !onSave || !modifiedEditorRef.current) return;
const content = modifiedEditorRef.current.getValue();
onSave(content);
onSave(modifiedEditorRef.current.getValue());
}, [editable, onSave]);

const handleMount: DiffOnMount = useCallback(
(editor) => {
modifiedEditorRef.current = editor.getModifiedEditor();
const originalEditor = editor.getOriginalEditor();
const modifiedEditor = editor.getModifiedEditor();
modifiedEditorRef.current = modifiedEditor;

registerCopyPathLineAction(originalEditor, filePath);
registerCopyPathLineAction(modifiedEditor, filePath);

if (editable && modifiedEditorRef.current) {
modifiedEditorRef.current.addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS,
handleSave,
);
if (editable) {
registerSaveCommand(modifiedEditor, handleSave);
}
},
[editable, handleSave],
[editable, handleSave, filePath],
);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type * as Monaco from "monaco-editor";
import { monaco } from "renderer/contexts/MonacoProvider";

export function registerCopyPathLineAction(
editor: Monaco.editor.IStandaloneCodeEditor,
filePath: string,
) {
editor.addAction({
id: "copy-path-line",
label: "Copy Path:Line",
keybindings: [
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyC,
],
contextMenuGroupId: "9_cutcopypaste",
contextMenuOrder: 4,
run: (ed) => {
const selection = ed.getSelection();
if (!selection) return;

const { startLineNumber, endLineNumber } = selection;
const pathWithLine =
startLineNumber === endLineNumber
? `${filePath}:${startLineNumber}`
: `${filePath}:${startLineNumber}-${endLineNumber}`;

navigator.clipboard.writeText(pathWithLine);
},
});
}

export function registerSaveCommand(
editor: Monaco.editor.IStandaloneCodeEditor,
onSave: () => void,
) {
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, onSave);
}
Loading