Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 21996d8

Browse files
fix: review refactors
1 parent 9cc6e92 commit 21996d8

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

‎src/components/DiffViewer/components/DiffMinimap.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ import React, { useCallback, useEffect, useMemo, useRef } from "react";
22

33
import type { DiffMinimapProps, DiffRowOrCollapsed } from "../types";
44

5-
const ROW_HEIGHT = 20;
5+
import { getRowHeightFromCSS } from "../utils/constants";
6+
67
const SEARCH_HIGHLIGHT_COLOR = "#ffd700";
78
const CURRENT_MATCH_COLOR = "#ff4500";
89
const EQUAL_LINE_COLOR = "#363743";
910
const ADD_LINE_COLOR = "#4CAF50";
1011
const REMOVE_LINE_COLOR = "#F44336";
1112
const MODIFY_LINE_COLOR = "#FFC107";
12-
13-
// const MINIMAP_HOVER_SCROLL_COLOR = "#2196f3cc";
14-
// const MINIMAP_SCROLL_COLOR = "#2196f380";
15-
1613
const MINIMAP_HOVER_SCROLL_COLOR = "#7B7B7Bcc";
1714
const MINIMAP_SCROLL_COLOR = "#7B7B7B80";
1815

@@ -30,6 +27,8 @@ export const DiffMinimap: React.FC<DiffMinimapProps> = ({
3027
const containerRef = useRef<HTMLDivElement>(null);
3128
const isDragging = useRef(false);
3229

30+
const ROW_HEIGHT = useMemo(() => getRowHeightFromCSS(), []);
31+
3332
// Memoize expensive calculations
3433
const { totalLines, viewportHeight } = useMemo(() => {
3534
const totalShownLines = height / ROW_HEIGHT;

‎src/components/DiffViewer/components/VirtualizedDiffViewer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { SearchIcon } from "../../SearchIcon";
1010
import "../styles/JsonDiffCustomTheme.css";
1111
import { useRowHeights } from "../hooks/useRowHeights";
1212
import { useSearch } from "../hooks/useSearch";
13-
import { COLLAPSED_ROW_HEIGHT, isCollapsed,ROW_HEIGHT } from "../utils/constants";
13+
import { COLLAPSED_ROW_HEIGHT, getRowHeightFromCSS,isCollapsed } from "../utils/constants";
1414
import { buildViewFromSegments, generateSegments } from "../utils/preprocessDiff";
1515
import { DiffMinimap } from "./DiffMinimap";
1616
import ViewerRow from "./ViewerRow";
@@ -33,6 +33,8 @@ export const VirtualizedDiffViewer: React.FC<VirtualizedDiffViewerProps> = ({
3333

3434
const [segments, setSegments] = useState<SegmentItem[]>([]);
3535

36+
const ROW_HEIGHT = useMemo(() => getRowHeightFromCSS(), []);
37+
3638
const differ = useMemo(
3739
() =>
3840
new Differ({

‎src/components/DiffViewer/hooks/useSearch.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,11 @@ export function useSearch(leftView: DiffRowOrCollapsed[], initialTerm?: string,
6464
return () => observer.disconnect();
6565
}, [searchState.term]);
6666

67+
useEffect(() => {
68+
if (initialTerm !== undefined) {
69+
handleSearch(initialTerm);
70+
}
71+
}, [initialTerm, handleSearch]);
72+
6773
return { searchState, handleSearch, navigateMatch };
6874
}

‎src/components/DiffViewer/utils/constants.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
export const DIFF_VIEWER_CLASS = "json-diff-viewer-theme-custom";
22

3-
function getRowHeightFromCSS(): number {
4-
const root = document.documentElement;
5-
const value = getComputedStyle(root).getPropertyValue("--diff-row-height");
6-
return Number.parseInt(value, 10);
7-
}
3+
export const DEFAULT_ROW_HEIGHT = 20; // safe fallback
4+
5+
export function getRowHeightFromCSS(): number {
6+
if (typeof document === "undefined") {
7+
return DEFAULT_ROW_HEIGHT;
8+
}
89

9-
export const ROW_HEIGHT = getRowHeightFromCSS();
10+
try {
11+
const root = document.documentElement;
12+
const value = getComputedStyle(root).getPropertyValue("--diff-row-height");
13+
const parsed = Number.parseInt(value, 10);
14+
return Number.isNaN(parsed) ? DEFAULT_ROW_HEIGHT : parsed;
15+
}
16+
catch {
17+
return DEFAULT_ROW_HEIGHT;
18+
}
19+
}
1020

1121
export const COLLAPSED_ROW_HEIGHT = 20;
1222

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /