All files / app/assets/javascripts/diffs/components image_diff_overlay.vue

8.33% Statements 1/12
100% Branches 0/0
12.5% Functions 1/8
8.33% Lines 1/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                2x                                                                                                                                                                                                                                                                            
<script>
import { isArray } from 'lodash';
// eslint-disable-next-line no-restricted-imports
import { mapActions, mapGetters } from 'vuex';
import imageDiffMixin from 'ee_else_ce/diffs/mixins/image_diff';
import DesignNotePin from '~/vue_shared/components/design_management/design_note_pin.vue';
 
function calcPercent(pos, renderedSize) {
  return (100 * pos) / renderedSize;
}
 
export default {
  name: 'ImageDiffOverlay',
  components: {
    DesignNotePin,
  },
  mixins: [imageDiffMixin],
  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: '',
    },
    shouldToggleDiscussion: {
      type: Boolean,
      required: false,
      default: true,
    },
    renderedWidth: {
      type: Number,
      required: true,
    },
    renderedHeight: {
      type: Number,
      required: true,
    },
  },
  computed: {
    ...mapGetters('diffs', ['getDiffFileByHash', 'getCommentFormForDiffFile']),
    currentCommentForm() {
      return this.getCommentFormForDiffFile(this.fileHash);
    },
    allDiscussions() {
      return isArray(this.discussions) ? this.discussions : [this.discussions];
    },
  },
  methods: {
    ...mapActions('diffs', ['openDiffFileCommentForm']),
    getImageDimensions() {
      return {
        width: Math.round(this.$parent.width),
        height: Math.round(this.$parent.height),
      };
    },
    getPositionForObject(meta) {
      const { x, y, width, height } = meta;
 
      return {
        x: (x / width) * 100,
        y: (y / height) * 100,
      };
    },
    getPosition(discussion) {
      const { x, y } = this.getPositionForObject(discussion.position);
 
      return {
        left: `${x}%`,
        top: `${y}%`,
      };
    },
    clickedImage(x, y) {
      const { width, height } = this.getImageDimensions();
      const xPercent = calcPercent(x, this.renderedWidth);
      const yPercent = calcPercent(y, this.renderedHeight);
 
      this.openDiffFileCommentForm({
        fileHash: this.fileHash,
        width,
        height,
        x: Math.round(width * (xPercent / 100)),
        y: Math.round(height * (yPercent / 100)),
        xPercent,
        yPercent,
      });
    },
  },
};
</script>
 
<template>
  <div class="position-absolute gl-w-full gl-h-full image-diff-overlay">
    <button
      v-if="canComment"
      type="button"
      class="btn-transparent position-absolute image-diff-overlay-add-comment gl-w-full gl-h-full js-add-image-diff-note-button"
      @click="clickedImage($event.offsetX, $event.offsetY)"
    >
      <span class="sr-only"> {{ __('Add image comment') }} </span>
    </button>
 
    <design-note-pin
      v-for="(discussion, index) in allDiscussions"
      :key="discussion.id"
      :label="showCommentIcon ? null : toggleText(discussion, index)"
      :position="getPosition(discussion)"
      :aria-label="__('Show comments')"
      class="js-image-badge"
      :class="badgeClass"
      :is-draft="discussion.isDraft"
      :is-resolved="discussion.resolved"
      is-on-image
      :disabled="!shouldToggleDiscussion"
      @click="clickedToggle(discussion)"
    />
 
    <design-note-pin
      v-if="canComment && currentCommentForm"
      :position="/* eslint-disable @gitlab/vue-no-new-non-primitive-in-template */ {
        left: `${currentCommentForm.xPercent}%`,
        top: `${currentCommentForm.yPercent}%`,
      } /* eslint-enable @gitlab/vue-no-new-non-primitive-in-template */"
    />
  </div>
</template>