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
92 changes: 18 additions & 74 deletions src/components/Draw/FileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
@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"
:stencil-props="stencilProps"
:canvas="canvasConstraints"
image-restriction="none"
@change="change" />
<div class="zoom-controls">
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 Down Expand Up @@ -145,11 +145,6 @@ type CropperInstance = {
getResult?: () => CropperResult
}

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

defineOptions({
name: 'FileUpload',
})
Expand Down Expand Up @@ -180,13 +175,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,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) {
Expand Down Expand Up @@ -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({
Expand All @@ -425,12 +375,9 @@ defineExpose({
mdiFitToPageOutline,
file,
cropper,
cropperContainer,
resizeObserver,
modal,
image,
imageData,
containerWidth,
pendingFitCenter,
zoomLevel,
zoomMin,
Expand All @@ -443,9 +390,6 @@ defineExpose({
zoomPercentValue,
stencilAspectRatio,
stencilProps,
defaultStencilSize,
initResizeObserver,
updateContainerWidth,
updateZoomLevel,
updateZoomLevelFromResult,
zoomBy,
Expand Down
43 changes: 28 additions & 15 deletions src/tests/components/Draw/FileUpload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ vi.mock('vue-advanced-cropper', () => ({
Cropper: {
name: 'Cropper',
template: '<div class="cropper-stub" />',
props: ['src', 'defaultSize', 'stencilProps', 'imageRestriction'],
props: ['src', 'defaultSize', 'stencilProps', 'imageRestriction', 'canvas'],
emits: ['change'],
methods: {
zoom: vi.fn(),
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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('')
})
})
Loading