debian-mirror-gitlab/app/assets/javascripts/diffs/components/diff_row.vue

458 lines
15 KiB
Vue
Raw Normal View History

2021-01-29 00:20:46 +05:30
<script>
2021-04-29 21:17:54 +05:30
/* eslint-disable vue/no-v-html */
import { GlTooltipDirective } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { mapActions, mapGetters, mapState } from 'vuex';
import { BV_HIDE_TOOLTIP } from '~/lib/utils/constants';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
2021-03-08 18:12:59 +05:30
import {
CONTEXT_LINE_CLASS_NAME,
PARALLEL_DIFF_VIEW_TYPE,
CONFLICT_MARKER_OUR,
CONFLICT_MARKER_THEIR,
CONFLICT_OUR,
CONFLICT_THEIR,
CONFLICT_MARKER,
} from '../constants';
2021-04-29 21:17:54 +05:30
import {
getInteropInlineAttributes,
getInteropOldSideAttributes,
getInteropNewSideAttributes,
} from '../utils/interoperability';
2021-01-29 00:20:46 +05:30
import DiffGutterAvatars from './diff_gutter_avatars.vue';
import * as utils from './diff_row_utils';
export default {
components: {
DiffGutterAvatars,
2021-09-04 01:27:46 +05:30
CodeQualityGutterIcon: () =>
import('ee_component/diffs/components/code_quality_gutter_icon.vue'),
2021-01-29 00:20:46 +05:30
},
directives: {
GlTooltip: GlTooltipDirective,
},
2021-03-08 18:12:59 +05:30
mixins: [glFeatureFlagsMixin()],
2021-01-29 00:20:46 +05:30
props: {
fileHash: {
type: String,
required: true,
},
filePath: {
type: String,
required: true,
},
line: {
type: Object,
required: true,
},
isCommented: {
type: Boolean,
required: false,
default: false,
},
inline: {
type: Boolean,
required: false,
default: false,
},
2021-03-08 18:12:59 +05:30
index: {
type: Number,
required: true,
},
},
data() {
return {
dragging: false,
};
2021-01-29 00:20:46 +05:30
},
computed: {
...mapGetters('diffs', ['fileLineCoverage']),
...mapGetters(['isLoggedIn']),
...mapState({
isHighlighted(state) {
const line = this.line.left?.line_code ? this.line.left : this.line.right;
2021-03-08 18:12:59 +05:30
return utils.isHighlighted(state, line, false);
2021-01-29 00:20:46 +05:30
},
}),
classNameMap() {
return {
[CONTEXT_LINE_CLASS_NAME]: this.line.isContextLineLeft,
2021-03-08 18:12:59 +05:30
[PARALLEL_DIFF_VIEW_TYPE]: !this.inline,
commented: this.isCommented,
2021-01-29 00:20:46 +05:30
};
},
parallelViewLeftLineType() {
2021-03-08 18:12:59 +05:30
return utils.parallelViewLeftLineType(this.line, this.isHighlighted || this.isCommented);
2021-01-29 00:20:46 +05:30
},
2021-03-08 18:12:59 +05:30
coverageStateLeft() {
if (!this.inline || !this.line.left) return {};
return this.fileLineCoverage(this.filePath, this.line.left.new_line);
},
coverageStateRight() {
if (!this.line.right) return {};
2021-01-29 00:20:46 +05:30
return this.fileLineCoverage(this.filePath, this.line.right.new_line);
},
2021-09-04 01:27:46 +05:30
showCodequalityLeft() {
return (
this.glFeatures.codequalityMrDiffAnnotations &&
this.inline &&
this.line.left?.codequality?.length > 0
);
},
showCodequalityRight() {
return (
this.glFeatures.codequalityMrDiffAnnotations &&
!this.inline &&
this.line.right?.codequality?.length > 0
);
},
2021-01-29 00:20:46 +05:30
classNameMapCellLeft() {
2021-03-08 18:12:59 +05:30
return utils.classNameMapCell({
line: this.line.left,
hll: this.isHighlighted || this.isCommented,
isLoggedIn: this.isLoggedIn,
});
2021-01-29 00:20:46 +05:30
},
classNameMapCellRight() {
2021-03-08 18:12:59 +05:30
return utils.classNameMapCell({
line: this.line.right,
hll: this.isHighlighted || this.isCommented,
isLoggedIn: this.isLoggedIn,
});
2021-01-29 00:20:46 +05:30
},
addCommentTooltipLeft() {
2021-03-11 19:13:27 +05:30
return utils.addCommentTooltip(this.line.left, this.glFeatures.dragCommentSelection);
2021-01-29 00:20:46 +05:30
},
addCommentTooltipRight() {
2021-03-11 19:13:27 +05:30
return utils.addCommentTooltip(this.line.right, this.glFeatures.dragCommentSelection);
2021-01-29 00:20:46 +05:30
},
2021-03-08 18:12:59 +05:30
emptyCellRightClassMap() {
return { conflict_their: this.line.left?.type === CONFLICT_OUR };
},
emptyCellLeftClassMap() {
return { conflict_our: this.line.right?.type === CONFLICT_THEIR };
},
2021-01-29 00:20:46 +05:30
shouldRenderCommentButton() {
2021-03-11 19:13:27 +05:30
return this.isLoggedIn && !this.line.isContextLineLeft && !this.line.isMetaLineLeft;
2021-01-29 00:20:46 +05:30
},
2021-03-08 18:12:59 +05:30
isLeftConflictMarker() {
return [CONFLICT_MARKER_OUR, CONFLICT_MARKER_THEIR].includes(this.line.left?.type);
},
2021-04-29 21:17:54 +05:30
interopLeftAttributes() {
if (this.inline) {
return getInteropInlineAttributes(this.line.left);
}
return getInteropOldSideAttributes(this.line.left);
},
interopRightAttributes() {
return getInteropNewSideAttributes(this.line.right);
},
2021-01-29 00:20:46 +05:30
},
mounted() {
this.scrollToLineIfNeededParallel(this.line);
},
methods: {
...mapActions('diffs', [
'scrollToLineIfNeededParallel',
'showCommentForm',
'setHighlightedRow',
'toggleLineDiscussions',
]),
// Prevent text selecting on both sides of parallel diff view
// Backport of the same code from legacy diff notes.
handleParallelLineMouseDown(e) {
const line = e.currentTarget;
const table = line.closest('.diff-table');
table.classList.remove('left-side-selected', 'right-side-selected');
2021-03-08 18:12:59 +05:30
const [lineClass] = ['left-side', 'right-side'].filter((name) =>
line.classList.contains(name),
);
2021-01-29 00:20:46 +05:30
if (lineClass) {
table.classList.add(`${lineClass}-selected`);
}
},
handleCommentButton(line) {
this.showCommentForm({ lineCode: line.line_code, fileHash: this.fileHash });
},
2021-03-08 18:12:59 +05:30
conflictText(line) {
return line.type === CONFLICT_MARKER_THEIR
? this.$options.THEIR_CHANGES
: this.$options.OUR_CHANGES;
},
onDragEnd() {
this.dragging = false;
if (!this.glFeatures.dragCommentSelection) return;
this.$emit('stopdragging');
},
onDragEnter(line, index) {
if (!this.glFeatures.dragCommentSelection) return;
this.$emit('enterdragging', { ...line, index });
},
onDragStart(line) {
2021-03-11 19:13:27 +05:30
this.$root.$emit(BV_HIDE_TOOLTIP);
2021-03-08 18:12:59 +05:30
this.dragging = true;
this.$emit('startdragging', line);
},
2021-01-29 00:20:46 +05:30
},
2021-03-08 18:12:59 +05:30
OUR_CHANGES: 'HEAD//our changes',
THEIR_CHANGES: 'origin//their changes',
CONFLICT_MARKER,
CONFLICT_MARKER_THEIR,
CONFLICT_OUR,
CONFLICT_THEIR,
2021-01-29 00:20:46 +05:30
};
</script>
<template>
<div :class="classNameMap" class="diff-grid-row diff-tr line_holder">
2021-03-08 18:12:59 +05:30
<div
data-testid="left-side"
class="diff-grid-left left-side"
2021-04-29 21:17:54 +05:30
v-bind="interopLeftAttributes"
2021-03-08 18:12:59 +05:30
@dragover.prevent
@dragenter="onDragEnter(line.left, index)"
@dragend="onDragEnd"
>
<template v-if="line.left && line.left.type !== $options.CONFLICT_MARKER">
2021-01-29 00:20:46 +05:30
<div
:class="classNameMapCellLeft"
2021-06-08 01:23:25 +05:30
data-testid="left-line-number"
2021-03-08 18:12:59 +05:30
class="diff-td diff-line-num"
2021-06-08 01:23:25 +05:30
data-qa-selector="new_diff_line_link"
2021-01-29 00:20:46 +05:30
>
2021-03-08 18:12:59 +05:30
<template v-if="!isLeftConflictMarker">
<span
2021-03-11 19:13:27 +05:30
v-if="shouldRenderCommentButton && !line.hasDiscussionsLeft"
2021-03-08 18:12:59 +05:30
v-gl-tooltip
class="add-diff-note tooltip-wrapper"
:title="addCommentTooltipLeft"
2021-01-29 00:20:46 +05:30
>
2021-06-08 01:23:25 +05:30
<div
data-testid="left-comment-button"
role="button"
tabindex="0"
:draggable="!line.left.commentsDisabled && glFeatures.dragCommentSelection"
2021-03-08 18:12:59 +05:30
type="button"
2021-06-08 01:23:25 +05:30
class="add-diff-note unified-diff-components-diff-note-button note-button js-add-diff-note-button"
2021-04-29 21:17:54 +05:30
data-qa-selector="diff_comment_button"
2021-03-08 18:12:59 +05:30
:class="{ 'gl-cursor-grab': dragging }"
:disabled="line.left.commentsDisabled"
2021-06-08 01:23:25 +05:30
:aria-disabled="line.left.commentsDisabled"
@click="!line.left.commentsDisabled && handleCommentButton(line.left)"
@keydown.enter="!line.left.commentsDisabled && handleCommentButton(line.left)"
@keydown.space="!line.left.commentsDisabled && handleCommentButton(line.left)"
@dragstart="!line.left.commentsDisabled && onDragStart({ ...line.left, index })"
></div>
2021-03-08 18:12:59 +05:30
</span>
</template>
2021-01-29 00:20:46 +05:30
<a
2021-03-08 18:12:59 +05:30
v-if="line.left.old_line && line.left.type !== $options.CONFLICT_THEIR"
2021-01-29 00:20:46 +05:30
:data-linenumber="line.left.old_line"
:href="line.lineHrefOld"
@click="setHighlightedRow(line.lineCode)"
>
</a>
<diff-gutter-avatars
v-if="line.hasDiscussionsLeft"
:discussions="line.left.discussions"
:discussions-expanded="line.left.discussionsExpanded"
2021-06-08 01:23:25 +05:30
data-testid="left-discussions"
2021-01-29 00:20:46 +05:30
@toggleLineDiscussions="
toggleLineDiscussions({
lineCode: line.left.line_code,
fileHash,
expanded: !line.left.discussionsExpanded,
})
"
/>
</div>
2021-03-08 18:12:59 +05:30
<div v-if="inline" :class="classNameMapCellLeft" class="diff-td diff-line-num">
2021-01-29 00:20:46 +05:30
<a
2021-03-08 18:12:59 +05:30
v-if="line.left.new_line && line.left.type !== $options.CONFLICT_OUR"
2021-02-22 17:27:13 +05:30
:data-linenumber="line.left.new_line"
2021-01-29 00:20:46 +05:30
:href="line.lineHrefOld"
@click="setHighlightedRow(line.lineCode)"
>
</a>
</div>
2021-03-08 18:12:59 +05:30
<div
v-gl-tooltip.hover
:title="coverageStateLeft.text"
:class="[...parallelViewLeftLineType, coverageStateLeft.class]"
class="diff-td line-coverage left-side"
></div>
2021-09-04 01:27:46 +05:30
<div class="diff-td line-codequality left-side" :class="[...parallelViewLeftLineType]">
<code-quality-gutter-icon
v-if="showCodequalityLeft"
:file-path="filePath"
:codequality="line.left.codequality"
/>
</div>
2021-01-29 00:20:46 +05:30
<div
:id="line.left.line_code"
:key="line.left.line_code"
2021-03-08 18:12:59 +05:30
:class="[parallelViewLeftLineType, { parallel: !inline }]"
class="diff-td line_content with-coverage left-side"
2021-06-08 01:23:25 +05:30
data-testid="left-content"
2021-01-29 00:20:46 +05:30
@mousedown="handleParallelLineMouseDown"
2021-03-08 18:12:59 +05:30
>
<strong v-if="isLeftConflictMarker">{{ conflictText(line.left) }}</strong>
2021-04-29 21:17:54 +05:30
<span v-else v-html="line.left.rich_text"></span>
2021-03-08 18:12:59 +05:30
</div>
2021-01-29 00:20:46 +05:30
</template>
2021-03-08 18:12:59 +05:30
<template v-else-if="!inline || (line.left && line.left.type === $options.CONFLICT_MARKER)">
<div
2021-06-08 01:23:25 +05:30
data-testid="left-empty-cell"
2021-03-08 18:12:59 +05:30
class="diff-td diff-line-num old_line empty-cell"
:class="emptyCellLeftClassMap"
>
&nbsp;
</div>
<div
v-if="inline"
class="diff-td diff-line-num old_line empty-cell"
:class="emptyCellLeftClassMap"
></div>
<div
class="diff-td line-coverage left-side empty-cell"
:class="emptyCellLeftClassMap"
></div>
2021-09-04 01:27:46 +05:30
<div
v-if="inline"
class="diff-td line-codequality left-side empty-cell"
:class="emptyCellLeftClassMap"
></div>
2021-03-08 18:12:59 +05:30
<div
class="diff-td line_content with-coverage left-side empty-cell"
:class="[emptyCellLeftClassMap, { parallel: !inline }]"
></div>
2021-01-29 00:20:46 +05:30
</template>
</div>
2021-03-08 18:12:59 +05:30
<div
v-if="!inline"
data-testid="right-side"
class="diff-grid-right right-side"
2021-04-29 21:17:54 +05:30
v-bind="interopRightAttributes"
2021-03-08 18:12:59 +05:30
@dragover.prevent
@dragenter="onDragEnter(line.right, index)"
@dragend="onDragEnd"
>
2021-01-29 00:20:46 +05:30
<template v-if="line.right">
2021-02-22 17:27:13 +05:30
<div :class="classNameMapCellRight" class="diff-td diff-line-num new_line">
2021-03-08 18:12:59 +05:30
<template v-if="line.right.type !== $options.CONFLICT_MARKER_THEIR">
<span
2021-03-11 19:13:27 +05:30
v-if="shouldRenderCommentButton && !line.hasDiscussionsRight"
2021-03-08 18:12:59 +05:30
v-gl-tooltip
class="add-diff-note tooltip-wrapper"
:title="addCommentTooltipRight"
2021-01-29 00:20:46 +05:30
>
2021-06-08 01:23:25 +05:30
<div
data-testid="right-comment-button"
role="button"
tabindex="0"
:draggable="!line.right.commentsDisabled && glFeatures.dragCommentSelection"
2021-03-08 18:12:59 +05:30
type="button"
2021-06-08 01:23:25 +05:30
class="add-diff-note unified-diff-components-diff-note-button note-button js-add-diff-note-button"
2021-03-08 18:12:59 +05:30
:class="{ 'gl-cursor-grab': dragging }"
:disabled="line.right.commentsDisabled"
2021-06-08 01:23:25 +05:30
:aria-disabled="line.right.commentsDisabled"
@click="!line.right.commentsDisabled && handleCommentButton(line.right)"
@keydown.enter="!line.right.commentsDisabled && handleCommentButton(line.right)"
@keydown.space="!line.right.commentsDisabled && handleCommentButton(line.right)"
@dragstart="!line.right.commentsDisabled && onDragStart({ ...line.right, index })"
></div>
2021-03-08 18:12:59 +05:30
</span>
</template>
2021-01-29 00:20:46 +05:30
<a
v-if="line.right.new_line"
:data-linenumber="line.right.new_line"
:href="line.lineHrefNew"
@click="setHighlightedRow(line.lineCode)"
>
</a>
<diff-gutter-avatars
v-if="line.hasDiscussionsRight"
:discussions="line.right.discussions"
:discussions-expanded="line.right.discussionsExpanded"
2021-06-08 01:23:25 +05:30
data-testid="right-discussions"
2021-01-29 00:20:46 +05:30
@toggleLineDiscussions="
toggleLineDiscussions({
lineCode: line.right.line_code,
fileHash,
expanded: !line.right.discussionsExpanded,
})
"
/>
</div>
<div
v-gl-tooltip.hover
2021-03-08 18:12:59 +05:30
:title="coverageStateRight.text"
:class="[
line.right.type,
coverageStateRight.class,
{ hll: isHighlighted, hll: isCommented },
]"
2021-01-29 00:20:46 +05:30
class="diff-td line-coverage right-side"
></div>
2021-09-04 01:27:46 +05:30
<div
class="diff-td line-codequality right-side"
:class="[line.right.type, { hll: isHighlighted, hll: isCommented }]"
>
<code-quality-gutter-icon
v-if="showCodequalityRight"
:file-path="filePath"
:codequality="line.right.codequality"
/>
</div>
2021-01-29 00:20:46 +05:30
<div
:id="line.right.line_code"
:key="line.right.rich_text"
:class="[
line.right.type,
{
hll: isHighlighted,
2021-03-08 18:12:59 +05:30
hll: isCommented,
parallel: !inline,
2021-01-29 00:20:46 +05:30
},
]"
2021-03-08 18:12:59 +05:30
class="diff-td line_content with-coverage right-side"
2021-01-29 00:20:46 +05:30
@mousedown="handleParallelLineMouseDown"
2021-03-08 18:12:59 +05:30
>
<strong v-if="line.right.type === $options.CONFLICT_MARKER_THEIR">{{
conflictText(line.right)
}}</strong>
2021-04-29 21:17:54 +05:30
<span v-else v-html="line.right.rich_text"></span>
2021-03-08 18:12:59 +05:30
</div>
2021-01-29 00:20:46 +05:30
</template>
<template v-else>
2021-03-08 18:12:59 +05:30
<div
2021-06-08 01:23:25 +05:30
data-testid="right-empty-cell"
2021-03-08 18:12:59 +05:30
class="diff-td diff-line-num old_line empty-cell"
:class="emptyCellRightClassMap"
></div>
<div
v-if="inline"
class="diff-td diff-line-num old_line empty-cell"
:class="emptyCellRightClassMap"
></div>
<div
class="diff-td line-coverage right-side empty-cell"
:class="emptyCellRightClassMap"
></div>
2021-09-04 01:27:46 +05:30
<div
class="diff-td line-codequality right-side empty-cell"
:class="emptyCellRightClassMap"
></div>
2021-03-08 18:12:59 +05:30
<div
class="diff-td line_content with-coverage right-side empty-cell"
:class="[emptyCellRightClassMap, { parallel: !inline }]"
></div>
2021-01-29 00:20:46 +05:30
</template>
</div>
</div>
</template>