Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Feb 27, 2024
1 parent 7834bf0 commit 023915e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/react/src/components/DiffSplitView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ export const DiffSplitView = memo(({ diffFile }: { diffFile: DiffFile }) => {
const widgetLineNumber = ref<number>(undefined);

const setWidget = ({ side, lineNumber }: { side?: SplitSide; lineNumber?: number }) => {
const { renderWidgetLine } = useDiffContext.getReadonlyState();

if (typeof renderWidgetLine !== "function") return;

widgetSide.value = side;

widgetLineNumber.value = lineNumber;
};

return { widgetSide, widgetLineNumber, setWidget };
}),
[]
[useDiffContext]
);

const contextValue = useMemo(() => ({ useWidget }), [useWidget]);
Expand Down
12 changes: 10 additions & 2 deletions packages/react/src/components/DiffUnifiedView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { Fragment, memo, useEffect, useMemo } from "react";
import { createStore, ref } from "reactivity-store";
import { useSyncExternalStore } from "use-sync-external-store/shim";

import { useDiffViewContext, type SplitSide } from "..";

import { DiffUnifiedExtendLine } from "./DiffUnifiedExtendLine";
import { DiffUnifiedHunkLine } from "./DiffUnifiedHunkLine";
import { DiffUnifiedLine } from "./DiffUnifiedLine";
import { DiffUnifiedWidgetLine } from "./DiffUnifiedWidgetLine";
import { DiffWidgetContext } from "./DiffWidgetContext";
import { removeAllSelection } from "./tools";

import type { SplitSide } from "..";
import type { DiffFile } from "@git-diff-view/core";
import type { MouseEventHandler } from "react";

Expand All @@ -26,6 +27,8 @@ const onMouseDown: MouseEventHandler<HTMLTableSectionElement> = (e) => {
};

export const DiffUnifiedView = memo(({ diffFile }: { diffFile: DiffFile }) => {
const { useDiffContext } = useDiffViewContext();

// performance optimization
const useWidget = useMemo(
() =>
Expand All @@ -35,13 +38,18 @@ export const DiffUnifiedView = memo(({ diffFile }: { diffFile: DiffFile }) => {
const widgetLineNumber = ref<number>(undefined);

const setWidget = ({ side, lineNumber }: { side?: SplitSide; lineNumber?: number }) => {
const { renderWidgetLine } = useDiffContext.getReadonlyState();

if (typeof renderWidgetLine !== "function") return;

widgetSide.value = side;

widgetLineNumber.value = lineNumber;
};

return { widgetSide, widgetLineNumber, setWidget };
}),
[]
[useDiffContext]
);

const contextValue = useMemo(() => ({ useWidget }), [useWidget]);
Expand Down
4 changes: 3 additions & 1 deletion packages/vue/src/components/DiffView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export const DiffView = defineComponent<

const widgetState = ref<{ side?: SplitSide; lineNumber?: number }>({});

const setWidget = (v: { side?: SplitSide; lineNumber?: number }) => (widgetState.value = v);
const setWidget = (v: { side?: SplitSide; lineNumber?: number }) => {
typeof options.slots.widget === "function" && (widgetState.value = v);
};

const enableHighlight = computed(() => props.diffViewHighlight ?? true);

Expand Down

0 comments on commit 023915e

Please sign in to comment.