-
Notifications
You must be signed in to change notification settings - Fork 0
/
addProgramEditting.js
193 lines (144 loc) · 4.69 KB
/
addProgramEditting.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { createListener } from "./createListener.js"
export function addProgramEditting(state) {
const listener = createListener(document.body);
let removed = false;
let fromToolbox = false;
let dragged = false;
let downTarget = null;
let initialX = null;
let initialY = null;
listener("pointerdown", "", e => {
downTarget = e.target;
initialX = e.clientX;
initialY = e.clientY;
})
listener("touchstart", ".box, .macro-name, .draggable-box", e => {
e.preventDefault();
}, { passive: false })
listener("pointerdown", ".box", (e) => {
const trigger = e.target;
const index = Number(trigger.dataset.index);
const data = state.boxes[index];
const shiftX = e.clientX - trigger.getBoundingClientRect().left;
const shiftY = e.clientY - trigger.getBoundingClientRect().top;
state.dragId = {
name: "",
index,
data,
shiftX,
shiftY
};
STATE.mouse.x = e.clientX;
STATE.mouse.y = e.clientY;
fromToolbox = true;
})
listener("pointerdown", ".macro-name", (e) => {
const trigger = e.target;
if (e.target.data === "main") return;
const data = {
data: {type: 'macro', value: e.target.data },
index: 0,
name: "",
shiftX: 0,
shiftY: 0,
color: [0,0]
}
state.dragId = data;
data.shiftX = e.clientX - trigger.getBoundingClientRect().left;
data.shiftY = e.clientY - trigger.getBoundingClientRect().top;
STATE.mouse.x = e.clientX;
STATE.mouse.y = e.clientY;
fromToolbox = true;
})
listener("pointerdown", ".draggable-box", (e) => {
const trigger = e.target;
const index = Number(trigger.dataset.index);
const name = trigger.dataset.programName;
const data = state.programs[name][index];
const shiftX = e.clientX - trigger.getBoundingClientRect().left;
const shiftY = e.clientY - trigger.getBoundingClientRect().top;
state.dragId = {
name,
index,
data,
shiftX,
shiftY
};
STATE.mouse.x = e.clientX;
STATE.mouse.y = e.clientY;
});
listener("pointermove", "", e => {
if (initialX === null || initialY === null) return;
const deltaX = event.clientX - initialX;
const deltaY = event.clientY - initialY;
if (removed) return;
if (fromToolbox) return;
if (state.dragId === null) return;
const { name, index } = state.dragId;
const targetArr = state.programs[name];
targetArr.splice(index, 1);
if (state.programs[name].length === 0 && name !== "main") delete state.programs[name];
removed = true;
dragged = true;
})
listener("pointerup", "", e => {
if (!removed && !fromToolbox) return;
if (state.dragId === null) return;
const els = elsAtLoc(e.clientX, e.clientY, ".draggable-box, .program-spacer-start, .program-spacer-end");
if (els.length === 0) return;
const el = els[0];
const hoverId = {
name: el.dataset.programName,
index: Number(el.dataset.index)
}
const targetArr = state.programs[hoverId.name];
insertAtIndex(targetArr, JSON.parse(JSON.stringify(state.dragId.data)), Number(el.dataset.index));
})
listener("pointerup", "", (e) => {
const deltaX = event.clientX - initialX;
const deltaY = event.clientY - initialY;
if (!downTarget.matches(".draggable-box")) return;
if (fromToolbox) return;
if (Math.abs(deltaX) > 3 || Math.abs(deltaY) > 3) return;
const { programName, index} = downTarget.dataset;
const value = state.programs[programName][index];
state.editor = value;
})
listener("pointerup", "", e => {
removed = false;
fromToolbox = false;
dragged = false;
state.dragId = null;
downTarget = null
})
}
function elsAtLoc(x, y, selector) {
const matchedElements = [];
const elements = document.querySelectorAll(selector);
for (const element of elements) {
const rect = element.getBoundingClientRect();
if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
matchedElements.push(element);
}
}
return matchedElements;
}
function insertAtIndex(array, value, index) {
// Check if index is valid
if (index < 0 || index > array.length) {
console.error("Invalid index");
return;
}
array.splice(index, 0, value);
}
function moveArrayElement(arr, sourceIndex, targetIndex) {
// Check if sourceIndex and targetIndex are valid
if (sourceIndex < 0 || sourceIndex >= arr.length || targetIndex < 0 || targetIndex >= arr.length) {
console.error("Invalid sourceIndex or targetIndex");
return;
}
// Remove the element from sourceIndex
const element = arr.splice(sourceIndex, 1)[0];
// Insert it back to the array at targetIndex
arr.splice(targetIndex, 0, element);
}