-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-DiffView.txt
More file actions
208 lines (202 loc) · 7.26 KB
/
diff-DiffView.txt
File metadata and controls
208 lines (202 loc) · 7.26 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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,4 +1,4 @@
-import { Fragment, useMemo } from "react";
+import { useMemo, useState } from "react";
import {
useTokenizeWorker,
type FileData,
@@ -7,25 +7,144 @@ import {
Decoration,
Hunk,
parseDiff,
+ getCollapsedLinesCountBetween,
+ textLinesToHunk,
+ insertHunk,
} from "react-diff-view";
import { Diff } from "react-diff-view";
import type { RenderToken } from "react-diff-view";
import { getDefaultTokenizeWorker } from "./tokenizeDefaultDiff";
import styles from "./DiffFile.module.css";
import type { ReactNode } from "react";
+import { getLines, lines } from "../baseFile";
+import type { ExtendedChange } from "./types";
const getFileExtension = (filePath: string) => {
return filePath.split(".").pop() ?? "";
};
-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;
+};
const defaultRenderToken: RenderToken = (token, defaultRender, i) => {
if (token.type === "indent-guide") {
@@ -73,36 +192,36 @@ export const DiffView = ({
() => tokenizeWorkerProp ?? getDefaultTokenizeWorker(),
[tokenizeWorkerProp]
);
+
+ const { oldRevision, newRevision, type, hunks } = file;
+ const [diffHunks, setHunks] = useState(hunks);
+
const tokenizePayload = useMemo(
() => ({
- hunks: file.hunks ?? "",
+ hunks: diffHunks ?? "",
oldSource: null,
language: getFileExtension(file.newPath ?? ""),
enableComments,
}),
- [file.hunks, file.newPath, enableComments]
+ [diffHunks, file.newPath, enableComments]
);
const { tokens } = useTokenizeWorker(tokenizeWorker, tokenizePayload);
- if (!file) {
- return null;
- }
-
- const { oldRevision, newRevision, type, hunks } = file;
+ console.log("DIFF HUNKS===", diffHunks);
return (
<Diff
key={`${oldRevision}-${newRevision}`}
viewType={viewType}
diffType={type}
- hunks={hunks}
+ hunks={diffHunks}
tokens={tokens}
renderToken={renderToken ?? defaultRenderToken}
optimizeSelection
className={styles.diff}
widgets={widgets}
>
- {renderDiffChildren}
+ {(diffHunks) => renderDiffChildren(diffHunks, setHunks)}
</Diff>
);
};