Skip to content

Commit ffdf0f2

Browse files
update
1 parent f742431 commit ffdf0f2

File tree

18 files changed

+51
-26
lines changed

18 files changed

+51
-26
lines changed

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "@git-diff-view/cli",
44
"author": "MrWangJustToDo",
55
"license": "MIT",
6-
"version": "0.0.6",
6+
"version": "0.0.7",
77
"main": "index.mjs",
88
"type": "module",
99
"types": "index.d.ts",

packages/cli/src/components/DiffView.tsx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { DiffFile, _cacheMap, SplitSide } from "@git-diff-view/core";
33
import { DiffModeEnum } from "@git-diff-view/utils";
44
import { Box } from "ink";
5-
import React, { memo, useEffect, useMemo, useRef } from "react";
5+
import React, { forwardRef, memo, useEffect, useImperativeHandle, useMemo, useRef } from "react";
66

77
import { useIsMounted } from "../hooks/useIsMounted";
88
import { useUnmount } from "../hooks/useUnmount";
@@ -14,7 +14,7 @@ import { createDiffConfigStore } from "./tools";
1414

1515
import type { DiffHighlighter, DiffHighlighterLang } from "@git-diff-view/core";
1616
import type { DOMElement } from "ink";
17-
import type { ReactNode, RefObject } from "react";
17+
import type { ForwardedRef, ReactNode, RefObject } from "react";
1818

1919
_cacheMap.name = "@git-diff-view/cli";
2020

@@ -182,10 +182,13 @@ const InternalDiffView = <T extends unknown>(
182182

183183
const MemoedInternalDiffView = memo(InternalDiffView);
184184

185-
const DiffViewContainer = <T extends unknown>(props: DiffViewProps<T>) => {
185+
const DiffViewContainerWithRef = <T extends unknown>(
186+
props: DiffViewProps<T>,
187+
ref: ForwardedRef<{ getDiffFileInstance: () => DiffFile }>
188+
) => {
186189
const { registerHighlighter, data, diffViewTheme, diffFile: _diffFile, width, ...restProps } = props;
187190

188-
const ref = useRef<DOMElement>(null);
191+
const domRef = useRef<DOMElement>(null);
189192

190193
const diffFile = useMemo(() => {
191194
if (_diffFile) {
@@ -246,19 +249,21 @@ const DiffViewContainer = <T extends unknown>(props: DiffViewProps<T>) => {
246249
// fix react strict mode error
247250
useUnmount(() => (__DEV__ ? diffFile?._destroy?.() : diffFile?.clear?.()), [diffFile]);
248251

252+
useImperativeHandle(ref, () => ({ getDiffFileInstance: () => diffFile }), [diffFile]);
253+
249254
if (!diffFile) return null;
250255

251256
return (
252257
<Box
253-
ref={ref}
258+
ref={domRef}
254259
width={width}
255260
flexGrow={typeof width === "number" ? undefined : 1}
256261
flexShrink={typeof width === "number" ? 0 : undefined}
257262
>
258263
<MemoedInternalDiffView
259264
key={diffFile.getId()}
260265
{...restProps}
261-
wrapperRef={ref}
266+
wrapperRef={domRef}
262267
diffFile={diffFile}
263268
isMounted={isMounted}
264269
diffViewTheme={diffViewTheme}
@@ -270,12 +275,21 @@ const DiffViewContainer = <T extends unknown>(props: DiffViewProps<T>) => {
270275
};
271276

272277
// type helper function
273-
function ReactDiffView<T>(props: DiffViewProps_1<T>): JSX.Element;
274-
function ReactDiffView<T>(props: DiffViewProps_2<T>): JSX.Element;
275-
function ReactDiffView<T>(props: DiffViewProps<T>) {
276-
return <DiffViewContainer {...props} />;
278+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
279+
function ReactDiffView<T>(
280+
props: DiffViewProps_1<T> & { ref?: ForwardedRef<{ getDiffFileInstance: () => DiffFile }> }
281+
): JSX.Element;
282+
function ReactDiffView<T>(
283+
props: DiffViewProps_2<T> & { ref?: ForwardedRef<{ getDiffFileInstance: () => DiffFile }> }
284+
): JSX.Element;
285+
function ReactDiffView<T>(_props: DiffViewProps<T> & { ref?: ForwardedRef<{ getDiffFileInstance: () => DiffFile }> }) {
286+
return <></>;
277287
}
278288

279-
export const DiffView = ReactDiffView;
289+
const InnerDiffView = forwardRef(DiffViewContainerWithRef);
290+
291+
InnerDiffView.displayName = "DiffView";
292+
293+
export const DiffView = InnerDiffView as typeof ReactDiffView;
280294

281295
export const version = __VERSION__;

packages/cli/test/index.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { getDiffViewHighlighter } from "@git-diff-view/shiki";
2-
// import { createElement } from "@my-react/react";
3-
// import { Box, render, Text } from "@my-react/react-terminal";
4-
import { Box, render, Text } from "ink";
5-
import { createElement } from "react";
2+
import { createElement } from "@my-react/react";
3+
import { Box, render, Text } from "@my-react/react-terminal";
4+
// import { Box, render, Text } from "ink";
5+
// import { createElement } from "react";
66

77
import { DiffView, DiffModeEnum } from "@git-diff-view/cli";
88

packages/core/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ declare class File$1 {
1515
readonly lang: DiffHighlighterLang | string;
1616
readonly fileName?: string;
1717
ast?: DiffAST;
18+
theme?: "light" | "dark";
1819
rawFile: Record<number, string>;
1920
hasDoRaw: boolean;
2021
rawLength?: number;

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "@git-diff-view/core",
44
"author": "MrWangJustToDo",
55
"license": "MIT",
6-
"version": "0.0.35",
6+
"version": "0.0.36",
77
"main": "index.js",
88
"types": "index.d.ts",
99
"files": [

packages/core/src/file.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type SyntaxLineWithTemplate = SyntaxLine & {
3434
export class File {
3535
ast?: DiffAST;
3636

37+
theme?: "light" | "dark";
38+
3739
rawFile: Record<number, string> = {};
3840

3941
hasDoRaw: boolean = false;
@@ -61,6 +63,8 @@ export class File {
6163

6264
file.ast = data?.ast;
6365

66+
file.theme = data?.theme;
67+
6468
file.rawFile = data?.rawFile || {};
6569

6670
file.plainFile = data?.plainFile || {};
@@ -105,7 +109,7 @@ export class File {
105109
registerHighlighter?: Omit<DiffHighlighter, "getHighlighterEngine">;
106110
theme?: "light" | "dark";
107111
}) {
108-
if (!this.raw || this.hasDoSyntax) return;
112+
if (!this.raw || (this.hasDoSyntax && this.theme === theme)) return;
109113

110114
const finalHighlighter = registerHighlighter || highlighter;
111115

@@ -137,6 +141,8 @@ export class File {
137141

138142
this.ast = supportEngin.getAST(this.raw, this.fileName, this.lang, theme);
139143

144+
this.theme = theme;
145+
140146
if (!this.ast) return;
141147

142148
const { syntaxFileObject, syntaxFileLineNumber } = supportEngin.processAST(this.ast);

packages/file/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ declare class File$1 {
1414
readonly lang: DiffHighlighterLang | string;
1515
readonly fileName?: string;
1616
ast?: DiffAST;
17+
theme?: "light" | "dark";
1718
rawFile: Record<number, string>;
1819
hasDoRaw: boolean;
1920
rawLength?: number;

packages/file/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "@git-diff-view/file",
44
"author": "MrWangJustToDo",
55
"license": "MIT",
6-
"version": "0.0.35",
6+
"version": "0.0.36",
77
"main": "index.js",
88
"types": "index.d.ts",
99
"files": [

packages/lowlight/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "@git-diff-view/lowlight",
44
"author": "MrWangJustToDo",
55
"license": "MIT",
6-
"version": "0.0.35",
6+
"version": "0.0.36",
77
"main": "index.js",
88
"types": "index.d.ts",
99
"files": [

packages/react/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ declare class File$1 {
1414
readonly lang: DiffHighlighterLang | string;
1515
readonly fileName?: string;
1616
ast?: DiffAST;
17+
theme?: "light" | "dark";
1718
rawFile: Record<number, string>;
1819
hasDoRaw: boolean;
1920
rawLength?: number;

0 commit comments

Comments
 (0)