@bagelink/vue 1.20.5 → 1.20.17
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/assets.d.ts +9 -0
- package/dist/assets.d.ts.map +1 -1
- package/dist/components/form/inputs/CodeEditor/Index.vue.d.ts +1 -1
- package/dist/components/form/inputs/Upload/AssetFileRow.vue.d.ts +37 -0
- package/dist/components/form/inputs/Upload/AssetFileRow.vue.d.ts.map +1 -0
- package/dist/components/form/inputs/Upload/AssetUploadInput.vue.d.ts +33 -3
- package/dist/components/form/inputs/Upload/AssetUploadInput.vue.d.ts.map +1 -1
- package/dist/components/form/inputs/Upload/ImageCropper.vue.d.ts +33 -0
- package/dist/components/form/inputs/Upload/ImageCropper.vue.d.ts.map +1 -0
- package/dist/components/form/inputs/Upload/UploadInput.vue.d.ts +13 -79
- package/dist/components/form/inputs/Upload/UploadInput.vue.d.ts.map +1 -1
- package/dist/components/form/inputs/Upload/upload.types.d.ts +14 -0
- package/dist/components/form/inputs/Upload/upload.types.d.ts.map +1 -1
- package/dist/components/lightbox/index.d.ts.map +1 -1
- package/dist/core.css +2 -2
- package/dist/i18n/index.d.ts +108 -0
- package/dist/i18n/index.d.ts.map +1 -1
- package/dist/index.cjs +99 -99
- package/dist/index.css +1 -1
- package/dist/index.mjs +15677 -14427
- package/dist/style.css +2 -2
- package/dist/types/BagelForm.d.ts +34 -0
- package/dist/types/BagelForm.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/assets.ts +27 -3
- package/src/components/dataTable/DataTable.vue +3 -3
- package/src/components/form/inputs/Upload/AssetFileRow.vue +150 -0
- package/src/components/form/inputs/Upload/AssetUploadInput.vue +694 -37
- package/src/components/form/inputs/Upload/ImageCropper.vue +525 -0
- package/src/components/form/inputs/Upload/UploadInput.vue +18 -3
- package/src/components/form/inputs/Upload/upload.css +109 -1
- package/src/components/form/inputs/Upload/upload.types.ts +15 -0
- package/src/components/lightbox/index.ts +6 -3
- package/src/i18n/locales/en.json +19 -1
- package/src/i18n/locales/es.json +19 -1
- package/src/i18n/locales/fr.json +21 -2
- package/src/i18n/locales/he.json +19 -1
- package/src/i18n/locales/it.json +19 -1
- package/src/i18n/locales/ru.json +19 -1
- package/src/styles/inputs.css +4 -0
- package/src/types/BagelForm.ts +34 -0
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onUnmounted, ref, watch } from 'vue'
|
|
3
|
+
import { Btn, Icon, NumberInput, RangeInput, useI18n } from '../../../../index'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Cropping surface: the picked image is always shown whole — the stage takes the
|
|
7
|
+
* image's own aspect ratio, so there is no letterbox/empty area around it — and a
|
|
8
|
+
* crop window is drawn on top of the photo. The window is moved by dragging it and
|
|
9
|
+
* resized from its corners, which is enough to pick any region without a zoom level.
|
|
10
|
+
*
|
|
11
|
+
* Export maps the window (stored in fractions of the stage) straight onto source
|
|
12
|
+
* pixels, then draws it into a canvas at the chosen pixel size.
|
|
13
|
+
*/
|
|
14
|
+
const props = withDefaults(defineProps<{
|
|
15
|
+
/** Object URL (or any displayable URL) of the picked image. */
|
|
16
|
+
src: string
|
|
17
|
+
/** Locked crop ratio, e.g. `'1'`, `'16-9'`, `'3-4'`. Omit to let the user choose (starts free). */
|
|
18
|
+
aspect?: string
|
|
19
|
+
/** Longest side of the exported file, in pixels. */
|
|
20
|
+
outputSize?: number
|
|
21
|
+
}>(), {
|
|
22
|
+
aspect: undefined,
|
|
23
|
+
outputSize: 2048,
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
const { $t } = useI18n()
|
|
27
|
+
|
|
28
|
+
const RATIOS = ['free', '1', '4-3', '16-9', '3-4', 'original'] as const
|
|
29
|
+
const HANDLES = ['nw', 'ne', 'sw', 'se'] as const
|
|
30
|
+
type Handle = typeof HANDLES[number]
|
|
31
|
+
|
|
32
|
+
/** Smallest crop window, in stage pixels — room for all four corner handles. */
|
|
33
|
+
const MIN_PX = 56
|
|
34
|
+
|
|
35
|
+
const stage = ref<HTMLElement>()
|
|
36
|
+
const image = ref<HTMLImageElement>()
|
|
37
|
+
const natural = ref({ w: 0, h: 0 })
|
|
38
|
+
const ready = ref(false)
|
|
39
|
+
|
|
40
|
+
/** Unset `aspect` starts free: the window is the whole image, and any preset is one click away. */
|
|
41
|
+
const ratio = ref<string>(props.aspect ?? 'free')
|
|
42
|
+
|
|
43
|
+
/** The stage follows the source image, which is what removes the empty frame. */
|
|
44
|
+
const imageRatio = computed(() => {
|
|
45
|
+
const { w, h } = natural.value
|
|
46
|
+
return w && h ? w / h : 1
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/** Free crop: no locked ratio, the window resizes on both axes independently. */
|
|
50
|
+
const isFree = computed(() => ratio.value === 'free')
|
|
51
|
+
|
|
52
|
+
/** Crop window ratio: a preset, or the image's own for `original`. */
|
|
53
|
+
const frameRatio = computed(() => {
|
|
54
|
+
if (ratio.value === 'original' || isFree.value) return imageRatio.value
|
|
55
|
+
const [w, h] = ratio.value.split('-').map(Number)
|
|
56
|
+
return w && h ? w / h : 1
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
/** Crop window in fractions of the stage (0–1), so it survives a viewport resize. */
|
|
60
|
+
const selection = ref({ x: 0, y: 0, w: 1, h: 1 })
|
|
61
|
+
|
|
62
|
+
/** Largest window of the current ratio that fits inside the image. */
|
|
63
|
+
const maxWindow = computed(() => {
|
|
64
|
+
if (isFree.value) return { w: 1, h: 1 }
|
|
65
|
+
const a = imageRatio.value
|
|
66
|
+
const r = frameRatio.value
|
|
67
|
+
return r >= a ? { w: 1, h: a / r } : { w: r / a, h: 1 }
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
function clamp(value: number, min: number, max: number) {
|
|
71
|
+
return Math.min(max, Math.max(min, value))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function fitSelection() {
|
|
75
|
+
const { w, h } = maxWindow.value
|
|
76
|
+
selection.value = { x: (1 - w) / 2, y: (1 - h) / 2, w, h }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* ─── Dragging / resizing the window ──────────────────────────────────────── */
|
|
80
|
+
|
|
81
|
+
const cropWindow = ref<HTMLElement>()
|
|
82
|
+
|
|
83
|
+
type DragMode = 'move' | Handle
|
|
84
|
+
|
|
85
|
+
interface Rect { x: number; y: number; w: number; h: number }
|
|
86
|
+
|
|
87
|
+
let drag: {
|
|
88
|
+
mode: DragMode
|
|
89
|
+
startX: number
|
|
90
|
+
startY: number
|
|
91
|
+
box: { w: number; h: number }
|
|
92
|
+
rect: Rect
|
|
93
|
+
next: Rect
|
|
94
|
+
x: number
|
|
95
|
+
y: number
|
|
96
|
+
frame: number
|
|
97
|
+
} | null = null
|
|
98
|
+
|
|
99
|
+
function beginDrag(event: PointerEvent, mode: DragMode) {
|
|
100
|
+
if (!ready.value) return
|
|
101
|
+
// A second finger would overwrite the drag in progress and orphan its listeners.
|
|
102
|
+
if (!event.isPrimary) return
|
|
103
|
+
const element = stage.value
|
|
104
|
+
if (!element) return
|
|
105
|
+
const box = { w: element.clientWidth, h: element.clientHeight }
|
|
106
|
+
if (!box.w || !box.h) return
|
|
107
|
+
event.preventDefault()
|
|
108
|
+
event.stopPropagation()
|
|
109
|
+
const rect = { ...selection.value }
|
|
110
|
+
drag = {
|
|
111
|
+
mode,
|
|
112
|
+
startX: event.clientX,
|
|
113
|
+
startY: event.clientY,
|
|
114
|
+
box,
|
|
115
|
+
rect,
|
|
116
|
+
next: rect,
|
|
117
|
+
x: event.clientX,
|
|
118
|
+
y: event.clientY,
|
|
119
|
+
frame: 0,
|
|
120
|
+
}
|
|
121
|
+
window.addEventListener('pointermove', onDragMove)
|
|
122
|
+
window.addEventListener('pointerup', endDrag, { once: true })
|
|
123
|
+
window.addEventListener('pointercancel', endDrag, { once: true })
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* During a drag the window is painted straight into the DOM: a pointermove burst
|
|
128
|
+
* arrives far faster than the frame rate, and writing reactive state would re-render
|
|
129
|
+
* the slider and both pixel inputs for every event. One paint per frame, and the
|
|
130
|
+
* whole component updates once — on release.
|
|
131
|
+
*/
|
|
132
|
+
function paint(rect: Rect) {
|
|
133
|
+
const element = cropWindow.value
|
|
134
|
+
if (!element) return
|
|
135
|
+
element.style.left = `${rect.x * 100}%`
|
|
136
|
+
element.style.top = `${rect.y * 100}%`
|
|
137
|
+
element.style.width = `${rect.w * 100}%`
|
|
138
|
+
element.style.height = `${rect.h * 100}%`
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Where the window should be for the pointer at (x, y), given the drag it started. */
|
|
142
|
+
function resolve(x: number, y: number): Rect {
|
|
143
|
+
const { mode, startX, startY, box, rect } = drag!
|
|
144
|
+
const dx = (x - startX) / box.w
|
|
145
|
+
const dy = (y - startY) / box.h
|
|
146
|
+
|
|
147
|
+
if (mode === 'move') {
|
|
148
|
+
return {
|
|
149
|
+
...rect,
|
|
150
|
+
x: clamp(rect.x + dx, 0, 1 - rect.w),
|
|
151
|
+
y: clamp(rect.y + dy, 0, 1 - rect.h),
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Free crop: each axis follows the pointer on its own, so any shape can be picked.
|
|
156
|
+
const east = mode === 'ne' || mode === 'se'
|
|
157
|
+
const south = mode === 'se' || mode === 'sw'
|
|
158
|
+
// Room left for growth, measured from the corner opposite to the dragged one.
|
|
159
|
+
const roomX = east ? 1 - rect.x : rect.x + rect.w
|
|
160
|
+
const roomY = south ? 1 - rect.y : rect.y + rect.h
|
|
161
|
+
const minW = MIN_PX / box.w
|
|
162
|
+
const minH = MIN_PX / box.h
|
|
163
|
+
|
|
164
|
+
if (isFree.value) {
|
|
165
|
+
const w = clamp(rect.w + (east ? dx : -dx), Math.min(minW, roomX), roomX)
|
|
166
|
+
const h = clamp(rect.h + (south ? dy : -dy), Math.min(minH, roomY), roomY)
|
|
167
|
+
return {
|
|
168
|
+
w,
|
|
169
|
+
h,
|
|
170
|
+
x: east ? rect.x : rect.x + rect.w - w,
|
|
171
|
+
y: south ? rect.y : rect.y + rect.h - h,
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// A locked ratio: take the axis the pointer travelled furthest on, converted to
|
|
176
|
+
// a width delta (h = w · imageRatio / frameRatio).
|
|
177
|
+
const a = imageRatio.value
|
|
178
|
+
const r = frameRatio.value
|
|
179
|
+
const horizontal = east ? dx : -dx
|
|
180
|
+
const vertical = (south ? dy : -dy) * (r / a)
|
|
181
|
+
const delta = Math.abs(horizontal) >= Math.abs(vertical) ? horizontal : vertical
|
|
182
|
+
|
|
183
|
+
const minWidth = Math.max(minW, minH * (r / a))
|
|
184
|
+
const maxW = Math.min(roomX, roomY * (r / a))
|
|
185
|
+
const w = clamp(rect.w + delta, Math.min(minWidth, maxW), maxW)
|
|
186
|
+
const h = w * (a / r)
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
w,
|
|
190
|
+
h,
|
|
191
|
+
x: east ? rect.x : rect.x + rect.w - w,
|
|
192
|
+
y: south ? rect.y : rect.y + rect.h - h,
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function onDragMove(event: PointerEvent) {
|
|
197
|
+
if (!drag) return
|
|
198
|
+
drag.x = event.clientX
|
|
199
|
+
drag.y = event.clientY
|
|
200
|
+
// A mouse released outside the browser window never delivers `pointerup`; without
|
|
201
|
+
// this the window would keep following a pointer with no button held down.
|
|
202
|
+
if (!event.buttons) {
|
|
203
|
+
endDrag()
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
if (drag.frame) return
|
|
207
|
+
drag.frame = requestAnimationFrame(() => {
|
|
208
|
+
if (!drag) return
|
|
209
|
+
drag.frame = 0
|
|
210
|
+
drag.next = resolve(drag.x, drag.y)
|
|
211
|
+
paint(drag.next)
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function endDrag() {
|
|
216
|
+
if (drag) {
|
|
217
|
+
if (drag.frame) cancelAnimationFrame(drag.frame)
|
|
218
|
+
// Resolve from the final pointer position, not the last painted frame, so a
|
|
219
|
+
// release inside a single frame still keeps the movement.
|
|
220
|
+
drag.next = resolve(drag.x, drag.y)
|
|
221
|
+
// Commit once, so the slider and the pixel fields catch up in a single render.
|
|
222
|
+
selection.value = drag.next
|
|
223
|
+
drag = null
|
|
224
|
+
}
|
|
225
|
+
window.removeEventListener('pointermove', onDragMove)
|
|
226
|
+
window.removeEventListener('pointerup', endDrag)
|
|
227
|
+
window.removeEventListener('pointercancel', endDrag)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
onUnmounted(() => {
|
|
231
|
+
// Closing the dialog mid-drag would otherwise leave a window listener and a
|
|
232
|
+
// pending frame pointing at a detached element.
|
|
233
|
+
if (drag?.frame) cancelAnimationFrame(drag.frame)
|
|
234
|
+
drag = null
|
|
235
|
+
window.removeEventListener('pointermove', onDragMove)
|
|
236
|
+
window.removeEventListener('pointerup', endDrag)
|
|
237
|
+
window.removeEventListener('pointercancel', endDrag)
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
/* ─── Size of the crop, as a percentage of the largest window (slider) ────── */
|
|
241
|
+
|
|
242
|
+
const cropPercent = computed(() => Math.round((selection.value.w / maxWindow.value.w) * 100))
|
|
243
|
+
|
|
244
|
+
function onCropPercent(value: number | [number, number]) {
|
|
245
|
+
if (Array.isArray(value)) return
|
|
246
|
+
const target = clamp(value, 1, 100) / 100
|
|
247
|
+
const rect = selection.value
|
|
248
|
+
|
|
249
|
+
let w: number
|
|
250
|
+
let h: number
|
|
251
|
+
if (isFree.value) {
|
|
252
|
+
// Free crop: scale the box uniformly so its own shape is preserved.
|
|
253
|
+
const factor = Math.min(target / Math.max(rect.w, 0.0001), 1 / Math.max(rect.h, 0.0001))
|
|
254
|
+
w = rect.w * factor
|
|
255
|
+
h = rect.h * factor
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
const a = imageRatio.value
|
|
259
|
+
const r = frameRatio.value
|
|
260
|
+
w = maxWindow.value.w * target
|
|
261
|
+
h = w * (a / r)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const centerX = rect.x + rect.w / 2
|
|
265
|
+
const centerY = rect.y + rect.h / 2
|
|
266
|
+
selection.value = {
|
|
267
|
+
w,
|
|
268
|
+
h,
|
|
269
|
+
x: clamp(centerX - w / 2, 0, 1 - w),
|
|
270
|
+
y: clamp(centerY - h / 2, 0, 1 - h),
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/* ─── Output size in pixels ───────────────────────────────────────────────── */
|
|
275
|
+
|
|
276
|
+
/** Size of the cropped region in source pixels — the ceiling for the output. */
|
|
277
|
+
const sourceWidth = computed(() => Math.max(1, Math.round(selection.value.w * natural.value.w)))
|
|
278
|
+
const sourceHeight = computed(() => Math.max(1, Math.round(selection.value.h * natural.value.h)))
|
|
279
|
+
|
|
280
|
+
/** Default output: the cropped region, capped by `outputSize` on its longest side. */
|
|
281
|
+
const autoWidth = computed(() => {
|
|
282
|
+
const longest = Math.max(sourceWidth.value, sourceHeight.value)
|
|
283
|
+
const scale = Math.min(1, props.outputSize / longest)
|
|
284
|
+
return Math.max(1, Math.round(sourceWidth.value * scale))
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
const outWidth = ref(1)
|
|
288
|
+
/** Once the user types a size, dragging the crop no longer overwrites it. */
|
|
289
|
+
const tuned = ref(false)
|
|
290
|
+
|
|
291
|
+
const outHeight = computed(() => Math.max(
|
|
292
|
+
1,
|
|
293
|
+
Math.round(outWidth.value * (sourceHeight.value / sourceWidth.value)),
|
|
294
|
+
))
|
|
295
|
+
|
|
296
|
+
function setOutWidth(value?: number) {
|
|
297
|
+
if (!value) return
|
|
298
|
+
tuned.value = true
|
|
299
|
+
outWidth.value = clamp(Math.round(value), 1, sourceWidth.value)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function setOutHeight(value?: number) {
|
|
303
|
+
if (!value) return
|
|
304
|
+
tuned.value = true
|
|
305
|
+
const width = Math.round(value * (sourceWidth.value / sourceHeight.value))
|
|
306
|
+
outWidth.value = clamp(width, 1, sourceWidth.value)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
watch([autoWidth, sourceWidth], () => {
|
|
310
|
+
if (!tuned.value) {
|
|
311
|
+
outWidth.value = autoWidth.value
|
|
312
|
+
return
|
|
313
|
+
}
|
|
314
|
+
// A smaller crop can't deliver a bigger export — pull the size down with it.
|
|
315
|
+
if (outWidth.value > sourceWidth.value) outWidth.value = sourceWidth.value
|
|
316
|
+
}, { immediate: true })
|
|
317
|
+
|
|
318
|
+
const maxLabel = computed(() => $t('upload.cropMax', {
|
|
319
|
+
w: sourceWidth.value.toLocaleString(),
|
|
320
|
+
h: sourceHeight.value.toLocaleString(),
|
|
321
|
+
}))
|
|
322
|
+
|
|
323
|
+
/* ─── Lifecycle ───────────────────────────────────────────────────────────── */
|
|
324
|
+
|
|
325
|
+
function onLoad() {
|
|
326
|
+
const element = image.value
|
|
327
|
+
if (!element) return
|
|
328
|
+
natural.value = { w: element.naturalWidth, h: element.naturalHeight }
|
|
329
|
+
ready.value = true
|
|
330
|
+
fitSelection()
|
|
331
|
+
tuned.value = false
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function reset() {
|
|
335
|
+
fitSelection()
|
|
336
|
+
tuned.value = false
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Re-fit the window whenever the chosen ratio changes. */
|
|
340
|
+
watch(ratio, () => {
|
|
341
|
+
if (ready.value) {
|
|
342
|
+
fitSelection()
|
|
343
|
+
tuned.value = false
|
|
344
|
+
}
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
watch(() => props.aspect, value => { ratio.value = value ?? 'free' })
|
|
348
|
+
watch(() => props.src, () => { ready.value = false; natural.value = { w: 0, h: 0 } })
|
|
349
|
+
|
|
350
|
+
function ratioLabel(value: string) {
|
|
351
|
+
if (value === 'free') return $t('upload.cropFree')
|
|
352
|
+
if (value === 'original') return $t('upload.cropOriginal')
|
|
353
|
+
return value.replace('-', ':')
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const selectionStyle = computed(() => ({
|
|
357
|
+
left: `${selection.value.x * 100}%`,
|
|
358
|
+
top: `${selection.value.y * 100}%`,
|
|
359
|
+
width: `${selection.value.w * 100}%`,
|
|
360
|
+
height: `${selection.value.h * 100}%`,
|
|
361
|
+
}))
|
|
362
|
+
|
|
363
|
+
/* ─── Export ──────────────────────────────────────────────────────────────── */
|
|
364
|
+
|
|
365
|
+
/** Renders the crop window to a canvas and returns it as a File. */
|
|
366
|
+
async function getFile(): Promise<File | undefined> {
|
|
367
|
+
const element = image.value
|
|
368
|
+
const { w, h } = natural.value
|
|
369
|
+
if (!element || !w || !h) return
|
|
370
|
+
|
|
371
|
+
const rect = selection.value
|
|
372
|
+
const srcX = Math.round(rect.x * w)
|
|
373
|
+
const srcY = Math.round(rect.y * h)
|
|
374
|
+
const srcW = Math.max(1, Math.round(rect.w * w))
|
|
375
|
+
const srcH = Math.max(1, Math.round(rect.h * h))
|
|
376
|
+
const outW = Math.max(1, Math.min(sourceWidth.value, Math.round(outWidth.value)))
|
|
377
|
+
const outH = Math.max(1, Math.round(outW * (srcH / srcW)))
|
|
378
|
+
|
|
379
|
+
const canvas = document.createElement('canvas')
|
|
380
|
+
canvas.width = outW
|
|
381
|
+
canvas.height = outH
|
|
382
|
+
const context = canvas.getContext('2d')
|
|
383
|
+
if (!context) return
|
|
384
|
+
context.imageSmoothingQuality = 'high'
|
|
385
|
+
context.drawImage(element, srcX, srcY, srcW, srcH, 0, 0, outW, outH)
|
|
386
|
+
|
|
387
|
+
const isPng = /^data:image\/png/i.test(props.src) || /\.png(\?|#|$)/i.test(props.src)
|
|
388
|
+
const type = isPng ? 'image/png' : 'image/jpeg'
|
|
389
|
+
const blob = await new Promise<Blob | null>(resolve => canvas.toBlob(resolve, type, 0.92))
|
|
390
|
+
if (!blob) return
|
|
391
|
+
return new File([blob], `crop-${Date.now()}.${isPng ? 'png' : 'jpg'}`, { type })
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
defineExpose({ getFile, reset })
|
|
395
|
+
</script>
|
|
396
|
+
|
|
397
|
+
<template>
|
|
398
|
+
<div class="grid gap-1">
|
|
399
|
+
<div
|
|
400
|
+
ref="stage" class="crop-stage relative overflow-hidden radius-1 bg-black user-select-none"
|
|
401
|
+
:style="{ '--crop-ratio': imageRatio }"
|
|
402
|
+
@pointerdown="beginDrag($event, 'move')"
|
|
403
|
+
>
|
|
404
|
+
<img
|
|
405
|
+
ref="image" :src="src" alt="" draggable="false"
|
|
406
|
+
class="absolute inset w-100p h-100p object-fit-cover pointer-events-none"
|
|
407
|
+
@load="onLoad"
|
|
408
|
+
>
|
|
409
|
+
<div
|
|
410
|
+
v-if="ready" ref="cropWindow" class="crop-window absolute" :style="selectionStyle"
|
|
411
|
+
@pointerdown="beginDrag($event, 'move')"
|
|
412
|
+
>
|
|
413
|
+
<span
|
|
414
|
+
v-for="handle in HANDLES" :key="handle" class="crop-handle"
|
|
415
|
+
:class="`crop-handle--${handle}`"
|
|
416
|
+
@pointerdown="beginDrag($event, handle)"
|
|
417
|
+
/>
|
|
418
|
+
</div>
|
|
419
|
+
</div>
|
|
420
|
+
|
|
421
|
+
<div v-if="!aspect" class="flex flex-wrap gap-05">
|
|
422
|
+
<Btn
|
|
423
|
+
v-for="preset in RATIOS" :key="preset" thin
|
|
424
|
+
:outline="ratio !== preset" :color="ratio === preset ? 'primary' : undefined"
|
|
425
|
+
:value="ratioLabel(preset)" @click="ratio = preset"
|
|
426
|
+
/>
|
|
427
|
+
</div>
|
|
428
|
+
|
|
429
|
+
<RangeInput
|
|
430
|
+
:model-value="cropPercent" :min="1" :max="100" :step="1" size="sm" hide-min-max
|
|
431
|
+
:label="$t('upload.cropSize')"
|
|
432
|
+
:format-value="value => `${Math.round(value)}%`"
|
|
433
|
+
@update:model-value="onCropPercent"
|
|
434
|
+
class="m_w-95p"
|
|
435
|
+
/>
|
|
436
|
+
|
|
437
|
+
<div class="grid gap-025">
|
|
438
|
+
<span class="txt12 opacity-6">{{ $t('upload.cropPixels') }}</span>
|
|
439
|
+
<div class="flex flex-wrap gap-05 align-items-center">
|
|
440
|
+
<NumberInput
|
|
441
|
+
:model-value="outWidth" :min="1" :max="sourceWidth" :step="1" :spinner="false"
|
|
442
|
+
:placeholder="$t('upload.cropWidth')" hide-label max-width="6.5rem"
|
|
443
|
+
@update:model-value="setOutWidth"
|
|
444
|
+
class="mb-0"
|
|
445
|
+
/>
|
|
446
|
+
<span class="opacity-6">×</span>
|
|
447
|
+
<NumberInput
|
|
448
|
+
:model-value="outHeight" :min="1" :max="sourceHeight" :step="1" :spinner="false"
|
|
449
|
+
:placeholder="$t('upload.cropHeight')" hide-label max-width="6.5rem"
|
|
450
|
+
@update:model-value="setOutHeight"
|
|
451
|
+
class="mb-0"
|
|
452
|
+
/>
|
|
453
|
+
<span class="txt12 m_none opacity-6">px</span>
|
|
454
|
+
<span class="txt12 m_none opacity-5 ms-auto">{{ maxLabel }}</span>
|
|
455
|
+
</div>
|
|
456
|
+
</div>
|
|
457
|
+
|
|
458
|
+
<p class="m-0 txt12 opacity-6 flex gap-025">
|
|
459
|
+
<Icon icon="open_with" :size="1.25" />{{ $t('upload.cropHint') }}
|
|
460
|
+
</p>
|
|
461
|
+
</div>
|
|
462
|
+
</template>
|
|
463
|
+
|
|
464
|
+
<style scoped>
|
|
465
|
+
/* The stage is as tall/wide as the image itself: `fit` the image by aspect ratio,
|
|
466
|
+
capped by a share of the viewport height, never letterboxed. */
|
|
467
|
+
.crop-stage {
|
|
468
|
+
width: 100%;
|
|
469
|
+
max-width: calc(min(48vh, 460px) * var(--crop-ratio, 1));
|
|
470
|
+
aspect-ratio: var(--crop-ratio, 1);
|
|
471
|
+
margin-inline: auto;
|
|
472
|
+
cursor: move;
|
|
473
|
+
touch-action: none;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/* A thin outline marks the window; the corners carry the (thicker) grip. The dim is
|
|
477
|
+
a bounded spread — `100vmax` would ask for a shadow millions of pixels wide. */
|
|
478
|
+
.crop-window {
|
|
479
|
+
border: 1px solid color-mix(in oklab, var(--bgl-white) 60%, transparent);
|
|
480
|
+
box-shadow: 0 0 0 1000px color-mix(in oklab, var(--bgl-black) 55%, transparent);
|
|
481
|
+
cursor: move;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/* Corner brackets: two white strokes per corner, keeping the image visible between
|
|
485
|
+
them — a filled square would hide the very corner you are lining up. Bars with a
|
|
486
|
+
box-shadow instead of a `filter: drop-shadow` keep the drag cheap to repaint. */
|
|
487
|
+
.crop-handle {
|
|
488
|
+
position: absolute;
|
|
489
|
+
width: 22px;
|
|
490
|
+
height: 22px;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
.crop-handle::before,
|
|
494
|
+
.crop-handle::after {
|
|
495
|
+
content: '';
|
|
496
|
+
position: absolute;
|
|
497
|
+
background: var(--bgl-white);
|
|
498
|
+
box-shadow: 0 0 2px color-mix(in oklab, var(--bgl-black) 55%, transparent);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
.crop-handle::before { width: 100%; height: 4px; }
|
|
502
|
+
.crop-handle::after { width: 4px; height: 100%; }
|
|
503
|
+
|
|
504
|
+
.crop-handle--nw { left: -2px; top: -2px; cursor: nwse-resize; }
|
|
505
|
+
.crop-handle--nw::before { left: 0; top: 0; }
|
|
506
|
+
.crop-handle--nw::after { left: 0; top: 0; }
|
|
507
|
+
|
|
508
|
+
.crop-handle--ne { right: -2px; top: -2px; cursor: nesw-resize; }
|
|
509
|
+
.crop-handle--ne::before { right: 0; top: 0; }
|
|
510
|
+
.crop-handle--ne::after { right: 0; top: 0; }
|
|
511
|
+
|
|
512
|
+
.crop-handle--sw { left: -2px; bottom: -2px; cursor: nesw-resize; }
|
|
513
|
+
.crop-handle--sw::before { left: 0; bottom: 0; }
|
|
514
|
+
.crop-handle--sw::after { left: 0; bottom: 0; }
|
|
515
|
+
|
|
516
|
+
.crop-handle--se { right: -2px; bottom: -2px; cursor: nwse-resize; }
|
|
517
|
+
.crop-handle--se::before { right: 0; bottom: 0; }
|
|
518
|
+
.crop-handle--se::after { right: 0; bottom: 0; }
|
|
519
|
+
|
|
520
|
+
@media screen and (max-width: 910px) {
|
|
521
|
+
.crop-stage {
|
|
522
|
+
max-width: calc(min(36vh, 320px) * var(--crop-ratio, 1));
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
</style>
|
|
@@ -7,16 +7,27 @@ import { useFileUpload } from './useFileUpload'
|
|
|
7
7
|
import { useBagelInputShell } from '../bagelInputShell'
|
|
8
8
|
import AssetUploadInput from './AssetUploadInput.vue'
|
|
9
9
|
|
|
10
|
-
const props = withDefaults(defineProps<UploadInputProps>(), {
|
|
10
|
+
const props = withDefaults(defineProps<UploadInputProps & { previewState?: import('./upload.types').UploadPreviewState }>(), {
|
|
11
11
|
height: '215px',
|
|
12
12
|
theme: 'dropzone',
|
|
13
13
|
accept: '*',
|
|
14
14
|
showIcon: true,
|
|
15
15
|
icon: 'upload_2',
|
|
16
16
|
iconSize: 2.5,
|
|
17
|
+
iconWeight: 300,
|
|
17
18
|
uploadFrame: true,
|
|
18
19
|
readonly: false,
|
|
19
20
|
disabled: false,
|
|
21
|
+
// Declared here as well: Vue casts an absent boolean prop to `false`, so
|
|
22
|
+
// `v-bind="props"` would otherwise forward `false` to the asset uploader and
|
|
23
|
+
// defeat its own defaults (showButton / showSuccess / scroll / cropExisting).
|
|
24
|
+
showButton: 'auto',
|
|
25
|
+
showSuccess: true,
|
|
26
|
+
scroll: true,
|
|
27
|
+
cropExisting: true,
|
|
28
|
+
previewLayout: 'inside',
|
|
29
|
+
previewType: 'list',
|
|
30
|
+
cropOutputSize: 2048,
|
|
20
31
|
})
|
|
21
32
|
|
|
22
33
|
const emit = defineEmits(['update:modelValue', 'addFileStart', 'uploadError'])
|
|
@@ -124,7 +135,12 @@ function fileName(pathKey: string) {
|
|
|
124
135
|
|
|
125
136
|
<template>
|
|
126
137
|
<AssetUploadInput v-if="assetMode" v-bind="props"
|
|
127
|
-
@update:model-value="emit('update:modelValue', $event)" @add-file-start="emit('addFileStart')"
|
|
138
|
+
@update:model-value="emit('update:modelValue', $event)" @add-file-start="emit('addFileStart')"
|
|
139
|
+
@upload-error="(error, file) => emit('uploadError', error, file)">
|
|
140
|
+
<template v-for="(_, name) in slots" #[name]="slotProps">
|
|
141
|
+
<slot :name="name" v-bind="slotProps || {}" />
|
|
142
|
+
</template>
|
|
143
|
+
</AssetUploadInput>
|
|
128
144
|
<div
|
|
129
145
|
v-else
|
|
130
146
|
class="bagel-input" :class="[shellClass, { 'has-error': !!error, 'has-value': hasValue }]"
|
|
@@ -150,7 +166,6 @@ function fileName(pathKey: string) {
|
|
|
150
166
|
<template v-for="path_key in pathKeys" :key="path_key">
|
|
151
167
|
<div class="txt-gray txt-12 flex">
|
|
152
168
|
<div class="m-05 flex opacity-8 z-99 gap-025">
|
|
153
|
-
{{ readonly }}
|
|
154
169
|
<template v-if="!readonly && !disabled">
|
|
155
170
|
<Btn
|
|
156
171
|
v-tooltip="'Delete'" color="gray" thin icon="delete"
|
|
@@ -300,4 +300,112 @@
|
|
|
300
300
|
|
|
301
301
|
.bagel-input.has-error :deep(.bgl-card) {
|
|
302
302
|
border-color: var(--bgl-red);
|
|
303
|
-
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/* ─── Asset uploader (AssetUploadInput) — corrections on top of the rules above ── */
|
|
306
|
+
|
|
307
|
+
/* `uploadFrame: false` must actually drop the hairline, not just the border utility. */
|
|
308
|
+
.bgl_asset_upload .fileUploadWrap.bgl_no-frame {
|
|
309
|
+
outline: none;
|
|
310
|
+
background: transparent;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/* Oval frames are image-shaped: the preview covers the circle instead of letterboxing. */
|
|
314
|
+
.bgl_asset_upload .fileUploadWrap.bgl_oval-upload .single-image-item-preview,
|
|
315
|
+
.bgl_asset_upload .fileUploadWrap.bgl_oval-upload .single-image-item-preview>div:has(img) {
|
|
316
|
+
height: 100%;
|
|
317
|
+
width: 100%;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
.bgl_asset_upload .fileUploadWrap.bgl_oval-upload .single-preview {
|
|
321
|
+
height: 100% !important;
|
|
322
|
+
width: 100% !important;
|
|
323
|
+
max-width: unset;
|
|
324
|
+
max-height: unset;
|
|
325
|
+
margin: 0;
|
|
326
|
+
object-fit: cover;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/* Oval: the actions are one row under the circle, so the frame must not clip them.
|
|
330
|
+
The preview is rounded instead, which keeps the avatar circular. */
|
|
331
|
+
.bgl_asset_upload .fileUploadWrap.bgl_oval-upload {
|
|
332
|
+
overflow: visible;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
.bgl_asset_upload .fileUploadWrap.bgl_oval-upload .single-preview,
|
|
336
|
+
.bgl_asset_upload .fileUploadWrap.bgl_oval-upload .single-image-item-preview>div:has(img) {
|
|
337
|
+
border-radius: 100%;
|
|
338
|
+
overflow: hidden;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/* Sits on the circle's bottom edge, mostly outside it so the avatar stays visible. */
|
|
342
|
+
.bgl_asset_upload .bgl-oval-actions {
|
|
343
|
+
bottom: -0.25rem;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/* A handful of files in a circle: shrink the hint so the frame stays readable. */
|
|
347
|
+
.bgl_asset_upload .bgl_oval-hint {
|
|
348
|
+
padding: 0.25rem;
|
|
349
|
+
gap: 0.25rem;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/* Auto height defaults to `cover`; without it the image stays contained and centered. */
|
|
353
|
+
.bgl_asset_upload .fileUploadWrap[style*='height: auto'].bgl_fill-image .single-image-item-preview {
|
|
354
|
+
position: relative;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
.bgl_asset_upload .fileUploadWrap[style*='height: auto'].bgl_fill-image .single-preview {
|
|
358
|
+
position: absolute;
|
|
359
|
+
inset: 0;
|
|
360
|
+
height: 100%;
|
|
361
|
+
width: 100%;
|
|
362
|
+
max-width: unset;
|
|
363
|
+
max-height: unset;
|
|
364
|
+
margin: 0;
|
|
365
|
+
object-fit: cover;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/* A list of files keeps the frame's hover shadow across the whole area, but the rows
|
|
369
|
+
paint on top of it (`z-index: 1`) so the shadow reads as the frame behind them.
|
|
370
|
+
Only the `filter` is dropped: it would tint the rows themselves. */
|
|
371
|
+
.bgl_asset_upload .fileUploadWrap.bgl-list-split:hover,
|
|
372
|
+
.bgl_asset_upload .fileUploadWrap.bgl-list-split:active {
|
|
373
|
+
filter: none !important;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
.bgl_asset_upload .fileUploadWrap.bgl-list-split .bgl-file-row {
|
|
377
|
+
position: relative;
|
|
378
|
+
z-index: 1;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
.bgl_asset_upload .bgl-add-more {
|
|
382
|
+
border-radius: var(--bgl-input-border-radius);
|
|
383
|
+
padding: 0.25rem;
|
|
384
|
+
display: flex;
|
|
385
|
+
justify-content: center;
|
|
386
|
+
transition: var(--bgl-transition);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/* A file row reads at the input's own type size everywhere it appears. The drop frame
|
|
390
|
+
already sets this on `.fileUploadWrap`; the compact card and the rows-below list sit
|
|
391
|
+
outside it, so they'd inherit the page size (16px) and look oversized. */
|
|
392
|
+
.bgl_asset_upload .bgl-file-row {
|
|
393
|
+
font-size: var(--bgl-input-font-size);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/* Compact with several files: each file owns a line, so its actions line up under each other. */
|
|
397
|
+
.bgl_asset_upload .bgl-compact .bgl-file-row {
|
|
398
|
+
width: 100%;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/* ─── In-flight ring over a preview ──────────────────────────────────────── */
|
|
402
|
+
|
|
403
|
+
/* The ring's hole is see-through here (a solid disc would hide the photo), and its
|
|
404
|
+
track is a translucent white that reads on any image, light or dark. */
|
|
405
|
+
.bgl_asset_upload .bgl-upload-ring {
|
|
406
|
+
--bgl-progress-track: color-mix(in oklab, var(--bgl-white) 32%, transparent);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
.bgl_asset_upload :deep(.bgl-progress-ring-hole) {
|
|
410
|
+
background: transparent;
|
|
411
|
+
}
|