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

142 lines
3.5 KiB
Vue
Raw Normal View History

2018-12-13 13:39:08 +05:30
<script>
2020-04-08 14:13:33 +05:30
import { isArray } from 'lodash';
2021-03-11 19:13:27 +05:30
import { mapActions, mapGetters } from 'vuex';
2019-07-31 22:56:46 +05:30
import imageDiffMixin from 'ee_else_ce/diffs/mixins/image_diff';
2022-04-04 11:22:00 +05:30
import DesignNotePin from '~/vue_shared/components/design_management/design_note_pin.vue';
2018-12-13 13:39:08 +05:30
2022-03-02 08:16:31 +05:30
function calcPercent(pos, renderedSize) {
return (100 * pos) / renderedSize;
2021-02-22 17:27:13 +05:30
}
2018-12-13 13:39:08 +05:30
export default {
name: 'ImageDiffOverlay',
components: {
2022-04-04 11:22:00 +05:30
DesignNotePin,
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,
2022-04-04 11:22:00 +05:30
default: '',
2018-12-13 13:39:08 +05:30
},
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 {
2022-03-02 08:16:31 +05:30
width: Math.round(this.$parent.width),
height: Math.round(this.$parent.height),
2018-12-13 13:39:08 +05:30
};
},
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();
2022-03-02 08:16:31 +05:30
const xPercent = calcPercent(x, this.renderedWidth);
const yPercent = calcPercent(y, this.renderedHeight);
2018-12-13 13:39:08 +05:30
this.openDiffFileCommentForm({
fileHash: this.fileHash,
width,
height,
2022-03-02 08:16:31 +05:30
x: Math.round(width * (xPercent / 100)),
y: Math.round(height * (yPercent / 100)),
2021-02-22 17:27:13 +05:30
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>
2022-04-04 11:22:00 +05:30
<design-note-pin
2018-12-13 13:39:08 +05:30
v-for="(discussion, index) in allDiscussions"
:key="discussion.id"
2022-04-04 11:22:00 +05:30
:label="showCommentIcon ? null : toggleText(discussion, index)"
:position="getPosition(discussion)"
2021-04-29 21:17:54 +05:30
:aria-label="__('Show comments')"
2022-04-04 11:22:00 +05:30
class="js-image-badge"
:class="badgeClass"
:is-draft="discussion.isDraft"
:is-resolved="discussion.resolved"
is-on-image
:disabled="!shouldToggleDiscussion"
2019-07-31 22:56:46 +05:30
@click="clickedToggle(discussion)"
2022-04-04 11:22:00 +05:30
/>
<design-note-pin
2021-02-22 17:27:13 +05:30
v-if="canComment && currentCommentForm"
2022-04-04 11:22:00 +05:30
:position="{
left: `${currentCommentForm.xPercent}%`,
top: `${currentCommentForm.yPercent}%`,
}"
/>
2018-12-13 13:39:08 +05:30
</div>
</template>