@git-diff-view/react 0.0.30 → 0.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,338 +0,0 @@
1
- import { DiffLineType, type DiffFile, type DiffLine, type diffChanges, type SyntaxLine } from "@git-diff-view/core";
2
- import {
3
- addContentHighlightBGName,
4
- delContentHighlightBGName,
5
- diffFontSizeName,
6
- getSymbol,
7
- NewLineSymbol,
8
- } from "@git-diff-view/utils";
9
- import * as React from "react";
10
-
11
- import { getStyleObjectFromString } from "./DiffContent";
12
- import { DiffNoNewLine } from "./DiffNoNewLine";
13
-
14
- const RenderRange = ({
15
- str,
16
- startIndex,
17
- endIndex,
18
- ranges,
19
- isAdd,
20
- indexRef,
21
- }: {
22
- str: string;
23
- startIndex: number;
24
- endIndex: number;
25
- isAdd: boolean;
26
- ranges: ReturnType<typeof diffChanges>["addRange"]["range"];
27
- indexRef: { current: number };
28
- }) => {
29
- if (!str) return null;
30
-
31
- const index = indexRef.current;
32
-
33
- const range = ranges[index];
34
-
35
- if (!range || endIndex < range.location || range.location + range.length < startIndex) return str.replace("\n", "");
36
-
37
- const index1 = range.location - startIndex;
38
- const index2 = index1 < 0 ? 0 : index1;
39
- const str1 = str.slice(0, index2);
40
- const str2 = str.slice(index2, index1 + range.length);
41
- const str3 = str.slice(index1 + range.length);
42
- const isStart = str1.length || range.location === startIndex;
43
-
44
- const isEnd = str3.length || endIndex === range.location + range.length - 1;
45
-
46
- if (isEnd && str3.length) indexRef.current++;
47
-
48
- const isLast = str2.includes("\n");
49
-
50
- const _str2 = isLast ? str2.replace("\n", "").replace("\r", "") : str2;
51
-
52
- return (
53
- <span
54
- data-range-start={range.location}
55
- data-range-end={range.location + range.length}
56
- data-total={ranges.length}
57
- data-index={index}
58
- >
59
- <span data-start={startIndex} data-end={endIndex}>
60
- {str1}
61
- <span
62
- data-diff-highlight
63
- style={{
64
- backgroundColor: isAdd ? `var(${addContentHighlightBGName})` : `var(${delContentHighlightBGName})`,
65
- borderTopLeftRadius: isStart ? "0.2em" : undefined,
66
- borderBottomLeftRadius: isStart ? "0.2em" : undefined,
67
- borderTopRightRadius: isEnd || isLast ? "0.2em" : undefined,
68
- borderBottomRightRadius: isEnd || isLast ? "0.2em" : undefined,
69
- }}
70
- >
71
- {_str2}
72
- </span>
73
- {/* 可能会出现一个node跨越多个range的情况 */}
74
- {RenderRange({
75
- str: str3,
76
- startIndex: startIndex + str1.length + str2.length,
77
- endIndex,
78
- ranges,
79
- isAdd,
80
- indexRef,
81
- })}
82
- </span>
83
- </span>
84
- );
85
- };
86
-
87
- // TODO
88
- const DiffSyntax = ({
89
- rawLine,
90
- diffLine,
91
- operator,
92
- syntaxLine,
93
- enableWrap,
94
- }: {
95
- rawLine: string;
96
- diffLine?: DiffLine;
97
- syntaxLine?: SyntaxLine;
98
- operator?: "add" | "del";
99
- enableWrap?: boolean;
100
- }) => {
101
- if (!syntaxLine) {
102
- return <DiffString rawLine={rawLine} diffLine={diffLine} operator={operator} />;
103
- }
104
-
105
- const diffRange = diffLine?.diffChanges;
106
-
107
- if (diffRange?.hasLineChange) {
108
- const isAdd = operator === "add";
109
-
110
- const targetRange = diffRange.range.filter((i) => i.type === (isAdd ? 1 : -1));
111
-
112
- const isNewLineSymbolChanged = diffRange.newLineSymbol;
113
-
114
- const newLineSymbol = getSymbol(isNewLineSymbolChanged);
115
-
116
- const hasNoTrailingNewLine = isNewLineSymbolChanged === NewLineSymbol.NEWLINE;
117
-
118
- const rangeIndexRef = { current: 0 };
119
-
120
- return (
121
- <span className="diff-line-syntax-raw">
122
- {syntaxLine.nodeList?.map(({ node, wrapper }, index) => {
123
- const range = targetRange[rangeIndexRef.current];
124
- if (!range || node.endIndex < range.location || range.location + range.length < node.startIndex) {
125
- return (
126
- <span
127
- key={index}
128
- data-start={node.startIndex}
129
- data-end={node.endIndex}
130
- className={wrapper?.properties?.className?.join(" ")}
131
- style={getStyleObjectFromString(wrapper?.properties?.style || "")}
132
- >
133
- {node.value.replace("\n", "")}
134
- </span>
135
- );
136
- } else {
137
- return (
138
- <span
139
- key={index}
140
- data-start={node.startIndex}
141
- data-end={node.endIndex}
142
- className={wrapper?.properties?.className?.join(" ")}
143
- style={getStyleObjectFromString(wrapper?.properties?.style || "")}
144
- >
145
- {/* 将组件转换为函数调用,避免同级渲染时后续组件的index没有正确更新的问题 */}
146
- {RenderRange({
147
- str: node.value,
148
- startIndex: node.startIndex,
149
- endIndex: node.endIndex,
150
- ranges: targetRange,
151
- isAdd,
152
- indexRef: rangeIndexRef,
153
- })}
154
- </span>
155
- );
156
- }
157
- })}
158
-
159
- {newLineSymbol && (
160
- <span
161
- data-newline-symbol
162
- data-diff-highlight
163
- className="rounded-[0.2em]"
164
- style={{
165
- backgroundColor: isAdd ? `var(${addContentHighlightBGName})` : `var(${delContentHighlightBGName})`,
166
- }}
167
- >
168
- {newLineSymbol}
169
- </span>
170
- )}
171
-
172
- {hasNoTrailingNewLine && (
173
- <span
174
- data-no-newline-at-end-of-file-symbol
175
- className={enableWrap ? "block !text-red-500" : "inline-block align-middle !text-red-500"}
176
- style={{
177
- width: `var(${diffFontSizeName})`,
178
- height: `var(${diffFontSizeName})`,
179
- }}
180
- >
181
- <DiffNoNewLine />
182
- </span>
183
- )}
184
- </span>
185
- );
186
- }
187
-
188
- return (
189
- <span className="diff-line-syntax-raw">
190
- {syntaxLine?.nodeList?.map(({ node, wrapper }, index) => (
191
- <span
192
- key={index}
193
- data-start={node.startIndex}
194
- data-end={node.endIndex}
195
- className={wrapper?.properties?.className?.join(" ")}
196
- style={getStyleObjectFromString(wrapper?.properties?.style || "")}
197
- >
198
- {node.value}
199
- </span>
200
- ))}
201
- </span>
202
- );
203
- };
204
-
205
- // TODO
206
- const DiffString = ({
207
- rawLine,
208
- diffLine,
209
- operator,
210
- enableWrap,
211
- }: {
212
- rawLine: string;
213
- diffLine?: DiffLine;
214
- operator?: "add" | "del";
215
- enableWrap?: boolean;
216
- }) => {
217
- const diffRange = diffLine?.diffChanges;
218
-
219
- const isAdd = operator === "add";
220
-
221
- if (diffRange?.hasLineChange) {
222
- const targetRange = diffRange.range;
223
-
224
- const isNewLineSymbolChanged = diffRange.newLineSymbol;
225
-
226
- const newLineSymbol = getSymbol(isNewLineSymbolChanged);
227
-
228
- const hasNoTrailingNewLine = isNewLineSymbolChanged === NewLineSymbol.NEWLINE;
229
-
230
- return (
231
- <span className="diff-line-content-raw">
232
- {targetRange.map(({ type, str, location, length }, index) => {
233
- if (type === 0) {
234
- return <span key={index}>{str}</span>;
235
- } else {
236
- return (
237
- <span key={index} data-range-start={location} data-range-end={location + length}>
238
- <span
239
- data-diff-highlight
240
- className="rounded-[0.2em]"
241
- style={{
242
- backgroundColor: isAdd ? `var(${addContentHighlightBGName})` : `var(${delContentHighlightBGName})`,
243
- }}
244
- >
245
- {str}
246
- </span>
247
- </span>
248
- );
249
- }
250
- })}
251
-
252
- {newLineSymbol && (
253
- <span
254
- data-newline-symbol
255
- data-diff-highlight
256
- className="rounded-[0.2em]"
257
- style={{
258
- backgroundColor: isAdd ? `var(${addContentHighlightBGName})` : `var(${delContentHighlightBGName})`,
259
- }}
260
- >
261
- {newLineSymbol}
262
- </span>
263
- )}
264
-
265
- {hasNoTrailingNewLine && (
266
- <span
267
- data-no-newline-at-end-of-file-symbol
268
- className={enableWrap ? "block !text-red-500" : "inline-block align-middle !text-red-500"}
269
- style={{
270
- width: `var(${diffFontSizeName})`,
271
- height: `var(${diffFontSizeName})`,
272
- }}
273
- >
274
- <DiffNoNewLine />
275
- </span>
276
- )}
277
- </span>
278
- );
279
- } else {
280
- return <span className="diff-line-content-raw">{rawLine}</span>;
281
- }
282
- };
283
-
284
- // TODO
285
- export const DiffContent_v2 = ({
286
- diffLine,
287
- rawLine,
288
- syntaxLine,
289
- enableWrap,
290
- enableHighlight,
291
- }: {
292
- rawLine: string;
293
- syntaxLine?: SyntaxLine;
294
- diffLine?: DiffLine;
295
- diffFile: DiffFile;
296
- enableWrap: boolean;
297
- enableHighlight: boolean;
298
- }) => {
299
- const isAdded = diffLine?.type === DiffLineType.Add;
300
-
301
- const isDelete = diffLine?.type === DiffLineType.Delete;
302
-
303
- const isMaxLineLengthToIgnoreSyntax = syntaxLine?.nodeList?.length > 150;
304
-
305
- return (
306
- <div
307
- className="diff-line-content-item pl-[2.0em]"
308
- // data-val={rawLine}
309
- style={{
310
- whiteSpace: enableWrap ? "pre-wrap" : "pre",
311
- wordBreak: enableWrap ? "break-all" : "initial",
312
- }}
313
- >
314
- <span
315
- data-operator={isAdded ? "+" : isDelete ? "-" : undefined}
316
- className="diff-line-content-operator ml-[-1.5em] inline-block w-[1.5em] select-none indent-[0.2em]"
317
- >
318
- {isAdded ? "+" : isDelete ? "-" : " "}
319
- </span>
320
- {enableHighlight && syntaxLine && !isMaxLineLengthToIgnoreSyntax ? (
321
- <DiffSyntax
322
- operator={isAdded ? "add" : isDelete ? "del" : undefined}
323
- rawLine={rawLine}
324
- diffLine={diffLine}
325
- syntaxLine={syntaxLine}
326
- enableWrap={enableWrap}
327
- />
328
- ) : (
329
- <DiffString
330
- operator={isAdded ? "add" : isDelete ? "del" : undefined}
331
- rawLine={rawLine}
332
- diffLine={diffLine}
333
- enableWrap={enableWrap}
334
- />
335
- )}
336
- </div>
337
- );
338
- };