Skip to content

Commit

Permalink
simple widget usage
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Jan 17, 2024
1 parent f74b50d commit 0d2cb56
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 33 deletions.
84 changes: 79 additions & 5 deletions src/components/DiffAddWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { DiffFile } from "../diff";
import { DiffFileExtends } from "../diff";
import { SplitSide } from "./DiffSplitView";
import { addWidgetBGName, addWidgetColorName } from "./color";

// TODO
//@ts-ignore
export const DiffAddWidget = ({ diffFile }: { diffFile: DiffFile }) => {
export const DiffSplitAddWidget = ({
diffFile,
side,
index,
}: {
index: number;
diffFile: DiffFileExtends;
side: SplitSide;
}) => {
return (
<div
className="diff-add-widget-wrapper absolute left-[100%] top-[1px] translate-x-[-50%]"
Expand All @@ -21,6 +27,74 @@ export const DiffAddWidget = ({ diffFile }: { diffFile: DiffFile }) => {
fontSize: `1.2em`,
backgroundColor: `var(${addWidgetBGName})`,
}}
onClick={() => {
diffFile.addSplitExtendLine(
index,
side === SplitSide.old ? "left" : "right",
<div className="border flex flex-col w-full">
<textarea className="w-full border min-h-[80px] p-[2px]" />
<div className="m-[5px] mt-[0.8em]">
<button
className="border float-right px-[12px] py-[6px] rounded-[4px] sticky right-[10px]"
onClick={() =>
diffFile.removeSplitExtendLine(
index,
side === SplitSide.old ? "left" : "right"
)
}
>
cancel
</button>
</div>
</div>
);
}}
>
+
</button>
</div>
);
};

export const DiffUnifiedAddWidget = ({
diffFile,
index,
}: {
index: number;
diffFile: DiffFileExtends;
}) => {
return (
<div
className="diff-add-widget-wrapper absolute left-[100%] top-[1px] translate-x-[-50%]"
style={{
width: "calc(var(--diff-font-size--) * 1.4)",
height: "calc(var(--diff-font-size--) * 1.4)",
}}
>
<button
className="diff-add-widget absolute overflow-hidden cursor-pointer rounded-md w-0 h-0 left-[0] top-[0] flex items-center justify-center transition-transform origin-center group-hover:w-full group-hover:h-full hover:scale-110"
style={{
color: `var(${addWidgetColorName})`,
zIndex: 1,
fontSize: `1.2em`,
backgroundColor: `var(${addWidgetBGName})`,
}}
onClick={() => {
diffFile.addUnifiedLine(
index,
<div className="border flex flex-col w-full px-[4px] py-[8px]">
<textarea className="w-full border min-h-[80px] p-[2px]" />
<div className="m-[5px] mt-[0.8em]">
<button
className="border float-right px-[12px] py-[6px] rounded-[4px] sticky right-[10px]"
onClick={() => diffFile.removeUnifiedExtendLine(index)}
>
cancel
</button>
</div>
</div>
);
}}
>
+
</button>
Expand Down
56 changes: 56 additions & 0 deletions src/components/DiffSplitExtendLine.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { DiffFileExtends } from "../diff";
import { useSyncHeight } from "../hooks/useSyncHeight";
import { SplitSide } from "./DiffSplitView";
import { emptyBGName } from "./color";

export const DiffSplitExtendLine = ({
index,
diffFile,
isWrap,
side,
lineNumber,
}: {
index: number;
side: SplitSide;
diffFile: DiffFileExtends;
lineNumber: number;
isWrap: boolean;
isHighlight: boolean;
}) => {
const currentLeftExtend = diffFile.getSplitExtendLine(index, "left");

const currentRightExtend = diffFile.getSplitExtendLine(index, "right");

const currentIsShow = currentLeftExtend || currentRightExtend;

useSyncHeight({
selector: `tr[data-line="${lineNumber}-extend"]`,
side: side === SplitSide.old ? "left" : "right",
enable: !!currentIsShow,
});

if (!currentIsShow) return null;

const currentExtend =
side === SplitSide.old ? currentLeftExtend : currentRightExtend;

return (
<tr
data-line={`${lineNumber}-extend`}
data-state="hunk"
data-side={side === SplitSide.old ? "left" : "right"}
className="diff-line diff-line-extend"
style={{
backgroundColor: !currentExtend ? `var(${emptyBGName})` : undefined,
}}
>
<td
className="diff-line-extend-content"
style={{ position: isWrap ? "relative" : "sticky" }}
colSpan={2}
>
{currentExtend}
</td>
</tr>
);
};
6 changes: 3 additions & 3 deletions src/components/DiffSplitLine.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DiffFile } from "../diff";
import { DiffFile, DiffFileExtends } from "../diff";
import { DiffLineType } from "../diff/diff-line";
import { useSyncHeight } from "../hooks/useSyncHeight";
import { DiffAddWidget } from "./DiffAddWidget";
import { DiffSplitAddWidget } from "./DiffAddWidget";
import { DiffContent } from "./DiffContent";
import { SplitSide } from "./DiffSplitView";
import {
Expand Down Expand Up @@ -96,7 +96,7 @@ export const DiffSplitLine = ({
color: `var(${plainLineNumberColorName})`,
}}
>
{hasDiff && <DiffAddWidget diffFile={diffFile} />}
{hasDiff && <DiffSplitAddWidget index={index} side={side} diffFile={diffFile as DiffFileExtends} />}
<span
data-line-num={currentItem.lineNumber}
style={{ opacity: hasChange ? undefined : 0.5 }}
Expand Down
28 changes: 22 additions & 6 deletions src/components/DiffSplitView.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Fragment, useEffect, useRef } from "react";
import debounce from "lodash/debounce";
import { DiffFile, numIterator } from "../diff";
import { DiffFile, DiffFileExtends, numIterator } from "../diff";
import { DiffSplitLine } from "./DiffSplitLine";
import {
DiffSplitExpandLastLine,
DiffSplitHunkLine,
} from "./DiffSplitHunkLine";
import { useDiffViewContext } from "./DiffViewContext";
import { DiffSplitExtendLine } from "./DiffSplitExtendLine";

export enum SplitSide {
old,
new,
}

const syncScroll = (left: HTMLElement, right: HTMLElement) => {
const onScroll = debounce(function (event: Event) {
const onScroll = function (event: Event) {
if (event === null || event.target === null) return;
if (event.target === left) {
right.scrollTop = left.scrollTop;
Expand All @@ -23,7 +23,7 @@ const syncScroll = (left: HTMLElement, right: HTMLElement) => {
left.scrollTop = right.scrollTop;
left.scrollLeft = right.scrollLeft;
}
});
};
if (!left.onscroll) {
left.onscroll = onScroll;
}
Expand Down Expand Up @@ -97,6 +97,14 @@ const DiffSplitViewTable = ({
isHighlight={isHighlight}
diffFile={diffFile}
/>
<DiffSplitExtendLine
index={index}
side={side}
isWrap={isWrap}
lineNumber={index + 1}
isHighlight={isHighlight}
diffFile={diffFile as DiffFileExtends}
/>
</Fragment>
))}
{showExpandLast && (
Expand Down Expand Up @@ -131,7 +139,11 @@ export const DiffSplitView = ({ diffFile }: { diffFile: DiffFile }) => {

return (
<div className="split-diff-view w-full flex basis-[50%]">
<div className="old-diff-table-wrapper overflow-auto w-full" ref={ref1}>
<div
className="old-diff-table-wrapper overflow-auto w-full"
ref={ref1}
style={{ overscrollBehaviorX: "none" }}
>
<DiffSplitViewTable
side={SplitSide.old}
isHighlight={isHighlight}
Expand All @@ -141,7 +153,11 @@ export const DiffSplitView = ({ diffFile }: { diffFile: DiffFile }) => {
/>
</div>
<div className="diff-split-line w-[1.5px] bg-[grey] opacity-[0.4]" />
<div className="new-diff-table-wrapper overflow-auto w-full" ref={ref2}>
<div
className="new-diff-table-wrapper overflow-auto w-full"
ref={ref2}
style={{ overscrollBehaviorX: "none" }}
>
<DiffSplitViewTable
side={SplitSide.new}
isHighlight={isHighlight}
Expand Down
32 changes: 32 additions & 0 deletions src/components/DiffUnifiedExtendLine.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DiffFileExtends } from "../diff";

export const DiffUnifiedExtendLine = ({
index,
diffFile,
isWrap,
}: {
index: number;
diffFile: DiffFileExtends;
lineNumber: number;
isWrap: boolean;
isHighlight: boolean;
}) => {
const currentExtend = diffFile.getUnifiedExtendLine(index);

if (!currentExtend) return null;

return (
<tr
data-state="extend"
className="diff-line diff-line-extend"
>
<td
className="diff-line-extend-content"
style={{ position: isWrap ? "relative" : "sticky" }}
colSpan={4}
>
{currentExtend}
</td>
</tr>
);
};
3 changes: 2 additions & 1 deletion src/components/DiffUnifiedHunkLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ export const DiffUnifiedExpandLastLine = ({
if (
!diffFile.unifiedLastStartIndex ||
!Number.isFinite(diffFile.unifiedLastStartIndex)
)
) {
return null;
}

return (
<tr
Expand Down
21 changes: 16 additions & 5 deletions src/components/DiffUnifiedLine.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DiffFile, SyntaxLine } from "../diff";
import { DiffFile, DiffFileExtends, SyntaxLine } from "../diff";
import { DiffLine } from "../diff/diff-line";
import { DiffAddWidget } from "./DiffAddWidget";
import { DiffUnifiedAddWidget } from "./DiffAddWidget";
import { DiffContent } from "./DiffContent";
import {
addContentBGName,
Expand Down Expand Up @@ -48,7 +48,10 @@ const DiffUnifiedOldLine = ({
backgroundColor: `var(${delLineNumberBGName})`,
}}
>
<DiffAddWidget diffFile={diffFile} />
<DiffUnifiedAddWidget
index={index - 1}
diffFile={diffFile as DiffFileExtends}
/>
<div className="flex">
<span data-line-num-old={lineNumber} className="inline-block w-[50%]">
{lineNumber}
Expand Down Expand Up @@ -106,7 +109,10 @@ const DiffUnifiedNewLine = ({
backgroundColor: `var(${addLineNumberBGName})`,
}}
>
<DiffAddWidget diffFile={diffFile} />
<DiffUnifiedAddWidget
index={index - 1}
diffFile={diffFile as DiffFileExtends}
/>
<div className="flex">
<span className="inline-block w-[50%]" />
<span className="shrink-0 w-[10px]" />
Expand Down Expand Up @@ -214,7 +220,12 @@ export const DiffUnifiedLine = ({
: `var(${expandContentBGName})`,
}}
>
{hasDiff && <DiffAddWidget diffFile={diffFile} />}
{hasDiff && (
<DiffUnifiedAddWidget
index={index}
diffFile={diffFile as DiffFileExtends}
/>
)}
<div className="flex opacity-[0.5]">
<span
data-line-num-old={unifiedLine.oldLineNumber}
Expand Down
10 changes: 9 additions & 1 deletion src/components/DiffUnifiedView.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Fragment } from "react";
import { DiffFile, numIterator } from "../diff";
import { DiffFile, DiffFileExtends, numIterator } from "../diff";
import { DiffUnifiedLine } from "./DiffUnifiedLine";
import {
DiffUnifiedExpandLastLine,
DiffUnifiedHunkLine,
} from "./DiffUnifiedHunkLine";
import { useDiffViewContext } from "./DiffViewContext";
import { DiffUnifiedExtendLine } from "./DiffUnifiedExtendLine";

export const DiffUnifiedView = ({ diffFile }: { diffFile: DiffFile }) => {
const { isWrap, isHighlight } = useDiffViewContext();
Expand Down Expand Up @@ -49,6 +50,13 @@ export const DiffUnifiedView = ({ diffFile }: { diffFile: DiffFile }) => {
isWrap={isWrap}
diffFile={diffFile}
/>
<DiffUnifiedExtendLine
index={index}
lineNumber={index + 1}
isHighlight={isHighlight}
isWrap={isWrap}
diffFile={diffFile as DiffFileExtends}
/>
</Fragment>
))}
<DiffUnifiedExpandLastLine
Expand Down
14 changes: 12 additions & 2 deletions src/components/DiffView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useState,
useSyncExternalStore,
} from "react";
import { DiffFileData, parseDiffFile } from "../diff";
import { DiffFileData, DiffFileExtends } from "../diff";
import { useDiffConfig } from "../hooks/useDiffConfig";
import { DiffSplitView } from "./DiffSplitView";
import { DiffUnifiedView } from "./DiffUnifiedView";
Expand Down Expand Up @@ -68,7 +68,17 @@ export const DiffView = ({ data }: { data: DiffFileData }) => {
// @ts-ignore
const [newList, setNewList] = useState<Record<number, ReactNode[]>>({});

const diffFile = useMemo(() => parseDiffFile(data), [data]);
const diffFile = useMemo(
() =>
new DiffFileExtends(
data.oldFile?.filePath || "",
data.oldFile?.content || "",
data.newFile?.filePath || "",
data.newFile?.content || "",
data.hunks.map(({ patchContent }) => patchContent)
),
[data]
);

useSyncExternalStore(diffFile.subscribe, diffFile.getUpdateCount);

Expand Down
Loading

0 comments on commit 0d2cb56

Please sign in to comment.