@scm-manager/ui-components 2.31.0 → 2.31.2-20220121-143434

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.
@@ -21,499 +21,17 @@
21
21
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
  * SOFTWARE.
23
23
  */
24
- import React from "react";
25
- import { withTranslation, WithTranslation } from "react-i18next";
26
- import classNames from "classnames";
27
- import styled from "styled-components";
28
- // @ts-ignore
29
- import { Decoration, getChangeKey, Hunk } from "react-diff-view";
30
- import { ButtonGroup } from "../buttons";
31
- import Tag from "../Tag";
32
- import Icon from "../Icon";
33
- import { Change, FileDiff, Hunk as HunkType } from "@scm-manager/ui-types";
34
- import { ChangeEvent, DiffObjectProps } from "./DiffTypes";
35
- import TokenizedDiffView from "./TokenizedDiffView";
36
- import DiffButton from "./DiffButton";
37
- import { MenuContext, OpenInFullscreenButton } from "@scm-manager/ui-components";
38
- import DiffExpander, { ExpandableHunk } from "./DiffExpander";
39
- import HunkExpandLink from "./HunkExpandLink";
40
- import { Modal } from "../modals";
41
- import ErrorNotification from "../ErrorNotification";
42
- import HunkExpandDivider from "./HunkExpandDivider";
43
- import { escapeWhitespace } from "./diffs";
44
24
 
45
- const EMPTY_ANNOTATION_FACTORY = {};
25
+ import React, { FC, Suspense } from "react";
26
+ import { DiffFileProps } from "./LazyDiffFile";
27
+ import Loading from "../Loading";
46
28
 
47
- type Props = DiffObjectProps &
48
- WithTranslation & {
49
- file: FileDiff;
50
- };
29
+ const LazyDiffFile = React.lazy(() => import("./LazyDiffFile"));
51
30
 
52
- type Collapsible = {
53
- collapsed?: boolean;
54
- };
31
+ const DiffFile: FC<DiffFileProps> = props => (
32
+ <Suspense fallback={<Loading />}>
33
+ <LazyDiffFile {...props} />
34
+ </Suspense>
35
+ );
55
36
 
56
- type State = Collapsible & {
57
- file: FileDiff;
58
- sideBySide?: boolean;
59
- diffExpander: DiffExpander;
60
- expansionError?: any;
61
- };
62
-
63
- const DiffFilePanel = styled.div`
64
- /* remove bottom border for collapsed panels */
65
- ${(props: Collapsible) => (props.collapsed ? "border-bottom: none;" : "")};
66
- `;
67
-
68
- const FullWidthTitleHeader = styled.div`
69
- max-width: 100%;
70
- `;
71
-
72
- const MarginlessModalContent = styled.div`
73
- margin: -1.25rem;
74
-
75
- & .panel-block {
76
- flex-direction: column;
77
- align-items: stretch;
78
- }
79
- `;
80
-
81
- class DiffFile extends React.Component<Props, State> {
82
- static defaultProps: Partial<Props> = {
83
- defaultCollapse: false,
84
- markConflicts: true
85
- };
86
-
87
- constructor(props: Props) {
88
- super(props);
89
- this.state = {
90
- collapsed: this.defaultCollapse(),
91
- sideBySide: props.sideBySide,
92
- diffExpander: new DiffExpander(props.file),
93
- file: props.file
94
- };
95
- }
96
-
97
- componentDidUpdate(prevProps: Readonly<Props>) {
98
- if (!this.props.isCollapsed && this.props.defaultCollapse !== prevProps.defaultCollapse) {
99
- this.setState({
100
- collapsed: this.defaultCollapse()
101
- });
102
- }
103
- }
104
-
105
- defaultCollapse: () => boolean = () => {
106
- const { defaultCollapse, file } = this.props;
107
- if (typeof defaultCollapse === "boolean") {
108
- return defaultCollapse;
109
- } else if (typeof defaultCollapse === "function") {
110
- return defaultCollapse(file.oldPath, file.newPath);
111
- } else {
112
- return false;
113
- }
114
- };
115
-
116
- toggleCollapse = () => {
117
- const { onCollapseStateChange } = this.props;
118
- const { file } = this.state;
119
- if (this.hasContent(file)) {
120
- if (onCollapseStateChange) {
121
- onCollapseStateChange(file);
122
- } else {
123
- this.setState(state => ({
124
- collapsed: !state.collapsed
125
- }));
126
- }
127
- }
128
- };
129
-
130
- toggleSideBySide = (callback: () => void) => {
131
- this.setState(
132
- state => ({
133
- sideBySide: !state.sideBySide
134
- }),
135
- () => callback()
136
- );
137
- };
138
-
139
- setCollapse = (collapsed: boolean) => {
140
- const { onCollapseStateChange } = this.props;
141
- if (onCollapseStateChange) {
142
- onCollapseStateChange(this.state.file, collapsed);
143
- } else {
144
- this.setState({
145
- collapsed
146
- });
147
- }
148
- };
149
-
150
- createHunkHeader = (expandableHunk: ExpandableHunk) => {
151
- if (expandableHunk.maxExpandHeadRange > 0) {
152
- if (expandableHunk.maxExpandHeadRange <= 10) {
153
- return (
154
- <HunkExpandDivider>
155
- <HunkExpandLink
156
- icon={"fa-angle-double-up"}
157
- onClick={this.expandHead(expandableHunk, expandableHunk.maxExpandHeadRange)}
158
- text={this.props.t("diff.expandComplete", { count: expandableHunk.maxExpandHeadRange })}
159
- />
160
- </HunkExpandDivider>
161
- );
162
- } else {
163
- return (
164
- <HunkExpandDivider>
165
- <HunkExpandLink
166
- icon={"fa-angle-up"}
167
- onClick={this.expandHead(expandableHunk, 10)}
168
- text={this.props.t("diff.expandByLines", { count: 10 })}
169
- />{" "}
170
- <HunkExpandLink
171
- icon={"fa-angle-double-up"}
172
- onClick={this.expandHead(expandableHunk, expandableHunk.maxExpandHeadRange)}
173
- text={this.props.t("diff.expandComplete", { count: expandableHunk.maxExpandHeadRange })}
174
- />
175
- </HunkExpandDivider>
176
- );
177
- }
178
- }
179
- // hunk header must be defined
180
- return <span />;
181
- };
182
-
183
- createHunkFooter = (expandableHunk: ExpandableHunk) => {
184
- if (expandableHunk.maxExpandBottomRange > 0) {
185
- if (expandableHunk.maxExpandBottomRange <= 10) {
186
- return (
187
- <HunkExpandDivider>
188
- <HunkExpandLink
189
- icon={"fa-angle-double-down"}
190
- onClick={this.expandBottom(expandableHunk, expandableHunk.maxExpandBottomRange)}
191
- text={this.props.t("diff.expandComplete", { count: expandableHunk.maxExpandBottomRange })}
192
- />
193
- </HunkExpandDivider>
194
- );
195
- } else {
196
- return (
197
- <HunkExpandDivider>
198
- <HunkExpandLink
199
- icon={"fa-angle-down"}
200
- onClick={this.expandBottom(expandableHunk, 10)}
201
- text={this.props.t("diff.expandByLines", { count: 10 })}
202
- />{" "}
203
- <HunkExpandLink
204
- icon={"fa-angle-double-down"}
205
- onClick={this.expandBottom(expandableHunk, expandableHunk.maxExpandBottomRange)}
206
- text={this.props.t("diff.expandComplete", { count: expandableHunk.maxExpandBottomRange })}
207
- />
208
- </HunkExpandDivider>
209
- );
210
- }
211
- }
212
- // hunk footer must be defined
213
- return <span />;
214
- };
215
-
216
- createLastHunkFooter = (expandableHunk: ExpandableHunk) => {
217
- if (expandableHunk.maxExpandBottomRange !== 0) {
218
- return (
219
- <HunkExpandDivider>
220
- <HunkExpandLink
221
- icon={"fa-angle-down"}
222
- onClick={this.expandBottom(expandableHunk, 10)}
223
- text={this.props.t("diff.expandLastBottomByLines", { count: 10 })}
224
- />{" "}
225
- <HunkExpandLink
226
- icon={"fa-angle-double-down"}
227
- onClick={this.expandBottom(expandableHunk, expandableHunk.maxExpandBottomRange)}
228
- text={this.props.t("diff.expandLastBottomComplete")}
229
- />
230
- </HunkExpandDivider>
231
- );
232
- }
233
- // hunk header must be defined
234
- return <span />;
235
- };
236
-
237
- expandHead = (expandableHunk: ExpandableHunk, count: number) => {
238
- return () => {
239
- return expandableHunk
240
- .expandHead(count)
241
- .then(this.diffExpanded)
242
- .catch(this.diffExpansionFailed);
243
- };
244
- };
245
-
246
- expandBottom = (expandableHunk: ExpandableHunk, count: number) => {
247
- return () => {
248
- return expandableHunk
249
- .expandBottom(count)
250
- .then(this.diffExpanded)
251
- .catch(this.diffExpansionFailed);
252
- };
253
- };
254
-
255
- diffExpanded = (newFile: FileDiff) => {
256
- this.setState({ file: newFile, diffExpander: new DiffExpander(newFile) });
257
- };
258
-
259
- diffExpansionFailed = (err: any) => {
260
- this.setState({ expansionError: err });
261
- };
262
-
263
- collectHunkAnnotations = (hunk: HunkType) => {
264
- const { annotationFactory } = this.props;
265
- const { file } = this.state;
266
- if (annotationFactory) {
267
- return annotationFactory({
268
- hunk,
269
- file
270
- });
271
- } else {
272
- return EMPTY_ANNOTATION_FACTORY;
273
- }
274
- };
275
-
276
- handleClickEvent = (change: Change, hunk: HunkType) => {
277
- const { onClick } = this.props;
278
- const { file } = this.state;
279
- const context = {
280
- changeId: getChangeKey(change),
281
- change,
282
- hunk,
283
- file
284
- };
285
- if (onClick) {
286
- onClick(context);
287
- }
288
- };
289
-
290
- createGutterEvents = (hunk: HunkType) => {
291
- const { onClick } = this.props;
292
- if (onClick) {
293
- return {
294
- onClick: (event: ChangeEvent) => {
295
- this.handleClickEvent(event.change, hunk);
296
- }
297
- };
298
- }
299
- };
300
-
301
- renderHunk = (file: FileDiff, expandableHunk: ExpandableHunk, i: number) => {
302
- const hunk = expandableHunk.hunk;
303
- if (this.props.markConflicts && hunk.changes) {
304
- this.markConflicts(hunk);
305
- }
306
- const items = [];
307
- if (file._links?.lines) {
308
- items.push(this.createHunkHeader(expandableHunk));
309
- } else if (i > 0) {
310
- items.push(
311
- <Decoration>
312
- <hr className="my-2" />
313
- </Decoration>
314
- );
315
- }
316
-
317
- items.push(
318
- <Hunk
319
- key={"hunk-" + hunk.content}
320
- hunk={expandableHunk.hunk}
321
- widgets={this.collectHunkAnnotations(hunk)}
322
- gutterEvents={this.createGutterEvents(hunk)}
323
- className={this.props.hunkClass ? this.props.hunkClass(hunk) : null}
324
- />
325
- );
326
- if (file._links?.lines) {
327
- if (i === file.hunks!.length - 1) {
328
- items.push(this.createLastHunkFooter(expandableHunk));
329
- } else {
330
- items.push(this.createHunkFooter(expandableHunk));
331
- }
332
- }
333
- return items;
334
- };
335
-
336
- markConflicts = (hunk: HunkType) => {
337
- let inConflict = false;
338
- for (let i = 0; i < hunk.changes.length; ++i) {
339
- if (hunk.changes[i].content === "<<<<<<< HEAD") {
340
- inConflict = true;
341
- }
342
- if (inConflict) {
343
- hunk.changes[i].type = "conflict";
344
- }
345
- if (hunk.changes[i].content.startsWith(">>>>>>>")) {
346
- inConflict = false;
347
- }
348
- }
349
- };
350
-
351
- getAnchorId(file: FileDiff) {
352
- let path: string;
353
- if (file.type === "delete") {
354
- path = file.oldPath;
355
- } else {
356
- path = file.newPath;
357
- }
358
- return escapeWhitespace(path);
359
- }
360
-
361
- renderFileTitle = (file: FileDiff) => {
362
- const { t } = this.props;
363
- if (file.oldPath !== file.newPath && (file.type === "copy" || file.type === "rename")) {
364
- return (
365
- <>
366
- {file.oldPath} <Icon name="arrow-right" color="inherit" alt={t("diff.renamedTo")} /> {file.newPath}
367
- </>
368
- );
369
- } else if (file.type === "delete") {
370
- return file.oldPath;
371
- }
372
- return file.newPath;
373
- };
374
-
375
- hoverFileTitle = (file: FileDiff): string => {
376
- if (file.oldPath !== file.newPath && (file.type === "copy" || file.type === "rename")) {
377
- return `${file.oldPath} > ${file.newPath}`;
378
- } else if (file.type === "delete") {
379
- return file.oldPath;
380
- }
381
- return file.newPath;
382
- };
383
-
384
- renderChangeTag = (file: FileDiff) => {
385
- const { t } = this.props;
386
- if (!file.type) {
387
- return;
388
- }
389
- const key = "diff.changes." + file.type;
390
- let value = t(key);
391
- if (key === value) {
392
- value = file.type;
393
- }
394
-
395
- const color = value === "added" ? "success" : value === "deleted" ? "danger" : "info";
396
- return (
397
- <Tag
398
- className={classNames("has-text-weight-normal", "ml-3")}
399
- rounded={true}
400
- outlined={true}
401
- color={color}
402
- label={value}
403
- />
404
- );
405
- };
406
-
407
- isCollapsed = () => {
408
- const { file, isCollapsed } = this.props;
409
- if (isCollapsed) {
410
- return isCollapsed(file);
411
- }
412
- return this.state.collapsed;
413
- };
414
-
415
- hasContent = (file: FileDiff) => file && !file.isBinary && file.hunks && file.hunks.length > 0;
416
-
417
- render() {
418
- const { fileControlFactory, fileAnnotationFactory, t } = this.props;
419
- const { file, sideBySide, diffExpander, expansionError } = this.state;
420
- const viewType = sideBySide ? "split" : "unified";
421
- const collapsed = this.isCollapsed();
422
-
423
- const fileAnnotations = fileAnnotationFactory ? fileAnnotationFactory(file) : null;
424
- const innerContent = (
425
- <div className="panel-block p-0">
426
- {fileAnnotations}
427
- <TokenizedDiffView className={viewType} viewType={viewType} file={file}>
428
- {(hunks: HunkType[]) =>
429
- hunks?.map((hunk, n) => {
430
- return this.renderHunk(file, diffExpander.getHunk(n), n);
431
- })
432
- }
433
- </TokenizedDiffView>
434
- </div>
435
- );
436
- let icon = <Icon name="angle-right" color="inherit" alt={t("diff.showContent")} />;
437
- let body = null;
438
- if (!collapsed) {
439
- icon = <Icon name="angle-down" color="inherit" alt={t("diff.hideContent")} />;
440
- body = innerContent;
441
- }
442
- const collapseIcon = this.hasContent(file) ? icon : null;
443
- const fileControls = fileControlFactory ? fileControlFactory(file, this.setCollapse) : null;
444
- const modalTitle = file.type === "delete" ? file.oldPath : file.newPath;
445
- const openInFullscreen = file?.hunks?.length ? (
446
- <OpenInFullscreenButton
447
- modalTitle={modalTitle}
448
- modalBody={<MarginlessModalContent>{innerContent}</MarginlessModalContent>}
449
- />
450
- ) : null;
451
- const sideBySideToggle = file?.hunks?.length && (
452
- <MenuContext.Consumer>
453
- {({ setCollapsed }) => (
454
- <DiffButton
455
- icon={sideBySide ? "align-left" : "columns"}
456
- tooltip={t(sideBySide ? "diff.combined" : "diff.sideBySide")}
457
- onClick={() =>
458
- this.toggleSideBySide(() => {
459
- if (this.state.sideBySide) {
460
- setCollapsed(true);
461
- }
462
- })
463
- }
464
- />
465
- )}
466
- </MenuContext.Consumer>
467
- );
468
- const headerButtons = (
469
- <div className={classNames("level-right", "is-flex", "ml-auto")}>
470
- <ButtonGroup>
471
- {sideBySideToggle}
472
- {openInFullscreen}
473
- {fileControls}
474
- </ButtonGroup>
475
- </div>
476
- );
477
-
478
- let errorModal;
479
- if (expansionError) {
480
- errorModal = (
481
- <Modal
482
- title={t("diff.expansionFailed")}
483
- closeFunction={() => this.setState({ expansionError: undefined })}
484
- body={<ErrorNotification error={expansionError} />}
485
- active={true}
486
- />
487
- );
488
- }
489
-
490
- return (
491
- <DiffFilePanel
492
- className={classNames("panel", "is-size-6")}
493
- collapsed={(file && file.isBinary) || collapsed}
494
- id={this.getAnchorId(file)}
495
- >
496
- {errorModal}
497
- <div className="panel-heading">
498
- <div className={classNames("level", "is-flex-wrap-wrap")}>
499
- <FullWidthTitleHeader
500
- className={classNames("level-left", "is-flex", "is-clickable")}
501
- onClick={this.toggleCollapse}
502
- title={this.hoverFileTitle(file)}
503
- >
504
- {collapseIcon}
505
- <h4 className={classNames("has-text-weight-bold", "is-ellipsis-overflow", "is-size-6", "ml-1")}>
506
- {this.renderFileTitle(file)}
507
- </h4>
508
- {this.renderChangeTag(file)}
509
- </FullWidthTitleHeader>
510
- {headerButtons}
511
- </div>
512
- </div>
513
- {body}
514
- </DiffFilePanel>
515
- );
516
- }
517
- }
518
-
519
- export default withTranslation("repos")(DiffFile);
37
+ export default DiffFile;