schangxiang@126.com
2025-09-10 3d43ffa3152110b7823f9fa6320c08a6ae02358a
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
/* eslint-disable */
var selectable = {
  bind: function (el, binding, vnode) {
    var rect = {
      el: null,
      isMove: false,
      from: {
        x: 0,
        y: 0
      },
      to: {
        x: 0,
        y: 0
      },
      init() {
        this.el = document.createElement('div');
        el.append(this.el);
        this.el.style.left = this.from.x + "px";
        this.el.style.top = this.from.y + "px";
        this.el.setAttribute("class", "selectable");
      },
      move() {
        var width = this.to.x - this.from.x;
        var height = this.to.y - this.from.y;
 
        if (width >= 0) {
          this.el.style.left = this.from.x + "px";
        } else {
          this.el.style.left = this.to.x + "px";
        }
 
        if (height >= 0) {
          this.el.style.top = this.from.y + "px";
        } else {
          this.el.style.top = this.to.y + "px";
        }
 
        this.el.style.width = Math.abs(width) + 'px';
        this.el.style.height = Math.abs(height) + 'px';
 
        this.selectItems();
      },
      remove() {
        if (this.el) {
          this.el.remove();
        }
      },
      selectItems() {
        document.querySelectorAll('.resable-item').forEach(item => {
          if (this.isIntersection(this.el, item)) {
            item.classList.add('selected');
          } else {
            item.classList.remove('selected');
          }
        })
      },
      // 判断两个矩形是否存在交集
      isIntersection(rect1, rect2) {
        let maxX, maxY, minX, minY;
 
        rect1.width = parseInt(rect1.style.width);
        rect1.height = parseInt(rect1.style.height);
        rect2.width = parseInt(rect2.style.width);
        rect2.height = parseInt(rect2.style.height);
 
        maxX = rect1.offsetLeft + rect1.width >= rect2.offsetLeft + rect2.width ? rect1.offsetLeft + rect1.width : rect2.offsetLeft + rect2.width;
        maxY = rect1.offsetTop + rect1.height >= rect2.offsetTop + rect2.height ? rect1.offsetTop + rect1.height : rect2.offsetTop + rect2.height;
        minX = rect1.offsetLeft <= rect2.offsetLeft ? rect1.offsetLeft : rect2.offsetLeft;
        minY = rect1.offsetTop <= rect2.offsetTop ? rect1.offsetTop : rect2.offsetTop;
        // console.log('rect1.offsetLeft=' + rect1.offsetLeft + ", rect1.offsetTop=" + rect1.offsetTop + ', rect2.offsetLeft=' + rect2.offsetLeft + ", rect2.offsetTop = " + rect2.offsetTop);
        // console.log('rect1.width=' + rect1.width + ", rect1.height=" + rect1.height + ', rect2.width=' + rect2.width + ", rect2.height = " + rect2.height);
 
        if (maxX - minX <= rect1.width + rect2.width && maxY - minY <= rect1.height + rect2.height) {
          return true;
        } else {
          return false;
        }
      }
    }
    document.oncontextmenu = new Function('event.returnValue=false;');
    document.onselectstart = new Function('event.returnValue=false;');
    el.onmousedown = function (e) {
      rect.from.x = e.offsetX;
      rect.from.y = e.offsetY;
      rect.to.x = e.offsetX;
      rect.to.y = e.offsetY;
      rect.isMove = true;
      rect.init();
      document.onmouseup = function (e) {
        rect.isMove = false;
        rect.remove();
      }
    }
    el.onmousemove = function (e) {
      if (rect.isMove) {
        rect.to.x += e.movementX;
        rect.to.y += e.movementY;
        // console.log("movementX=" + e.movementX + ", movementY=" + e.movementY)
        rect.move();
      }
    }
    document.onmousedown = function () {
      document.querySelectorAll('.resable-item').forEach(item => {
        item.classList.remove('selected');
      });
    }
  },
  update: function (el, binding, vnode) {
    var fields = binding.value;
    //  键盘操作
    document.onkeyup = function (e) {
      let dX = 0;
      let dY = 0;
      if (e.keyCode == 39) {
        dX = 1;
      } // 右键
      else if (e.keyCode == 37) {
        dX = -1;
      } // 左键
      else if (e.keyCode == 38) {
        dY = -1;
      } // 上键
      else if (e.keyCode == 40) {
        dY = 1;
      } // 下键
 
      document.querySelectorAll(".resable-item.selected").forEach(item => {
        let key = item.attributes["data-key"].value;
        let field = fields.find(item => {
          return item.key == key;
        });
        if (!field) return;
 
        field.options.x += dX;
        field.options.y += dY;
      });
    }
  }
}
 
export {
  selectable
}