scinote-web/app/javascript/vue/shared/content/attachments/clipboard_paste_modal.vue

135 lines
4.5 KiB
Vue
Raw Normal View History

<template>
<div ref="modal" @keydown.esc="cancel"
@keyup.enter="uploadImage"
class="modal clipboardPreviewModal fade"
role="dialog" aria-hidden="true" tabindex="-1">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
2023-08-17 19:55:17 +08:00
<i class="sn-icon sn-icon-close"></i>
</button>
<h4 class="modal-title">{{i18n.t('assets.from_clipboard.modal_title')}}</h4>
</div>
<div class="modal-body">
2023-10-26 18:01:48 +08:00
<label class="sci-label">{{i18n.t('assets.from_clipboard.image_preview')}}</label>
2023-10-26 19:24:29 +08:00
<div class="flex justify-center w-full">
<canvas class="max-h-80 max-w-lg rounded border border-solid border-sn-light-grey" ref="preview" />
</div>
2023-10-26 18:01:48 +08:00
<div class="w-full py-6">
<label class="sci-label">{{i18n.t(`assets.from_clipboard.select_${objectType}`)}}</label>
2023-11-29 18:36:44 +08:00
<SelectDropdown
2023-10-26 18:01:48 +08:00
:value="target"
@change="setTarget"
:options="targets"
2023-11-29 18:36:44 +08:00
:searchable="true"
2023-10-26 18:01:48 +08:00
:placeholder="
i18n.t(`protocols.steps.modals.move_element.${objectType}.search_placeholder`)
"
/>
</div>
<label class="sci-label">{{i18n.t('assets.from_clipboard.file_name')}}</label>
<div class="sci-input-container-v2">
<input id="clipboardImageName" type="text" class="sci-input-field !pr-16" v-model="fileName"
:placeholder="i18n.t('assets.from_clipboard.file_name_placeholder')" aria-describedby="image-name">
2023-10-26 18:01:48 +08:00
<span class="absolute right-2.5 text-sn-grey ">
.{{ extension }}
</span>
</div>
</div>
<div class="modal-footer">
2023-06-19 21:45:22 +08:00
<button type="button" class="btn btn-secondary" @click="cancel">{{i18n.t('general.cancel')}}</button>
2023-10-26 18:01:48 +08:00
<button type="button" class="btn btn-success" :disabled="!valid" @click="uploadImage">{{i18n.t('assets.from_clipboard.add_image')}}</button>
</div>
</div>
</div>
</div>
</template>
<script>
2023-11-29 18:36:44 +08:00
import SelectDropdown from "../../select_dropdown.vue";
2023-10-26 18:01:48 +08:00
export default {
name: 'clipboardPasteModal',
props: {
objects: Array,
image: DataTransferItem,
objectType: String,
selectedObjectId: String
},
data() {
return {
target: null,
targets: [],
fileName: '',
extension: ''
};
},
components: {
SelectDropdown
},
computed: {
valid() {
return this.target && this.fileName.length > 0;
}
},
mounted() {
$(this.$refs.modal).modal('show');
this.appendImage(this.image);
$(this.$refs.modal).on('hidden.bs.modal', () => {
this.$emit('cancel');
});
2023-10-26 18:01:48 +08:00
if (this.selectedObjectId) this.target = this.selectedObjectId;
this.targets = this.objects.map((object) => [
object.id,
object.attributes.name
]);
},
methods: {
setTarget(target) {
this.target = target;
2023-10-26 18:01:48 +08:00
},
cancel() {
$(this.$refs.modal).modal('hide');
},
appendImage(item) {
const imageBlob = item.getAsFile();
if (imageBlob) {
const canvas = this.$refs.preview;
const ctx = canvas.getContext('2d');
const img = new Image();
img.onload = function () {
canvas.width = this.width;
canvas.height = this.height;
ctx.drawImage(img, 0, 0);
};
const URLObj = window.URL || window.webkitURL;
img.src = URLObj.createObjectURL(imageBlob);
const extension = imageBlob.name.slice(
(Math.max(0, imageBlob.name.lastIndexOf('.')) || Infinity) + 1
);
this.extension = extension;
this.imageBlob = imageBlob;
}
},
uploadImage() {
const newName = this.fileName;
const imageBlog = this.imageBlob;
// check if the name is set
if (newName && newName.length > 0) {
const extension = imageBlog.name.slice(
(Math.max(0, imageBlog.name.lastIndexOf('.')) || Infinity) + 1
);
// hack to inject custom name in File object
const name = newName + '.' + extension;
const blob = imageBlog.slice(0, imageBlog.size, imageBlog.type);
// make new blob with the correct name;
this.imageBlob = new File([blob], name, { type: imageBlog.type });
}
$(this.$refs.modal).modal('hide');
this.$emit('files', this.imageBlob, this.target);
}
}
};
</script>