@m3ui-vue/m3ui-vue 1.3.4 → 1.3.5
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/dist/API.md +3 -2
- package/dist/api.json +1 -1
- package/dist/components/MFloorPlan.vue.d.ts +21 -2
- package/dist/m3ui.js +185 -180
- package/dist/m3ui.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/MFloorPlan.test.ts +38 -0
- package/src/components/MFloorPlan.vue +29 -8
package/package.json
CHANGED
|
@@ -42,6 +42,44 @@ describe('MFloorPlan', () => {
|
|
|
42
42
|
expect(w.emitted('move')![0]![0]).toEqual([{ id: 1, x: 140, y: 140 }, { id: 2, x: 340, y: 140 }])
|
|
43
43
|
expect((w.props('tables') as FloorTable[])[0]).toMatchObject({ x: 140, y: 140 })
|
|
44
44
|
})
|
|
45
|
+
it('editable + selectable multiple: a click toggles a table in the selection, a drag moves that table alone', async () => {
|
|
46
|
+
const w = setup({ editable: true, selectable: 'multiple', dragMerged: true, merges: [{ id: 'm', tables: [1, 2] }], grid: 20, selection: [], 'onUpdate:selection': (v: unknown) => w.setProps({ selection: v as number[] }) })
|
|
47
|
+
const press = (label: string, dx = 0) => {
|
|
48
|
+
table(w, label).element.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, button: 0, clientX: 0, clientY: 0, pointerId: 1 }))
|
|
49
|
+
if (dx) window.dispatchEvent(new PointerEvent('pointermove', { clientX: dx, clientY: 0, pointerId: 1 }))
|
|
50
|
+
window.dispatchEvent(new PointerEvent('pointerup', { clientX: dx, clientY: 0, pointerId: 1 }))
|
|
51
|
+
}
|
|
52
|
+
// A press alone doesn't change the selection; the click toggles
|
|
53
|
+
press('T1')
|
|
54
|
+
expect(w.props('selection')).toEqual([])
|
|
55
|
+
await table(w, 'T1').trigger('click')
|
|
56
|
+
await table(w, 'T2').trigger('click')
|
|
57
|
+
expect(w.props('selection')).toEqual([1, 2])
|
|
58
|
+
await table(w, 'T1').trigger('click')
|
|
59
|
+
expect(w.props('selection')).toEqual([2])
|
|
60
|
+
// A drag moves only the table pressed: not the rest of the selection, nor its merge (dragMerged); the selection stays
|
|
61
|
+
await table(w, 'T1').trigger('click')
|
|
62
|
+
expect(w.props('selection')).toEqual([2, 1])
|
|
63
|
+
await new Promise((r) => setTimeout(r))
|
|
64
|
+
press('T2', 40)
|
|
65
|
+
await nextTick()
|
|
66
|
+
expect((w.props('tables') as FloorTable[]).map((t) => t.x)).toEqual([100, 340])
|
|
67
|
+
await new Promise((r) => setTimeout(r))
|
|
68
|
+
press('T1', 60)
|
|
69
|
+
await nextTick()
|
|
70
|
+
expect((w.props('tables') as FloorTable[]).map((t) => t.x)).toEqual([160, 340])
|
|
71
|
+
expect(w.props('selection')).toEqual([2, 1])
|
|
72
|
+
})
|
|
73
|
+
it('tableSnap cell: a dropped table sits in the middle of a grid square, and arrows keep it there', async () => {
|
|
74
|
+
const w = setup({ editable: true, grid: 20, tableSnap: 'cell', selection: [1], 'onUpdate:selection': (v: unknown) => w.setProps({ selection: v as number[] }) })
|
|
75
|
+
table(w, 'T1').element.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, button: 0, clientX: 0, clientY: 0, pointerId: 1 }))
|
|
76
|
+
window.dispatchEvent(new PointerEvent('pointermove', { clientX: 33, clientY: 47, pointerId: 1 }))
|
|
77
|
+
window.dispatchEvent(new PointerEvent('pointerup', { clientX: 33, clientY: 47, pointerId: 1 }))
|
|
78
|
+
await nextTick()
|
|
79
|
+
expect((w.props('tables') as FloorTable[])[0]).toMatchObject({ x: 130, y: 150 })
|
|
80
|
+
await table(w, 'T1').trigger('keydown', { key: 'ArrowRight' })
|
|
81
|
+
expect((w.props('tables') as FloorTable[])[0]).toMatchObject({ x: 150, y: 150 })
|
|
82
|
+
})
|
|
45
83
|
it('editable: arrows nudge by the grid, and tables stay inside the plan', async () => {
|
|
46
84
|
const w = setup({ editable: true, grid: 10 })
|
|
47
85
|
await table(w, 'T1').trigger('keydown', { key: 'ArrowLeft' })
|
|
@@ -111,6 +111,11 @@ const props = withDefaults(defineProps<{
|
|
|
111
111
|
grid?: number
|
|
112
112
|
/** Draw the grid while editing */
|
|
113
113
|
showGrid?: boolean
|
|
114
|
+
/**
|
|
115
|
+
* Where a table's middle snaps: `point` a grid point (default), `cell` the middle of a grid square, so tables sit in
|
|
116
|
+
* the squares between the points while walls, zones and decor snap to the points (a plan of cells)
|
|
117
|
+
*/
|
|
118
|
+
tableSnap?: 'point' | 'cell'
|
|
114
119
|
/** Draw chairs from `seats` */
|
|
115
120
|
chairs?: boolean
|
|
116
121
|
/** Decor kinds of yours, or the built-in ones restyled (merged over them by name) */
|
|
@@ -119,7 +124,11 @@ const props = withDefaults(defineProps<{
|
|
|
119
124
|
rotatable?: boolean
|
|
120
125
|
/** Dragging (or nudging) a merged table moves its whole merge with it */
|
|
121
126
|
dragMerged?: boolean
|
|
122
|
-
/**
|
|
127
|
+
/**
|
|
128
|
+
* What a click on a table selects outside editing: nothing, one, or several (toggling; to merge them). While
|
|
129
|
+
* editing, `multiple` keeps clicks toggling tables in and out of the selection, and a drag moves the table pressed
|
|
130
|
+
* on its own (to bring the ones being merged together)
|
|
131
|
+
*/
|
|
123
132
|
selectable?: 'none' | 'single' | 'multiple'
|
|
124
133
|
/** Whether these tables may be merged: `true`, or `false` / a reason to refuse (shown by `merge-refused`) */
|
|
125
134
|
canMerge?: (ids: (string | number)[]) => boolean | string
|
|
@@ -137,6 +146,7 @@ const props = withDefaults(defineProps<{
|
|
|
137
146
|
editable: false,
|
|
138
147
|
grid: 10,
|
|
139
148
|
showGrid: true,
|
|
149
|
+
tableSnap: 'point',
|
|
140
150
|
chairs: true,
|
|
141
151
|
selectable: 'none',
|
|
142
152
|
dragMerged: false,
|
|
@@ -305,6 +315,12 @@ const mergeShapes = computed(() =>
|
|
|
305
315
|
let drag: { id: number; sx: number; sy: number; start: Map<string | number, { x: number; y: number }>; moved: boolean; pick?: string | number } | null = null
|
|
306
316
|
let suppressClick = false
|
|
307
317
|
const snap = (v: number) => (props.grid ? Math.round(v / props.grid) * props.grid : v)
|
|
318
|
+
/** A table's middle: on a grid point, or with `tableSnap="cell"` in the middle of a grid square */
|
|
319
|
+
const snapTable = (v: number) => {
|
|
320
|
+
const g = props.grid
|
|
321
|
+
if (!g || props.tableSnap !== 'cell') return snap(v)
|
|
322
|
+
return Math.round((v - g / 2) / g) * g + g / 2
|
|
323
|
+
}
|
|
308
324
|
function clamp(t: FloorTable, x: number, y: number) {
|
|
309
325
|
const s = sizeOf(t)
|
|
310
326
|
return { x: Math.min(props.width - s.w / 2, Math.max(s.w / 2, x)), y: Math.min(props.height - s.h / 2, Math.max(s.h / 2, y)) }
|
|
@@ -327,10 +343,12 @@ function onDown(e: PointerEvent, t: FloorTable) {
|
|
|
327
343
|
selection.value = selection.value.includes(t.id) ? selection.value.filter((x) => x !== t.id) : [...selection.value, t.id]
|
|
328
344
|
navigator.vibrate?.(10)
|
|
329
345
|
}, LONG_PRESS)
|
|
330
|
-
} else if (!selection.value.includes(t.id)) selection.value = e.shiftKey || e.ctrlKey || e.metaKey ? [...selection.value, t.id] : [t.id]
|
|
331
|
-
// A touch picks the table once it moves (or on release), so a long press can add to what's picked
|
|
332
|
-
|
|
333
|
-
|
|
346
|
+
} else if (!selection.value.includes(t.id) && !toggling.value) selection.value = e.shiftKey || e.ctrlKey || e.metaKey ? [...selection.value, t.id] : [t.id]
|
|
347
|
+
// A touch picks the table once it moves (or on release), so a long press can add to what's picked. With
|
|
348
|
+
// `selectable="multiple"` the selection is the click's to change, and a drag moves only the table pressed (not the
|
|
349
|
+
// rest of the selection nor its merge): to bring the tables being merged together one by one
|
|
350
|
+
const ids = toggling.value ? [t.id] : withMerged(selection.value.includes(t.id) ? selection.value : [t.id])
|
|
351
|
+
drag = { id: e.pointerId, sx: e.clientX, sy: e.clientY, start: new Map(tables.value.filter((x) => ids.includes(x.id)).map((x) => [x.id, { x: x.x, y: x.y }])), moved: false, pick: touch && !toggling.value && !selection.value.includes(t.id) ? t.id : undefined }
|
|
334
352
|
window.addEventListener('pointermove', onMove)
|
|
335
353
|
window.addEventListener('pointerup', onUp)
|
|
336
354
|
window.addEventListener('pointercancel', onUp)
|
|
@@ -366,16 +384,19 @@ function onUp(e: PointerEvent) {
|
|
|
366
384
|
if (!moved) return
|
|
367
385
|
suppressClick = true
|
|
368
386
|
setTimeout(() => (suppressClick = false))
|
|
369
|
-
commitMoves(new Map([...d!].map(([id, p]) => [id, clamp(tables.value.find((x) => x.id === id)!,
|
|
387
|
+
commitMoves(new Map([...d!].map(([id, p]) => [id, clamp(tables.value.find((x) => x.id === id)!, snapTable(p.x), snapTable(p.y))])))
|
|
370
388
|
}
|
|
371
389
|
function commitMoves(next: Map<string | number, { x: number; y: number }>) {
|
|
372
390
|
record()
|
|
373
391
|
tables.value = tables.value.map((t) => (next.has(t.id) ? { ...t, ...next.get(t.id)! } : t))
|
|
374
392
|
emit('move', [...next].map(([id, p]) => ({ id, ...p })))
|
|
375
393
|
}
|
|
394
|
+
/** Editing with `selectable="multiple"`: a click toggles a table in the selection (to merge them), a drag moves that table alone */
|
|
395
|
+
const toggling = computed(() => props.editable && props.selectable === 'multiple')
|
|
376
396
|
function onClick(e: MouseEvent, t: FloorTable) {
|
|
377
397
|
if (suppressClick) return
|
|
378
|
-
if (
|
|
398
|
+
if (toggling.value) selection.value = selection.value.includes(t.id) ? selection.value.filter((x) => x !== t.id) : [...selection.value, t.id]
|
|
399
|
+
else if (props.editable && !(e.shiftKey || e.ctrlKey || e.metaKey)) selection.value = [t.id]
|
|
379
400
|
else if (!props.editable && props.selectable === 'single') selection.value = selection.value.length === 1 && selection.value[0] === t.id ? [] : [t.id]
|
|
380
401
|
else if (!props.editable && props.selectable === 'multiple')
|
|
381
402
|
selection.value = selection.value.includes(t.id) ? selection.value.filter((x) => x !== t.id) : [...selection.value, t.id]
|
|
@@ -736,7 +757,7 @@ function update(kind: FloorItemKind, id: string | number, patch: Record<string,
|
|
|
736
757
|
}
|
|
737
758
|
function addTable(t: Partial<FloorTable> = {}): FloorTable {
|
|
738
759
|
const n = Math.max(0, ...tables.value.map((x) => Number(x.label)).filter((x) => Number.isFinite(x))) + 1
|
|
739
|
-
const table: FloorTable = { id: newId('table'), label: String(n), x:
|
|
760
|
+
const table: FloorTable = { id: newId('table'), label: String(n), x: snapTable(props.width / 2), y: snapTable(props.height / 2), shape: 'square', seats: 4, ...t }
|
|
740
761
|
record()
|
|
741
762
|
tables.value = [...tables.value, table]
|
|
742
763
|
selection.value = [table.id]
|