Skip to content

Commit

Permalink
vue component almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Jan 23, 2024
1 parent 974da75 commit 48d3ebb
Show file tree
Hide file tree
Showing 11 changed files with 689 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/react/src/components/DiffViewContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { DiffViewProps, SplitSide } from "..";
import type { DiffFileExtends } from "../utils";

export enum DiffModeEnum {
Split,
Unified,
Split = 1,
Unified = 2,
}

export type DiffViewContextProps<T = any> = {
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/src/components/DiffAddWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const DiffUnifiedAddWidget = ({
diffFile: DiffFileExtends;
lineNumber: number;
side: SplitSide;
onWidgetClick?: (lineNumber: number, side: SplitSide) => void;
onWidgetClick?: (event: "onAddWidgetClick", lineNumber: number, side: SplitSide) => void;
}) => {
return (
<div
Expand All @@ -72,7 +72,7 @@ export const DiffUnifiedAddWidget = ({
}}
onClick={() => {
diffFile.onOpenAddWidget(lineNumber, side);
onWidgetClick?.(lineNumber, side);
onWidgetClick?.("onAddWidgetClick", lineNumber, side);
}}
>
+
Expand Down
25 changes: 15 additions & 10 deletions packages/vue/src/components/DiffSplitLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DiffLineType, type DiffFile } from "@git-diff-view/core";
import { computed, defineComponent, ref } from "vue";

import { useEnableAddWidget, useEnableHighlight, useEnableWrap, useOnAddWidgetClick } from "../context";
import { useForceUpdate } from "../hooks/useForceUpdate";
import { useSubscribeDiffFile } from "../hooks/useSubscribeDiffFile";
import { useSyncHeight } from "../hooks/useSyncHeight";

Expand All @@ -25,11 +24,17 @@ export const DiffSplitLine = defineComponent(

const currentItem = ref(props.side === SplitSide.old ? props.diffFile.getSplitLeftLine(props.index) : props.diffFile.getSplitRightLine(props.index));

const currentItemHasDiff = ref(!!currentItem.value.diff);
const currentItemHasDiff = ref(!!currentItem.value?.diff);

const currentItemHasChange = ref(currentItem.value?.diff?.isIncludeableLine());

const currentSyntaxItem = ref(props.side === SplitSide.old ? props.diffFile.getOldSyntaxLine(currentItem.value.lineNumber) : props.diffFile.getNewSyntaxLine(currentItem.value.lineNumber));
const currentItemHasHidden = ref(currentItem.value?.isHidden);

const currentSyntaxItem = ref(
props.side === SplitSide.old
? props.diffFile.getOldSyntaxLine(currentItem.value?.lineNumber)
: props.diffFile.getNewSyntaxLine(currentItem.value?.lineNumber)
);

useSubscribeDiffFile(
props,
Expand All @@ -38,14 +43,16 @@ export const DiffSplitLine = defineComponent(

useSubscribeDiffFile(
props,
(diffFile) => (currentSyntaxItem.value = props.side === SplitSide.old ? diffFile.getOldSyntaxLine(currentItem.value.lineNumber) : diffFile.getNewSyntaxLine(currentItem.value.lineNumber))
(diffFile) =>
(currentSyntaxItem.value =
props.side === SplitSide.old ? diffFile.getOldSyntaxLine(currentItem.value?.lineNumber) : diffFile.getNewSyntaxLine(currentItem.value?.lineNumber))
);

useSubscribeDiffFile(props, () => (currentItemHasDiff.value = !!currentItem.value.diff));
useSubscribeDiffFile(props, () => (currentItemHasDiff.value = !!currentItem.value?.diff));

useSubscribeDiffFile(props, () => (currentItemHasChange.value = currentItem.value.diff?.isIncludeableLine()));
useSubscribeDiffFile(props, () => (currentItemHasHidden.value = currentItem.value?.isHidden));

const count = useForceUpdate(props);
useSubscribeDiffFile(props, () => (currentItemHasChange.value = currentItem.value?.diff?.isIncludeableLine()));

const lineSelector = computed(() => `tr[data-line="${props.lineNumber}"]`);

Expand All @@ -58,9 +65,7 @@ export const DiffSplitLine = defineComponent(
});

return () => {
count.value;

if (currentItem.value?.isHidden) return null;
if (currentItemHasHidden.value) return null;

const isAdded = currentItem.value?.diff?.type === DiffLineType.Add;

Expand Down
71 changes: 71 additions & 0 deletions packages/vue/src/components/DiffUnifiedExtendLine.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { computed, defineComponent, ref } from "vue";

import { SplitSide } from "..";
import { useExtendData, useSlots } from "../context";
import { useDomWidth } from "../hooks/useDomWidth";
import { useSubscribeDiffFile } from "../hooks/useSubscribeDiffFile";

import type { DiffFileExtends } from "../utils";

export const DiffUnifiedExtendLine = defineComponent(
(props: { index: number; diffFile: DiffFileExtends; lineNumber: number }) => {
const extendData = useExtendData();

const slots = useSlots();

const unifiedItem = ref(props.diffFile.getUnifiedLine(props.index));

const oldExtend = ref(extendData.value?.oldFile?.[unifiedItem.value.oldLineNumber]);

const newExtend = ref(extendData.value?.newFile?.[unifiedItem.value.newLineNumber]);

useSubscribeDiffFile(props, (diffFile) => {
unifiedItem.value = diffFile.getUnifiedLine(props.index);
});

useSubscribeDiffFile(props, () => (oldExtend.value = extendData.value?.oldFile?.[unifiedItem.value.oldLineNumber]));

useSubscribeDiffFile(props, () => (newExtend.value = extendData.value?.newFile?.[unifiedItem.value.newLineNumber]));

const currentIsShow = computed(() =>
Boolean((oldExtend.value || newExtend.value) && unifiedItem.value && !unifiedItem.value.isHidden && unifiedItem.value.diff)
);

const width = useDomWidth({
selector: ref(".unified-diff-table-wrapper"),
enable: currentIsShow,
});

return () => {
if (!currentIsShow.value) return null;

return (
<tr data-line={`${props.lineNumber}-extend`} data-state="extend" class="diff-line diff-line-extend">
<td class="diff-line-extend-content align-top p-0" colspan={4}>
<div class="diff-line-extend-wrapper sticky left-0" style={{ width: width.value + "px" }}>
{width.value > 0 &&
oldExtend.value &&
slots.extend({
diffFile: props.diffFile,
side: SplitSide.old,
lineNumber: unifiedItem.value.oldLineNumber,
data: oldExtend.value.data,
onUpdate: props.diffFile.notifyAll,
})}
{width.value > 0 &&
newExtend.value &&
slots.extend({
diffFile: props.diffFile,
side: SplitSide.new,
lineNumber: unifiedItem.value.newLineNumber,
data: newExtend.value.data,
onUpdate: props.diffFile.notifyAll,
})}
</div>
</td>
</tr>
);
};
},
{ name: "DiffUnifiedExtendLine", props: ["diffFile", "index", "lineNumber"] }
);
136 changes: 136 additions & 0 deletions packages/vue/src/components/DiffUnifiedHunkLine.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { composeLen } from "@git-diff-view/core";
import { defineComponent, ref } from "vue";

import { useEnableWrap } from "../context";
import { useSubscribeDiffFile } from "../hooks/useSubscribeDiffFile";

import { hunkContentBGName, hunkLineNumberBGName, plainLineNumberColorName } from "./color";
import { ExpandAll, ExpandDown, ExpandUp } from "./DiffExpand";

import type { DiffFile } from "@git-diff-view/core";

export const DiffUnifiedHunkLine = defineComponent(
(props: { index: number; diffFile: DiffFile; lineNumber: number }) => {
const currentHunk = ref(props.diffFile.getUnifiedHunkLine(props.index));

const enableWrap = useEnableWrap();

const currentIsShow = ref(currentHunk.value && currentHunk.value.unifiedInfo.startHiddenIndex < currentHunk.value.unifiedInfo.endHiddenIndex);

const currentIsEnableAll = ref(
currentHunk.value && currentHunk.value.unifiedInfo.endHiddenIndex - currentHunk.value.unifiedInfo.startHiddenIndex < composeLen
);

useSubscribeDiffFile(props, (diffFile) => (currentHunk.value = diffFile.getUnifiedHunkLine(props.index)));

useSubscribeDiffFile(
props,
() => (currentIsShow.value = currentHunk.value && currentHunk.value.unifiedInfo.startHiddenIndex < currentHunk.value.unifiedInfo.endHiddenIndex)
);

useSubscribeDiffFile(
props,
() =>
(currentIsEnableAll.value =
currentHunk.value && currentHunk.value.unifiedInfo.endHiddenIndex - currentHunk.value.unifiedInfo.startHiddenIndex < composeLen)
);

return () => {
if (!currentIsShow.value) return null;

return (
<tr
data-line={`${props.lineNumber}-hunk`}
data-state="hunk"
class="diff-line diff-line-hunk select-none"
style={{ backgroundColor: `var(${hunkContentBGName})` }}
>
<td
class="diff-line-num diff-line-num-hunk left-0 text-left select-none w-[1%] min-w-[60px] whitespace-nowrap z-[1]"
style={{
position: enableWrap.value ? "relative" : "sticky",
backgroundColor: `var(${hunkLineNumberBGName})`,
color: `var(${plainLineNumberColorName})`,
}}
>
{currentIsEnableAll.value ? (
<button
class="w-full hover:bg-blue-300 flex justify-center items-center py-[6px] cursor-pointer rounded-[2px]"
title="Expand All"
onClick={() => props.diffFile.onUnifiedHunkExpand("all", props.index)}
>
<ExpandAll className="fill-current" />
</button>
) : (
<>
<button
class="w-full hover:bg-blue-300 flex justify-center items-center py-[2px] cursor-pointer rounded-[2px]"
title="Expand Down"
onClick={() => props.diffFile.onUnifiedHunkExpand("down", props.index)}
>
<ExpandDown className="fill-current" />
</button>
<button
class="w-full hover:bg-blue-300 flex justify-center items-center py-[2px] cursor-pointer rounded-[2px]"
title="Expand Up"
onClick={() => props.diffFile.onUnifiedHunkExpand("up", props.index)}
>
<ExpandUp className="fill-current" />
</button>
</>
)}
</td>
<td class="diff-line-content diff-line-content-hunk pr-[10px] align-middle">
<div
class="opacity-[0.5] pl-[1.5em]"
style={{
whiteSpace: enableWrap.value ? "pre-wrap" : "pre",
wordBreak: enableWrap.value ? "break-all" : "initial",
}}
>
{currentHunk.value.unifiedInfo.plainText}
</div>
</td>
</tr>
);
};
},
{ name: "DiffUnifiedHunkLine", props: ["index", "diffFile", "lineNumber"] }
);

export const DiffUnifiedExpandLastLine = defineComponent(
(props: { diffFile: DiffFile }) => {
const currentIsShow = ref(props.diffFile.getNeedShowExpandAll("unified"));

const enableWrap = useEnableWrap();

useSubscribeDiffFile(props, (diffFile) => (currentIsShow.value = diffFile.getNeedShowExpandAll("unified")));

return () => {
if (!currentIsShow.value) return null;

return (
<tr data-line="last-hunk" data-state="hunk" class="diff-line diff-line-hunk select-none" style={{ backgroundColor: `var(${hunkContentBGName})` }}>
<td
class="diff-line-num diff-line-num-hunk left-0 text-left select-none w-[1%] min-w-[60px] whitespace-nowrap"
style={{
position: enableWrap.value ? "relative" : "sticky",
backgroundColor: `var(${hunkLineNumberBGName})`,
color: `var(${plainLineNumberColorName})`,
}}
>
<button
class="w-full hover:bg-blue-300 flex justify-center items-center py-[2px] cursor-pointer rounded-[2px]"
title="Expand Down"
onClick={() => props.diffFile.onUnifiedLastExpand()}
>
<ExpandDown className="fill-current" />
</button>
</td>
<td class="diff-line-content diff-line-content-hunk pr-[10px] align-middle" />
</tr>
);
};
},
{ name: "DiffUnifiedExpandLastLine", props: ["diffFile"] }
);
Loading

0 comments on commit 48d3ebb

Please sign in to comment.