2022-12-26 08:50:11 +00:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import type { Boundaries } from 'vue-advanced-cropper'
|
|
|
|
import { Cropper } from 'vue-advanced-cropper'
|
|
|
|
import 'vue-advanced-cropper/dist/style.css'
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
/** Crop frame aspect ratio (width/height), default 1/1 */
|
|
|
|
stencilAspectRatio?: number
|
|
|
|
/** The ratio of the longest edge of the cut box to the length of the cut screen, default 0.9, not more than 1 */
|
|
|
|
stencilSizePercentage?: number
|
|
|
|
}
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
stencilAspectRatio: 1 / 1,
|
|
|
|
stencilSizePercentage: 0.9,
|
|
|
|
})
|
|
|
|
|
2023-08-01 10:43:55 +01:00
|
|
|
const file = defineModel<File | null>()
|
2022-12-26 08:50:11 +00:00
|
|
|
|
|
|
|
const cropperDialog = ref(false)
|
|
|
|
|
|
|
|
const cropper = ref<InstanceType<typeof Cropper>>()
|
|
|
|
|
|
|
|
const cropperFlag = ref(false)
|
|
|
|
|
|
|
|
const cropperImage = reactive({
|
|
|
|
src: '',
|
|
|
|
type: 'image/jpg',
|
|
|
|
})
|
|
|
|
|
2023-03-30 20:01:24 +01:00
|
|
|
function stencilSize({ boundaries }: { boundaries: Boundaries }) {
|
2022-12-26 08:50:11 +00:00
|
|
|
return {
|
|
|
|
width: boundaries.width * props.stencilSizePercentage,
|
|
|
|
height: boundaries.height * props.stencilSizePercentage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 15:46:36 +00:00
|
|
|
watch(file, (file, _, onCleanup) => {
|
2022-12-26 08:50:11 +00:00
|
|
|
let expired = false
|
|
|
|
onCleanup(() => expired = true)
|
|
|
|
|
|
|
|
if (file && !cropperFlag.value) {
|
|
|
|
cropperDialog.value = true
|
|
|
|
const reader = new FileReader()
|
|
|
|
reader.readAsDataURL(file)
|
|
|
|
reader.onload = (e) => {
|
|
|
|
if (expired)
|
|
|
|
return
|
|
|
|
cropperImage.src = e.target?.result as string
|
|
|
|
cropperImage.type = file.type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cropperFlag.value = false
|
|
|
|
})
|
|
|
|
|
2023-03-30 20:01:24 +01:00
|
|
|
function cropImage() {
|
2023-01-06 15:46:36 +00:00
|
|
|
if (cropper.value && file.value) {
|
2022-12-26 08:50:11 +00:00
|
|
|
cropperFlag.value = true
|
|
|
|
cropperDialog.value = false
|
|
|
|
const { canvas } = cropper.value.getResult()
|
|
|
|
canvas?.toBlob((blob) => {
|
2023-01-06 15:46:36 +00:00
|
|
|
file.value = new File([blob as any], `cropped${file.value?.name}` as string, { type: blob?.type })
|
2022-12-26 08:50:11 +00:00
|
|
|
}, cropperImage.type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<ModalDialog v-model="cropperDialog" :use-v-if="false" max-w-500px flex>
|
|
|
|
<div flex-1 w-0>
|
|
|
|
<div text-lg text-center my2 px3>
|
|
|
|
<h1>
|
|
|
|
{{ $t('action.edit') }}
|
|
|
|
</h1>
|
|
|
|
</div>
|
|
|
|
<div aspect-ratio-1>
|
|
|
|
<Cropper
|
|
|
|
ref="cropper"
|
|
|
|
class="overflow-hidden w-full h-full"
|
|
|
|
:src="cropperImage.src"
|
|
|
|
:resize-image="{
|
|
|
|
adjustStencil: false,
|
|
|
|
}"
|
|
|
|
:stencil-size="stencilSize"
|
|
|
|
:stencil-props="{
|
|
|
|
aspectRatio: props.stencilAspectRatio,
|
|
|
|
movable: false,
|
|
|
|
resizable: false,
|
|
|
|
handlers: {},
|
|
|
|
}"
|
|
|
|
image-restriction="stencil"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div m-4>
|
|
|
|
<button
|
|
|
|
btn-solid w-full rounded text-sm
|
|
|
|
@click="cropImage()"
|
|
|
|
>
|
|
|
|
{{ $t('action.confirm') }}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ModalDialog>
|
|
|
|
</template>
|