forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickTools.js
More file actions
401 lines (331 loc) · 9.71 KB
/
quickTools.js
File metadata and controls
401 lines (331 loc) · 9.71 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import "./style.scss";
import Page from "components/page";
import items, { description } from "components/quickTools/items";
import actionStack from "lib/actionStack";
import settings from "lib/settings";
import { hideAd } from "lib/startAd";
import helpers from "utils/helpers";
export default function QuickTools() {
const $page = Page(strings["shortcut buttons"]);
$page.id = "quicktools-settings-page";
$page.style.overflow = "hidden";
$page.style.display = "flex";
$page.style.flexDirection = "column";
const manager = new QuickToolsManager();
$page.body = manager.getContainer();
const onShow = $page.onshow;
$page.onshow = function () {
if (onShow) onShow.call(this);
const scrollContainer = $page.get(".scroll-container") || $page;
scrollContainer.style.overflow = "hidden";
manager.getContainer().style.height = "100%";
};
actionStack.push({
id: "quicktools-settings",
action: $page.hide,
});
$page.onhide = () => {
actionStack.remove("quicktools-settings");
hideAd();
// Cleanup manager
manager.destroy();
};
app.append($page);
helpers.showAd();
}
class QuickToolsManager {
constructor() {
this.container = <div id="quicktools-settings"></div>;
this.render();
this.bindEvents();
this.longPressTimer = null;
this.dragState = null;
}
getContainer() {
return this.container;
}
render() {
this.destroy(); // Cleanup potential drag states before re-rendering
this.container.textContent = "";
// --- Active Tools Section ---
const activeSection = <div className="section active-tools"></div>;
activeSection.appendChild(
<div className="section-title">{strings["active tools"]}</div>,
);
const activeGrid = <div className="quicktools-grid active-grid"></div>;
const totalSlots =
settings.QUICKTOOLS_ROWS *
settings.QUICKTOOLS_GROUPS *
settings.QUICKTOOLS_GROUP_CAPACITY;
for (let i = 0; i < totalSlots; i++) {
const itemIndex = settings.value.quicktoolsItems[i];
const itemDef = items[itemIndex];
const el = this.createItemElement(itemDef, i, "active");
activeGrid.appendChild(el);
}
activeSection.appendChild(activeGrid);
this.container.appendChild(activeSection);
// --- Available Tools Section ---
const availableSection = <div className="section available-tools"></div>;
availableSection.appendChild(
<div className="section-title">{strings["available tools"]}</div>,
);
// Group items
const categories = {
Modifiers: ["ctrl", "shift", "alt", "meta"],
Commands: ["command", "undo", "redo", "save", "search"],
Navigation: ["key"],
Symbols: ["insert"],
Other: [],
};
const groupedItems = {};
items.forEach((item, index) => {
let category = "Other";
for (const [cat, actions] of Object.entries(categories)) {
if (actions.includes(item.action)) {
category = cat;
break;
}
}
if (!groupedItems[category]) groupedItems[category] = [];
groupedItems[category].push({ item, index });
});
Object.entries(groupedItems).forEach(([category, list]) => {
const catHeader = <div className="category-header">{category}</div>;
const catGrid = <div className="quicktools-grid source-grid"></div>;
list.forEach(({ item, index }) => {
const el = this.createItemElement(item, index, "source");
catGrid.appendChild(el);
});
availableSection.appendChild(catHeader);
availableSection.appendChild(catGrid);
});
this.container.appendChild(availableSection);
}
createItemElement(itemDef, index, type) {
if (!itemDef)
return (
<div
className="tool-item empty"
data-index={index}
data-type={type}
></div>
);
const hasIcon = itemDef.icon && itemDef.icon !== "letters";
// If it's not an icon, we assume it relies on 'letters'
// Some items might have both, but 'letters' mode implies text rendering
const el = (
<div
className={`tool-item ${hasIcon ? "has-icon" : "has-letters"}`}
data-index={index} // active: slot index, source: item index
data-type={type}
data-letters={itemDef.letters || ""}
>
{hasIcon ? <span className={`icon ${itemDef.icon}`}></span> : null}
</div>
);
return el;
}
bindEvents() {
const c = this.container;
c.addEventListener("touchstart", this.handleTouchStart.bind(this), {
passive: false,
});
c.addEventListener("touchmove", this.handleTouchMove.bind(this), {
passive: false,
});
c.addEventListener("touchend", this.handleTouchEnd.bind(this));
c.addEventListener("contextmenu", (e) => e.preventDefault());
c.addEventListener("mousedown", this.handleMouseDown.bind(this));
}
// --- Touch Handlers ---
handleTouchStart(e) {
// If already dragging or pending, ignore new touches (prevent multi-touch mess)
if (this.dragState || this.longPressTimer) return;
const target = e.target.closest(".tool-item");
if (!target) return;
this.longPressTimer = setTimeout(() => {
this.startDrag(target, e.touches[0]);
}, 300);
this.touchStartX = e.touches[0].clientX;
this.touchStartY = e.touches[0].clientY;
this.potentialTarget = target;
}
handleTouchMove(e) {
const touch = e.touches[0];
if (this.dragState) {
e.preventDefault();
this.updateDrag(touch);
return;
}
if (
Math.hypot(
touch.clientX - this.touchStartX,
touch.clientY - this.touchStartY,
) > 10
) {
clearTimeout(this.longPressTimer);
this.longPressTimer = null;
this.potentialTarget = null;
}
}
handleTouchEnd(e) {
clearTimeout(this.longPressTimer);
if (this.dragState) {
this.endDrag();
} else if (this.potentialTarget) {
// It was a tap
if (e.cancelable) e.preventDefault();
this.handleClick(this.potentialTarget);
}
this.potentialTarget = null;
}
// --- Mouse Handlers ---
handleMouseDown(e) {
const target = e.target.closest(".tool-item");
if (!target) return;
this.mouseDownInfo = {
target,
x: e.clientX,
y: e.clientY,
isDrag: false,
};
const moveHandler = (ev) => {
if (!this.mouseDownInfo.isDrag) {
if (
Math.hypot(
ev.clientX - this.mouseDownInfo.x,
ev.clientY - this.mouseDownInfo.y,
) > 5
) {
this.mouseDownInfo.isDrag = true;
this.startDrag(target, this.mouseDownInfo);
}
}
if (this.dragState) {
this.updateDrag(ev);
}
};
const upHandler = () => {
document.removeEventListener("mousemove", moveHandler);
document.removeEventListener("mouseup", upHandler);
if (this.dragState) {
this.endDrag();
} else {
this.handleClick(target);
}
};
document.addEventListener("mousemove", moveHandler);
document.addEventListener("mouseup", upHandler);
}
// --- Core Drag Logic ---
startDrag(el, pointer) {
// Double check state
if (this.dragState) {
this.destroy();
return;
}
if (navigator.vibrate) navigator.vibrate(30);
const rect = el.getBoundingClientRect();
const ghost = el.cloneNode(true);
ghost.classList.add("tool-ghost");
ghost.style.width = rect.width + "px";
ghost.style.height = rect.height + "px";
document.body.appendChild(ghost);
el.classList.add("dragging");
const type = el.dataset.type;
const index = Number.parseInt(el.dataset.index, 10);
this.dragState = {
el,
type, // 'active' or 'source'
index, // slot index (active) or item ID (source)
ghost,
offsetX: pointer.clientX - rect.left - rect.width / 2,
offsetY: pointer.clientY - rect.top - rect.height / 2,
};
this.updateDrag(pointer);
}
updateDrag(pointer) {
const { ghost } = this.dragState;
ghost.style.left = pointer.clientX + "px";
ghost.style.top = pointer.clientY + "px";
const elementBelow = document.elementFromPoint(
pointer.clientX,
pointer.clientY,
);
this.cleanupHighlight();
const targetItem = elementBelow?.closest(".tool-item");
if (targetItem && targetItem.dataset.type === "active") {
targetItem.classList.add("highlight-target");
this.dragState.dropTarget = targetItem;
} else {
this.dragState.dropTarget = null;
}
}
cleanupHighlight() {
const highlighted = this.container.querySelectorAll(".highlight-target");
highlighted.forEach((el) => el.classList.remove("highlight-target"));
}
endDrag() {
const { el, ghost, dropTarget, type, index } = this.dragState;
this.cleanupHighlight();
el.classList.remove("dragging");
ghost.remove();
this.dragState = null;
if (dropTarget) {
const targetIndex = Number.parseInt(dropTarget.dataset.index, 10);
if (type === "active") {
// Swap within active
if (targetIndex !== index) {
this.swapItems(index, targetIndex);
}
} else if (type === "source") {
// Replace active slot with source item
this.replaceItem(targetIndex, index);
}
}
}
swapItems(srcIndex, destIndex) {
const temp = settings.value.quicktoolsItems[srcIndex];
settings.value.quicktoolsItems[srcIndex] =
settings.value.quicktoolsItems[destIndex];
settings.value.quicktoolsItems[destIndex] = temp;
settings.update();
this.render(); // Re-render to reflect changes
}
replaceItem(slotIndex, newItemId) {
settings.value.quicktoolsItems[slotIndex] = newItemId;
settings.update();
this.render();
}
async handleClick(el) {
const type = el.dataset.type;
const index = Number.parseInt(el.dataset.index, 10);
let itemDef;
if (type === "active") {
const itemIndex = settings.value.quicktoolsItems[index];
itemDef = items[itemIndex];
} else {
itemDef = items[index];
}
if (itemDef) {
const desc = description(itemDef.id);
window.toast(desc, 2000);
}
}
destroy() {
if (this.longPressTimer) clearTimeout(this.longPressTimer);
this.longPressTimer = null;
if (this.dragState) {
if (this.dragState.ghost) {
this.dragState.ghost.remove();
}
if (this.dragState.el) {
this.dragState.el.classList.remove("dragging");
}
}
this.cleanupHighlight();
this.dragState = null;
this.potentialTarget = null;
}
}