summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/diff_viewer/viewers/image_diff/onion_skin_viewer.vue
blob: efcc39197b09cf2c1a091ee16d485719349dbc34 (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
<script>
import { pixeliseValue } from '../../../lib/utils/dom_utils';
import ImageViewer from '../../../content_viewer/viewers/image_viewer.vue';

export default {
  components: {
    ImageViewer,
  },
  props: {
    newPath: {
      type: String,
      required: true,
    },
    oldPath: {
      type: String,
      required: true,
    },
    projectPath: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      onionMaxWidth: undefined,
      onionMaxHeight: undefined,
      onionOldImgInfo: null,
      onionNewImgInfo: null,
      onionDraggerPos: 0,
      onionOpacity: 1,
      dragging: false,
    };
  },
  computed: {
    onionMaxPixelWidth() {
      return pixeliseValue(this.onionMaxWidth);
    },
    onionMaxPixelHeight() {
      return pixeliseValue(this.onionMaxHeight);
    },
    onionDraggerPixelPos() {
      return pixeliseValue(this.onionDraggerPos);
    },
  },
  beforeDestroy() {
    document.body.removeEventListener('mouseup', this.stopDrag);
    this.$refs.dragger.removeEventListener('mousedown', this.startDrag);
  },
  methods: {
    dragMove(e) {
      if (!this.dragging) return;
      const left = e.pageX - this.$refs.dragTrack.getBoundingClientRect().left;
      const dragTrackWidth =
        this.$refs.dragTrack.clientWidth - this.$refs.dragger.clientWidth || 100;

      let leftValue = left;
      if (leftValue < 0) leftValue = 0;
      if (leftValue > dragTrackWidth) leftValue = dragTrackWidth;

      this.onionOpacity = left / dragTrackWidth;
      this.onionDraggerPos = leftValue;
    },
    startDrag() {
      this.dragging = true;
      document.body.style.userSelect = 'none';
      document.body.addEventListener('mousemove', this.dragMove);
    },
    stopDrag() {
      this.dragging = false;
      document.body.style.userSelect = '';
      document.body.removeEventListener('mousemove', this.dragMove);
    },
    prepareOnionSkin() {
      if (this.onionOldImgInfo && this.onionNewImgInfo) {
        this.onionMaxWidth = Math.max(
          this.onionOldImgInfo.renderedWidth,
          this.onionNewImgInfo.renderedWidth,
        );
        this.onionMaxHeight = Math.max(
          this.onionOldImgInfo.renderedHeight,
          this.onionNewImgInfo.renderedHeight,
        );

        this.onionOpacity = 1;
        this.onionDraggerPos =
          this.$refs.dragTrack.clientWidth - this.$refs.dragger.clientWidth || 100;

        document.body.addEventListener('mouseup', this.stopDrag);
      }
    },
    onionNewImgLoaded(imgInfo) {
      this.onionNewImgInfo = imgInfo;
      this.prepareOnionSkin();
    },
    onionOldImgLoaded(imgInfo) {
      this.onionOldImgInfo = imgInfo;
      this.prepareOnionSkin();
    },
  },
};
</script>

<template>
  <div class="onion-skin view">
    <div
      class="onion-skin-frame"
      :style="{
        'width': onionMaxPixelWidth,
        'height': onionMaxPixelHeight,
        'user-select': dragging === true ? 'none' : '',
    }">
      <div
        class="frame deleted"
        :style="{
          'width': onionMaxPixelWidth,
          'height': onionMaxPixelHeight,
      }">
        <image-viewer
          key="onionOldImg"
          :render-info="false"
          :path="oldPath"
          :project-path="projectPath"
          @imgLoaded="onionOldImgLoaded"
        />
      </div>
      <div
        class="added frame"
        ref="addedFrame"
        :style="{
          'opacity': onionOpacity,
          'width': onionMaxPixelWidth,
          'height': onionMaxPixelHeight,
      }">
        <image-viewer
          key="onionNewImg"
          :render-info="false"
          :path="newPath"
          :project-path="projectPath"
          @imgLoaded="onionNewImgLoaded"
        />
      </div>
      <div class="controls">
        <div class="transparent"></div>
        <div
          class="drag-track"
          ref="dragTrack"
          @mousedown="startDrag"
          @mouseup="stopDrag">
          <div
            class="dragger"
            ref="dragger"
            :style="{ 'left': onionDraggerPixelPos }">
          </div>
        </div>
        <div class="opaque"></div>
      </div>
    </div>
  </div>
</template>