Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 9 additions & 64 deletions src/components/Draw/FileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@change="fileSelect">
</div>

<div v-if="hasImage" ref="cropperContainer" class="cropper-container">
<div v-if="hasImage" class="cropper-container">
<Cropper ref="cropper"
:src="image"
:default-size="defaultStencilSize"
Expand Down Expand Up @@ -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'

Expand All @@ -122,6 +122,7 @@ import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper'
import 'vue-advanced-cropper/dist/style.css'
import type { LibresignCapabilities } from '../../types/index'
import logger from '../../logger.js'
import { fitStencilToImage, type CropperImageSize } from '../../helpers/cropperDefaultSize'

type CropperResult = {
canvas?: {
Expand All @@ -145,11 +146,6 @@ type CropperInstance = {
getResult?: () => CropperResult
}

type ResizeObserverLike = {
disconnect: () => void
observe: (target: Element) => void
}

defineOptions({
name: 'FileUpload',
})
Expand Down Expand Up @@ -180,13 +176,10 @@ const confirmSignatureDialogTitle = t('libresign', 'Confirm your signature')

const file = ref<HTMLInputElement | null>(null)
const cropper = ref<CropperInstance | null>(null)
const cropperContainer = ref<HTMLElement | null>(null)
const resizeObserver = ref<ResizeObserverLike | null>(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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = ''
Expand All @@ -425,12 +375,9 @@ defineExpose({
mdiFitToPageOutline,
file,
cropper,
cropperContainer,
resizeObserver,
modal,
image,
imageData,
containerWidth,
pendingFitCenter,
zoomLevel,
zoomMin,
Expand All @@ -444,8 +391,6 @@ defineExpose({
stencilAspectRatio,
stencilProps,
defaultStencilSize,
initResizeObserver,
updateContainerWidth,
updateZoomLevel,
updateZoomLevelFromResult,
zoomBy,
Expand Down
31 changes: 31 additions & 0 deletions src/helpers/cropperDefaultSize.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
19 changes: 4 additions & 15 deletions src/tests/components/Draw/FileUpload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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'
Expand All @@ -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)
})
Expand Down
39 changes: 39 additions & 0 deletions src/tests/helpers/cropperDefaultSize.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})