summaryrefslogtreecommitdiffstats
path: root/browser/extensions/screenshots/build/selection.js
blob: db93dce72bea7e63e97dc1088cfd66c1387bdf28 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

this.selection = (function () {
  let exports = {};
  class Selection {
    constructor(x1, y1, x2, y2) {
      this.x1 = x1;
      this.y1 = y1;
      this.x2 = x2;
      this.y2 = y2;
    }

    get top() {
      return Math.min(this.y1, this.y2);
    }
    set top(val) {
      if (this.y1 < this.y2) {
        this.y1 = val;
      } else {
        this.y2 = val;
      }
    }

    get bottom() {
      return Math.max(this.y1, this.y2);
    }
    set bottom(val) {
      if (this.y1 > this.y2) {
        this.y1 = val;
      } else {
        this.y2 = val;
      }
    }

    get left() {
      return Math.min(this.x1, this.x2);
    }
    set left(val) {
      if (this.x1 < this.x2) {
        this.x1 = val;
      } else {
        this.x2 = val;
      }
    }

    get right() {
      return Math.max(this.x1, this.x2);
    }
    set right(val) {
      if (this.x1 > this.x2) {
        this.x1 = val;
      } else {
        this.x2 = val;
      }
    }

    get width() {
      return Math.abs(this.x2 - this.x1);
    }
    get height() {
      return Math.abs(this.y2 - this.y1);
    }

    rect() {
      return {
        top: Math.floor(this.top),
        left: Math.floor(this.left),
        bottom: Math.floor(this.bottom),
        right: Math.floor(this.right),
      };
    }

    union(other) {
      return new Selection(
        Math.min(this.left, other.left),
        Math.min(this.top, other.top),
        Math.max(this.right, other.right),
        Math.max(this.bottom, other.bottom)
      );
    }

    /** Sort x1/x2 and y1/y2 so x1<x2, y1<y2 */
    sortCoords() {
      if (this.x1 > this.x2) {
        [this.x1, this.x2] = [this.x2, this.x1];
      }
      if (this.y1 > this.y2) {
        [this.y1, this.y2] = [this.y2, this.y1];
      }
    }

    clone() {
      return new Selection(this.x1, this.y1, this.x2, this.y2);
    }

    toJSON() {
      return {
        left: this.left,
        right: this.right,
        top: this.top,
        bottom: this.bottom,
      };
    }

    static getBoundingClientRect(el) {
      if (!el.getBoundingClientRect) {
        // Typically the <html> element or somesuch
        return null;
      }
      const rect = el.getBoundingClientRect();
      if (!rect) {
        return null;
      }
      return new Selection(rect.left, rect.top, rect.right, rect.bottom);
    }
  }

  if (typeof exports !== "undefined") {
    exports.Selection = Selection;
  }

  return exports;
})();
null;