-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-DiffView-U1.txt
More file actions
186 lines (180 loc) · 6.63 KB
/
diff-DiffView-U1.txt
File metadata and controls
186 lines (180 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
diff --git a/src/DiffFile/DiffView.tsx b/src/DiffFile/DiffView.tsx
index fa97fa8..fd3d40c 100644
--- a/src/DiffFile/DiffView.tsx
+++ b/src/DiffFile/DiffView.tsx
@@ -1,2 +1,2 @@
-import { Fragment, useMemo } from "react";
+import { useMemo, useState } from "react";
import {
@@ -9,2 +9,5 @@ import {
parseDiff,
+ getCollapsedLinesCountBetween,
+ textLinesToHunk,
+ insertHunk,
} from "react-diff-view";
@@ -15,2 +18,4 @@ import styles from "./DiffFile.module.css";
import type { ReactNode } from "react";
+import { getLines, lines } from "../baseFile";
+import type { ExtendedChange } from "./types";
@@ -20,10 +25,124 @@ const getFileExtension = (filePath: string) => {
-const renderHunk = (hunk: HunkData) => (
- <Fragment key={hunk.content}>
- <Decoration className={styles.decoration}>{hunk.content}</Decoration>
- <Hunk key={hunk.content} hunk={hunk} />
- </Fragment>
-);
+type ExpandType = 'up' | 'down' | "up&down" | 'all' | "none";
-const renderDiffChildren = (hunks: HunkData[]) => hunks.map(renderHunk);
+const ExpandButton = ({expand, onClick}: {expand: ExpandType, onClick: (type?: "up" | "down") => void}) => {
+ if (expand === "up&down") {
+ return (<div className={styles.decorationGutterButtonsWrapper}>
+ <button style={{backgroundColor: 'rgb(133, 206, 255)'}} className={styles.decorationGutterButton} onClick={() => onClick("down")}>↓</button>
+ <button style={{backgroundColor: 'rgb(133, 206, 255)'}} className={styles.decorationGutterButton} onClick={() => onClick("up")}>↑</button>
+ </div>)
+ }
+ if (expand === "up") return <button style={{backgroundColor: 'rgb(133, 206, 255)'}} className={styles.decorationGutterButton} onClick={() => onClick()}>↑</button>;
+ if (expand === "down") return <button style={{backgroundColor: 'rgb(133, 206, 255)'}} className={styles.decorationGutterButton} onClick={() => onClick()}>↓</button>;
+ if (expand === "none") return null;
+ return <button style={{backgroundColor: 'rgb(133, 206, 255)'}} className={styles.decorationGutterButton} onClick={() => onClick()}>↓↑</button>;
+};
+
+const ExpandedDecoratedHunk = ({hunk, expand, onClick}: {hunk?: HunkData, expand: ExpandType, onClick?: (type?: "up" | "down") => void} ) => {
+ return (<>
+ <Decoration className={styles.decoration} gutterClassName={styles.decorationGutter} contentClassName={styles.decorationContent}>
+ <ExpandButton expand={expand} onClick={(type) => onClick?.(type)} />
+ {hunk && hunk.content}
+ </Decoration>
+ {hunk && <Hunk hunk={hunk} />}
+</>)
+}
+
+const TOTAL_NUMBER = lines.length - 1; // TODO: какое-то число от бэка нужно
+
+console.log("TOTAL_NUMBER: ", TOTAL_NUMBER, lines);
+
+const renderDiffChildren = (hunks: HunkData[], setHunks: (hunk: HunkData[]) => void) => {
+ const expand = async (startOld: number, endOld: number, startNew: number) => {
+ console.log("DIFF EXPAND REQUEST: ", startOld, endOld);
+
+ // Тип запрос
+ const getReponse = async () => {
+ await new Promise(resolve => setTimeout(resolve, 100));
+
+ return getLines(startOld, endOld);
+ }
+
+ const lines = await getReponse();
+
+ console.log(lines);
+
+ const newHunk = textLinesToHunk(lines, startOld, startNew);
+
+ if (newHunk) {
+ newHunk.changes.forEach(change => {
+ (change as ExtendedChange).isExpanded = true;
+ })
+
+ const nextHunks = insertHunk(hunks, newHunk)
+ setHunks(nextHunks);
+ };
+ };
+
+ const elements = hunks.reduce<React.ReactElement[]>((acc, hunk, ind) => {
+ if (ind === 0) {
+ if (hunk.oldStart > 1) {
+ const startOld = Math.max(1, hunk.oldStart - 20);
+ const endOld = hunk.oldStart - 1;
+
+ acc.push(
+ <ExpandedDecoratedHunk key={hunk.content} hunk={hunk} expand="up" onClick={() => expand(startOld, endOld, startOld)}/>
+ );
+ }
+ else {
+ acc.push(
+ <ExpandedDecoratedHunk key={hunk.content} hunk={hunk} expand="none" />
+ );
+ }
+ }
+
+ const prevHunk = hunks[ind - 1]
+
+ if (ind > 0 && prevHunk) {
+ const collapsedCount = getCollapsedLinesCountBetween(prevHunk, hunk);
+
+ if (collapsedCount > 0 && collapsedCount <= 20) {
+ const startOld = prevHunk.oldStart + prevHunk.oldLines;
+ const endOld = hunk.oldStart - 1;
+ const startNew = prevHunk.newStart + prevHunk.newLines;
+
+ acc.push(
+ <ExpandedDecoratedHunk key={hunk.content} hunk={hunk} expand="all" onClick={() => expand(startOld, endOld, startNew)} />
+ );
+ };
+
+ if (collapsedCount > 20) {
+ acc.push(
+ <ExpandedDecoratedHunk key={hunk.content} hunk={hunk} expand="up&down" onClick={(type) => {
+ if (type === "up") {
+ const startOld = hunk.oldStart - 20;
+ const endOld = hunk.oldStart - 1;
+ const startNew = hunk.newStart - (hunk.oldStart - startOld);
+
+ expand(startOld, endOld, startNew)};
+ if (type === "down") {
+ const startOld = prevHunk.oldStart + prevHunk.oldLines;
+ const endOld = startOld + 19;
+ const startNew = prevHunk.newStart + prevHunk.newLines;
+
+ expand(startOld, endOld, startNew)};
+ }} />
+ );
+ };
+ };
+
+ if (hunks.length - 1 === ind && hunk.oldStart + hunk.oldLines < TOTAL_NUMBER) {
+ const startOld = hunk.oldStart + hunk.oldLines;
+ const endOld = Math.min(startOld + 19, TOTAL_NUMBER);
+ const startNew = hunk.newStart + hunk.newLines;
+
+ acc.push(<ExpandedDecoratedHunk key={`${hunk.content}-down`} expand="down" onClick={() => expand(startOld, endOld, startNew)}/>)
+ }
+
+ return acc;
+ }, []);
+
+
+ return elements;
+};
@@ -75,5 +194,9 @@ export const DiffView = ({
);
+
+ const { oldRevision, newRevision, type, hunks } = file;
+ const [diffHunks, setHunks] = useState(hunks);
+
const tokenizePayload = useMemo(
() => ({
- hunks: file.hunks ?? "",
+ hunks: diffHunks ?? "",
oldSource: null,
@@ -82,3 +205,3 @@ export const DiffView = ({
}),
- [file.hunks, file.newPath, enableComments]
+ [diffHunks, file.newPath, enableComments]
);
@@ -86,7 +209,3 @@ export const DiffView = ({
- if (!file) {
- return null;
- }
-
- const { oldRevision, newRevision, type, hunks } = file;
+ console.log("DIFF HUNKS===", diffHunks);
@@ -97,3 +216,3 @@ export const DiffView = ({
diffType={type}
- hunks={hunks}
+ hunks={diffHunks}
tokens={tokens}
@@ -104,3 +223,3 @@ export const DiffView = ({
>
- {renderDiffChildren}
+ {(diffHunks) => renderDiffChildren(diffHunks, setHunks)}
</Diff>