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

143 lines
3.6 KiB
Vue
Raw Normal View History

2018-12-13 13:39:08 +05:30
<script>
import { mapActions, mapGetters } from 'vuex';
2020-04-08 14:13:33 +05:30
import { isArray } from 'lodash';
2019-07-31 22:56:46 +05:30
import imageDiffMixin from 'ee_else_ce/diffs/mixins/image_diff';
2020-11-24 15:15:51 +05:30
import { GlIcon } from '@gitlab/ui';
2018-12-13 13:39:08 +05:30
2021-02-22 17:27:13 +05:30
function calcPercent(pos, size, renderedSize) {
return (((pos / size) * 100) / ((renderedSize / size) * 100)) * 100;
}
2018-12-13 13:39:08 +05:30
export default {
name: 'ImageDiffOverlay',
components: {
2020-11-24 15:15:51 +05:30
GlIcon,
2018-12-13 13:39:08 +05:30
},
2019-07-31 22:56:46 +05:30
mixins: [imageDiffMixin],
2018-12-13 13:39:08 +05:30
props: {
discussions: {
type: [Array, Object],
required: true,
},
fileHash: {
type: String,
required: true,
},
canComment: {
type: Boolean,
required: false,
default: false,
},
showCommentIcon: {
type: Boolean,
required: false,
default: false,
},
badgeClass: {
type: String,
required: false,
default: 'badge badge-pill',
},
shouldToggleDiscussion: {
type: Boolean,
required: false,
default: true,
},
2021-02-22 17:27:13 +05:30
renderedWidth: {
type: Number,
required: true,
},
renderedHeight: {
type: Number,
required: true,
},
2018-12-13 13:39:08 +05:30
},
computed: {
...mapGetters('diffs', ['getDiffFileByHash', 'getCommentFormForDiffFile']),
currentCommentForm() {
return this.getCommentFormForDiffFile(this.fileHash);
},
allDiscussions() {
2020-04-08 14:13:33 +05:30
return isArray(this.discussions) ? this.discussions : [this.discussions];
2018-12-13 13:39:08 +05:30
},
},
methods: {
...mapActions('diffs', ['openDiffFileCommentForm']),
getImageDimensions() {
return {
width: this.$parent.width,
height: this.$parent.height,
};
},
getPositionForObject(meta) {
const { x, y, width, height } = meta;
return {
2021-02-22 17:27:13 +05:30
x: (x / width) * 100,
y: (y / height) * 100,
2018-12-13 13:39:08 +05:30
};
},
getPosition(discussion) {
const { x, y } = this.getPositionForObject(discussion.position);
return {
2021-02-22 17:27:13 +05:30
left: `${x}%`,
top: `${y}%`,
2018-12-13 13:39:08 +05:30
};
},
clickedImage(x, y) {
const { width, height } = this.getImageDimensions();
2021-02-22 17:27:13 +05:30
const xPercent = calcPercent(x, width, this.renderedWidth);
const yPercent = calcPercent(y, height, this.renderedHeight);
2018-12-13 13:39:08 +05:30
this.openDiffFileCommentForm({
fileHash: this.fileHash,
width,
height,
2021-02-22 17:27:13 +05:30
x: width * (xPercent / 100),
y: height * (yPercent / 100),
xPercent,
yPercent,
2018-12-13 13:39:08 +05:30
});
},
},
};
</script>
<template>
<div class="position-absolute w-100 h-100 image-diff-overlay">
<button
v-if="canComment"
type="button"
class="btn-transparent position-absolute image-diff-overlay-add-comment w-100 h-100 js-add-image-diff-note-button"
2019-03-02 22:35:43 +05:30
@click="clickedImage($event.offsetX, $event.offsetY)"
2018-12-13 13:39:08 +05:30
>
2019-02-15 15:39:39 +05:30
<span class="sr-only"> {{ __('Add image comment') }} </span>
2018-12-13 13:39:08 +05:30
</button>
<button
v-for="(discussion, index) in allDiscussions"
:key="discussion.id"
:style="getPosition(discussion)"
2019-07-31 22:56:46 +05:30
:class="[badgeClass, { 'is-draft': discussion.isDraft }]"
2018-12-13 13:39:08 +05:30
:disabled="!shouldToggleDiscussion"
class="js-image-badge"
type="button"
2019-07-31 22:56:46 +05:30
@click="clickedToggle(discussion)"
2018-12-13 13:39:08 +05:30
>
2021-02-22 17:27:13 +05:30
<gl-icon v-if="showCommentIcon" name="image-comment-dark" :size="24" />
2018-12-13 13:39:08 +05:30
<template v-else>
2019-07-31 22:56:46 +05:30
{{ toggleText(discussion, index) }}
2018-12-13 13:39:08 +05:30
</template>
</button>
<button
2021-02-22 17:27:13 +05:30
v-if="canComment && currentCommentForm"
:style="{ left: `${currentCommentForm.xPercent}%`, top: `${currentCommentForm.yPercent}%` }"
2018-12-13 13:39:08 +05:30
:aria-label="__('Comment form position')"
2021-02-22 17:27:13 +05:30
class="btn-transparent comment-indicator position-absolute"
2018-12-13 13:39:08 +05:30
type="button"
>
2021-02-22 17:27:13 +05:30
<gl-icon name="image-comment-dark" :size="24" />
2018-12-13 13:39:08 +05:30
</button>
</div>
</template>