Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 24 additions & 1 deletion src/components/DiffViewer/components/VirtualizedDiffViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DiffResult } from "json-diff-kit";
import type { VariableSizeList as List } from "react-window";

import { Differ } from "json-diff-kit";
Expand All @@ -7,6 +8,7 @@ import type { DiffRowOrCollapsed, SegmentItem, VirtualizedDiffViewerProps } from

import "../styles/JsonDiffCustomTheme.css";
import { useSearch } from "../hooks/useSearch";
import { fastHash } from "../utils/json-diff/diff-hash";
import { expandSegment, hasExpandedSegments, hideAllSegments } from "../utils/json-diff/segment-util";
import { buildViewFromSegments, generateSegments } from "../utils/preprocessDiff";
import { DiffMinimap } from "./DiffMinimap";
Expand All @@ -21,6 +23,8 @@ export const VirtualizedDiffViewer: React.FC<VirtualizedDiffViewerProps> = ({
leftTitle,
rightTitle,
hideSearch,
customDiffer,
getDiffData,
showSingleMinimap,
onSearchMatch,
differOptions,
Expand All @@ -31,8 +35,10 @@ export const VirtualizedDiffViewer: React.FC<VirtualizedDiffViewerProps> = ({
}) => {
const outerRef = useRef<Node>(null);
const listRef = useRef<List>(null);
const getDiffDataRef = useRef<typeof getDiffData>();
const lastSent = useRef<number>();

const differ = useMemo(
const differ = customDiffer ?? useMemo(
() =>
new Differ({
detectCircular: true,
Expand Down Expand Up @@ -101,6 +107,23 @@ export const VirtualizedDiffViewer: React.FC<VirtualizedDiffViewerProps> = ({
setRightView(rightBuilt);
}, [segments, rawLeftDiff, rawRightDiff]);

useEffect(() => {
getDiffDataRef.current = getDiffData;
}, [getDiffData]);

useEffect(() => {
if (!getDiffDataRef.current)
return;

const data: [DiffResult[], DiffResult[]] = [rawLeftDiff, rawRightDiff];
const hash = fastHash(data);

if (lastSent.current !== hash) {
lastSent.current = hash;
getDiffDataRef.current(data);
}
}, [rawLeftDiff, rawRightDiff]);

return (
<div className={`diff-viewer-container${className ? ` ${className}` : ""}`}>

Expand Down
4 changes: 3 additions & 1 deletion src/components/DiffViewer/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DifferOptions, DiffResult, InlineDiffOptions } from "json-diff-kit";
import type { Differ, DifferOptions, DiffResult, InlineDiffOptions } from "json-diff-kit";

export type DiffRow = {
originalIndex: number;
Expand Down Expand Up @@ -41,7 +41,9 @@ export type VirtualizedDiffViewerProps = {
leftTitle?: string;
rightTitle?: string;
onSearchMatch?: (index: number) => void;
getDiffData?: (diffData: [DiffResult[], DiffResult[]]) => void;
differOptions?: DifferOptions;
customDiffer?: Differ;
showSingleMinimap?: boolean;
className?: string;
miniMapWidth?: number;
Expand Down
11 changes: 11 additions & 0 deletions src/components/DiffViewer/utils/json-diff/diff-hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { DiffResult } from "json-diff-kit";

export function fastHash(diff: [DiffResult[], DiffResult[]]) {
let hash = 0;
for (const arr of diff) {
for (const row of arr) {
hash = (hash + row.text.length + row.type.charCodeAt(0)) >>> 0;
}
}
return hash;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Buttons
export { default as VirtualDiffViewer } from "./components/DiffViewer";
export { Differ, type DiffResult } from "json-diff-kit";