Skip to content

Commit

Permalink
feat: single download support for each crop type
Browse files Browse the repository at this point in the history
added download button on each crop panel
  • Loading branch information
selimdoyranli committed Jun 27, 2023
1 parent a86666b commit 2698f2d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
31 changes: 28 additions & 3 deletions components/Panel/CropPanel/CropPanel.component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@
template(v-if="panel.cropper")
span.crop-panel-footer__label {{ $t('editor.original') }}: {{ panel.cropper.image.width }}x{{ panel.cropper.image.height }}
span.crop-panel-footer__label {{ $t('editor.cropped') }}: {{ panel.cropper.coordinates.width }}x{{ panel.cropper.coordinates.height }}
vs-tooltip(bottom dark not-arrow)
vs-button(flat active dark size="mini" :loading="panel.isBusy" :disabled="panel.isBusy" @click="download({ type })")
AppIcon(name="charm:download")
template(#tooltip)
span {{ $t('general.download') }} ({{ $t(`cropType.${type}.title`) }})
</template>

<script>
import { defineComponent, useStore, ref, reactive, computed, onMounted, onUnmounted } from '@nuxtjs/composition-api'
import { defineComponent, useStore, ref, reactive, computed, onMounted } from '@nuxtjs/composition-api'
import { cropTypeEnum } from '@/enums'
import useEditor from '@/hooks/useEditor'
import { Cropper } from 'vue-advanced-cropper'
Expand Down Expand Up @@ -81,15 +86,18 @@ export default defineComponent({
}
},
setup(props) {
const FileSaver = require('file-saver')
const store = useStore()
const { sleep } = useEditor()
const { sleep, getFileExtension } = useEditor()
const cropperRef = ref(null)
const isReady = computed(() => store.getters['editor/isReady'])
const original = computed(() => store.getters['editor/original'])
const panel = reactive({
isBusy: false,
cropper: null
})
Expand Down Expand Up @@ -136,13 +144,15 @@ export default defineComponent({
const handleOnChangeCropper = ({ coordinates, image, visibleArea, canvas }) => {
panel.cropper = { coordinates, image, visibleArea, canvas }
panel.isBusy = true
store.commit('editor/SET_IS_BUSY', true)
canvas.toBlob(blob => {
store.commit('editor/SET_CROPPED', { type: props.type, coordinates, file: blob })
}, original.value.file.type)
sleep(1000).then(() => {
panel.isBusy = false
store.commit('editor/SET_IS_BUSY', false)
})
}
Expand All @@ -151,6 +161,20 @@ export default defineComponent({
cropperRef.value.zoom(value)
}
const cropped = computed(() => store.getters['editor/cropped'])
const download = ({ type }) => {
store.commit('editor/SET_IS_BUSY', true)
panel.isBusy = true
FileSaver.saveAs(cropped.value[type].file, `image-${cropped.value[type].key}.${getFileExtension(original.value.file.name)}`)
sleep(1000).then(() => {
store.commit('editor/SET_IS_BUSY', false)
panel.isBusy = false
})
}
onMounted(() => {
window.addEventListener('resize', () => {
if (isReady.value) {
Expand All @@ -167,7 +191,8 @@ export default defineComponent({
handleOnSelectFreeFormRatio,
getFreeStencilProps,
handleOnReadyCropper,
handleOnChangeCropper
handleOnChangeCropper,
download
}
}
})
Expand Down
17 changes: 17 additions & 0 deletions locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ export default {
cropped: 'Cropped',
freeform: 'Freeform'
},
cropType: {
original: {
title: 'Original'
},
horizontal: {
title: 'Horizontal'
},
vertical: {
title: 'Vertical'
},
square: {
title: 'Square'
},
free: {
title: 'Free ratio'
}
},
credits: {
description: 'Useful for blog, photo blog, news websites and social media posts.'
}
Expand Down
17 changes: 17 additions & 0 deletions locales/tr.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ export default {
cropped: 'Kırpılmış',
freeform: 'Serbest mod'
},
cropType: {
original: {
title: 'Orijinal'
},
horizontal: {
title: 'Yatay'
},
vertical: {
title: 'Dikey'
},
square: {
title: 'Kare'
},
free: {
title: 'Serbest mod'
}
},
credits: {
description: 'Blog, foto blog, haber siteleri ve sosyal medya postları için kullanışlıdır.'
}
Expand Down
4 changes: 4 additions & 0 deletions store/editor/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default {
return state.original
},

cropped(state) {
return state.cropped
},

horizontalCropped(state) {
return state.cropped.horizontal
},
Expand Down

0 comments on commit 2698f2d

Please sign in to comment.