diff --git a/src/components/Draw/FileUpload.vue b/src/components/Draw/FileUpload.vue index 3863f2075e..7b5da31b68 100644 --- a/src/components/Draw/FileUpload.vue +++ b/src/components/Draw/FileUpload.vue @@ -18,11 +18,11 @@ @change="fileSelect"> -
+
@@ -110,7 +110,7 @@ import { mdiMagnifyMinusOutline, mdiMagnifyPlusOutline, } from '@mdi/js' -import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue' +import { computed, nextTick, ref, watch } from 'vue' import { Cropper } from 'vue-advanced-cropper' import { getCapabilities } from '@nextcloud/capabilities' @@ -145,11 +145,6 @@ type CropperInstance = { getResult?: () => CropperResult } -type ResizeObserverLike = { - disconnect: () => void - observe: (target: Element) => void -} - defineOptions({ name: 'FileUpload', }) @@ -180,13 +175,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,36 +211,17 @@ const stencilProps = computed(() => ({ aspectRatio: stencilAspectRatio.value, })) -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), - } -}) - -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 -} +// Keep the source resolution where possible, but bound the exported PNG. +// A one-megapixel RGBA canvas stays under the backend's 5000 KiB image limit. +const canvasConstraints = computed(() => ({ + maxWidth: Number.isFinite(stencilBaseWidth) && stencilBaseWidth > 0 + ? stencilBaseWidth * 3 + : undefined, + maxHeight: Number.isFinite(stencilBaseHeight) && stencilBaseHeight > 0 + ? stencilBaseHeight * 3 + : undefined, + maxArea: 1_000_000, +})) function updateZoomLevel(result?: CropperResult) { if (result) { @@ -387,35 +360,12 @@ 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 + if (!value) { + zoomLevel.value = 1 + pendingFitCenter.value = false + imageData.value = '' } - resizeObserver.value?.disconnect() - containerWidth.value = 0 - zoomLevel.value = 1 - pendingFitCenter.value = false - imageData.value = '' }) defineExpose({ @@ -425,12 +375,9 @@ defineExpose({ mdiFitToPageOutline, file, cropper, - cropperContainer, - resizeObserver, modal, image, imageData, - containerWidth, pendingFitCenter, zoomLevel, zoomMin, @@ -443,9 +390,6 @@ defineExpose({ zoomPercentValue, stencilAspectRatio, stencilProps, - defaultStencilSize, - initResizeObserver, - updateContainerWidth, updateZoomLevel, updateZoomLevelFromResult, zoomBy, diff --git a/src/tests/components/Draw/FileUpload.spec.ts b/src/tests/components/Draw/FileUpload.spec.ts index de01f9d014..cb8e6fbb67 100644 --- a/src/tests/components/Draw/FileUpload.spec.ts +++ b/src/tests/components/Draw/FileUpload.spec.ts @@ -59,7 +59,7 @@ vi.mock('vue-advanced-cropper', () => ({ Cropper: { name: 'Cropper', template: '
', - props: ['src', 'defaultSize', 'stencilProps', 'imageRestriction'], + props: ['src', 'defaultSize', 'stencilProps', 'imageRestriction', 'canvas'], emits: ['change'], methods: { zoom: vi.fn(), @@ -75,31 +75,47 @@ 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() { return mount(FileUpload) } - it('initializes stencil dimensions from capabilities', () => { + it('keeps the configured signature aspect ratio', () => { const wrapper = mountComponent() expect(wrapper.vm.stencilBaseWidth).toBe(700) expect(wrapper.vm.stencilBaseHeight).toBe(200) - expect(wrapper.vm.defaultStencilSize).toEqual({ width: 700, height: 200 }) + expect(wrapper.vm.stencilAspectRatio).toBe(3.5) }) - it('scales the default stencil size to fit the cropper container', async () => { + it('delegates initial crop selection size to the cropper', async () => { const wrapper = mountComponent() - wrapper.vm.containerWidth = 374 + wrapper.vm.image = 'data:image/png;base64,uploaded' + await wrapper.vm.$nextTick() + + const cropper = wrapper.findComponent({ name: 'Cropper' }) + expect(cropper.exists()).toBe(true) + expect(cropper.props('defaultSize')).toBeUndefined() + expect(cropper.props('stencilProps')).toEqual({ aspectRatio: 3.5 }) + }) + + it('limits exported PNG dimensions while retaining source resolution for small images', async () => { + const wrapper = mountComponent() + wrapper.vm.image = 'data:image/png;base64,uploaded' await wrapper.vm.$nextTick() - expect(wrapper.vm.defaultStencilSize).toEqual({ width: 350, height: 100 }) + const cropper = wrapper.findComponent({ name: 'Cropper' }) + const canvas = cropper.props('canvas') + + expect(canvas).toEqual({ + maxWidth: 2100, + maxHeight: 600, + maxArea: 1_000_000, + }) + expect(canvas).not.toHaveProperty('width') + expect(canvas).not.toHaveProperty('height') }) it('loads the selected file as a data URL', () => { @@ -233,22 +249,19 @@ 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' + wrapper.vm.imageData = 'data:image/png;base64,old-crop' await wrapper.vm.$nextTick() 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) + expect(wrapper.vm.imageData).toBe('') }) })