Skip to content

Commit 98be0db

Browse files
feildmasterclaude
andcommitted
feat: drag archive rows by touch
Native HTML drag does not start from a touch on iPad Safari, so the rows could not be dragged: the touch scrolled the page behind the archive instead. Touch input now uses Shopify Draggable's touch sensor on the elements page, with a 250ms hold to start. Mouse and pen keep the native drag. Drop targets register with initDrop, and a touch drop walks up from the element under the finger to the first target that accepts the item, so touch and native drops share one set of rules. Draggable's drag:stop event carries no sensor event, so the release point is read from the DOM event the touch sensor dispatches on the container. A cancelled touch drops nothing. The handle is the whole left side of the row, so holding the blank space next to the name also starts a drag. The floating copy is appended inside the archive popover so it is not hidden behind it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 65d0923 commit 98be0db

5 files changed

Lines changed: 157 additions & 11 deletions

File tree

‎public/resources/modules/archive/elements.js‎

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { openExport } from '../exportDialog/index.js';
99
import { bindFilePicker, importFiles, initFileDrop } from '../importer/index.js';
1010
import { bindFilter, filterRows } from './filter.js';
1111
import Item from './Item.js';
12+
import initTouchDrag from './touchDrag.js';
1213
import App from '../UndercardEditor.js';
1314
import editor from '../editor/editor.js';
1415

@@ -73,6 +74,9 @@ class TrashItem extends Item {
7374

7475
const trashRef = new TrashItem();
7576

77+
/** @type {WeakMap<Element, { accepted: () => Item | undefined; receive: () => boolean; }>} */
78+
const dropTargets = new WeakMap();
79+
7680
let dragSrc;
7781
let ready = false;
7882

@@ -194,6 +198,15 @@ export function load() {
194198
},
195199
});
196200

201+
initTouchDrag(page, {
202+
draggable: 'li[draggable="true"]',
203+
handle: '.info',
204+
mirrorParent: archive,
205+
start: beginDrag,
206+
move: touchMove,
207+
stop: touchStop,
208+
});
209+
197210
initFileDrop(archive, (files) => dropFiles(files));
198211
initFileDrop(document.querySelector('.archive-button'), (files) => dropFiles(files, { reveal: false }));
199212
bindFilePicker(page.querySelector('button.import'), page.querySelector('input[type="file"]'));
@@ -418,25 +431,55 @@ function initButtons(container, item) {
418431
});
419432
}
420433

434+
/** @param {HTMLElement} source */
435+
function beginDrag(source) {
436+
dragSrc = source;
437+
closeTip();
438+
requestAnimationFrame(refreshDropOut);
439+
}
440+
441+
function endDrag() {
442+
removeClass('drag-over');
443+
dragSrc = undefined;
444+
refreshDropOut();
445+
}
446+
421447
/** @param {HTMLElement} container */
422448
function initDrag(container) {
423449
container.addEventListener('dragstart', (e) => {
424-
dragSrc = container;
425450
container.classList.add('dragging');
426451
e.stopPropagation();
427452
e.dataTransfer.effectAllowed = 'move';
428-
closeTip();
429-
requestAnimationFrame(refreshDropOut);
453+
beginDrag(container);
430454
});
431455

432456
container.addEventListener('dragend', () => {
433457
container.classList.remove('dragging');
434-
removeClass('drag-over');
435-
dragSrc = undefined;
436-
refreshDropOut();
458+
endDrag();
437459
});
438460
}
439461

462+
/** @param {Element | null} element */
463+
function dropTargetAt(element) {
464+
for (let node = element; node; node = node.parentElement) {
465+
if (dropTargets.get(node)?.accepted()) return node;
466+
}
467+
return undefined;
468+
}
469+
470+
/** @param {Element | null} element */
471+
function touchMove(element) {
472+
removeClass('drag-over');
473+
dropTargetAt(element)?.classList.add('drag-over');
474+
}
475+
476+
/** @param {Element | null} element */
477+
function touchStop(element) {
478+
const target = element && dropTargetAt(element);
479+
if (target) dropTargets.get(target).receive();
480+
endDrag();
481+
}
482+
440483
function dragged() {
441484
if (!dragSrc) return undefined;
442485
const { id, type } = dragSrc.dataset;
@@ -466,6 +509,16 @@ function initDrop(container, { accepts, drop }) {
466509
return item && accepts(item) ? item : undefined;
467510
}
468511

512+
function receive() {
513+
const item = accepted();
514+
if (!item) return false;
515+
if (item.trashed) cascade(item, 'restore');
516+
drop(item);
517+
return true;
518+
}
519+
520+
dropTargets.set(container, { accepted, receive });
521+
469522
container.addEventListener('dragover', (e) => {
470523
if (!accepted()) return;
471524
e.preventDefault();
@@ -477,11 +530,8 @@ function initDrop(container, { accepts, drop }) {
477530
container.classList.remove('drag-over');
478531
});
479532
container.addEventListener('drop', (e) => {
480-
const item = accepted();
481-
if (!item) return;
482-
e.stopPropagation();
483-
if (item.trashed) cascade(item, 'restore');
484-
drop(item);
533+
if (accepted()) e.stopPropagation();
534+
receive();
485535
});
486536
}
487537

‎public/resources/modules/archive/elements.test.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { beforeAll, describe, expect, it, vi } from 'vitest';
22
import registryEvents from '../elements/registryEvents.js';
33
import settings, { Settings } from '../settings.js';
44
import EventEmitter from '../utils/EventEmitter.js';
5+
import initTouchDrag from './touchDrag.js';
56

67
const { store } = vi.hoisted(() => ({ store: new Map() }));
78

@@ -10,6 +11,7 @@ vi.mock('../UndercardEditor.js', () => ({ default: { addGroup: vi.fn(), save: vi
1011
vi.mock('../editor/editor.js', () => ({ default: { on: () => {}, open: vi.fn() } }));
1112
vi.mock('../exportDialog/index.js', () => ({ openExport: vi.fn() }));
1213
vi.mock('../importer/index.js', () => ({ bindFilePicker: vi.fn(), importFiles: vi.fn(), initFileDrop: vi.fn() }));
14+
vi.mock('./touchDrag.js', () => ({ default: vi.fn() }));
1315
vi.mock('../elements/registry.js', () => ({
1416
getAll: () => [...store.values()],
1517
init: (props) => element(props),
@@ -24,6 +26,7 @@ vi.mock('../elements/registry.js', () => ({
2426
}));
2527

2628
let copies = 0;
29+
let touchArgs;
2730

2831
function element({ id, type, content }) {
2932
const el = new EventEmitter();
@@ -97,6 +100,7 @@ beforeAll(async () => {
97100
store.set('other', element({ id: 'other', type: 'group', content: [] }));
98101
const { load } = await import('./elements.js');
99102
load();
103+
[touchArgs] = vi.mocked(initTouchDrag).mock.calls;
100104
});
101105

102106
describe('load', () => {
@@ -253,6 +257,54 @@ describe('leaving a group', () => {
253257
});
254258
});
255259

260+
describe('touch drag', () => {
261+
const touch = () => touchArgs[1];
262+
const label = (id) => row(id).querySelector('.name');
263+
const highlighted = () => document.querySelectorAll('.drag-over').length;
264+
265+
it('hands the elements page to the touch sensor', () => {
266+
expect(touchArgs[0]).toBe(document.querySelector('.archive [data-page="elements"]'));
267+
});
268+
269+
it('drops on the row under the finger', () => {
270+
touch().start(row('card'));
271+
touch().move(label('group'));
272+
expect(row('group').classList.contains('drag-over')).toBe(true);
273+
touch().stop(label('group'));
274+
expect(row('card').parentElement).toBe(extraOf('group'));
275+
expect(store.get('group').content).toContain('card');
276+
expect(highlighted()).toBe(0);
277+
});
278+
279+
it('offers the drop-out target for a grouped item, and leaves through it', async () => {
280+
touch().start(row('card'));
281+
await frame();
282+
expect(hidden(dropOut())).toBe(false);
283+
touch().move(dropOut());
284+
expect(dropOut().classList.contains('drag-over')).toBe(true);
285+
touch().stop(dropOut());
286+
expect(row('card').parentElement).toBe(root());
287+
expect(hidden(dropOut())).toBe(true);
288+
});
289+
290+
it('ignores a target that rejects the item', () => {
291+
touch().start(row('group'));
292+
touch().move(label('other'));
293+
expect(highlighted()).toBe(0);
294+
touch().stop(label('other'));
295+
expect(row('group').parentElement).toBe(root());
296+
expect(store.get('other').content).not.toContain('group');
297+
});
298+
299+
it('drops nothing when the touch is cancelled', () => {
300+
touch().start(row('card'));
301+
touch().move(label('group'));
302+
touch().stop(null);
303+
expect(row('card').parentElement).toBe(root());
304+
expect(highlighted()).toBe(0);
305+
});
306+
});
307+
256308
describe('duplicate', () => {
257309
const fixture = ['card', 'child', 'group', 'other', 'trash'];
258310
const extras = () => [...root().querySelectorAll('li[data-id]')]

‎public/resources/modules/archive/elements.trash.test.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ vi.mock('../UndercardEditor.js', () => ({ default: { addGroup: vi.fn(), save: vi
3333
vi.mock('../editor/editor.js', () => ({ default: { on: () => {}, open: vi.fn() } }));
3434
vi.mock('../exportDialog/index.js', () => ({ openExport: vi.fn() }));
3535
vi.mock('../importer/index.js', () => ({ bindFilePicker: vi.fn(), importFiles: vi.fn(), initFileDrop: vi.fn() }));
36+
vi.mock('./touchDrag.js', () => ({ default: vi.fn() }));
3637
vi.mock('../elements/registry.js', () => ({
3738
getAll: () => [...store.values()],
3839
init: (props) => makeElement(props),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Draggable } from 'https://ga.jspm.io/npm:@shopify/draggable@1.2.1/build/esm/index.mjs';
2+
3+
const HOLD_MS = 250;
4+
5+
/**
6+
* @param {HTMLElement} container
7+
* @param {{
8+
* draggable: string;
9+
* handle: string;
10+
* mirrorParent: HTMLElement;
11+
* start: (source: HTMLElement) => void;
12+
* move: (target: Element | null) => void;
13+
* stop: (target: Element | null) => void;
14+
* }} options stop receives null when the touch was cancelled
15+
*/
16+
export default function initTouchDrag(container, { draggable, handle, mirrorParent, start, move, stop }) {
17+
const { Plugins, Sensors } = Draggable;
18+
new Draggable([container], {
19+
draggable,
20+
handle,
21+
delay: { touch: HOLD_MS },
22+
mirror: { appendTo: mirrorParent, constrainDimensions: true },
23+
exclude: {
24+
plugins: [Plugins.Focusable, Plugins.Announcement],
25+
sensors: [Sensors.MouseSensor],
26+
},
27+
})
28+
.on('drag:start', ({ originalSource }) => start(originalSource))
29+
.on('drag:move', ({ sensorEvent }) => move(sensorEvent.target));
30+
31+
container.addEventListener('drag:stop', ({ detail }) => {
32+
stop(detail.originalEvent?.type === 'touchcancel' ? null : detail.target);
33+
});
34+
}

‎public/resources/styles/archive.css‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@
9595
display: block;
9696
}
9797

98+
.archive .draggable-source--is-dragging {
99+
opacity: 0.4;
100+
}
101+
102+
.archive .draggable-mirror {
103+
border: 2px solid var(--border);
104+
}
105+
98106
.archive ul {
99107
list-style: none;
100108
margin: 0;
@@ -116,6 +124,7 @@
116124
}
117125

118126
.archive .info {
127+
flex: 1;
119128
display: flex;
120129
align-items: center;
121130
gap: 5px;

0 commit comments

Comments
 (0)