Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diff_row_utils.js « components « diffs « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a489c96b0c9574f18f6e34a5f4972d19bdba08c2 (plain)
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
import { __ } from '~/locale';
import {
  MATCH_LINE_TYPE,
  CONTEXT_LINE_TYPE,
  LINE_HOVER_CLASS_NAME,
  OLD_NO_NEW_LINE_TYPE,
  NEW_NO_NEW_LINE_TYPE,
  EMPTY_CELL_TYPE,
  CONFLICT_MARKER_OUR,
  CONFLICT_MARKER_THEIR,
  CONFLICT_THEIR,
  CONFLICT_OUR,
  EXPANDED_LINE_TYPE,
} from '../constants';

export const isHighlighted = (highlightedRow, line, isCommented) => {
  if (isCommented) return true;

  const lineCode = line?.line_code;
  return lineCode ? lineCode === highlightedRow : false;
};

export const isContextLine = (type) => type === CONTEXT_LINE_TYPE;

export const isMatchLine = (type) => type === MATCH_LINE_TYPE;

export const isMetaLine = (type) =>
  [OLD_NO_NEW_LINE_TYPE, NEW_NO_NEW_LINE_TYPE, EMPTY_CELL_TYPE].includes(type);

export const shouldRenderCommentButton = (isLoggedIn, isCommentButtonRendered) => {
  return isCommentButtonRendered && isLoggedIn;
};

export const hasDiscussions = (line) => line?.discussions?.length > 0;

export const lineHref = (line) => `#${line?.line_code || ''}`;

export const lineCode = (line) => {
  if (!line) return undefined;
  return line.line_code || line.left?.line_code || line.right?.line_code;
};

export const classNameMapCell = ({
  line,
  highlighted,
  commented,
  selectionStart,
  selectionEnd,
  isLoggedIn,
  isHover,
}) => {
  const classes = {
    'highlight-top': highlighted || selectionStart,
    'highlight-bottom': highlighted || selectionEnd,
    hll: highlighted,
    commented,
  };

  if (line) {
    const { type } = line;
    Object.assign(classes, {
      [type]: true,
      [LINE_HOVER_CLASS_NAME]: isLoggedIn && isHover && !isContextLine(type) && !isMetaLine(type),
      old_line: type === 'old',
      new_line: type === 'new',
    });
  }

  return [classes];
};

export const addCommentTooltip = (line) => {
  let tooltip;
  if (!line) {
    return tooltip;
  }

  tooltip = __('Add a comment to this line or drag for multiple lines');

  if (!line.problems) {
    return tooltip;
  }

  const { brokenSymlink, brokenLineCode, fileOnlyMoved } = line.problems;

  if (brokenSymlink) {
    if (brokenSymlink.wasSymbolic || brokenSymlink.isSymbolic) {
      tooltip = __(
        'Commenting on symbolic links that replace or are replaced by files is not supported',
      );
    } else if (brokenSymlink.wasReal || brokenSymlink.isReal) {
      tooltip = __(
        'Commenting on files that replace or are replaced by symbolic links is not supported',
      );
    }
  } else if (fileOnlyMoved) {
    tooltip = __('Commenting on files that are only moved or renamed is not supported');
  } else if (brokenLineCode) {
    tooltip = __('Commenting on this line is not supported');
  }

  return tooltip;
};

export const parallelViewLeftLineType = ({
  line,
  highlighted,
  commented,
  selectionStart,
  selectionEnd,
}) => {
  if (line?.right?.type === NEW_NO_NEW_LINE_TYPE) {
    return OLD_NO_NEW_LINE_TYPE;
  }

  const lineTypeClass = line?.left ? line.left.type : EMPTY_CELL_TYPE;

  return [
    lineTypeClass,
    {
      hll: highlighted,
      commented,
      'highlight-top': highlighted || selectionStart,
      'highlight-bottom': highlighted || selectionEnd,
    },
  ];
};

export const shouldShowCommentButton = (hover, context, meta, discussions) => {
  return hover && !context && !meta && !discussions;
};

export const mapParallel = (content) => (line) => {
  let { left, right } = line;

  // Dicussions/Comments
  const hasExpandedDiscussionOnLeft =
    left?.discussions?.length > 0 ? left?.discussionsExpanded : false;
  const hasExpandedDiscussionOnRight =
    right?.discussions?.length > 0 ? right?.discussionsExpanded : false;

  const renderCommentRow =
    hasExpandedDiscussionOnLeft || hasExpandedDiscussionOnRight || left?.hasForm || right?.hasForm;

  if (left) {
    left = {
      ...left,
      renderDiscussion: hasExpandedDiscussionOnLeft,
      hasDraft: content.hasParallelDraftLeft(content.diffFile.file_hash, line),
      lineDrafts: content.draftsForLine(content.diffFile.file_hash, line, 'left'),
      hasCommentForm: left.hasForm,
      isConflictMarker:
        line.left.type === CONFLICT_MARKER_OUR || line.left.type === CONFLICT_MARKER_THEIR,
      emptyCellClassMap: { conflict_our: line.right?.type === CONFLICT_THEIR },
      addCommentTooltip: addCommentTooltip(line.left),
    };
  }
  if (right) {
    right = {
      ...right,
      renderDiscussion: Boolean(
        hasExpandedDiscussionOnRight && right.type && right.type !== EXPANDED_LINE_TYPE,
      ),
      hasDraft: content.hasParallelDraftRight(content.diffFile.file_hash, line),
      lineDrafts: content.draftsForLine(content.diffFile.file_hash, line, 'right'),
      hasCommentForm: Boolean(right.hasForm && right.type && right.type !== EXPANDED_LINE_TYPE),
      emptyCellClassMap: { conflict_their: line.left?.type === CONFLICT_OUR },
      addCommentTooltip: addCommentTooltip(line.right),
    };
  }

  return {
    ...line,
    left,
    right,
    isMatchLineLeft: isMatchLine(left?.type),
    isMatchLineRight: isMatchLine(right?.type),
    isContextLineLeft: isContextLine(left?.type),
    isContextLineRight: isContextLine(right?.type),
    hasDiscussionsLeft: hasDiscussions(left),
    hasDiscussionsRight: hasDiscussions(right),
    lineHrefOld: lineHref(left),
    lineHrefNew: lineHref(right),
    lineCode: lineCode(line),
    isMetaLineLeft: isMetaLine(left?.type),
    isMetaLineRight: isMetaLine(right?.type),
    draftRowClasses: left?.hasDraft || right?.hasDraft ? '' : 'js-temp-notes-holder',
    renderCommentRow,
    commentRowClasses: hasDiscussions(left) || hasDiscussions(right) ? '' : 'js-temp-notes-holder',
  };
};