1+ /* eslint-disable @typescript-eslint/ban-ts-comment */
12import { getUnifiedContentLine } from "@git-diff-view/core" ;
23import * as React from "react" ;
3- import { Fragment , memo , useEffect , useMemo } from "react" ;
4+ import { Fragment , memo , useEffect , useMemo , useCallback } from "react" ;
45import { createStore , ref } from "reactivity-store" ;
56import { useSyncExternalStore } from "use-sync-external-store/shim" ;
67
78import { useDiffViewContext , type SplitSide } from ".." ;
9+ import { useTextWidth } from "../hooks/useTextWidth" ;
810
911import { DiffUnifiedExtendLine } from "./DiffUnifiedExtendLine" ;
1012import { DiffUnifiedHunkLine } from "./DiffUnifiedHunkLine" ;
1113import { DiffUnifiedLine } from "./DiffUnifiedLine" ;
1214import { DiffUnifiedWidgetLine } from "./DiffUnifiedWidgetLine" ;
1315import { DiffWidgetContext } from "./DiffWidgetContext" ;
14- import { removeAllSelection } from "./tools" ;
16+ import { asideWidth , removeAllSelection } from "./tools" ;
1517
1618import type { DiffFile } from "@git-diff-view/core" ;
1719import type { MouseEventHandler } from "react" ;
@@ -43,7 +45,7 @@ export const DiffUnifiedView = memo(({ diffFile }: { diffFile: DiffFile }) => {
4345 if ( typeof renderWidgetLine !== "function" ) return ;
4446
4547 widgetSide . value = side ;
46-
48+
4749 widgetLineNumber . value = lineNumber ;
4850 } ;
4951
@@ -54,6 +56,8 @@ export const DiffUnifiedView = memo(({ diffFile }: { diffFile: DiffFile }) => {
5456
5557 const contextValue = useMemo ( ( ) => ( { useWidget } ) , [ useWidget ] ) ;
5658
59+ const fontSize = useDiffContext ( useCallback ( ( s ) => s . fontSize , [ ] ) ) ;
60+
5761 useSyncExternalStore ( diffFile . subscribe , diffFile . getUpdateCount ) ;
5862
5963 useEffect ( ( ) => {
@@ -62,14 +66,28 @@ export const DiffUnifiedView = memo(({ diffFile }: { diffFile: DiffFile }) => {
6266 setWidget ( { } ) ;
6367 } , [ diffFile , useWidget ] ) ;
6468
69+ const unifiedLineLength = diffFile . unifiedLineLength ;
70+
71+ const _width = useTextWidth ( {
72+ text : unifiedLineLength . toString ( ) ,
73+ font : { fontSize : fontSize + "px" , fontFamily : "Menlo, Consolas, monospace" } ,
74+ } ) ;
75+
76+ const width = Math . max ( 40 , _width + 25 ) ;
77+
6578 const lines = getUnifiedContentLine ( diffFile ) ;
6679
6780 return (
6881 < DiffWidgetContext . Provider value = { contextValue } >
6982 < div className = "unified-diff-view w-full" >
7083 < div
7184 className = "unified-diff-table-wrapper overflow-auto w-full scrollbar-hide scrollbar-disable"
72- style = { { fontFamily : "Menlo, Consolas, monospace" , fontSize : "var(--diff-font-size--)" } }
85+ style = { {
86+ // @ts -ignore
87+ [ asideWidth ] : `${ Math . round ( width ) } px` ,
88+ fontFamily : "Menlo, Consolas, monospace" ,
89+ fontSize : "var(--diff-font-size--)" ,
90+ } }
7391 >
7492 < table className = "unified-diff-table border-collapse w-full" >
7593 < colgroup >
@@ -82,7 +100,7 @@ export const DiffUnifiedView = memo(({ diffFile }: { diffFile: DiffFile }) => {
82100 < th scope = "col" > line content</ th >
83101 </ tr >
84102 </ thead >
85- < tbody className = "diff-table-body leading-[1.4]" onMouseDownCapture = { onMouseDown } >
103+ < tbody className = "diff-table-body leading-[1.4]" onMouseDownCapture = { onMouseDown } >
86104 { lines . map ( ( item ) => (
87105 < Fragment key = { item . index } >
88106 < DiffUnifiedHunkLine index = { item . index } lineNumber = { item . lineNumber } diffFile = { diffFile } />
0 commit comments