diff --git a/src/components/Draw/FileUpload.vue b/src/components/Draw/FileUpload.vue index 3863f2075e..4ae9ecd738 100644 --- a/src/components/Draw/FileUpload.vue +++ b/src/components/Draw/FileUpload.vue @@ -18,7 +18,7 @@ @change="fileSelect"> -
+
CropperResult } -type ResizeObserverLike = { - disconnect: () => void - observe: (target: Element) => void -} - defineOptions({ name: 'FileUpload', }) @@ -180,13 +176,10 @@ const confirmSignatureDialogTitle = t('libresign', 'Confirm your signature') const file = ref(null) const cropper = ref(null) -const cropperContainer = ref(null) -const resizeObserver = ref(null) const modal = ref(false) const image = ref('') const imageData = ref('') -const containerWidth = ref(0) const pendingFitCenter = ref(false) const zoomLevel = ref(1) const zoomMin = 0.1 @@ -219,37 +212,16 @@ const stencilProps = computed(() => ({ aspectRatio: stencilAspectRatio.value, })) +// vue-advanced-cropper reads an object `defaultSize` as image pixels, so the +// size is computed from the loaded image through the documented function form. const defaultStencilSize = computed(() => { - if (!containerWidth.value) { - return { - width: stencilBaseWidth, - height: stencilBaseHeight, - } - } - const availableWidth = Math.max(0, containerWidth.value - 24) - const scale = Math.min(1, availableWidth / stencilBaseWidth) - return { - width: Math.floor(stencilBaseWidth * scale), - height: Math.floor(stencilBaseHeight * scale), + const aspectRatio = stencilAspectRatio.value + if (!aspectRatio) { + return undefined } + return ({ imageSize }: { imageSize?: CropperImageSize }) => fitStencilToImage(imageSize, aspectRatio) }) -function initResizeObserver() { - if (!window.ResizeObserver || !cropperContainer.value) { - return - } - if (!resizeObserver.value) { - resizeObserver.value = new window.ResizeObserver(() => updateContainerWidth()) as ResizeObserverLike - } else { - resizeObserver.value.disconnect() - } - resizeObserver.value.observe(cropperContainer.value) -} - -function updateContainerWidth() { - containerWidth.value = cropperContainer.value?.offsetWidth || 0 -} - function updateZoomLevel(result?: CropperResult) { if (result) { updateZoomLevelFromResult(result) @@ -387,32 +359,10 @@ function close() { emit('close') } -onMounted(() => { - updateContainerWidth() - if (window.ResizeObserver) { - void nextTick(() => { - initResizeObserver() - }) - } else { - window.addEventListener('resize', updateContainerWidth) - } -}) - -onBeforeUnmount(() => { - resizeObserver.value?.disconnect() - window.removeEventListener('resize', updateContainerWidth) -}) - watch(hasImage, value => { if (value) { - void nextTick(() => { - updateContainerWidth() - initResizeObserver() - }) return } - resizeObserver.value?.disconnect() - containerWidth.value = 0 zoomLevel.value = 1 pendingFitCenter.value = false imageData.value = '' @@ -425,12 +375,9 @@ defineExpose({ mdiFitToPageOutline, file, cropper, - cropperContainer, - resizeObserver, modal, image, imageData, - containerWidth, pendingFitCenter, zoomLevel, zoomMin, @@ -444,8 +391,6 @@ defineExpose({ stencilAspectRatio, stencilProps, defaultStencilSize, - initResizeObserver, - updateContainerWidth, updateZoomLevel, updateZoomLevelFromResult, zoomBy, diff --git a/src/helpers/cropperDefaultSize.ts b/src/helpers/cropperDefaultSize.ts new file mode 100644 index 0000000000..d7eb92e2a9 --- /dev/null +++ b/src/helpers/cropperDefaultSize.ts @@ -0,0 +1,31 @@ +/** + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +export type CropperImageSize = { + width: number + height: number +} + +/** + * Largest stencil that fits inside the image while keeping the given aspect + * ratio (width / height). Input and result are in image pixels, which is the + * unit vue-advanced-cropper expects from a `defaultSize` function. + */ +export function fitStencilToImage( + imageSize: CropperImageSize | undefined, + aspectRatio: number | undefined, +): CropperImageSize | undefined { + if (!imageSize || !aspectRatio || !Number.isFinite(aspectRatio) || aspectRatio <= 0) { + return undefined + } + const { width, height } = imageSize + if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) { + return undefined + } + if (width / height <= aspectRatio) { + return { width, height: width / aspectRatio } + } + return { width: height * aspectRatio, height } +} diff --git a/src/tests/components/Draw/FileUpload.spec.ts b/src/tests/components/Draw/FileUpload.spec.ts index de01f9d014..27361c00a3 100644 --- a/src/tests/components/Draw/FileUpload.spec.ts +++ b/src/tests/components/Draw/FileUpload.spec.ts @@ -75,10 +75,6 @@ vi.mock('vue-advanced-cropper', () => ({ describe('FileUpload.vue - Uploaded signature flow', () => { beforeEach(() => { vi.clearAllMocks() - vi.stubGlobal('ResizeObserver', class { - observe = vi.fn() - disconnect = vi.fn() - }) }) function mountComponent() { @@ -90,16 +86,14 @@ describe('FileUpload.vue - Uploaded signature flow', () => { expect(wrapper.vm.stencilBaseWidth).toBe(700) expect(wrapper.vm.stencilBaseHeight).toBe(200) - expect(wrapper.vm.defaultStencilSize).toEqual({ width: 700, height: 200 }) }) - it('scales the default stencil size to fit the cropper container', async () => { + it('computes the default stencil size in image pixels', () => { const wrapper = mountComponent() + const defaultSize = wrapper.vm.defaultStencilSize as (state: { imageSize: { width: number, height: number } }) => unknown - wrapper.vm.containerWidth = 374 - await wrapper.vm.$nextTick() - - expect(wrapper.vm.defaultStencilSize).toEqual({ width: 350, height: 100 }) + expect(defaultSize({ imageSize: { width: 3000, height: 1200 } })).toEqual({ width: 3000, height: 3000 / 3.5 }) + expect(defaultSize({ imageSize: { width: 700, height: 1000 } })).toEqual({ width: 700, height: 200 }) }) it('loads the selected file as a data URL', () => { @@ -233,11 +227,8 @@ describe('FileUpload.vue - Uploaded signature flow', () => { }) it('resets crop state when the image is cleared', async () => { - const disconnect = vi.fn() const wrapper = mountComponent() - wrapper.vm.resizeObserver = { observe: vi.fn(), disconnect } - wrapper.vm.containerWidth = 480 wrapper.vm.zoomLevel = 2.4 wrapper.vm.pendingFitCenter = true wrapper.vm.image = 'data:image/png;base64,existing' @@ -246,8 +237,6 @@ describe('FileUpload.vue - Uploaded signature flow', () => { wrapper.vm.image = '' await wrapper.vm.$nextTick() - expect(disconnect).toHaveBeenCalled() - expect(wrapper.vm.containerWidth).toBe(0) expect(wrapper.vm.zoomLevel).toBe(1) expect(wrapper.vm.pendingFitCenter).toBe(false) }) diff --git a/src/tests/helpers/cropperDefaultSize.spec.ts b/src/tests/helpers/cropperDefaultSize.spec.ts new file mode 100644 index 0000000000..fd70a05937 --- /dev/null +++ b/src/tests/helpers/cropperDefaultSize.spec.ts @@ -0,0 +1,39 @@ +/** + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { describe, expect, it } from 'vitest' +import { fitStencilToImage } from '../../helpers/cropperDefaultSize' + +describe('fitStencilToImage', () => { + it('fits the width when the image is narrower than the aspect ratio', () => { + expect(fitStencilToImage({ width: 700, height: 1000 }, 3.5)).toEqual({ width: 700, height: 200 }) + }) + + it('fits the height when the image is wider than the aspect ratio', () => { + expect(fitStencilToImage({ width: 3000, height: 400 }, 3.5)).toEqual({ width: 1400, height: 400 }) + }) + + it('covers the image instead of using a screen-pixel size on large photos', () => { + expect(fitStencilToImage({ width: 3000, height: 1200 }, 3.5)).toEqual({ width: 3000, height: 3000 / 3.5 }) + }) + + it('keeps the aspect ratio', () => { + const size = fitStencilToImage({ width: 4032, height: 3024 }, 3.5) + expect(size).toBeDefined() + expect(size!.width / size!.height).toBeCloseTo(3.5) + }) + + it('returns undefined without a usable aspect ratio', () => { + expect(fitStencilToImage({ width: 3000, height: 1200 }, undefined)).toBeUndefined() + expect(fitStencilToImage({ width: 3000, height: 1200 }, 0)).toBeUndefined() + expect(fitStencilToImage({ width: 3000, height: 1200 }, Number.NaN)).toBeUndefined() + }) + + it('returns undefined for an invalid image size', () => { + expect(fitStencilToImage(undefined, 3.5)).toBeUndefined() + expect(fitStencilToImage({ width: 0, height: 100 }, 3.5)).toBeUndefined() + expect(fitStencilToImage({ width: Number.NaN, height: 100 }, 3.5)).toBeUndefined() + }) +})