@m3ui-vue/m3ui-vue 0.8.7 → 0.9.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/README.md +13 -3
- package/dist/{MMenuItem-DmrsQrK7.js → MTextField-B0wGv1Mx.js} +374 -364
- package/dist/MTextField-B0wGv1Mx.js.map +1 -0
- package/dist/barcode.js +9 -11
- package/dist/barcode.js.map +1 -1
- package/dist/code-editor.js +2 -2
- package/dist/code-editor.js.map +1 -1
- package/dist/components/MCarousel.vue.d.ts +55 -36
- package/dist/components/MDialog.vue.d.ts +4 -2
- package/dist/components/MEmojiSelector.vue.d.ts +21 -5
- package/dist/components/MFabMenu.vue.d.ts +1 -1
- package/dist/components/MFlex.vue.d.ts +1 -1
- package/dist/components/MLightbox.vue.d.ts +15 -5
- package/dist/components/MScheduler.vue.d.ts +38 -1
- package/dist/components/MShape.vue.d.ts +1 -1
- package/dist/components/MSimpleCarousel.vue.d.ts +2 -2
- package/dist/components/MSlider.vue.d.ts +1 -1
- package/dist/components/MStack.vue.d.ts +1 -1
- package/dist/components/MToolbar.vue.d.ts +1 -1
- package/dist/components/MWindow.vue.d.ts +1 -1
- package/dist/composables/_carouselStrategy.d.ts +101 -0
- package/dist/composables/useLocale.d.ts +23 -0
- package/dist/data/emoji-names/emoji-names-de.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-en.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-es.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-fr.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-ja.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-ko.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-pt.d.ts +2 -0
- package/dist/data/emoji-names/emoji-names-zh.d.ts +2 -0
- package/dist/data/emojis.d.ts +7 -1
- package/dist/emoji-names-de-BphO5XpV.js +1877 -0
- package/dist/emoji-names-de-BphO5XpV.js.map +1 -0
- package/dist/emoji-names-en-9ZvMRvzi.js +1877 -0
- package/dist/emoji-names-en-9ZvMRvzi.js.map +1 -0
- package/dist/emoji-names-es-DvfWU3Pj.js +1877 -0
- package/dist/emoji-names-es-DvfWU3Pj.js.map +1 -0
- package/dist/emoji-names-fr-B-jhUlsP.js +1877 -0
- package/dist/emoji-names-fr-B-jhUlsP.js.map +1 -0
- package/dist/emoji-names-ja-l8cMFJo1.js +1877 -0
- package/dist/emoji-names-ja-l8cMFJo1.js.map +1 -0
- package/dist/emoji-names-ko-YP-o9G0c.js +1877 -0
- package/dist/emoji-names-ko-YP-o9G0c.js.map +1 -0
- package/dist/emoji-names-pt-DGj_Js4M.js +1877 -0
- package/dist/emoji-names-pt-DGj_Js4M.js.map +1 -0
- package/dist/emoji-names-zh-Dzecwtw_.js +1877 -0
- package/dist/emoji-names-zh-Dzecwtw_.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/locales/index.js +168 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +3926 -2430
- package/dist/m3ui.js.map +1 -1
- package/dist/plugin.d.ts +6 -0
- package/dist/rich-text-editor.js +9 -9
- package/dist/styles.css +1 -1
- package/dist/terminal.js +34 -27
- package/dist/terminal.js.map +1 -1
- package/dist/{useLocale-Bircp823.js → useLocale-CmI675Ab.js} +25 -2
- package/dist/useLocale-CmI675Ab.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MCarousel.test.ts +183 -21
- package/src/__tests__/MDialog.test.ts +18 -0
- package/src/__tests__/MEmojiSelector.test.ts +138 -0
- package/src/__tests__/MLightbox.test.ts +313 -5
- package/src/__tests__/MPagination.test.ts +37 -0
- package/src/__tests__/MPullToRefresh.test.ts +39 -0
- package/src/__tests__/MScheduler.test.ts +122 -0
- package/src/__tests__/MTerminal.test.ts +63 -0
- package/src/__tests__/attrsFallthrough.test.ts +65 -0
- package/src/__tests__/plugin.test.ts +28 -1
- package/src/components/MBarcode.vue +4 -2
- package/src/components/MBottomSheet.vue +1 -1
- package/src/components/MCarousel.vue +526 -200
- package/src/components/MCodeEditor.vue +3 -2
- package/src/components/MCommandPalette.vue +1 -1
- package/src/components/MContextMenu.vue +3 -1
- package/src/components/MDatePickerModal.vue +1 -1
- package/src/components/MDateRangePickerModal.vue +1 -1
- package/src/components/MDialog.vue +19 -6
- package/src/components/MEmojiSelector.vue +479 -78
- package/src/components/MFab.vue +3 -1
- package/src/components/MLightbox.vue +750 -268
- package/src/components/MLoadingOverlay.vue +6 -1
- package/src/components/MMenu.vue +3 -1
- package/src/components/MNavigationDrawer.vue +5 -1
- package/src/components/MNavigationRail.vue +8 -2
- package/src/components/MOverlay.vue +1 -0
- package/src/components/MPagination.vue +8 -5
- package/src/components/MPullToRefresh.vue +13 -1
- package/src/components/MScheduler.vue +282 -115
- package/src/components/MSideSheet.vue +1 -1
- package/src/components/MSplitButton.vue +3 -1
- package/src/components/MSpotlightSearch.vue +1 -1
- package/src/components/MTerminal.vue +22 -10
- package/src/components/MTimePickerModal.vue +1 -1
- package/src/components/MTooltip.vue +3 -0
- package/src/components/MTour.vue +2 -2
- package/src/composables/_carouselStrategy.ts +712 -0
- package/src/composables/useLocale.ts +48 -0
- package/src/data/emoji-names/emoji-names-de.ts +1875 -0
- package/src/data/emoji-names/emoji-names-en.ts +1875 -0
- package/src/data/emoji-names/emoji-names-es.ts +1875 -0
- package/src/data/emoji-names/emoji-names-fr.ts +1875 -0
- package/src/data/emoji-names/emoji-names-ja.ts +1875 -0
- package/src/data/emoji-names/emoji-names-ko.ts +1875 -0
- package/src/data/emoji-names/emoji-names-pt.ts +1875 -0
- package/src/data/emoji-names/emoji-names-zh.ts +1875 -0
- package/src/data/emojis.ts +29 -198
- package/src/index.ts +2 -2
- package/src/locales/de.ts +24 -0
- package/src/locales/es.ts +24 -0
- package/src/locales/fr.ts +24 -0
- package/src/locales/ja.ts +24 -0
- package/src/locales/ko.ts +24 -0
- package/src/locales/pt.ts +24 -0
- package/src/locales/zh.ts +24 -0
- package/src/plugin.ts +12 -1
- package/dist/MMenuItem-DmrsQrK7.js.map +0 -1
- package/dist/useLocale-Bircp823.js.map +0 -1
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
/*
|
|
3
|
+
* Full-screen image viewer. M3 has no lightbox component; the reference is Google Photos' viewer:
|
|
4
|
+
* a black stage, a top bar (close, position, actions) and a bottom area (caption, thumbnails) that
|
|
5
|
+
* a tap hides and shows, and gestures for everything.
|
|
6
|
+
*
|
|
7
|
+
* - Swipe between images on a track where the neighbors are already there (prev / current / next
|
|
8
|
+
* are rendered), velocity-aware; arrows and keys too.
|
|
9
|
+
* - Zoom: double tap / double click at a point, pinch, wheel; drag to pan while zoomed.
|
|
10
|
+
* - Drag the image down (or up) to dismiss, the backdrop fading as it goes.
|
|
11
|
+
* - Opens from and closes back into the image it came from when there is one (auto-scanned
|
|
12
|
+
* images, or `open(index, el)`); otherwise it scales and fades.
|
|
13
|
+
*
|
|
14
|
+
* Motion follows the library's convention: plain JS springs (Standard DefaultSpatial,
|
|
15
|
+
* dampingRatio 0.9 / stiffness 700) stepped in one rAF loop that writes transforms directly.
|
|
16
|
+
*/
|
|
17
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
18
|
+
import MIconButton from './MIconButton.vue'
|
|
19
|
+
import MSpinner from './MSpinner.vue'
|
|
4
20
|
import { useLocale } from '../composables/useLocale'
|
|
5
21
|
|
|
6
22
|
export interface LightboxItem {
|
|
@@ -9,12 +25,7 @@ export interface LightboxItem {
|
|
|
9
25
|
caption?: string
|
|
10
26
|
}
|
|
11
27
|
|
|
12
|
-
|
|
13
|
-
x: number
|
|
14
|
-
y: number
|
|
15
|
-
width: number
|
|
16
|
-
height: number
|
|
17
|
-
}
|
|
28
|
+
defineOptions({ inheritAttrs: false })
|
|
18
29
|
|
|
19
30
|
const props = withDefaults(
|
|
20
31
|
defineProps<{
|
|
@@ -26,8 +37,12 @@ const props = withDefaults(
|
|
|
26
37
|
closeLabel?: string
|
|
27
38
|
/** Show a row of clickable thumbnails at the bottom for jumping directly to an image. */
|
|
28
39
|
showThumbnails?: boolean
|
|
29
|
-
/** Round the corners of the
|
|
40
|
+
/** Round the corners of the opened image. */
|
|
30
41
|
rounded?: boolean
|
|
42
|
+
/** Allow zooming (double tap / double click, pinch, wheel, + and - keys). */
|
|
43
|
+
zoom?: boolean
|
|
44
|
+
/** Largest zoom factor. */
|
|
45
|
+
maxZoom?: number
|
|
31
46
|
}>(),
|
|
32
47
|
{
|
|
33
48
|
modelValue: false,
|
|
@@ -35,6 +50,8 @@ const props = withDefaults(
|
|
|
35
50
|
loop: true,
|
|
36
51
|
showThumbnails: false,
|
|
37
52
|
rounded: true,
|
|
53
|
+
zoom: true,
|
|
54
|
+
maxZoom: 4,
|
|
38
55
|
},
|
|
39
56
|
)
|
|
40
57
|
|
|
@@ -44,217 +61,670 @@ const emit = defineEmits<{
|
|
|
44
61
|
change: [index: number]
|
|
45
62
|
}>()
|
|
46
63
|
|
|
64
|
+
defineSlots<{
|
|
65
|
+
default?: () => unknown
|
|
66
|
+
/** Extra buttons in the top bar (download, share, info…). */
|
|
67
|
+
actions?: (scope: { item: LightboxItem; index: number }) => unknown
|
|
68
|
+
}>()
|
|
69
|
+
|
|
47
70
|
const locale = useLocale()
|
|
48
71
|
|
|
49
72
|
const containerEl = ref<HTMLElement>()
|
|
50
|
-
const
|
|
73
|
+
const rootEl = ref<HTMLElement>()
|
|
74
|
+
const stageEl = ref<HTMLElement>()
|
|
75
|
+
const thumbsEl = ref<HTMLElement>()
|
|
51
76
|
const scannedItems = ref<LightboxItem[]>([])
|
|
52
|
-
const
|
|
77
|
+
const scannedEls: HTMLElement[] = []
|
|
78
|
+
/** Elements the viewer can open from / close into, by item index. */
|
|
79
|
+
const origins = new Map<number, HTMLElement>()
|
|
80
|
+
|
|
81
|
+
const rendered = ref(false)
|
|
53
82
|
const currentIndex = ref(props.index)
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
83
|
+
const chrome = ref(true)
|
|
84
|
+
const zoomed = ref(false)
|
|
85
|
+
const loaded = ref(new Set<string>())
|
|
86
|
+
/** Width / height of each image, measured once it loads. */
|
|
87
|
+
const ratios = ref(new Map<string, number>())
|
|
88
|
+
/** The area an image can fill: the stage minus the slide's padding (p-4, sm:p-12). */
|
|
89
|
+
const box = ref({ w: 0, h: 0 })
|
|
59
90
|
|
|
60
91
|
const galleryItems = computed(() => props.items ?? scannedItems.value)
|
|
92
|
+
const count = computed(() => galleryItems.value.length)
|
|
61
93
|
const current = computed(() => galleryItems.value[currentIndex.value])
|
|
62
|
-
const hasMultiple = computed(() =>
|
|
94
|
+
const hasMultiple = computed(() => count.value > 1)
|
|
63
95
|
|
|
64
|
-
|
|
65
|
-
|
|
96
|
+
function neighbor(i: number, delta: number) {
|
|
97
|
+
const n = i + delta
|
|
98
|
+
if (n >= 0 && n < count.value) return n
|
|
99
|
+
if (!props.loop || count.value < 2) return null
|
|
100
|
+
return (n + count.value) % count.value
|
|
101
|
+
}
|
|
102
|
+
const prevIndex = computed(() => neighbor(currentIndex.value, -1))
|
|
103
|
+
const nextIndex = computed(() => neighbor(currentIndex.value, 1))
|
|
104
|
+
// Current first in the DOM, then its neighbors (preloaded, ready for a swipe).
|
|
105
|
+
const slides = computed(() =>
|
|
106
|
+
[
|
|
107
|
+
{ offset: 0, index: currentIndex.value },
|
|
108
|
+
{ offset: 1, index: nextIndex.value },
|
|
109
|
+
{ offset: -1, index: prevIndex.value },
|
|
110
|
+
].filter((s): s is { offset: number; index: number } => s.index !== null && !!galleryItems.value[s.index]),
|
|
111
|
+
)
|
|
66
112
|
|
|
67
|
-
|
|
68
|
-
currentIndex.value
|
|
69
|
-
|
|
70
|
-
|
|
113
|
+
const positionLabel = computed(() =>
|
|
114
|
+
locale.carouselItem.replace('{index}', String(currentIndex.value + 1)).replace('{count}', String(count.value)),
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
// ── Springs ───────────────────────────────────────────────────────────────────────────────────
|
|
118
|
+
|
|
119
|
+
interface Spring { pos: number; vel: number; target: number; k: number; z: number }
|
|
120
|
+
const spring = (pos: number, k = 700, z = 0.9): Spring => ({ pos, vel: 0, target: pos, k, z })
|
|
121
|
+
|
|
122
|
+
const trackX = spring(0)
|
|
123
|
+
const dismissY = spring(0)
|
|
124
|
+
const scale = spring(1)
|
|
125
|
+
const panX = spring(0)
|
|
126
|
+
const panY = spring(0)
|
|
127
|
+
const intro = spring(0)
|
|
128
|
+
const backdrop = spring(0, 300, 1)
|
|
129
|
+
const springs = [trackX, dismissY, scale, panX, panY, intro, backdrop]
|
|
130
|
+
|
|
131
|
+
const GAP = 16
|
|
132
|
+
let stageW = 0
|
|
133
|
+
let stageH = 0
|
|
134
|
+
let raf = 0
|
|
135
|
+
let lastFrame = 0
|
|
136
|
+
let onSettled: (() => void) | null = null
|
|
137
|
+
/** The origin as a transform of the open image: offset, scale, and the crop that gives it the origin's shape. */
|
|
138
|
+
interface Origin { dx: number; dy: number; s: number; insetX: number; insetY: number; radius: number }
|
|
139
|
+
let introFrom: Origin | null = null
|
|
140
|
+
const reducedMotion = () => typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
141
|
+
|
|
142
|
+
function setNow(s: Spring, v: number) {
|
|
143
|
+
s.pos = v
|
|
144
|
+
s.target = v
|
|
145
|
+
s.vel = 0
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function animate() {
|
|
149
|
+
if (reducedMotion()) springs.forEach((s) => setNow(s, s.target))
|
|
150
|
+
if (!raf) {
|
|
151
|
+
lastFrame = performance.now()
|
|
152
|
+
raf = requestAnimationFrame(step)
|
|
153
|
+
}
|
|
154
|
+
write()
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function step(now: number) {
|
|
158
|
+
raf = 0
|
|
159
|
+
const dt = Math.min((now - lastFrame) / 1000, 1 / 30)
|
|
160
|
+
lastFrame = now
|
|
161
|
+
let moving = false
|
|
162
|
+
for (const s of springs) {
|
|
163
|
+
for (let left = dt; left > 0; left -= 0.004) {
|
|
164
|
+
const h = Math.min(0.004, left)
|
|
165
|
+
s.vel += (-s.k * (s.pos - s.target) - 2 * s.z * Math.sqrt(s.k) * s.vel) * h
|
|
166
|
+
s.pos += s.vel * h
|
|
167
|
+
}
|
|
168
|
+
const eps = s === scale || s === intro || s === backdrop ? 0.001 : 0.5
|
|
169
|
+
if (Math.abs(s.pos - s.target) < eps && Math.abs(s.vel) < eps * 10) setNow(s, s.target)
|
|
170
|
+
else moving = true
|
|
171
|
+
}
|
|
172
|
+
write()
|
|
173
|
+
if (moving || dragging()) raf = requestAnimationFrame(step)
|
|
174
|
+
else if (onSettled) {
|
|
175
|
+
const cb = onSettled
|
|
176
|
+
onSettled = null
|
|
177
|
+
cb()
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Writes every animated value to the DOM (never through reactive bindings). */
|
|
182
|
+
function write() {
|
|
183
|
+
const root = rootEl.value
|
|
184
|
+
if (!root) return
|
|
185
|
+
const fade = 1 - Math.min(Math.abs(dismissY.pos) / (stageH * 0.5 || 300), 1) * 0.85
|
|
186
|
+
const back = root.querySelector<HTMLElement>('[data-lb-backdrop]')
|
|
187
|
+
if (back) back.style.opacity = String(Math.max(0, Math.min(1, backdrop.pos * fade)))
|
|
188
|
+
root.querySelectorAll<HTMLElement>('[data-lb-slide]').forEach((el) => {
|
|
189
|
+
const k = Number(el.dataset.offset)
|
|
190
|
+
el.style.transform = `translate3d(${k * (stageW + GAP) + trackX.pos}px, 0, 0)`
|
|
191
|
+
})
|
|
192
|
+
// Neighbors show their image plainly: clear what was written while one of them was current.
|
|
193
|
+
root.querySelectorAll<HTMLElement>('[data-lb-slide]:not([data-offset="0"]) [data-lb-zoom]').forEach((el) => {
|
|
194
|
+
el.style.transform = ''
|
|
195
|
+
el.style.opacity = ''
|
|
196
|
+
el.style.clipPath = ''
|
|
197
|
+
})
|
|
198
|
+
const zoomEl = root.querySelector<HTMLElement>('[data-lb-slide][data-offset="0"] [data-lb-zoom]')
|
|
199
|
+
if (zoomEl) {
|
|
200
|
+
const shrink = 1 - Math.min(Math.abs(dismissY.pos) / (stageH || 600), 1) * 0.3
|
|
201
|
+
let tx = panX.pos
|
|
202
|
+
let ty = panY.pos + dismissY.pos
|
|
203
|
+
let s = scale.pos * shrink
|
|
204
|
+
let opacity = 1
|
|
205
|
+
let clip = ''
|
|
206
|
+
const p = intro.pos
|
|
207
|
+
if (p < 1) {
|
|
208
|
+
if (introFrom) {
|
|
209
|
+
// Container transform: start as the origin (its position, size, crop and corners) and
|
|
210
|
+
// open up into the whole image.
|
|
211
|
+
tx = introFrom.dx + (tx - introFrom.dx) * p
|
|
212
|
+
ty = introFrom.dy + (ty - introFrom.dy) * p
|
|
213
|
+
s = introFrom.s + (s - introFrom.s) * p
|
|
214
|
+
const q = Math.max(0, 1 - p)
|
|
215
|
+
const radius = introFrom.radius + ((props.rounded ? 16 : 0) - introFrom.radius) * p
|
|
216
|
+
clip = `inset(${introFrom.insetY * q}px ${introFrom.insetX * q}px round ${Math.max(0, radius)}px)`
|
|
217
|
+
} else {
|
|
218
|
+
s *= 0.92 + 0.08 * p
|
|
219
|
+
opacity = Math.max(0, Math.min(1, p))
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
zoomEl.style.transform = `translate3d(${tx}px, ${ty}px, 0) scale(${s})`
|
|
223
|
+
zoomEl.style.opacity = String(opacity)
|
|
224
|
+
zoomEl.style.clipPath = clip
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ── Opening and closing ───────────────────────────────────────────────────────────────────────
|
|
229
|
+
|
|
230
|
+
let restoreFocus: HTMLElement | null = null
|
|
231
|
+
|
|
232
|
+
function measure() {
|
|
233
|
+
stageW = stageEl.value?.clientWidth ?? 0
|
|
234
|
+
stageH = stageEl.value?.clientHeight ?? 0
|
|
235
|
+
const pad = window.innerWidth >= 640 ? 48 : 16
|
|
236
|
+
const w = Math.max(0, stageW - pad * 2)
|
|
237
|
+
const h = Math.max(0, stageH - pad * 2)
|
|
238
|
+
if (w !== box.value.w || h !== box.value.h) box.value = { w, h }
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function currentImg() {
|
|
242
|
+
return rootEl.value?.querySelector<HTMLImageElement>('[data-lb-slide][data-offset="0"] img') ?? null
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The origin element as a transform of the open image. The origin usually crops the picture
|
|
247
|
+
* (object-cover thumbnails), so the image is scaled to cover it and clipped to its shape.
|
|
248
|
+
*/
|
|
249
|
+
function originTransform(): Origin | null {
|
|
250
|
+
const el = origins.get(currentIndex.value)
|
|
251
|
+
const img = currentImg()
|
|
252
|
+
const stage = stageEl.value
|
|
253
|
+
if (!el || !el.isConnected || !img || !stage || !img.offsetWidth) return null
|
|
254
|
+
const o = el.getBoundingClientRect()
|
|
255
|
+
if (!o.width || !o.height) return null
|
|
256
|
+
const st = stage.getBoundingClientRect()
|
|
257
|
+
const iw = img.offsetWidth
|
|
258
|
+
const ih = img.offsetHeight
|
|
259
|
+
const s = Math.max(0.05, Math.max(o.width / iw, o.height / ih))
|
|
260
|
+
return {
|
|
261
|
+
dx: o.left + o.width / 2 - (st.left + st.width / 2),
|
|
262
|
+
dy: o.top + o.height / 2 - (st.top + st.height / 2),
|
|
263
|
+
s,
|
|
264
|
+
// Insets and radius are in the image's own (unscaled) pixels: the clip is scaled with it.
|
|
265
|
+
insetX: Math.max(0, (iw - o.width / s) / 2),
|
|
266
|
+
insetY: Math.max(0, (ih - o.height / s) / 2),
|
|
267
|
+
radius: (Number.parseFloat(getComputedStyle(el).borderTopLeftRadius) || 0) / s,
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async function show() {
|
|
272
|
+
restoreFocus = document.activeElement as HTMLElement | null
|
|
273
|
+
rendered.value = true
|
|
274
|
+
chrome.value = true
|
|
275
|
+
resetZoom(true)
|
|
276
|
+
setNow(trackX, 0)
|
|
277
|
+
setNow(dismissY, 0)
|
|
278
|
+
setNow(intro, 0)
|
|
279
|
+
setNow(backdrop, 0)
|
|
280
|
+
backdrop.k = 300
|
|
281
|
+
document.addEventListener('keydown', onKeydown)
|
|
282
|
+
document.body.style.overflow = 'hidden'
|
|
283
|
+
await nextTick()
|
|
284
|
+
measure()
|
|
285
|
+
backdrop.target = 1
|
|
286
|
+
animate()
|
|
287
|
+
rootEl.value?.querySelector<HTMLElement>('[data-lb-close]')?.focus()
|
|
288
|
+
// Grow out of the origin once the image has a size to grow into (or after a short wait).
|
|
289
|
+
const img = currentImg()
|
|
290
|
+
if (img && !img.complete) {
|
|
291
|
+
await Promise.race([new Promise((r) => img.addEventListener('load', r, { once: true })), new Promise((r) => setTimeout(r, 300))])
|
|
292
|
+
}
|
|
293
|
+
if (!rendered.value) return
|
|
294
|
+
introFrom = originTransform()
|
|
295
|
+
intro.target = 1
|
|
296
|
+
animate()
|
|
71
297
|
}
|
|
72
298
|
|
|
299
|
+
function hide() {
|
|
300
|
+
document.removeEventListener('keydown', onKeydown)
|
|
301
|
+
// The bars and the backdrop go with the image, not after it.
|
|
302
|
+
chrome.value = false
|
|
303
|
+
backdrop.k = 700
|
|
304
|
+
introFrom = originTransform()
|
|
305
|
+
resetZoom()
|
|
306
|
+
intro.target = 0
|
|
307
|
+
backdrop.target = 0
|
|
308
|
+
if (introFrom) dismissY.target = 0
|
|
309
|
+
else if (dismissY.pos !== 0) {
|
|
310
|
+
// Dismissed by a drag with nowhere to return to: carry on in that direction.
|
|
311
|
+
intro.target = 1
|
|
312
|
+
dismissY.target = Math.sign(dismissY.pos) * (stageH || 800)
|
|
313
|
+
}
|
|
314
|
+
onSettled = () => {
|
|
315
|
+
rendered.value = false
|
|
316
|
+
document.body.style.overflow = ''
|
|
317
|
+
restoreFocus?.focus?.()
|
|
318
|
+
restoreFocus = null
|
|
319
|
+
}
|
|
320
|
+
animate()
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
watch(() => props.modelValue, (v) => (v ? !rendered.value && show() : rendered.value && hide()), { immediate: true })
|
|
324
|
+
watch(() => props.index, (v) => {
|
|
325
|
+
if (v === currentIndex.value || !galleryItems.value[v]) return
|
|
326
|
+
// Closed, or still opening (a consumer usually sets the index and opens in the same tick): show
|
|
327
|
+
// that image directly. Only an index change while it's open slides.
|
|
328
|
+
if (!rendered.value || intro.pos < 0.999 || intro.target < 1) {
|
|
329
|
+
resetZoom(true)
|
|
330
|
+
setNow(trackX, 0)
|
|
331
|
+
currentIndex.value = v
|
|
332
|
+
} else {
|
|
333
|
+
go(v)
|
|
334
|
+
}
|
|
335
|
+
})
|
|
336
|
+
|
|
73
337
|
function open(i = 0, sourceEl?: HTMLElement) {
|
|
74
|
-
|
|
75
|
-
isFirstShow.value = true
|
|
338
|
+
if (sourceEl) origins.set(i, sourceEl)
|
|
76
339
|
setIndex(i)
|
|
77
|
-
isOpen.value = true
|
|
78
340
|
emit('update:modelValue', true)
|
|
341
|
+
if (!rendered.value) show()
|
|
79
342
|
}
|
|
80
343
|
|
|
81
344
|
function close() {
|
|
82
|
-
isOpen.value = false
|
|
83
345
|
emit('update:modelValue', false)
|
|
346
|
+
if (rendered.value) hide()
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ── Navigation ────────────────────────────────────────────────────────────────────────────────
|
|
350
|
+
|
|
351
|
+
function setIndex(i: number) {
|
|
352
|
+
if (i === currentIndex.value) return
|
|
353
|
+
currentIndex.value = i
|
|
354
|
+
emit('update:index', i)
|
|
355
|
+
emit('change', i)
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Moves to `i`. The index changes right away; the track starts where the new image already was
|
|
360
|
+
* (or one slot away, for a jump) and springs to center.
|
|
361
|
+
*/
|
|
362
|
+
function go(i: number, direction?: number) {
|
|
363
|
+
if (i === currentIndex.value || !galleryItems.value[i]) return
|
|
364
|
+
const dir = direction ?? (i > currentIndex.value ? 1 : -1)
|
|
365
|
+
resetZoom(true)
|
|
366
|
+
setNow(dismissY, 0)
|
|
367
|
+
setIndex(i)
|
|
368
|
+
trackX.pos += dir * (stageW + GAP)
|
|
369
|
+
trackX.target = 0
|
|
370
|
+
animate()
|
|
84
371
|
}
|
|
85
372
|
|
|
86
373
|
function next() {
|
|
87
|
-
|
|
88
|
-
if (!len) return
|
|
89
|
-
isFirstShow.value = false
|
|
90
|
-
direction.value = 1
|
|
91
|
-
if (currentIndex.value < len - 1) setIndex(currentIndex.value + 1)
|
|
92
|
-
else if (props.loop) setIndex(0)
|
|
374
|
+
if (nextIndex.value !== null) go(nextIndex.value, 1)
|
|
93
375
|
}
|
|
94
376
|
|
|
95
377
|
function prev() {
|
|
96
|
-
|
|
97
|
-
if (!len) return
|
|
98
|
-
isFirstShow.value = false
|
|
99
|
-
direction.value = -1
|
|
100
|
-
if (currentIndex.value > 0) setIndex(currentIndex.value - 1)
|
|
101
|
-
else if (props.loop) setIndex(len - 1)
|
|
378
|
+
if (prevIndex.value !== null) go(prevIndex.value, -1)
|
|
102
379
|
}
|
|
103
380
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
381
|
+
watch(currentIndex, async () => {
|
|
382
|
+
await nextTick()
|
|
383
|
+
write()
|
|
384
|
+
// Scroll only the strip: scrollIntoView() would also scroll the viewer's own (overflow-hidden)
|
|
385
|
+
// container and leave the whole thing shifted sideways.
|
|
386
|
+
const strip = thumbsEl.value
|
|
387
|
+
const thumb = strip?.querySelector<HTMLElement>('[data-lb-thumb][aria-current="true"]')
|
|
388
|
+
if (strip && thumb) strip.scrollTo({ left: thumb.offsetLeft - (strip.clientWidth - thumb.offsetWidth) / 2, behavior: 'smooth' })
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
// ── Zoom ──────────────────────────────────────────────────────────────────────────────────────
|
|
392
|
+
|
|
393
|
+
function panBounds(s: number) {
|
|
394
|
+
const img = currentImg()
|
|
395
|
+
const w = img?.offsetWidth ?? 0
|
|
396
|
+
const h = img?.offsetHeight ?? 0
|
|
397
|
+
return { x: Math.max(0, (w * s - stageW) / 2), y: Math.max(0, (h * s - stageH) / 2) }
|
|
109
398
|
}
|
|
110
399
|
|
|
111
|
-
function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
400
|
+
function clampPanTargets() {
|
|
401
|
+
const b = panBounds(scale.target)
|
|
402
|
+
panX.target = Math.max(-b.x, Math.min(b.x, panX.target))
|
|
403
|
+
panY.target = Math.max(-b.y, Math.min(b.y, panY.target))
|
|
115
404
|
}
|
|
116
405
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
406
|
+
function resetZoom(now = false) {
|
|
407
|
+
for (const [s, v] of [[scale, 1], [panX, 0], [panY, 0]] as const) {
|
|
408
|
+
if (now) setNow(s, v)
|
|
409
|
+
else s.target = v
|
|
410
|
+
}
|
|
411
|
+
zoomed.value = false
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Zooms to `factor` keeping the point (x, y), relative to the stage center, under the pointer. */
|
|
415
|
+
function zoomTo(factor: number, x: number, y: number, now = false) {
|
|
416
|
+
const s = Math.max(1, Math.min(props.maxZoom, factor))
|
|
417
|
+
const ratio = s / scale.target
|
|
418
|
+
const tx = x - (x - panX.target) * ratio
|
|
419
|
+
const ty = y - (y - panY.target) * ratio
|
|
420
|
+
scale.target = s
|
|
421
|
+
panX.target = tx
|
|
422
|
+
panY.target = ty
|
|
423
|
+
clampPanTargets()
|
|
424
|
+
if (now) [scale, panX, panY].forEach((sp) => setNow(sp, sp.target))
|
|
425
|
+
zoomed.value = s > 1.01
|
|
426
|
+
animate()
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function stagePoint(clientX: number, clientY: number) {
|
|
430
|
+
const r = stageEl.value!.getBoundingClientRect()
|
|
431
|
+
return { x: clientX - (r.left + r.width / 2), y: clientY - (r.top + r.height / 2) }
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function toggleZoomAt(clientX: number, clientY: number) {
|
|
435
|
+
if (!props.zoom) return
|
|
436
|
+
if (zoomed.value) {
|
|
437
|
+
resetZoom()
|
|
438
|
+
animate()
|
|
121
439
|
} else {
|
|
122
|
-
|
|
123
|
-
|
|
440
|
+
const p = stagePoint(clientX, clientY)
|
|
441
|
+
zoomTo(Math.min(2.5, props.maxZoom), p.x, p.y)
|
|
124
442
|
}
|
|
125
|
-
}
|
|
443
|
+
}
|
|
126
444
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
445
|
+
function onWheel(e: WheelEvent) {
|
|
446
|
+
if (!props.zoom || !rendered.value) return
|
|
447
|
+
e.preventDefault()
|
|
448
|
+
const p = stagePoint(e.clientX, e.clientY)
|
|
449
|
+
// Trackpad pinches arrive as ctrl+wheel with small deltas.
|
|
450
|
+
const factor = Math.exp(-e.deltaY * (e.ctrlKey ? 0.01 : 0.002))
|
|
451
|
+
zoomTo(scale.target * factor, p.x, p.y, true)
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// ── Gestures ──────────────────────────────────────────────────────────────────────────────────
|
|
455
|
+
|
|
456
|
+
type Mode = 'pending' | 'swipe' | 'dismiss' | 'pan' | 'pinch' | 'ignore'
|
|
457
|
+
const pointers = new Map<number, { x: number; y: number }>()
|
|
458
|
+
let mode: Mode | null = null
|
|
459
|
+
let start = { x: 0, y: 0, t: 0, trackX: 0, dismissY: 0, panX: 0, panY: 0 }
|
|
460
|
+
let pinch = { dist: 0, scale: 1, mid: { x: 0, y: 0 }, panX: 0, panY: 0 }
|
|
461
|
+
let samples: { t: number; x: number; y: number }[] = []
|
|
462
|
+
let lastTap = { t: -Infinity, x: 0, y: 0 }
|
|
463
|
+
let tapTimer: ReturnType<typeof setTimeout> | null = null
|
|
464
|
+
const dragging = () => mode !== null && mode !== 'pending'
|
|
465
|
+
|
|
466
|
+
function onPointerDown(e: PointerEvent) {
|
|
467
|
+
if (e.button !== 0 || (e.target as Element).closest('button, a, input')) return
|
|
468
|
+
measure()
|
|
469
|
+
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY })
|
|
470
|
+
try { stageEl.value?.setPointerCapture(e.pointerId) } catch { /* not capturable */ }
|
|
471
|
+
if (pointers.size === 2 && props.zoom) {
|
|
472
|
+
startPinch()
|
|
473
|
+
return
|
|
474
|
+
}
|
|
475
|
+
if (pointers.size > 1) return
|
|
476
|
+
springs.forEach((s) => (s.target = s.pos))
|
|
477
|
+
start = { x: e.clientX, y: e.clientY, t: performance.now(), trackX: trackX.pos, dismissY: dismissY.pos, panX: panX.pos, panY: panY.pos }
|
|
478
|
+
samples = [{ t: start.t, x: e.clientX, y: e.clientY }]
|
|
479
|
+
mode = 'pending'
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function onPointerMove(e: PointerEvent) {
|
|
483
|
+
if (!pointers.has(e.pointerId)) return
|
|
484
|
+
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY })
|
|
485
|
+
if (mode === 'pinch' && pointers.size >= 2) {
|
|
486
|
+
const [a, b] = [...pointers.values()] as [{ x: number; y: number }, { x: number; y: number }]
|
|
487
|
+
const s = Math.max(0.8, Math.min(props.maxZoom * 1.2, pinch.scale * (Math.hypot(a.x - b.x, a.y - b.y) / pinch.dist)))
|
|
488
|
+
const mid = stagePoint((a.x + b.x) / 2, (a.y + b.y) / 2)
|
|
489
|
+
lastMid = mid
|
|
490
|
+
const ratio = s / pinch.scale
|
|
491
|
+
setNow(scale, s)
|
|
492
|
+
setNow(panX, mid.x - (pinch.mid.x - pinch.panX) * ratio)
|
|
493
|
+
setNow(panY, mid.y - (pinch.mid.y - pinch.panY) * ratio)
|
|
494
|
+
zoomed.value = s > 1.01
|
|
495
|
+
write()
|
|
496
|
+
return
|
|
497
|
+
}
|
|
498
|
+
const dx = e.clientX - start.x
|
|
499
|
+
const dy = e.clientY - start.y
|
|
500
|
+
const now = performance.now()
|
|
501
|
+
samples.push({ t: now, x: e.clientX, y: e.clientY })
|
|
502
|
+
while (samples.length > 2 && now - samples[0]!.t > 100) samples.shift()
|
|
503
|
+
if (mode === 'pending') {
|
|
504
|
+
const slop = e.pointerType === 'touch' ? 8 : 4
|
|
505
|
+
if (Math.hypot(dx, dy) < slop) return
|
|
506
|
+
if (zoomed.value) mode = 'pan'
|
|
507
|
+
else if (Math.abs(dx) > Math.abs(dy)) mode = hasMultiple.value ? 'swipe' : 'dismiss'
|
|
508
|
+
else mode = 'dismiss'
|
|
509
|
+
chromeBeforeDrag = chrome.value
|
|
510
|
+
if (mode === 'dismiss') chrome.value = false
|
|
511
|
+
}
|
|
512
|
+
if (mode === 'swipe') {
|
|
513
|
+
// Resist past the ends of a non-looping gallery.
|
|
514
|
+
let x = start.trackX + dx
|
|
515
|
+
if ((x > 0 && prevIndex.value === null) || (x < 0 && nextIndex.value === null)) x = start.trackX + dx * 0.3
|
|
516
|
+
setNow(trackX, x)
|
|
517
|
+
} else if (mode === 'dismiss') {
|
|
518
|
+
setNow(dismissY, start.dismissY + dy)
|
|
519
|
+
} else if (mode === 'pan') {
|
|
520
|
+
setNow(panX, start.panX + dx)
|
|
521
|
+
setNow(panY, start.panY + dy)
|
|
522
|
+
}
|
|
523
|
+
write()
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
let chromeBeforeDrag = true
|
|
527
|
+
let lastMid = { x: 0, y: 0 }
|
|
528
|
+
|
|
529
|
+
function startPinch() {
|
|
530
|
+
if (tapTimer) clearTimeout(tapTimer)
|
|
531
|
+
tapTimer = null
|
|
532
|
+
// Fingers rarely land together: the first one may already have started a swipe or a
|
|
533
|
+
// dismiss drag. Undo it, or its offset would stay applied on top of the zoom.
|
|
534
|
+
if (mode === 'swipe' || mode === 'dismiss') {
|
|
535
|
+
trackX.target = 0
|
|
536
|
+
dismissY.target = 0
|
|
537
|
+
chrome.value = chromeBeforeDrag
|
|
538
|
+
}
|
|
539
|
+
const [a, b] = [...pointers.values()] as [{ x: number; y: number }, { x: number; y: number }]
|
|
540
|
+
const mid = stagePoint((a.x + b.x) / 2, (a.y + b.y) / 2)
|
|
541
|
+
pinch = { dist: Math.hypot(a.x - b.x, a.y - b.y) || 1, scale: scale.pos, mid, panX: panX.pos, panY: panY.pos }
|
|
542
|
+
lastMid = mid
|
|
543
|
+
setNow(scale, scale.pos)
|
|
544
|
+
setNow(panX, panX.pos)
|
|
545
|
+
setNow(panY, panY.pos)
|
|
546
|
+
mode = 'pinch'
|
|
547
|
+
animate()
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** After a pinch: back to 1 if it went below, into range around the fingers if it went past max. */
|
|
551
|
+
function settleZoom() {
|
|
552
|
+
if (scale.pos < 1) resetZoom()
|
|
553
|
+
else zoomTo(scale.pos, lastMid.x, lastMid.y)
|
|
554
|
+
animate()
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function velocity() {
|
|
558
|
+
const a = samples[0]
|
|
559
|
+
const b = samples[samples.length - 1]
|
|
560
|
+
if (!a || !b || b.t - a.t < 1) return { x: 0, y: 0 }
|
|
561
|
+
return { x: ((b.x - a.x) / (b.t - a.t)) * 1000, y: ((b.y - a.y) / (b.t - a.t)) * 1000 }
|
|
140
562
|
}
|
|
141
563
|
|
|
142
|
-
function
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
564
|
+
function onPointerUp(e: PointerEvent) {
|
|
565
|
+
if (!pointers.has(e.pointerId)) return
|
|
566
|
+
pointers.delete(e.pointerId)
|
|
567
|
+
if (mode === 'pinch') {
|
|
568
|
+
if (pointers.size > 1) return
|
|
569
|
+
settleZoom()
|
|
570
|
+
const rest = [...pointers.values()][0]
|
|
571
|
+
if (!rest) {
|
|
572
|
+
mode = null
|
|
573
|
+
return
|
|
574
|
+
}
|
|
575
|
+
// One finger left: it keeps moving a zoomed image, and does nothing otherwise.
|
|
576
|
+
const t = performance.now()
|
|
577
|
+
start = { x: rest.x, y: rest.y, t, trackX: trackX.pos, dismissY: dismissY.pos, panX: panX.pos, panY: panY.pos }
|
|
578
|
+
samples = [{ t, x: rest.x, y: rest.y }]
|
|
579
|
+
mode = scale.target > 1.01 ? 'pan' : 'ignore'
|
|
580
|
+
return
|
|
581
|
+
}
|
|
582
|
+
if (mode === 'ignore') {
|
|
583
|
+
if (pointers.size === 0) mode = null
|
|
584
|
+
return
|
|
585
|
+
}
|
|
586
|
+
const m = mode
|
|
587
|
+
mode = null
|
|
588
|
+
const v = velocity()
|
|
589
|
+
if (m === 'pending' && e.type === 'pointerup') return onTap(e)
|
|
590
|
+
if (m === 'swipe') {
|
|
591
|
+
const threshold = Math.min(120, (stageW || 480) * 0.25)
|
|
592
|
+
const dx = trackX.pos
|
|
593
|
+
if ((dx <= -threshold || v.x < -500) && nextIndex.value !== null) {
|
|
594
|
+
trackX.vel = v.x
|
|
595
|
+
next()
|
|
596
|
+
} else if ((dx >= threshold || v.x > 500) && prevIndex.value !== null) {
|
|
597
|
+
trackX.vel = v.x
|
|
598
|
+
prev()
|
|
156
599
|
} else {
|
|
157
|
-
target
|
|
600
|
+
trackX.target = 0
|
|
601
|
+
trackX.vel = v.x
|
|
602
|
+
animate()
|
|
158
603
|
}
|
|
159
|
-
} else {
|
|
160
|
-
|
|
604
|
+
} else if (m === 'dismiss') {
|
|
605
|
+
if (Math.abs(dismissY.pos) > 120 || Math.abs(v.y) > 800) {
|
|
606
|
+
dismissY.vel = v.y
|
|
607
|
+
close()
|
|
608
|
+
} else {
|
|
609
|
+
chrome.value = chromeBeforeDrag
|
|
610
|
+
dismissY.target = 0
|
|
611
|
+
dismissY.vel = v.y
|
|
612
|
+
animate()
|
|
613
|
+
}
|
|
614
|
+
} else if (m === 'pan') {
|
|
615
|
+
panX.vel = v.x
|
|
616
|
+
panY.vel = v.y
|
|
617
|
+
panX.target = panX.pos + v.x * 0.2
|
|
618
|
+
panY.target = panY.pos + v.y * 0.2
|
|
619
|
+
clampPanTargets()
|
|
620
|
+
animate()
|
|
161
621
|
}
|
|
162
|
-
target.style.opacity = '0'
|
|
163
622
|
}
|
|
164
623
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
624
|
+
/** A tap toggles the bars (or closes, outside the image); two taps zoom. */
|
|
625
|
+
function onTap(e: PointerEvent) {
|
|
626
|
+
const now = performance.now()
|
|
627
|
+
const double = now - lastTap.t < 300 && Math.hypot(e.clientX - lastTap.x, e.clientY - lastTap.y) < 30
|
|
628
|
+
lastTap = { t: double ? -Infinity : now, x: e.clientX, y: e.clientY }
|
|
629
|
+
if (tapTimer) clearTimeout(tapTimer)
|
|
630
|
+
tapTimer = null
|
|
631
|
+
if (double) {
|
|
632
|
+
toggleZoomAt(e.clientX, e.clientY)
|
|
633
|
+
return
|
|
171
634
|
}
|
|
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
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (offset <= -threshold) next()
|
|
228
|
-
else if (offset >= threshold) prev()
|
|
229
|
-
else if (stageImageEl.value) {
|
|
230
|
-
const el = stageImageEl.value
|
|
231
|
-
el.style.transition = 'transform 0.25s cubic-bezier(0.2, 0, 0, 1)'
|
|
232
|
-
el.style.transform = ''
|
|
233
|
-
const cleanup = (ev: TransitionEvent) => {
|
|
234
|
-
if (ev.target !== el) return
|
|
235
|
-
el.style.transition = ''
|
|
236
|
-
el.removeEventListener('transitionend', cleanup)
|
|
237
|
-
}
|
|
238
|
-
el.addEventListener('transitionend', cleanup)
|
|
635
|
+
// By position, not e.target: the stage captures the pointer, so pointerup targets the stage.
|
|
636
|
+
const r = currentImg()?.getBoundingClientRect()
|
|
637
|
+
const onImage = zoomed.value || (!!r && e.clientX >= r.left && e.clientX <= r.right && e.clientY >= r.top && e.clientY <= r.bottom)
|
|
638
|
+
tapTimer = setTimeout(() => {
|
|
639
|
+
tapTimer = null
|
|
640
|
+
if (onImage) chrome.value = !chrome.value
|
|
641
|
+
else close()
|
|
642
|
+
}, props.zoom ? 260 : 0)
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// ── Keyboard and focus ────────────────────────────────────────────────────────────────────────
|
|
646
|
+
|
|
647
|
+
function onKeydown(e: KeyboardEvent) {
|
|
648
|
+
if (e.key === 'Escape') close()
|
|
649
|
+
else if (e.key === 'ArrowRight') next()
|
|
650
|
+
else if (e.key === 'ArrowLeft') prev()
|
|
651
|
+
else if (props.zoom && (e.key === '+' || e.key === '=')) zoomTo(scale.target * 1.5, 0, 0)
|
|
652
|
+
else if (props.zoom && e.key === '-') zoomTo(scale.target / 1.5, 0, 0)
|
|
653
|
+
else if (props.zoom && e.key === '0') { resetZoom(); animate() }
|
|
654
|
+
else if (e.key === 'Tab') trapFocus(e)
|
|
655
|
+
else return
|
|
656
|
+
if (e.key !== 'Tab') e.preventDefault()
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function trapFocus(e: KeyboardEvent) {
|
|
660
|
+
const els = Array.from(rootEl.value?.querySelectorAll<HTMLElement>('button, a[href], input, [tabindex]:not([tabindex="-1"])') ?? [])
|
|
661
|
+
.filter((el) => !el.closest('[inert]'))
|
|
662
|
+
if (!els.length) return
|
|
663
|
+
const first = els[0]!
|
|
664
|
+
const last = els[els.length - 1]!
|
|
665
|
+
if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus() }
|
|
666
|
+
else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus() }
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function onImgLoad(src: string, e: Event) {
|
|
670
|
+
loaded.value = new Set(loaded.value).add(src)
|
|
671
|
+
measureRatio(src, e.target as HTMLImageElement)
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* An SVG with a viewBox but no width/height has a ratio but no size: inside a shrink-to-fit box it
|
|
676
|
+
* would collapse to 0×0 (and naturalWidth can't be trusted for it). So every image gets an explicit
|
|
677
|
+
* size from its ratio, read from an off-screen copy laid out at a fixed width.
|
|
678
|
+
*/
|
|
679
|
+
function measureRatio(src: string, img: HTMLImageElement) {
|
|
680
|
+
if (ratios.value.has(src)) return
|
|
681
|
+
const set = (r: number) => (ratios.value = new Map(ratios.value).set(src, r > 0 && Number.isFinite(r) ? r : 1))
|
|
682
|
+
const natural = img.naturalWidth && img.naturalHeight ? img.naturalWidth / img.naturalHeight : 0
|
|
683
|
+
if (natural && !/^data:image\/svg|\.svg(\?|#|$)/i.test(src)) return set(natural)
|
|
684
|
+
const probe = new Image()
|
|
685
|
+
probe.style.cssText = 'position:fixed;left:-10000px;top:0;width:1000px;height:auto;visibility:hidden;pointer-events:none'
|
|
686
|
+
const done = () => {
|
|
687
|
+
set(probe.offsetHeight ? 1000 / probe.offsetHeight : natural)
|
|
688
|
+
probe.remove()
|
|
239
689
|
}
|
|
690
|
+
probe.onload = done
|
|
691
|
+
probe.onerror = () => {
|
|
692
|
+
set(natural)
|
|
693
|
+
probe.remove()
|
|
694
|
+
}
|
|
695
|
+
probe.src = src
|
|
696
|
+
document.body.appendChild(probe)
|
|
697
|
+
if (probe.complete && probe.naturalWidth) done()
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/** The image's size: as large as fits in the box, keeping its ratio. */
|
|
701
|
+
function sizeFor(src: string) {
|
|
702
|
+
const r = ratios.value.get(src)
|
|
703
|
+
const { w, h } = box.value
|
|
704
|
+
if (!r || !w || !h) return undefined
|
|
705
|
+
const width = Math.min(w, h * r)
|
|
706
|
+
return { width: `${width}px`, height: `${width / r}px` }
|
|
240
707
|
}
|
|
241
708
|
|
|
242
|
-
// Auto-scan
|
|
243
|
-
// (e.g. MMarkdown output, a
|
|
244
|
-
// `items` array by hand. Opt an image
|
|
709
|
+
// ── Auto-scan ─────────────────────────────────────────────────────────────────────────────────
|
|
710
|
+
// Pull <img> elements out of whatever is rendered in the default slot (e.g. MMarkdown output, a
|
|
711
|
+
// hand-rolled gallery grid) so this works without building an `items` array by hand. Opt an image
|
|
712
|
+
// out with `data-lightbox="false"`.
|
|
245
713
|
let observer: MutationObserver | null = null
|
|
714
|
+
let resizeObserver: ResizeObserver | null = null
|
|
246
715
|
|
|
247
716
|
function scan() {
|
|
248
717
|
if (props.items || !containerEl.value) return
|
|
249
|
-
const imgs = Array.from(containerEl.value.querySelectorAll('img')).filter(
|
|
250
|
-
(img) => img.dataset.lightbox !== 'false',
|
|
251
|
-
)
|
|
718
|
+
const imgs = Array.from(containerEl.value.querySelectorAll('img')).filter((img) => img.dataset.lightbox !== 'false')
|
|
252
719
|
scannedItems.value = imgs.map((img) => ({
|
|
253
720
|
src: img.currentSrc || img.src,
|
|
254
721
|
alt: img.alt || undefined,
|
|
255
722
|
caption: img.dataset.caption || img.title || undefined,
|
|
256
723
|
}))
|
|
724
|
+
scannedEls.length = 0
|
|
257
725
|
imgs.forEach((img, i) => {
|
|
726
|
+
scannedEls.push(img)
|
|
727
|
+
origins.set(i, img)
|
|
258
728
|
img.style.cursor = 'zoom-in'
|
|
259
729
|
img.onclick = (e) => {
|
|
260
730
|
e.preventDefault()
|
|
@@ -264,142 +734,154 @@ function scan() {
|
|
|
264
734
|
}
|
|
265
735
|
|
|
266
736
|
onMounted(() => {
|
|
267
|
-
if (props.items
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
737
|
+
if (!props.items && containerEl.value) {
|
|
738
|
+
scan()
|
|
739
|
+
observer = new MutationObserver(scan)
|
|
740
|
+
observer.observe(containerEl.value, { childList: true, subtree: true, attributes: true, attributeFilter: ['src'] })
|
|
741
|
+
}
|
|
742
|
+
})
|
|
743
|
+
|
|
744
|
+
watch(stageEl, (el, old) => {
|
|
745
|
+
if (old) {
|
|
746
|
+
old.removeEventListener('wheel', onWheel)
|
|
747
|
+
resizeObserver?.disconnect()
|
|
748
|
+
}
|
|
749
|
+
if (el) {
|
|
750
|
+
el.addEventListener('wheel', onWheel, { passive: false })
|
|
751
|
+
resizeObserver = new ResizeObserver(() => {
|
|
752
|
+
measure()
|
|
753
|
+
write()
|
|
754
|
+
})
|
|
755
|
+
resizeObserver.observe(el)
|
|
756
|
+
}
|
|
276
757
|
})
|
|
277
758
|
|
|
278
759
|
onBeforeUnmount(() => {
|
|
279
760
|
observer?.disconnect()
|
|
761
|
+
resizeObserver?.disconnect()
|
|
762
|
+
stageEl.value?.removeEventListener('wheel', onWheel)
|
|
763
|
+
if (raf) cancelAnimationFrame(raf)
|
|
764
|
+
if (tapTimer) clearTimeout(tapTimer)
|
|
280
765
|
document.removeEventListener('keydown', onKeydown)
|
|
281
|
-
document.body.style.overflow = ''
|
|
766
|
+
if (rendered.value) document.body.style.overflow = ''
|
|
282
767
|
})
|
|
283
768
|
|
|
284
769
|
defineExpose({ open, close, next, prev })
|
|
285
770
|
</script>
|
|
286
771
|
|
|
287
772
|
<template>
|
|
288
|
-
<div ref="containerEl" class="contents">
|
|
773
|
+
<div ref="containerEl" v-bind="$attrs" class="contents">
|
|
289
774
|
<slot />
|
|
290
775
|
</div>
|
|
291
776
|
|
|
292
777
|
<Teleport to="body">
|
|
293
|
-
<
|
|
778
|
+
<div
|
|
779
|
+
v-if="rendered"
|
|
780
|
+
ref="rootEl"
|
|
781
|
+
class="fixed inset-0 z-[100] overflow-hidden text-white"
|
|
782
|
+
role="dialog"
|
|
783
|
+
aria-modal="true"
|
|
784
|
+
:aria-label="current?.alt || current?.caption || positionLabel"
|
|
785
|
+
>
|
|
786
|
+
<div data-lb-backdrop class="absolute inset-0 bg-black" style="opacity: 0" />
|
|
787
|
+
|
|
788
|
+
<!-- Stage: the image track -->
|
|
294
789
|
<div
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
790
|
+
ref="stageEl"
|
|
791
|
+
data-lb-stage
|
|
792
|
+
class="absolute inset-0 touch-none select-none"
|
|
793
|
+
:class="zoomed ? 'cursor-grab active:cursor-grabbing' : props.zoom ? 'cursor-zoom-in' : ''"
|
|
794
|
+
@pointerdown="onPointerDown"
|
|
795
|
+
@pointermove="onPointerMove"
|
|
796
|
+
@pointerup="onPointerUp"
|
|
797
|
+
@pointercancel="onPointerUp"
|
|
798
|
+
@dragstart.prevent
|
|
298
799
|
>
|
|
299
|
-
<
|
|
300
|
-
v-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
<button
|
|
307
|
-
type="button"
|
|
308
|
-
class="absolute right-2 top-2 z-10 flex h-11 w-11 items-center justify-center rounded-full bg-black/30 text-white transition-colors hover:bg-black/50 sm:right-4 sm:top-4"
|
|
309
|
-
:aria-label="closeLabel ?? locale.close"
|
|
310
|
-
@click="close"
|
|
800
|
+
<div
|
|
801
|
+
v-for="s in slides"
|
|
802
|
+
:key="s.offset === -1 && s.index === nextIndex ? `prev-${s.index}` : s.index"
|
|
803
|
+
data-lb-slide
|
|
804
|
+
:data-offset="s.offset"
|
|
805
|
+
class="absolute inset-0 flex items-center justify-center p-4 sm:p-12"
|
|
806
|
+
:aria-hidden="s.offset !== 0"
|
|
311
807
|
>
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
<MIcon name="chevron_left" :size="28" />
|
|
324
|
-
</button>
|
|
325
|
-
|
|
326
|
-
<div
|
|
327
|
-
class="relative h-full w-full touch-pan-y overflow-hidden"
|
|
328
|
-
@pointerdown="onStagePointerDown"
|
|
329
|
-
@pointermove="onStagePointerMove"
|
|
330
|
-
@pointerup="endDrag"
|
|
331
|
-
@pointercancel="endDrag"
|
|
332
|
-
>
|
|
333
|
-
<Transition
|
|
334
|
-
:css="false"
|
|
335
|
-
@before-enter="onImageBeforeEnter"
|
|
336
|
-
@enter="onImageEnter"
|
|
337
|
-
@leave="onImageLeave"
|
|
338
|
-
>
|
|
339
|
-
<div
|
|
340
|
-
v-if="current"
|
|
341
|
-
:key="currentIndex"
|
|
342
|
-
ref="stageImageEl"
|
|
343
|
-
class="absolute inset-0 flex items-center justify-center p-4"
|
|
344
|
-
@click="close"
|
|
345
|
-
>
|
|
346
|
-
<img
|
|
347
|
-
:src="current.src"
|
|
348
|
-
:alt="current.alt ?? ''"
|
|
349
|
-
class="max-h-full max-w-full select-none object-contain"
|
|
350
|
-
:class="rounded ? 'rounded-2xl' : ''"
|
|
351
|
-
draggable="false"
|
|
352
|
-
@click.stop
|
|
353
|
-
/>
|
|
354
|
-
</div>
|
|
355
|
-
</Transition>
|
|
808
|
+
<!-- No style binding here: transform / opacity / clip-path are written by write() -->
|
|
809
|
+
<div data-lb-zoom class="flex max-h-full max-w-full items-center justify-center">
|
|
810
|
+
<img
|
|
811
|
+
:src="galleryItems[s.index]!.src"
|
|
812
|
+
:alt="galleryItems[s.index]!.alt ?? ''"
|
|
813
|
+
class="block select-none object-contain"
|
|
814
|
+
:class="[rounded ? 'rounded-2xl' : '', sizeFor(galleryItems[s.index]!.src) ? '' : 'max-h-[calc(100dvh-2rem)] max-w-full sm:max-h-[calc(100dvh-6rem)]']"
|
|
815
|
+
:style="sizeFor(galleryItems[s.index]!.src)"
|
|
816
|
+
draggable="false"
|
|
817
|
+
@load="onImgLoad(galleryItems[s.index]!.src, $event)"
|
|
818
|
+
/>
|
|
356
819
|
</div>
|
|
820
|
+
<MSpinner
|
|
821
|
+
v-if="s.offset === 0 && !loaded.has(galleryItems[s.index]!.src)"
|
|
822
|
+
:size="40"
|
|
823
|
+
class="pointer-events-none absolute text-white/80"
|
|
824
|
+
/>
|
|
825
|
+
</div>
|
|
826
|
+
</div>
|
|
357
827
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
828
|
+
<!-- Top bar -->
|
|
829
|
+
<div
|
|
830
|
+
class="pointer-events-none absolute inset-x-0 top-0 z-10 flex items-center gap-1 bg-gradient-to-b from-black/60 to-transparent px-2 pt-2 pb-8 transition-opacity duration-200 sm:px-4"
|
|
831
|
+
:class="chrome ? 'opacity-100' : 'opacity-0'"
|
|
832
|
+
:inert="!chrome"
|
|
833
|
+
>
|
|
834
|
+
<!-- The bars let touches through (a pinch can start anywhere); only their controls take them -->
|
|
835
|
+
<MIconButton data-lb-close icon="close" :label="closeLabel ?? locale.close" class="pointer-events-auto" style="color: #fff" @click="close" />
|
|
836
|
+
<span v-if="hasMultiple" class="flex-1 px-2 text-title-medium" aria-hidden="true">{{ currentIndex + 1 }} / {{ count }}</span>
|
|
837
|
+
<span v-else class="flex-1" />
|
|
838
|
+
<div v-if="current && $slots.actions" class="pointer-events-auto flex items-center gap-1">
|
|
839
|
+
<slot name="actions" :item="current" :index="currentIndex" />
|
|
367
840
|
</div>
|
|
841
|
+
</div>
|
|
368
842
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
843
|
+
<!-- Previous / next, for mouse users -->
|
|
844
|
+
<template v-if="hasMultiple">
|
|
845
|
+
<div
|
|
846
|
+
class="absolute top-1/2 left-2 z-10 -translate-y-1/2 transition-opacity duration-200 sm:left-4 [@media(pointer:coarse)]:hidden"
|
|
847
|
+
:class="chrome && prevIndex !== null ? 'opacity-100' : 'pointer-events-none opacity-0'"
|
|
372
848
|
>
|
|
373
|
-
|
|
374
|
-
</
|
|
375
|
-
|
|
849
|
+
<MIconButton icon="chevron_left" :label="locale.previous" variant="tonal" @click="prev" />
|
|
850
|
+
</div>
|
|
376
851
|
<div
|
|
377
|
-
|
|
378
|
-
class="
|
|
852
|
+
class="absolute top-1/2 right-2 z-10 -translate-y-1/2 transition-opacity duration-200 sm:right-4 [@media(pointer:coarse)]:hidden"
|
|
853
|
+
:class="chrome && nextIndex !== null ? 'opacity-100' : 'pointer-events-none opacity-0'"
|
|
379
854
|
>
|
|
855
|
+
<MIconButton icon="chevron_right" :label="locale.next" variant="tonal" @click="next" />
|
|
856
|
+
</div>
|
|
857
|
+
</template>
|
|
858
|
+
|
|
859
|
+
<!-- Caption and thumbnails -->
|
|
860
|
+
<div
|
|
861
|
+
v-if="current?.caption || (showThumbnails && hasMultiple)"
|
|
862
|
+
class="pointer-events-none absolute inset-x-0 bottom-0 z-10 flex flex-col gap-3 bg-gradient-to-t from-black/70 to-transparent px-4 pt-10 pb-4 transition-opacity duration-200"
|
|
863
|
+
:class="chrome ? 'opacity-100' : 'opacity-0'"
|
|
864
|
+
:inert="!chrome"
|
|
865
|
+
>
|
|
866
|
+
<p v-if="current?.caption" class="mx-auto max-w-3xl text-center text-body-large">{{ current.caption }}</p>
|
|
867
|
+
<div v-if="showThumbnails && hasMultiple" ref="thumbsEl" class="pointer-events-auto flex gap-2 overflow-x-auto px-1 py-1 sm:justify-center" style="scrollbar-width: none">
|
|
380
868
|
<button
|
|
381
869
|
v-for="(item, i) in galleryItems"
|
|
382
870
|
:key="i"
|
|
383
871
|
type="button"
|
|
384
|
-
|
|
872
|
+
data-lb-thumb
|
|
873
|
+
class="h-14 w-14 shrink-0 cursor-pointer overflow-hidden rounded-md ring-2 transition-opacity"
|
|
385
874
|
:class="i === currentIndex ? 'opacity-100 ring-white' : 'opacity-50 ring-transparent hover:opacity-80'"
|
|
386
|
-
|
|
875
|
+
:aria-label="item.alt || locale.carouselItem.replace('{index}', String(i + 1)).replace('{count}', String(count))"
|
|
876
|
+
:aria-current="i === currentIndex ? 'true' : undefined"
|
|
877
|
+
@click="go(i)"
|
|
387
878
|
>
|
|
388
|
-
<img :src="item.src" :alt="
|
|
879
|
+
<img :src="item.src" :alt="''" class="h-full w-full object-cover" draggable="false" />
|
|
389
880
|
</button>
|
|
390
881
|
</div>
|
|
391
882
|
</div>
|
|
392
|
-
|
|
883
|
+
|
|
884
|
+
<span class="sr-only" aria-live="polite">{{ positionLabel }}</span>
|
|
885
|
+
</div>
|
|
393
886
|
</Teleport>
|
|
394
887
|
</template>
|
|
395
|
-
|
|
396
|
-
<style scoped>
|
|
397
|
-
.m3-lightbox-enter-active,
|
|
398
|
-
.m3-lightbox-leave-active {
|
|
399
|
-
transition: opacity 0.2s ease;
|
|
400
|
-
}
|
|
401
|
-
.m3-lightbox-enter-from,
|
|
402
|
-
.m3-lightbox-leave-to {
|
|
403
|
-
opacity: 0;
|
|
404
|
-
}
|
|
405
|
-
</style>
|