fablecut 1.22.0 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +29 -0
- package/README.md +2 -1
- package/app.js +232 -111
- package/compact.css +702 -0
- package/compact.js +785 -0
- package/doc-guard.js +97 -0
- package/index.html +28 -10
- package/layout.js +72 -0
- package/manifest.json +1 -1
- package/mcp-server.js +1 -1
- package/package.json +2 -1
- package/style.css +7 -0
- package/touch.js +815 -0
package/touch.js
ADDED
|
@@ -0,0 +1,815 @@
|
|
|
1
|
+
/* Fingers.
|
|
2
|
+
|
|
3
|
+
The editor was built for a mouse: drags, right-click, double-click and HTML
|
|
4
|
+
drag and drop. This script gives each a finger route and adds no edit of its
|
|
5
|
+
own. It decides what a touch means, then calls (or feeds) the handler the
|
|
6
|
+
mouse uses:
|
|
7
|
+
|
|
8
|
+
- anywhere: a long press is the right-click (`contextmenu`), two quick taps
|
|
9
|
+
are the double-click (`dblclick`), and a long press that then moves carries
|
|
10
|
+
a draggable item to wherever a mouse could drop it;
|
|
11
|
+
- on the timeline: one finger scrolls (with momentum), two zoom around
|
|
12
|
+
themselves, a tap selects, a long press picks a clip up, the grips of the
|
|
13
|
+
selected clip trim at once, and the view runs along at the edges.
|
|
14
|
+
|
|
15
|
+
A mouse or a pen passes through untouched. Loaded after app.js and compact.js. */
|
|
16
|
+
(() => {
|
|
17
|
+
"use strict";
|
|
18
|
+
const sc = els.timelineScroll;
|
|
19
|
+
const SLOP = 9; // px a finger may wander and still be a tap
|
|
20
|
+
const HOLD_MS = 430, TWICE_MS = 320;
|
|
21
|
+
const EDGE = 46; // px from the timeline's side where a drag makes it run
|
|
22
|
+
const centred = () => tlCentred();
|
|
23
|
+
const buzz = (ms) => { try { if (navigator.vibrate) navigator.vibrate(ms); } catch { } };
|
|
24
|
+
const isText = (el) => !!(el && el.closest && el.closest("input:not([type=range]):not([type=checkbox]):not([type=radio]), textarea, select, [contenteditable=true]"));
|
|
25
|
+
|
|
26
|
+
/* ═══ Timeline ═══ */
|
|
27
|
+
const pts = new Map(); // fingers on the timeline: pointerId → {x, y}
|
|
28
|
+
let g = null; // the gesture in hand: {kind: wait | scroll | pinch | edit | held | marquee}
|
|
29
|
+
let glide = 0, holdTimer = 0, run = 0, feeding = false;
|
|
30
|
+
let lastTap = { t: 0, x: 0, y: 0 };
|
|
31
|
+
|
|
32
|
+
const stopGlide = () => { cancelAnimationFrame(glide); glide = 0; };
|
|
33
|
+
function scrollBy(dx, dy) {
|
|
34
|
+
if (dy) sc.scrollTop += dy;
|
|
35
|
+
if (!dx) return;
|
|
36
|
+
if (!centred()) { sc.scrollLeft += dx; return; }
|
|
37
|
+
if (isSourceMode()) setMonitorMode("program");
|
|
38
|
+
scrubTimeline(dx);
|
|
39
|
+
}
|
|
40
|
+
function startGlide(vx, vy) { // px per ms
|
|
41
|
+
let last = performance.now();
|
|
42
|
+
const step = (now) => {
|
|
43
|
+
const dt = Math.min(40, now - last); last = now;
|
|
44
|
+
const before = centred() ? state.time : sc.scrollLeft, top = sc.scrollTop;
|
|
45
|
+
scrollBy(vx * dt, vy * dt);
|
|
46
|
+
const k = Math.pow(0.996, dt);
|
|
47
|
+
vx *= k; vy *= k;
|
|
48
|
+
const stuck = (centred() ? state.time : sc.scrollLeft) === before && sc.scrollTop === top;
|
|
49
|
+
glide = Math.hypot(vx, vy) > 0.02 && !stuck ? requestAnimationFrame(step) : 0;
|
|
50
|
+
noteActivity();
|
|
51
|
+
};
|
|
52
|
+
glide = requestAnimationFrame(step);
|
|
53
|
+
}
|
|
54
|
+
const two = () => { const [a, b] = [...pts.values()]; return { x: (a.x + b.x) / 2, d: Math.max(24, Math.abs(a.x - b.x), Math.hypot(a.x - b.x, a.y - b.y)) }; };
|
|
55
|
+
|
|
56
|
+
/* An edit in progress: app.js listens on window for the moves. They are fed to it from here, so the
|
|
57
|
+
view can run under a still finger and a row that grew under it can be allowed for. */
|
|
58
|
+
const evt = (type, x, y) => new PointerEvent(type, { clientX: x, clientY: y + (g && g.dy || 0), pointerType: "touch", pointerId: 1, button: type === "pointermove" ? -1 : 0, bubbles: true });
|
|
59
|
+
function feed(type, x, y) {
|
|
60
|
+
feeding = true;
|
|
61
|
+
try { window.dispatchEvent(evt(type, x, y)); } finally { feeding = false; }
|
|
62
|
+
}
|
|
63
|
+
const press = (target, x, y, extra) => ({ button: 0, target, clientX: x, clientY: y + (g && g.dy || 0), pointerType: "touch", pointerId: 1, ctrlKey: false, metaKey: false, shiftKey: false, altKey: false, preventDefault() { }, stopPropagation() { }, ...extra });
|
|
64
|
+
|
|
65
|
+
function beginEdit(target, x, y, extra) {
|
|
66
|
+
clearTimeout(holdTimer);
|
|
67
|
+
Object.assign(g, { kind: "edit", x, y, moved: false });
|
|
68
|
+
if (state.playing) pause();
|
|
69
|
+
if (target === els.ruler) onRulerPointerDown(press(target, x, y, extra));
|
|
70
|
+
else timelinePointerDown(press(target, x, y, extra));
|
|
71
|
+
if (!state.gesture) { g = { kind: "done" }; return; } // locked, or the press was all there was to it
|
|
72
|
+
runAt = performance.now();
|
|
73
|
+
run = requestAnimationFrame(runAlong);
|
|
74
|
+
}
|
|
75
|
+
let runAt = 0;
|
|
76
|
+
/* near the sides the timeline runs along, faster the closer the finger is to the edge */
|
|
77
|
+
function runAlong(now) {
|
|
78
|
+
if (!g || g.kind !== "edit") return;
|
|
79
|
+
run = requestAnimationFrame(runAlong);
|
|
80
|
+
const dt = clamp(now - runAt, 0, 50); runAt = now;
|
|
81
|
+
const r = sc.getBoundingClientRect();
|
|
82
|
+
const left = g.x - r.left, right = r.right - g.x;
|
|
83
|
+
const near = !g.moved ? 0 : left < EDGE ? -(EDGE - Math.max(0, left)) : right < EDGE ? EDGE - Math.max(0, right) : 0;
|
|
84
|
+
if (!near) return;
|
|
85
|
+
scrollBy(near / EDGE * 0.28 * dt, 0); // at most 280 px a second
|
|
86
|
+
feed("pointermove", g.x, g.y);
|
|
87
|
+
noteActivity();
|
|
88
|
+
}
|
|
89
|
+
let bubble = null, lastSnap = "";
|
|
90
|
+
function showBubble(x, y) {
|
|
91
|
+
const c = getClip(state.selId);
|
|
92
|
+
if (!c || !g || !g.side) { if (bubble) bubble.hidden = true; return; }
|
|
93
|
+
if (!bubble) { bubble = document.createElement("div"); bubble.className = "m-bubble"; document.body.appendChild(bubble); }
|
|
94
|
+
bubble.textContent = g.side === "move" ? fmt(c.start) : `${fmt(c.duration)}`;
|
|
95
|
+
bubble.hidden = false;
|
|
96
|
+
bubble.style.left = clamp(x, 44, window.innerWidth - 44) + "px";
|
|
97
|
+
bubble.style.top = Math.max(60, y - 66) + "px";
|
|
98
|
+
const snap = els.snapLine.classList.contains("hidden") ? "" : els.snapLine.style.left;
|
|
99
|
+
if (snap && snap !== lastSnap) buzz(8);
|
|
100
|
+
lastSnap = snap;
|
|
101
|
+
}
|
|
102
|
+
function endEdit(x, y) {
|
|
103
|
+
cancelAnimationFrame(run);
|
|
104
|
+
if (bubble) bubble.hidden = true;
|
|
105
|
+
feed("pointerup", x, y);
|
|
106
|
+
if (runtime.showEmptyTracks) { runtime.showEmptyTracks = false; state.dirtyTimeline = true; }
|
|
107
|
+
if (self.FableCutCompact) FableCutCompact.refresh();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* Held on a clip: it is picked up. Empty tracks open so it can go there; the rows above may push the
|
|
111
|
+
clip's own row down, which the fed positions allow for (g.dy). */
|
|
112
|
+
function pickUp() {
|
|
113
|
+
if (!g || g.kind !== "wait") return;
|
|
114
|
+
const t = g.target, div = t.closest && t.closest(".clip"), mk = t === els.ruler && markerAtRulerEvent({ clientX: g.x0, clientY: g.y0, pointerType: "touch" });
|
|
115
|
+
if (!div && !mk) return;
|
|
116
|
+
buzz(14);
|
|
117
|
+
g.kind = "held";
|
|
118
|
+
if (div) { div.classList.add("m-held"); g.div = div; const c = getClip(div.dataset.id); if (c && !state.selIds.has(c.id)) selectClip(c.id, runtime.selectMode ? { toggle: true } : undefined); g.id = c && c.id; }
|
|
119
|
+
}
|
|
120
|
+
function carryHeld(x, y) {
|
|
121
|
+
if (!g.id) { beginEdit(els.ruler, g.x0, g.y0); g.side = null; return; }
|
|
122
|
+
const row = () => els.tracks.querySelector(`[data-track="${getClip(g.id).track}"]`);
|
|
123
|
+
if (centred()) {
|
|
124
|
+
const top = row().getBoundingClientRect().top;
|
|
125
|
+
runtime.showEmptyTracks = true;
|
|
126
|
+
rebuildClips();
|
|
127
|
+
const d = row().getBoundingClientRect().top - top, st = sc.scrollTop;
|
|
128
|
+
sc.scrollTop += d;
|
|
129
|
+
g.dy = d - (sc.scrollTop - st);
|
|
130
|
+
}
|
|
131
|
+
// the selection may have rebuilt the clips: the live element stands for the one that was pressed
|
|
132
|
+
const live = els.tracks.querySelector(`.clip[data-id="${g.id}"]`);
|
|
133
|
+
if (!live) return;
|
|
134
|
+
const tool = state.tool;
|
|
135
|
+
if (tool !== "slip" && tool !== "slide") state.tool = "select";
|
|
136
|
+
beginEdit(live.querySelector(".clip-body") || live, g.x0, g.y0);
|
|
137
|
+
state.tool = tool;
|
|
138
|
+
if (g.kind === "edit") g.side = "move";
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const grip = (t) => !runtime.selectMode && t.closest && t.closest(".clip.selected") && t.closest(".handle, .fade-grip, .vol-pt, .trans-dur-handle");
|
|
142
|
+
|
|
143
|
+
/* An open menu or panel takes the whole screen's attention on a phone: a tap beside it closes it and
|
|
144
|
+
does nothing else. (It closes through its own outside-press handler; here the tap stops counting.) */
|
|
145
|
+
let dismiss = null;
|
|
146
|
+
window.addEventListener("pointerdown", (e) => {
|
|
147
|
+
dismiss = null;
|
|
148
|
+
if (e.pointerType !== "touch" || !tlCentred() || !self.FableCutCompact) return;
|
|
149
|
+
const open = [...document.querySelectorAll(FableCutCompact.modal.join(","))].filter((m) => m.getClientRects().length);
|
|
150
|
+
if (open.length && !open.some((m) => m.contains(e.target))) { dismiss = e.pointerId; swallow = performance.now(); }
|
|
151
|
+
}, true);
|
|
152
|
+
// (document's own listeners have closed it by now; nothing below the body hears of this touch)
|
|
153
|
+
document.body.addEventListener("pointerdown", (e) => { if (dismiss === e.pointerId) e.stopPropagation(); }, true);
|
|
154
|
+
|
|
155
|
+
function down(e) {
|
|
156
|
+
if (e.pointerType !== "touch") return;
|
|
157
|
+
e.preventDefault(); e.stopPropagation();
|
|
158
|
+
stopGlide();
|
|
159
|
+
pts.set(e.pointerId, { x: e.clientX, y: e.clientY });
|
|
160
|
+
if (g && (g.kind === "edit" || g.kind === "held" || g.kind === "marquee")) return; // a second finger does not disturb an edit
|
|
161
|
+
if (isTypingTarget(document.activeElement)) document.activeElement.blur();
|
|
162
|
+
hideTrackCtxMenu();
|
|
163
|
+
if (pts.size === 2) {
|
|
164
|
+
clearTimeout(holdTimer);
|
|
165
|
+
const p = two();
|
|
166
|
+
g = { kind: "pinch", d0: p.d, pps0: state.pps, x: p.x, ids: [...pts.keys()] };
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (pts.size > 2) return;
|
|
170
|
+
if (state.playing && centred()) pause();
|
|
171
|
+
g = { kind: "wait", id0: e.pointerId, target: e.target, x0: e.clientX, y0: e.clientY, x: e.clientX, y: e.clientY, t: e.timeStamp, vx: 0, vy: 0 };
|
|
172
|
+
const h = grip(e.target);
|
|
173
|
+
if (h) { // a grip moves at once, without a hold, but only when the finger does: a tap on one stays a tap
|
|
174
|
+
g.grip = h.classList.contains("handle") ? "trim" : "other";
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (e.target === els.ruler && !centred() && !markerAtRulerEvent(e)) { beginEdit(els.ruler, e.clientX, e.clientY); return; } // tablet: the ruler scrubs
|
|
178
|
+
holdTimer = setTimeout(pickUp, HOLD_MS);
|
|
179
|
+
}
|
|
180
|
+
function zoomNow() {
|
|
181
|
+
if (pts.size < 2) return;
|
|
182
|
+
const q = two();
|
|
183
|
+
setZoom(g.pps0 * q.d / g.d0, q.x);
|
|
184
|
+
if (!centred()) sc.scrollLeft -= q.x - g.x;
|
|
185
|
+
g.x = q.x;
|
|
186
|
+
}
|
|
187
|
+
function move(e) {
|
|
188
|
+
if (feeding) return;
|
|
189
|
+
const p = pts.get(e.pointerId);
|
|
190
|
+
if (!p || !g) return;
|
|
191
|
+
if (e.cancelable) e.preventDefault();
|
|
192
|
+
e.stopPropagation();
|
|
193
|
+
p.x = e.clientX; p.y = e.clientY;
|
|
194
|
+
if (g.kind === "pinch") {
|
|
195
|
+
if (pts.size < 2) return;
|
|
196
|
+
// each finger reports on its own: one redraw of the clips a frame does for both
|
|
197
|
+
if (!g.due) { const z = g; z.due = requestAnimationFrame(() => { z.due = 0; if (g === z) zoomNow(); }); }
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (e.pointerId !== g.id0) return;
|
|
201
|
+
if (g.kind === "edit" || g.kind === "marquee") {
|
|
202
|
+
if (Math.hypot(e.clientX - g.x0, e.clientY - g.y0) > 3) g.moved = true;
|
|
203
|
+
g.x = e.clientX; g.y = e.clientY;
|
|
204
|
+
feed("pointermove", g.x, g.y);
|
|
205
|
+
showBubble(g.x, g.y);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const dx0 = e.clientX - g.x0, dy0 = e.clientY - g.y0;
|
|
209
|
+
if (g.kind === "held") {
|
|
210
|
+
if (Math.hypot(dx0, dy0) > 5) { carryHeld(e.clientX, e.clientY); if (g && g.kind === "edit") { g.moved = true; g.x = e.clientX; g.y = e.clientY; feed("pointermove", g.x, g.y); } }
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (g.kind === "wait" && g.grip) {
|
|
214
|
+
if (Math.hypot(dx0, dy0) < 4) return;
|
|
215
|
+
const side = g.grip === "trim" ? "trim" : null;
|
|
216
|
+
beginEdit(g.target, g.x0, g.y0);
|
|
217
|
+
if (g.kind === "edit") { g.side = side; g.moved = true; g.x = e.clientX; g.y = e.clientY; feed("pointermove", g.x, g.y); showBubble(g.x, g.y); }
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (g.kind === "wait" && Math.hypot(dx0, dy0) > SLOP) {
|
|
221
|
+
clearTimeout(holdTimer);
|
|
222
|
+
const across = Math.abs(dx0) >= Math.abs(dy0), clip = g.target.closest && g.target.closest(".clip");
|
|
223
|
+
// Select mode: dragging over empty space draws a box
|
|
224
|
+
if (runtime.selectMode && !clip && g.target !== els.ruler) {
|
|
225
|
+
g.kind = "marquee";
|
|
226
|
+
startMarquee(press(g.target, g.x0, g.y0, { shiftKey: true }));
|
|
227
|
+
feed("pointermove", e.clientX, e.clientY);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const own = clip && clip.classList.contains("selected");
|
|
231
|
+
// the selected clip's volume line moves up and down; Slip and Slide drag the selected clip sideways
|
|
232
|
+
if (own && !across && g.target.closest(".vol-hit")) { beginEdit(g.target, g.x0, g.y0); if (g.kind === "edit") { g.moved = true; feed("pointermove", e.clientX, e.clientY); } return; }
|
|
233
|
+
if (own && across && (state.tool === "slip" || state.tool === "slide")) { beginEdit(g.target, g.x0, g.y0); if (g.kind === "edit") { g.moved = true; feed("pointermove", e.clientX, e.clientY); } return; }
|
|
234
|
+
g.kind = "scroll";
|
|
235
|
+
g.axis = across ? "x" : "y";
|
|
236
|
+
g.x = e.clientX; g.y = e.clientY; g.t = e.timeStamp;
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (g.kind !== "scroll") return;
|
|
240
|
+
const dx = g.axis === "x" ? g.x - e.clientX : 0, dy = g.axis === "y" ? g.y - e.clientY : 0;
|
|
241
|
+
const dt = Math.max(1, e.timeStamp - g.t);
|
|
242
|
+
g.vx = g.vx * 0.6 + (dx / dt) * 0.4; g.vy = g.vy * 0.6 + (dy / dt) * 0.4;
|
|
243
|
+
g.x = e.clientX; g.y = e.clientY; g.t = e.timeStamp;
|
|
244
|
+
scrollBy(dx, dy);
|
|
245
|
+
}
|
|
246
|
+
function up(e) {
|
|
247
|
+
if (feeding || !pts.has(e.pointerId)) return;
|
|
248
|
+
if (g && g.due) { cancelAnimationFrame(g.due); g.due = 0; zoomNow(); } // the last move counts too
|
|
249
|
+
pts.delete(e.pointerId);
|
|
250
|
+
e.stopPropagation();
|
|
251
|
+
const was = g;
|
|
252
|
+
if (!was) return;
|
|
253
|
+
if (was.kind === "pinch" || was.kind === "done") { // the second finger lifting is not a tap
|
|
254
|
+
if (!pts.size) g = null;
|
|
255
|
+
else if (was.kind === "pinch" && was.ids.includes(e.pointerId)) g = { kind: "done" }; // a third one (a palm) lifting changes nothing
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (e.pointerId !== was.id0) return;
|
|
259
|
+
clearTimeout(holdTimer);
|
|
260
|
+
if (was.div) was.div.classList.remove("m-held");
|
|
261
|
+
// a cancelled touch (a call, a system gesture) ends an edit where it was, like letting go
|
|
262
|
+
if (was.kind === "edit") { endEdit(was.x, was.y); g = null; return; }
|
|
263
|
+
if (was.kind === "marquee") { feed("pointerup", e.clientX, e.clientY); g = null; return; }
|
|
264
|
+
g = null;
|
|
265
|
+
if (e.type === "pointercancel") return;
|
|
266
|
+
if (was.kind === "scroll") { if (e.timeStamp - was.t < 60) startGlide(was.vx, was.vy); return; }
|
|
267
|
+
if (was.kind === "held") { openMenuAt(was.target, e.clientX, e.clientY); return; }
|
|
268
|
+
tap(was.target, e);
|
|
269
|
+
}
|
|
270
|
+
function openMenuAt(target, x, y) {
|
|
271
|
+
// (rebuilt since? the clip now under that id)
|
|
272
|
+
const id = target.closest && target.closest(".clip") && target.closest(".clip").dataset.id;
|
|
273
|
+
const live = target.isConnected ? target : els.tracks.querySelector(`.clip[data-id="${id}"]`);
|
|
274
|
+
if (live) live.dispatchEvent(new PointerEvent("contextmenu", { bubbles: true, cancelable: true, clientX: x, clientY: y, pointerType: "touch" }));
|
|
275
|
+
}
|
|
276
|
+
function tap(target, e) {
|
|
277
|
+
const again = e.timeStamp - lastTap.t < TWICE_MS && Math.hypot(e.clientX - lastTap.x, e.clientY - lastTap.y) < 26;
|
|
278
|
+
lastTap = { t: again ? 0 : e.timeStamp, x: e.clientX, y: e.clientY };
|
|
279
|
+
if (target === els.ruler) {
|
|
280
|
+
const mk = markerAtRulerEvent(e);
|
|
281
|
+
if (mk) { if (again) openMarkerEditor(mk, e.clientX - 20, e.clientY + 10); else seekToMarker(mk); }
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const div = target.closest && target.closest(".clip"), c = div && getClip(div.dataset.id);
|
|
285
|
+
if (!c) {
|
|
286
|
+
if (!runtime.selectMode) selectClip(null);
|
|
287
|
+
if (!centred()) setTime(timeAtEvent(e));
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (again && !runtime.selectMode) { // the double-click: open in Source, open a composition, remove a volume point
|
|
291
|
+
const live = els.tracks.querySelector(`.clip[data-id="${c.id}"]`), t = target.isConnected ? target : live && live.querySelector(".clip-body");
|
|
292
|
+
if (t) t.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, cancelable: true, clientX: e.clientX, clientY: e.clientY }));
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (runtime.selectMode) { selectClip(c.id, { toggle: true }); return; }
|
|
296
|
+
const mark = target.closest(".trans-mark"), key = target.closest(".clip-kf");
|
|
297
|
+
if (key) { selectClip(c.id); setTime(c.start + (+key.dataset.t || 0)); return; }
|
|
298
|
+
selectClip(c.id, mark ? { transFocus: mark.classList.contains("out") ? "out" : "in" } : undefined);
|
|
299
|
+
}
|
|
300
|
+
sc.addEventListener("pointerdown", down, { capture: true, passive: false });
|
|
301
|
+
window.addEventListener("pointermove", move, { capture: true, passive: false });
|
|
302
|
+
window.addEventListener("pointerup", up, true);
|
|
303
|
+
window.addEventListener("pointercancel", up, true);
|
|
304
|
+
|
|
305
|
+
/* ═══ The preview ═══
|
|
306
|
+
A tap selects. One finger moves the selected item (or a handle, or whatever tool is picked); on anything
|
|
307
|
+
else it pans a zoomed view and otherwise does nothing, so a swipe can't shove the background clip. Two
|
|
308
|
+
fingers scale, turn and move the selected item when they are on it, and zoom the view when they are not. */
|
|
309
|
+
const stage = els.monitorStage, mp = new Map();
|
|
310
|
+
let m = null, passing = false, lastMon = { t: 0, x: 0, y: 0 };
|
|
311
|
+
const monTool = () => {
|
|
312
|
+
const c = getClip(state.selId);
|
|
313
|
+
return isSourceMode() || runtime.anchorTool || trackUi.tool || colorState.pick || colorState.curvePick || colorState.qualPick ||
|
|
314
|
+
drawingLayerMask(c) || maskEditing(c) || maskEditActive(c) || shapeEditActive(c);
|
|
315
|
+
};
|
|
316
|
+
const monItem = () => { const c = getClip(state.selId); return c && isVisualClip(c) && activeAt(c, state.time) && !state.exporting ? c : null; };
|
|
317
|
+
/* on the selected item (reach: extra px on screen), or on one of its handles */
|
|
318
|
+
function onItem(c, x, y, reach = 0) {
|
|
319
|
+
const W = els.preview.width, H = els.preview.height, pt = canvasPt({ clientX: x, clientY: y }), b = clipBounds(c, evalProps(c, state.time), W, H), lp = toLocal(pt, b), r = reach * monScale();
|
|
320
|
+
if (Math.abs(lp.x) <= b.hw + r && Math.abs(lp.y) <= b.hh + r) return true;
|
|
321
|
+
const hd = overlayHandles(b, W, H), grab = monGrab(hd.hs * 1.8, { pointerType: "touch" });
|
|
322
|
+
return [hd.rotate, ...hd.corners].some((h) => Math.hypot(pt.x - h.x, pt.y - h.y) <= grab);
|
|
323
|
+
}
|
|
324
|
+
const pair = () => { const [a, b] = [...mp.values()]; return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, d: Math.max(20, Math.hypot(a.x - b.x, a.y - b.y)), a: Math.atan2(b.y - a.y, b.x - a.x) }; };
|
|
325
|
+
function handOver(id, x, y) { // the mouse's own handler takes this finger from here on
|
|
326
|
+
passing = true;
|
|
327
|
+
try { els.preview.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, cancelable: true, clientX: x, clientY: y, pointerId: id, pointerType: "touch", button: 0, buttons: 1 })); } finally { passing = false; }
|
|
328
|
+
}
|
|
329
|
+
function beginTwo() {
|
|
330
|
+
const q = pair(), c = monItem();
|
|
331
|
+
if (canvasDrag) { m = { ...m, didMove: canvasDidMove }; endCanvasDrag({ pointerId: m.id, clientX: q.x, clientY: q.y }); }
|
|
332
|
+
if (c && !monTool() && onItem(c, q.x, q.y, 24)) {
|
|
333
|
+
if (isGroupLocked(c)) { toastLocked(); m = { kind: "none" }; return; }
|
|
334
|
+
const pr = propsAtPlayhead(c);
|
|
335
|
+
m = { kind: "shape", ids: [...mp.keys()].slice(0, 2), c: c.id, q, undone: !!m && m.didMove, x: +pr.x || 0, y: +pr.y || 0, scale: +pr.scale || 1, rot: +pr.rotation || 0, box: hasTextBox(c.props) ? [+c.props.boxW, +c.props.boxH, +pr.fontSize || 0] : null, at: null };
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const cv = els.preview.getBoundingClientRect();
|
|
339
|
+
if (state.viewZoom <= 1.001) monitorFitCache = { w: els.preview.offsetWidth, h: els.preview.offsetHeight };
|
|
340
|
+
m = { kind: "view", ids: [...mp.keys()].slice(0, 2), q, z: state.viewZoom, rx: (q.x - cv.left) / cv.width, ry: (q.y - cv.top) / cv.height };
|
|
341
|
+
}
|
|
342
|
+
function moveTwo() {
|
|
343
|
+
const q = pair();
|
|
344
|
+
if (m.kind === "shape") {
|
|
345
|
+
const c = getClip(m.c);
|
|
346
|
+
if (!c) return;
|
|
347
|
+
if (!m.undone) { pushUndo(); m.undone = true; }
|
|
348
|
+
const k = q.d / m.q.d, cs = monScale();
|
|
349
|
+
let turn = (q.a - m.q.a) * 180 / Math.PI;
|
|
350
|
+
turn = ((turn + 540) % 360) - 180;
|
|
351
|
+
let rot = m.rot + (Math.abs(turn) < 5 && !m.turned ? 0 : turn); // a pinch is never perfectly straight
|
|
352
|
+
if (Math.abs(turn) >= 5) m.turned = true;
|
|
353
|
+
const sq = Math.round(rot / 90) * 90, hit = m.turned && Math.abs(rot - sq) < 4;
|
|
354
|
+
if (hit) rot = sq;
|
|
355
|
+
if (hit && m.at !== sq) buzz(8);
|
|
356
|
+
m.at = hit ? sq : null;
|
|
357
|
+
if (m.box) {
|
|
358
|
+
c.props.boxW = +clamp(m.box[0] * k, 20, project.width * 3).toFixed(1);
|
|
359
|
+
c.props.boxH = +clamp(m.box[1] * k, 16, project.height * 3).toFixed(1);
|
|
360
|
+
if (m.box[2]) setAnimProp(c, "fontSize", Math.round(clamp(m.box[2] * k, 4, 2000)));
|
|
361
|
+
} else setAnimProp(c, "scale", clamp(+(m.scale * k).toFixed(3), 0.05, 12));
|
|
362
|
+
setAnimProp(c, "rotation", Math.round(rot));
|
|
363
|
+
const d = screenDeltaToParent(c, (q.x - m.q.x) * cs, (q.y - m.q.y) * cs);
|
|
364
|
+
setAnimProp(c, "x", Math.round(m.x + d.x));
|
|
365
|
+
setAnimProp(c, "y", Math.round(m.y + d.y));
|
|
366
|
+
noteActivity();
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
if (m.kind !== "view") return;
|
|
370
|
+
const next = clamp(m.z * q.d / m.q.d, VIEW_ZOOM_MIN, maxViewZoom()), r = els.monitorScroll.getBoundingClientRect(), fit = monitorFitSize();
|
|
371
|
+
if (next <= 1.03) { if (state.viewZoom > 1.001) { resetMonitorView(); monitorFitCache = { w: els.preview.offsetWidth, h: els.preview.offsetHeight }; } return; }
|
|
372
|
+
state.viewZoom = next;
|
|
373
|
+
const sx = q.x - r.left, sy = q.y - r.top;
|
|
374
|
+
scheduleMonitorView(() => {
|
|
375
|
+
void els.monitorScroll.scrollWidth;
|
|
376
|
+
els.monitorScroll.scrollLeft = monitorViewPad.x + m0.rx * fit.w * next - sx;
|
|
377
|
+
els.monitorScroll.scrollTop = monitorViewPad.y + m0.ry * fit.h * next - sy;
|
|
378
|
+
});
|
|
379
|
+
const m0 = m;
|
|
380
|
+
}
|
|
381
|
+
/* the same event again, as if Shift / Alt / Ctrl were down */
|
|
382
|
+
function withKeys(e) {
|
|
383
|
+
passing = true;
|
|
384
|
+
try {
|
|
385
|
+
els.preview.dispatchEvent(new PointerEvent(e.type, { bubbles: true, cancelable: true, clientX: e.clientX, clientY: e.clientY, pointerId: e.pointerId, pointerType: "touch",
|
|
386
|
+
button: e.type === "pointermove" ? -1 : 0, buttons: e.type === "pointerup" ? 0 : 1, shiftKey: m.keys.shift, altKey: m.keys.alt, ctrlKey: m.keys.ctrl }));
|
|
387
|
+
} finally { passing = false; }
|
|
388
|
+
}
|
|
389
|
+
/* Picking a spot (a colour, a point to track): the finger hides it, so a magnifier above the finger shows it,
|
|
390
|
+
and the spot is taken where the finger lifts */
|
|
391
|
+
const picking = () => colorState.pick || colorState.curvePick || colorState.qualPick || trackUi.tool === "point";
|
|
392
|
+
let loupe = null;
|
|
393
|
+
function showLoupe(x, y) {
|
|
394
|
+
if (!loupe) { loupe = document.createElement("canvas"); loupe.className = "m-loupe"; loupe.width = loupe.height = 220; document.body.appendChild(loupe); }
|
|
395
|
+
const cx = loupe.getContext("2d"), pt = canvasPt({ clientX: x, clientY: y }), s = 110 * monScale() / 3; // three times as large
|
|
396
|
+
cx.fillStyle = "#000"; cx.fillRect(0, 0, 220, 220);
|
|
397
|
+
cx.drawImage(els.preview, pt.x - s / 2, pt.y - s / 2, s, s, 0, 0, 220, 220);
|
|
398
|
+
cx.lineWidth = 4; cx.strokeStyle = "#000a"; cx.strokeRect(100, 100, 20, 20);
|
|
399
|
+
cx.lineWidth = 2; cx.strokeStyle = "#fff"; cx.strokeRect(100, 100, 20, 20);
|
|
400
|
+
loupe.hidden = false;
|
|
401
|
+
loupe.style.left = clamp(x - 55, 6, window.innerWidth - 116) + "px";
|
|
402
|
+
loupe.style.top = (y > 180 ? y - 150 : y + 46) + "px";
|
|
403
|
+
}
|
|
404
|
+
/* a key or handle of the selected clip's motion path (it may lie outside the clip itself) */
|
|
405
|
+
function onPath(x, y) {
|
|
406
|
+
const c = getClip(state.selId);
|
|
407
|
+
if (!motionPathActive(c) || maskEditActive(c) || shapeEditActive(c)) return false;
|
|
408
|
+
const pt = canvasPt({ clientX: x, clientY: y }), g = motionPathGeom(c, els.preview.width, els.preview.height, state.time), grab = monGrab(8, { pointerType: "touch" });
|
|
409
|
+
const near = (q) => Math.hypot(q.x - pt.x, q.y - pt.y) <= grab, shown = (j, has) => has || (j === pathUi.key && pathUi.clip === c.id), last = g.keys.length - 1;
|
|
410
|
+
return g.keys.some((k, j) => near(k.a) || (j < last && shown(j, k.hasOut) && near(k.o)) || (j > 0 && shown(j, k.hasIn) && near(k.i)));
|
|
411
|
+
}
|
|
412
|
+
stage.addEventListener("pointerdown", (e) => {
|
|
413
|
+
if (e.pointerType !== "touch" || passing || dismiss === e.pointerId || e.target.closest(".kf-graphs, #exportFrameOverlay, .vu-meter, button")) return;
|
|
414
|
+
mp.set(e.pointerId, { x: e.clientX, y: e.clientY });
|
|
415
|
+
if (mp.size === 1 && picking() && !isSourceMode()) {
|
|
416
|
+
e.preventDefault(); e.stopPropagation();
|
|
417
|
+
m = { kind: "pick", id: e.pointerId };
|
|
418
|
+
showLoupe(e.clientX, e.clientY);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (mp.size === 1 && (monTool() || onPath(e.clientX, e.clientY))) { // pens, masks, pickers: the finger is the pointer…
|
|
422
|
+
m = { kind: "tool", id: e.pointerId, x0: e.clientX, y0: e.clientY, pen: !!(maskUi.tool || shapeUi.tool || trackUi.tool || colorState.pick || colorState.curvePick || colorState.qualPick) };
|
|
423
|
+
const k = self.FableCutCompact && FableCutCompact.mods;
|
|
424
|
+
if (k && (k.shift || k.alt || k.ctrl)) { m.keys = { ...k }; e.preventDefault(); e.stopPropagation(); withKeys(e); } // …with the keys held that the buttons stand for
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
e.preventDefault(); e.stopPropagation();
|
|
428
|
+
if (mp.size === 2) { if (!m || (m.kind !== "tool" && m.kind !== "pick")) beginTwo(); return; }
|
|
429
|
+
if (mp.size > 2) return;
|
|
430
|
+
const c = monItem();
|
|
431
|
+
m = { kind: "wait", id: e.pointerId, x0: e.clientX, y0: e.clientY, mine: !!c && onItem(c, e.clientX, e.clientY), sl: els.monitorScroll.scrollLeft, st: els.monitorScroll.scrollTop };
|
|
432
|
+
}, true);
|
|
433
|
+
window.addEventListener("pointermove", (e) => {
|
|
434
|
+
if (feeding || passing || !m || !mp.has(e.pointerId)) return;
|
|
435
|
+
Object.assign(mp.get(e.pointerId), { x: e.clientX, y: e.clientY });
|
|
436
|
+
if (m.kind === "tool" && m.keys && e.pointerId === m.id) { e.stopPropagation(); withKeys(e); return; }
|
|
437
|
+
if (m.kind === "shape" || m.kind === "view") { e.stopPropagation(); if (m.ids.includes(e.pointerId) && m.ids.every((id) => mp.has(id))) moveTwo(); return; }
|
|
438
|
+
if (e.pointerId !== m.id) return;
|
|
439
|
+
if (m.kind === "pick") { e.stopPropagation(); showLoupe(e.clientX, e.clientY); return; }
|
|
440
|
+
if (m.kind === "wait" && Math.hypot(e.clientX - m.x0, e.clientY - m.y0) > SLOP) {
|
|
441
|
+
if (state.playing && m.mine) pause();
|
|
442
|
+
if (m.mine) { m.kind = "drag"; handOver(m.id, m.x0, m.y0); } else m.kind = state.viewZoom > 1.001 ? "pan" : "none";
|
|
443
|
+
}
|
|
444
|
+
if (m.kind === "pan") { els.monitorScroll.scrollLeft = m.sl - (e.clientX - m.x0); els.monitorScroll.scrollTop = m.st - (e.clientY - m.y0); }
|
|
445
|
+
if (m.kind === "pan" || m.kind === "none" || m.kind === "wait") e.stopPropagation();
|
|
446
|
+
}, true);
|
|
447
|
+
const monUp = (e) => {
|
|
448
|
+
if (feeding || passing || !mp.delete(e.pointerId) || !m) return;
|
|
449
|
+
const was = m;
|
|
450
|
+
if (was.kind === "pick") {
|
|
451
|
+
e.stopPropagation();
|
|
452
|
+
if (e.pointerId !== was.id) return;
|
|
453
|
+
loupe.hidden = true;
|
|
454
|
+
m = null;
|
|
455
|
+
const r = els.preview.getBoundingClientRect(), inside = e.clientX >= r.left && e.clientX <= r.right && e.clientY >= r.top && e.clientY <= r.bottom;
|
|
456
|
+
if (e.type === "pointerup" && inside) handOver(e.pointerId, e.clientX, e.clientY); // (slid off the picture: nothing is picked)
|
|
457
|
+
if (self.FableCutCompact) FableCutCompact.refresh();
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (was.kind === "tool" && e.pointerId === was.id) {
|
|
461
|
+
if (was.keys) { e.stopPropagation(); withKeys(e); FableCutCompact.usedAddPoint(); }
|
|
462
|
+
// two taps on a point: the double-click that removes it or makes it a corner (a pen's taps are its points)
|
|
463
|
+
const still = Math.hypot(e.clientX - was.x0, e.clientY - was.y0) < SLOP, twice = still && !was.pen && e.timeStamp - lastMon.t < TWICE_MS && Math.hypot(e.clientX - lastMon.x, e.clientY - lastMon.y) < 30;
|
|
464
|
+
lastMon = { t: twice ? 0 : e.timeStamp, x: e.clientX, y: e.clientY };
|
|
465
|
+
m = null;
|
|
466
|
+
if (twice && e.type === "pointerup") els.preview.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, cancelable: true, clientX: e.clientX, clientY: e.clientY }));
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
if (was.kind === "shape" || was.kind === "view" || was.kind === "none") {
|
|
470
|
+
e.stopPropagation();
|
|
471
|
+
if (mp.size && was.ids && !was.ids.includes(e.pointerId)) return; // a third finger (a palm) lifting changes nothing
|
|
472
|
+
if (was.kind === "shape" && was.undone && mp.size) { scheduleSave(); renderInspector(); m = { kind: "none" }; }
|
|
473
|
+
else if (was.ids && mp.size) m = { kind: "none" };
|
|
474
|
+
if (!mp.size) m = null;
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
if (e.pointerId !== was.id) return;
|
|
478
|
+
m = mp.size ? { kind: "none" } : null;
|
|
479
|
+
if (was.kind !== "wait" || e.type === "pointercancel") return;
|
|
480
|
+
e.stopPropagation();
|
|
481
|
+
const again = e.timeStamp - lastMon.t < TWICE_MS && Math.hypot(e.clientX - lastMon.x, e.clientY - lastMon.y) < 30;
|
|
482
|
+
lastMon = { t: again ? 0 : e.timeStamp, x: e.clientX, y: e.clientY };
|
|
483
|
+
const hit = pickClipAt(canvasPt(e), els.preview.width, els.preview.height);
|
|
484
|
+
if (again) {
|
|
485
|
+
if (hit && hit.kind === "text" && hit.id === state.selId && self.FableCutCompact && monCompact()) { FableCutCompact.editText(); return; }
|
|
486
|
+
if (state.viewZoom > 1.001) { resetMonitorView(); return; }
|
|
487
|
+
}
|
|
488
|
+
if (runtime.selectMode && hit) selectClip(hit.id, { toggle: true });
|
|
489
|
+
else if (!hit) { if (!runtime.selectMode) selectClip(null); } else if (hit.id !== state.selId) selectClip(hit.id);
|
|
490
|
+
};
|
|
491
|
+
window.addEventListener("pointerup", monUp, true);
|
|
492
|
+
window.addEventListener("pointercancel", monUp, true);
|
|
493
|
+
|
|
494
|
+
/* ═══ Everywhere else: long press, double tap, carrying ═══ */
|
|
495
|
+
let p = null; // one finger down outside the timeline
|
|
496
|
+
let swallow = 0; // a click that belongs to a long press already dealt with
|
|
497
|
+
let lastClick = { t: 0, el: null };
|
|
498
|
+
let ours = false; // an event made here, not the browser's
|
|
499
|
+
const outside = (t) => t && t.closest && !sc.contains(t) && !stage.contains(t);
|
|
500
|
+
|
|
501
|
+
document.addEventListener("pointerdown", (e) => {
|
|
502
|
+
if (e.pointerType !== "touch" || dismiss === e.pointerId) { // a mouse button on a touch laptop doesn't end a carry
|
|
503
|
+
if (p && !p.carry) { clearTimeout(p.timer); if (p.item) p.item.classList.remove("m-held"); p = null; }
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (p && e.pointerId !== p.id) { clearTimeout(p.timer); if (!p.carry) p = null; return; }
|
|
507
|
+
if (!outside(e.target) || isText(e.target) || e.target.closest("input[type=range], canvas, a[href]")) { p = null; return; }
|
|
508
|
+
p = { id: e.pointerId, target: e.target, x0: e.clientX, y0: e.clientY, x: e.clientX, y: e.clientY, item: e.target.closest("[draggable=true]") };
|
|
509
|
+
p.timer = setTimeout(held, HOLD_MS + 40);
|
|
510
|
+
}, true);
|
|
511
|
+
/* names and labels whose double-click renames or resets them: a hold does it too */
|
|
512
|
+
const HOLD_TWICE = ".mix-strip.bus .mix-name, [data-greset], [data-wreset]";
|
|
513
|
+
function held() {
|
|
514
|
+
if (!p) return;
|
|
515
|
+
p.held = true;
|
|
516
|
+
if (p.item) { buzz(14); p.item.classList.add("m-held"); return; } // moving carries it, letting go opens its menu
|
|
517
|
+
const name = p.target.closest(HOLD_TWICE);
|
|
518
|
+
if (menuFor(p)) { buzz(14); swallow = performance.now(); }
|
|
519
|
+
else if (name) { buzz(14); swallow = performance.now(); name.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, cancelable: true, clientX: p.x, clientY: p.y })); }
|
|
520
|
+
p = null; // nothing to carry here
|
|
521
|
+
}
|
|
522
|
+
function menuFor(q) {
|
|
523
|
+
if (!q.target.isConnected) return false;
|
|
524
|
+
ours = true;
|
|
525
|
+
const ev = new PointerEvent("contextmenu", { bubbles: true, cancelable: true, clientX: q.x, clientY: q.y, pointerType: "touch" });
|
|
526
|
+
try { q.target.dispatchEvent(ev); } finally { ours = false; }
|
|
527
|
+
return ev.defaultPrevented;
|
|
528
|
+
}
|
|
529
|
+
document.addEventListener("pointermove", (e) => {
|
|
530
|
+
if (!p || e.pointerId !== p.id || feeding) return;
|
|
531
|
+
p.x = e.clientX; p.y = e.clientY;
|
|
532
|
+
const far = Math.hypot(p.x - p.x0, p.y - p.y0);
|
|
533
|
+
if (!p.held) { if (far > SLOP) { clearTimeout(p.timer); p = null; } return; }
|
|
534
|
+
if (!p.carry && far > 6) startCarry();
|
|
535
|
+
if (p && p.carry) moveCarry();
|
|
536
|
+
}, true);
|
|
537
|
+
const endPress = (e) => {
|
|
538
|
+
if (dismiss === e.pointerId) { swallow = performance.now(); return; }
|
|
539
|
+
if (!p || e.pointerId !== p.id || feeding) return;
|
|
540
|
+
clearTimeout(p.timer);
|
|
541
|
+
const q = p; p = null;
|
|
542
|
+
if (q.item) q.item.classList.remove("m-held");
|
|
543
|
+
if (q.carry) { if (e.type === "pointerup") { q.x = e.clientX; q.y = e.clientY; } endCarry(q, e.type === "pointerup"); swallow = performance.now(); return; }
|
|
544
|
+
const fold = q.item && q.item.closest(".bin-folder");
|
|
545
|
+
if (q.held && e.type === "pointerup" && fold && self.FableCutCompact) { FableCutCompact.folderMenu(q.x, q.y, fold.dataset.folderId); swallow = performance.now(); return; }
|
|
546
|
+
if (q.held && e.type === "pointerup" && menuFor(q)) swallow = performance.now();
|
|
547
|
+
};
|
|
548
|
+
document.addEventListener("pointerup", endPress, true);
|
|
549
|
+
document.addEventListener("pointercancel", endPress, true);
|
|
550
|
+
// a held finger that then moves is carrying, not scrolling the list
|
|
551
|
+
document.addEventListener("touchmove", (e) => { if (p && p.held && e.cancelable) e.preventDefault(); }, { capture: true, passive: false });
|
|
552
|
+
|
|
553
|
+
/* Carrying: the events a mouse drag makes, with a real DataTransfer, so every drop target works as it is */
|
|
554
|
+
function drag(type, target, dt, x, y) {
|
|
555
|
+
const ev = new DragEvent(type, { bubbles: true, cancelable: true, clientX: x, clientY: y, dataTransfer: dt });
|
|
556
|
+
target.dispatchEvent(ev);
|
|
557
|
+
return ev.defaultPrevented;
|
|
558
|
+
}
|
|
559
|
+
function startCarry() {
|
|
560
|
+
const dt = new DataTransfer();
|
|
561
|
+
ours = true;
|
|
562
|
+
try { if (drag("dragstart", p.item, dt, p.x, p.y)) { p = null; return; } } finally { ours = false; }
|
|
563
|
+
const name = (p.item.querySelector(".bin-name, .bin-folder-name") || p.item).textContent.trim().slice(0, 40);
|
|
564
|
+
const ghost = document.createElement("div"), th = p.item.querySelector(".bin-thumb");
|
|
565
|
+
ghost.className = "m-ghost";
|
|
566
|
+
if (th) { const t = th.cloneNode(true); t.className = "m-ghost-pic " + th.className; ghost.appendChild(t); }
|
|
567
|
+
ghost.appendChild(document.createTextNode(name));
|
|
568
|
+
document.body.appendChild(ghost);
|
|
569
|
+
p.carry = { dt, ghost, over: null };
|
|
570
|
+
// the sheet covers the timeline, so it gives way at once; with folders to move between, when the finger leaves it
|
|
571
|
+
const open = tlCentred() && self.FableCutCompact && FableCutCompact.sheet, folders = state.binTab === "project" && project.folders.length;
|
|
572
|
+
p.carry.sheetTop = open && folders ? document.querySelector(".m-head").getBoundingClientRect().top : null;
|
|
573
|
+
if (open && !folders) FableCutCompact.closeSheet();
|
|
574
|
+
runtime.showEmptyTracks = true; state.dirtyTimeline = true;
|
|
575
|
+
}
|
|
576
|
+
/* what is under the finger, and whether it takes the thing carried */
|
|
577
|
+
function overCarry(q) {
|
|
578
|
+
const c = q.carry, el = document.elementFromPoint(q.x, q.y);
|
|
579
|
+
if (el !== c.over) { if (c.over && c.over.isConnected) drag("dragleave", c.over, c.dt, q.x, q.y); c.over = el; if (el) drag("dragenter", el, c.dt, q.x, q.y); }
|
|
580
|
+
c.ok = !!el && drag("dragover", el, c.dt, q.x, q.y);
|
|
581
|
+
}
|
|
582
|
+
function moveCarry() {
|
|
583
|
+
const c = p.carry;
|
|
584
|
+
if (c.sheetTop != null && p.y < c.sheetTop + 10) { FableCutCompact.closeSheet(); c.sheetTop = null; }
|
|
585
|
+
c.ghost.style.transform = `translate(${p.x - 30}px, ${p.y - 76}px)`;
|
|
586
|
+
overCarry(p);
|
|
587
|
+
c.ghost.classList.toggle("ok", c.ok);
|
|
588
|
+
const r = sc.getBoundingClientRect();
|
|
589
|
+
if (p.y > r.top && p.y < r.bottom) { if (p.x < r.left + 26) scrollBy(-8, 0); else if (p.x > r.right - 26) scrollBy(8, 0); }
|
|
590
|
+
}
|
|
591
|
+
function endCarry(q, drop) {
|
|
592
|
+
const c = q.carry;
|
|
593
|
+
c.ghost.remove();
|
|
594
|
+
runtime.showEmptyTracks = false; state.dirtyTimeline = true;
|
|
595
|
+
if (drop) overCarry(q); // asked again where the finger lifted: the last move may have been elsewhere
|
|
596
|
+
if (drop && c.ok) drag("drop", c.over, c.dt, q.x, q.y);
|
|
597
|
+
else if (c.over && c.over.isConnected) drag("dragleave", c.over, c.dt, q.x, q.y);
|
|
598
|
+
if (q.item.isConnected) drag("dragend", q.item, c.dt, q.x, q.y);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/* The browser's own versions of these, where it has them, would do everything twice */
|
|
602
|
+
const fromFinger = (e) => e.pointerType === "touch" || (e.sourceCapabilities && e.sourceCapabilities.firesTouchEvents);
|
|
603
|
+
window.addEventListener("contextmenu", (e) => {
|
|
604
|
+
if (ours || !e.isTrusted || !fromFinger(e) || isText(e.target)) return;
|
|
605
|
+
e.preventDefault(); e.stopImmediatePropagation();
|
|
606
|
+
}, true);
|
|
607
|
+
window.addEventListener("dragstart", (e) => { if (e.isTrusted && p) { e.preventDefault(); e.stopImmediatePropagation(); } }, true);
|
|
608
|
+
window.addEventListener("dblclick", (e) => { if (e.isTrusted && fromFinger(e)) e.stopImmediatePropagation(); }, true);
|
|
609
|
+
window.addEventListener("click", (e) => {
|
|
610
|
+
if (!e.isTrusted || !fromFinger(e)) return;
|
|
611
|
+
if (performance.now() - swallow < 500) { swallow = 0; e.preventDefault(); e.stopImmediatePropagation(); return; }
|
|
612
|
+
const lab = e.target.closest && e.target.closest("label");
|
|
613
|
+
if (!outside(e.target) || isText(e.target) || e.target.closest("button, summary") || (lab && lab.control)) { lastClick.t = 0; return; }
|
|
614
|
+
const again = e.timeStamp - lastClick.t < TWICE_MS && lastClick.el === e.target;
|
|
615
|
+
lastClick = { t: again ? 0 : e.timeStamp, el: e.target };
|
|
616
|
+
if (again) e.target.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, cancelable: true, clientX: e.clientX, clientY: e.clientY }));
|
|
617
|
+
}, true);
|
|
618
|
+
|
|
619
|
+
/* ═══ Panels ═══ */
|
|
620
|
+
/* Sliders. The browser moves the thumb to wherever it is touched, so scrolling a panel changed whatever
|
|
621
|
+
was under the finger. Here a slider moves with the finger, from where it was, and only once the finger
|
|
622
|
+
goes sideways; up and down scrolls. A mixer fader
|
|
623
|
+
does the same up and down, and sideways scrolls the strips. */
|
|
624
|
+
let sl = null;
|
|
625
|
+
document.addEventListener("touchstart", (e) => {
|
|
626
|
+
const el = e.target, q = e.touches[0];
|
|
627
|
+
if (sl && sl.ended) sl = null;
|
|
628
|
+
if (!el || el.tagName !== "INPUT" || el.type !== "range" || el.disabled || e.touches.length !== 1) return;
|
|
629
|
+
const r = el.getBoundingClientRect(), up = el.classList.contains("mix-fader");
|
|
630
|
+
sl = { el, id: q.identifier, up, x: q.clientX, y: q.clientY, v: +el.value, cur: el.value, w: Math.max(40, (up ? r.height : r.width) - 16) };
|
|
631
|
+
}, { capture: true, passive: true });
|
|
632
|
+
document.addEventListener("touchmove", (e) => {
|
|
633
|
+
if (!sl || sl.ended || sl.dead) return;
|
|
634
|
+
const q = [...e.touches].find((t) => t.identifier === sl.id);
|
|
635
|
+
if (!q) return;
|
|
636
|
+
const dx = q.clientX - sl.x, dy = q.clientY - sl.y;
|
|
637
|
+
if (!sl.go) {
|
|
638
|
+
if (Math.hypot(dx, dy) < 7) return;
|
|
639
|
+
if (sl.up ? Math.abs(dx) > Math.abs(dy) : Math.abs(dy) > Math.abs(dx)) { sl.dead = true; return; }
|
|
640
|
+
sl.go = true; sl.x = q.clientX; sl.y = q.clientY;
|
|
641
|
+
sl.el.focus({ preventScroll: true }); // what arms the panel's one undo step per drag
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
if (e.cancelable) e.preventDefault();
|
|
645
|
+
const min = +sl.el.min || 0, max = sl.el.max === "" ? 100 : +sl.el.max;
|
|
646
|
+
sl.el.value = sl.v + (sl.up ? sl.y - q.clientY : q.clientX - sl.x) / sl.w * (max - min);
|
|
647
|
+
if (sl.el.value === sl.cur) return;
|
|
648
|
+
sl.cur = sl.el.value;
|
|
649
|
+
sl.el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
650
|
+
}, { capture: true, passive: false });
|
|
651
|
+
let slTap = { t: 0, el: null };
|
|
652
|
+
const slEnd = (e) => {
|
|
653
|
+
if (!sl || sl.ended || ![...e.changedTouches].some((t) => t.identifier === sl.id)) return; // another finger lifting doesn't end it
|
|
654
|
+
const s = sl;
|
|
655
|
+
s.ended = true;
|
|
656
|
+
// a tapped slider gets no click from the browser, so its double-click (reset) is counted here
|
|
657
|
+
if (!s.go && !s.dead && e.type === "touchend") {
|
|
658
|
+
const twice = slTap.el === s.el && e.timeStamp - slTap.t < TWICE_MS;
|
|
659
|
+
slTap = { t: twice ? 0 : e.timeStamp, el: s.el };
|
|
660
|
+
if (twice) s.el.dispatchEvent(new MouseEvent("dblclick", { bubbles: true, cancelable: true, clientX: s.x, clientY: s.y }));
|
|
661
|
+
if (twice) s.cur = s.el.value;
|
|
662
|
+
}
|
|
663
|
+
if (s.go) { s.el.dispatchEvent(new Event("change", { bubbles: true })); s.el.blur(); }
|
|
664
|
+
setTimeout(() => { if (sl === s) sl = null; }, 120); // the browser's own change event may still come
|
|
665
|
+
};
|
|
666
|
+
document.addEventListener("touchend", slEnd, true);
|
|
667
|
+
document.addEventListener("touchcancel", slEnd, true);
|
|
668
|
+
for (const ev of ["input", "change"]) window.addEventListener(ev, (e) => {
|
|
669
|
+
if (!sl || !e.isTrusted || e.target !== sl.el) return;
|
|
670
|
+
e.stopImmediatePropagation();
|
|
671
|
+
sl.el.value = sl.cur;
|
|
672
|
+
}, true);
|
|
673
|
+
|
|
674
|
+
/* Color wheels and the curve editor fill the sheet, so a swipe over them scrolls it. A wheel turns when the
|
|
675
|
+
finger starts on its dot; a curve bends when it starts on a point or on the line itself. */
|
|
676
|
+
let cd = null;
|
|
677
|
+
function discDot(cv) {
|
|
678
|
+
const c = getClip(state.selId), w = cv.dataset.wdisc, r = cv.getBoundingClientRect(), R = r.width / 2;
|
|
679
|
+
if (!c) return null;
|
|
680
|
+
const g = Color.fullGrade(targetGrade(c))[w], [cb, cr] = Color.rgbToWheel(g[0], g[1], g[2]);
|
|
681
|
+
return { x: r.left + R + cb / WHEEL_RIM[w] * R, y: r.top + r.height / 2 - cr / WHEEL_RIM[w] * R };
|
|
682
|
+
}
|
|
683
|
+
function onCurve(cv, x, y) {
|
|
684
|
+
const c = getClip(state.selId), q = cv._map, r = cv.getBoundingClientRect(), tab = colorState.curveTab;
|
|
685
|
+
if (!c || !q) return false;
|
|
686
|
+
const pts = curvePoints(c, tab, colorState.curveCh), at = (u, v) => Math.hypot(r.left + q.X(u) / q.dpr - x, r.top + q.Y(v) / q.dpr - y);
|
|
687
|
+
const u = q.toData(x - r.left, y - r.top)[0], span = curveSpec(tab).xMax, line = (d) => { const w = clamp(u + d * span, 0, span); return at(w, curveValueAt(pts, tab, w)); };
|
|
688
|
+
return pts.some(([a, v]) => at(a, v) < 22) || [-.05, -.025, 0, .025, .05].some((d) => line(d) < 22);
|
|
689
|
+
}
|
|
690
|
+
els.colorPage.addEventListener("pointerdown", (e) => {
|
|
691
|
+
const cv = e.pointerType === "touch" && !passing && e.target.closest("[data-wdisc], [data-cved]");
|
|
692
|
+
if (!cv) return;
|
|
693
|
+
const dot = cv.dataset.wdisc && discDot(cv);
|
|
694
|
+
const on = cv.dataset.wdisc ? !!dot && Math.hypot(dot.x - e.clientX, dot.y - e.clientY) < 34 : onCurve(cv, e.clientX, e.clientY);
|
|
695
|
+
cd = { id: e.pointerId, on, curve: !cv.dataset.wdisc, cv, x: e.clientX, y: e.clientY };
|
|
696
|
+
if (!on) e.stopPropagation();
|
|
697
|
+
}, true);
|
|
698
|
+
document.addEventListener("touchmove", (e) => { if (cd && cd.on && e.cancelable) e.preventDefault(); }, { capture: true, passive: false });
|
|
699
|
+
els.colorPage.addEventListener("pointerup", (e) => {
|
|
700
|
+
if (!cd || e.pointerId !== cd.id || passing) return;
|
|
701
|
+
const q = cd;
|
|
702
|
+
cd = null;
|
|
703
|
+
if (q.on || !q.curve || Math.hypot(e.clientX - q.x, e.clientY - q.y) > SLOP) return;
|
|
704
|
+
const send = (type) => q.cv.dispatchEvent(new PointerEvent(type, { bubbles: true, cancelable: true, clientX: q.x, clientY: q.y, pointerId: q.id, pointerType: "touch", button: 0, buttons: type === "pointerdown" ? 1 : 0 }));
|
|
705
|
+
passing = true;
|
|
706
|
+
q.cv.setPointerCapture = () => { }; // the finger is already lifting: there is nothing left to capture
|
|
707
|
+
try { send("pointerdown"); send("pointerup"); } finally { passing = false; delete q.cv.setPointerCapture; }
|
|
708
|
+
}, true);
|
|
709
|
+
els.colorPage.addEventListener("pointercancel", () => { cd = null; }, true);
|
|
710
|
+
|
|
711
|
+
/* Transcript: a tap goes to a word. Hold a word and drag to select a run; afterwards its first and last word
|
|
712
|
+
(they show a grip) move when dragged, like selected text anywhere on a phone */
|
|
713
|
+
let ts = null;
|
|
714
|
+
const wordAt = (x, y) => { const el = document.elementFromPoint(x, y); return el && el.closest && el.closest(".tr-body .tw"); };
|
|
715
|
+
const selectWords = (a, b) => { trState.anchor = a; trState.sel = { i: Math.min(a, b), j: Math.max(a, b) }; paintTranscriptMarks(); };
|
|
716
|
+
els.transcriptPage.addEventListener("pointerdown", (e) => {
|
|
717
|
+
const w = e.pointerType === "touch" && e.target.closest(".tw");
|
|
718
|
+
if (ts) { clearTimeout(ts.timer); ts = null; }
|
|
719
|
+
if (!w) return;
|
|
720
|
+
const k = +w.dataset.k, s = trState.sel;
|
|
721
|
+
ts = { id: e.pointerId, k, x: e.clientX, y: e.clientY, anchor: k, go: false };
|
|
722
|
+
if (s && s.j > s.i && (k === s.i || k === s.j)) { ts.anchor = k === s.i ? s.j : s.i; ts.go = true; }
|
|
723
|
+
else ts.timer = setTimeout(() => { if (!ts) return; ts.go = ts.picked = true; buzz(14); selectWords(ts.k, ts.k); }, HOLD_MS);
|
|
724
|
+
}, true);
|
|
725
|
+
document.addEventListener("touchmove", (e) => { if (ts && ts.go && e.cancelable) e.preventDefault(); }, { capture: true, passive: false });
|
|
726
|
+
document.addEventListener("pointermove", (e) => {
|
|
727
|
+
if (!ts || e.pointerId !== ts.id) return;
|
|
728
|
+
if (!ts.go) { if (Math.hypot(e.clientX - ts.x, e.clientY - ts.y) > SLOP) { clearTimeout(ts.timer); ts = null; } return; }
|
|
729
|
+
const body = trState.el.body, r = body.getBoundingClientRect();
|
|
730
|
+
if (e.clientY > r.bottom - 26) body.scrollTop += 9; else if (e.clientY < r.top + 26) body.scrollTop -= 9;
|
|
731
|
+
const w = wordAt(e.clientX, e.clientY);
|
|
732
|
+
if (w && +w.dataset.k !== ts.at) { ts.at = +w.dataset.k; ts.picked = true; selectWords(ts.anchor, ts.at); }
|
|
733
|
+
}, true);
|
|
734
|
+
for (const ev of ["pointerup", "pointercancel"]) document.addEventListener(ev, (e) => {
|
|
735
|
+
if (!ts || e.pointerId !== ts.id) return;
|
|
736
|
+
clearTimeout(ts.timer);
|
|
737
|
+
if (ts.picked) swallow = trTap = performance.now(); // the tap the browser still reports is not a tap on a word
|
|
738
|
+
ts = null;
|
|
739
|
+
}, true);
|
|
740
|
+
let trTap = 0;
|
|
741
|
+
els.transcriptPage.addEventListener("mousedown", (e) => { if (performance.now() - trTap < 500) { e.preventDefault(); e.stopImmediatePropagation(); } }, true);
|
|
742
|
+
|
|
743
|
+
/* A slider's number: tap it and type */
|
|
744
|
+
document.addEventListener("click", (e) => {
|
|
745
|
+
const val = e.target.closest && e.target.closest(".insp-row .val"), rowEl = val && val.closest(".insp-row");
|
|
746
|
+
const range = rowEl && rowEl.querySelector("input[type=range]");
|
|
747
|
+
if (!range || range.disabled || !fromFinger(e)) return;
|
|
748
|
+
const inp = document.createElement("input"), r = val.getBoundingClientRect();
|
|
749
|
+
inp.type = "text"; inp.inputMode = "decimal"; inp.className = "m-val"; inp.value = String(+range.value);
|
|
750
|
+
inp.setAttribute("aria-label", "Value");
|
|
751
|
+
inp.style.width = Math.max(64, r.width + 16) + "px";
|
|
752
|
+
val.hidden = true; val.after(inp);
|
|
753
|
+
inp.focus(); inp.select();
|
|
754
|
+
let done = false;
|
|
755
|
+
const close = (keep) => {
|
|
756
|
+
if (done) return;
|
|
757
|
+
done = true;
|
|
758
|
+
const v = parseFloat(inp.value.replace(",", "."));
|
|
759
|
+
inp.remove(); val.hidden = false;
|
|
760
|
+
if (!keep || !Number.isFinite(v) || v === +range.value || !range.isConnected) return;
|
|
761
|
+
range.focus({ preventScroll: true });
|
|
762
|
+
range.value = v;
|
|
763
|
+
for (const t of ["input", "change"]) range.dispatchEvent(new Event(t, { bubbles: true }));
|
|
764
|
+
range.blur();
|
|
765
|
+
};
|
|
766
|
+
inp.addEventListener("keydown", (k) => { k.stopPropagation(); if (k.key === "Enter") close(true); else if (k.key === "Escape") close(false); });
|
|
767
|
+
inp.addEventListener("blur", () => close(true));
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
/* Hold on a property's name: what Shift+click and Ctrl+click on it do */
|
|
771
|
+
els.inspector.addEventListener("contextmenu", (e) => {
|
|
772
|
+
const lab = ours && e.target.closest("label.insp-reset[data-reset]");
|
|
773
|
+
if (!lab) return;
|
|
774
|
+
e.preventDefault();
|
|
775
|
+
const keys = lab.dataset.reset.split(",").map((s) => s.trim()).filter(Boolean), menu = document.createElement("div");
|
|
776
|
+
menu.className = "ctx-menu";
|
|
777
|
+
const add = (text, all) => {
|
|
778
|
+
const b = document.createElement("button");
|
|
779
|
+
b.type = "button"; b.className = "ctx-item"; b.textContent = text;
|
|
780
|
+
b.addEventListener("click", () => { hideTrackCtxMenu(); if (isGroupLocked(getClip(state.selId))) toastLocked(); else applyInspectorReset(keys, all); });
|
|
781
|
+
menu.appendChild(b);
|
|
782
|
+
};
|
|
783
|
+
const c = getClip(state.selId), here = keys.some((k) => kfAtPlayhead(c, k)), any = keys.some((k) => c && c.keyframes && c.keyframes[k]);
|
|
784
|
+
add(here ? "Remove the keyframe at the playhead" : any ? "Reset it at the playhead" : `Reset ${lab.textContent.trim()}`, !any);
|
|
785
|
+
if (any) add("Reset it and clear all its keyframes", true);
|
|
786
|
+
showPopover(menu, e.clientX, e.clientY);
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
/* The on-screen keyboard covers the bottom of the page without resizing it: the open sheet moves up above
|
|
790
|
+
it and the field in use scrolls into view */
|
|
791
|
+
const vv = window.visualViewport;
|
|
792
|
+
function keyboard() {
|
|
793
|
+
const el = document.activeElement, app = $("app");
|
|
794
|
+
const gap = isText(el) && el.tagName !== "SELECT" && monCompact() ? Math.max(0, Math.round(window.innerHeight - vv.height - vv.offsetTop)) : 0;
|
|
795
|
+
const up = gap > 120 ? gap : 0;
|
|
796
|
+
if ((parseInt(app.style.getPropertyValue("--m-kb")) || 0) === up) return;
|
|
797
|
+
if (up) app.style.setProperty("--m-kb", up + "px"); else app.style.removeProperty("--m-kb");
|
|
798
|
+
document.documentElement.style.setProperty("--m-kb-page", up + "px");
|
|
799
|
+
if (up) requestAnimationFrame(() => el.scrollIntoView({ block: "center" }));
|
|
800
|
+
}
|
|
801
|
+
if (vv) { vv.addEventListener("resize", keyboard); vv.addEventListener("scroll", keyboard); document.addEventListener("focusout", () => setTimeout(keyboard, 80)); }
|
|
802
|
+
|
|
803
|
+
/* Holding a frame-step button keeps stepping */
|
|
804
|
+
for (const id of ["btnBack", "btnFwd"]) {
|
|
805
|
+
const b = $(id);
|
|
806
|
+
let t = 0;
|
|
807
|
+
const stop = () => { clearTimeout(t); clearInterval(t); t = 0; };
|
|
808
|
+
b.addEventListener("pointerdown", (e) => {
|
|
809
|
+
if (e.pointerType !== "touch") return;
|
|
810
|
+
stop();
|
|
811
|
+
t = setTimeout(() => { swallow = performance.now() + 1e6; t = setInterval(() => stepTransport(id === "btnFwd" ? 1 : -1), 70); }, 380);
|
|
812
|
+
});
|
|
813
|
+
for (const ev of ["pointerup", "pointercancel", "pointerleave"]) b.addEventListener(ev, () => { if (swallow > performance.now()) swallow = performance.now(); stop(); });
|
|
814
|
+
}
|
|
815
|
+
})();
|