import { EditorView } from "@codemirror/view"; import diff, { DiffChunk } from "../diff"; import EditorPane from "./editor-pane"; import ActionsGutter from "./actions-gutter"; import * as React from "react"; interface DiffViewProps { remoteText: string; localText: string; onRemoteTextChange: (content: string) => void; onLocalTextChange: (content: string) => void; onConflictResolved: () => void; } const DiffView: React.FC = ({ remoteText, localText, onRemoteTextChange, onLocalTextChange, onConflictResolved, }) => { // We need to know the line height to correctly draw the ribbon between the left // and right editor in the actions gutter const [lineHeight, setLineHeight] = React.useState(0); const handleEditorReady = (editor: EditorView) => { setLineHeight(editor.defaultLineHeight); }; const [leftEditorTopOffset, setLeftEditorTopOffset] = React.useState(0); const [rightEditorTopOffset, setRightEditorTopOffset] = React.useState(0); const diffs = diff(remoteText, localText); return (
{diffs.length === 0 && ( )} { if (chunk.type === "add") { const remoteLines = remoteText.split("\n"); remoteLines.splice( chunk.startLeftLine - 1, 0, ...localText .split("\n") .slice(chunk.startRightLine - 1, chunk.endRightLine - 1), ); onRemoteTextChange(remoteLines.join("\n")); } else if (chunk.type === "modify") { const remoteLines = remoteText.split("\n"); remoteLines.splice( chunk.startLeftLine - 1, chunk.endLeftLine - chunk.startLeftLine, ...localText .split("\n") .slice(chunk.startRightLine - 1, chunk.endRightLine - 1), ); onRemoteTextChange(remoteLines.join("\n")); } }} onAcceptRight={(chunk: DiffChunk) => { if (chunk.type === "remove") { const localLines = localText.split("\n"); localLines.splice( chunk.startRightLine - 1, 0, ...remoteText .split("\n") .slice(chunk.startLeftLine - 1, chunk.endLeftLine - 1), ); onLocalTextChange(localLines.join("\n")); } else if (chunk.type === "modify") { const localLines = localText.split("\n"); localLines.splice( chunk.startRightLine - 1, chunk.endRightLine - chunk.startRightLine, ...remoteText .split("\n") .slice(chunk.startLeftLine - 1, chunk.endLeftLine - 1), ); onLocalTextChange(localLines.join("\n")); } }} onReject={(chunk: DiffChunk) => { if (chunk.type === "add") { const localLines = localText.split("\n"); localLines.splice( chunk.startRightLine - 1, chunk.endRightLine - chunk.startRightLine, ); onLocalTextChange(localLines.join("\n")); } else if (chunk.type === "remove") { const remoteLines = remoteText.split("\n"); remoteLines.splice( chunk.startLeftLine - 1, chunk.endLeftLine - chunk.startLeftLine, ); onRemoteTextChange(remoteLines.join("\n")); } }} />
); }; export default DiffView;