2022-07-06 17:43:30 +08:00
|
|
|
<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>
|
2022-07-06 17:43:30 +08:00
|
|
|
</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>
|
|
|
|
<canvas class="w-full max-h-80 rounded border border-solid border-sn-light-grey" ref="preview" />
|
|
|
|
<div class="w-full py-6">
|
|
|
|
<label class="sci-label">{{i18n.t('assets.from_clipboard.select_target')}}</label>
|
|
|
|
<SelectSearch
|
|
|
|
:value="target"
|
|
|
|
@change="setTarget"
|
|
|
|
:options="targets"
|
|
|
|
:isLoading="false"
|
|
|
|
:placeholder="
|
|
|
|
i18n.t(`protocols.steps.modals.move_element.${objectType}.search_placeholder`)
|
|
|
|
"
|
|
|
|
:searchPlaceholder="
|
|
|
|
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"
|
2022-07-15 19:46:48 +08:00
|
|
|
: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>
|
2022-07-06 17:43:30 +08:00
|
|
|
</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>
|
2022-07-06 17:43:30 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
2023-10-26 18:01:48 +08:00
|
|
|
import SelectSearch from "../../select_search.vue";
|
|
|
|
|
2022-07-06 17:43:30 +08:00
|
|
|
export default {
|
|
|
|
name: 'clipboardPasteModal',
|
|
|
|
props: {
|
2023-10-26 18:01:48 +08:00
|
|
|
objects: Array,
|
|
|
|
image: DataTransferItem,
|
|
|
|
objectType: String,
|
|
|
|
selectedObjectId: String
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
target: null,
|
|
|
|
targets: [],
|
|
|
|
fileName: '',
|
|
|
|
extension: '',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
SelectSearch
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
valid() {
|
|
|
|
return this.target && this.fileName.length > 0;
|
|
|
|
}
|
2022-07-06 17:43:30 +08:00
|
|
|
},
|
|
|
|
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) => {
|
|
|
|
return [
|
|
|
|
object.id,
|
|
|
|
object.attributes.name
|
|
|
|
]
|
|
|
|
});
|
2022-07-06 17:43:30 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2023-10-26 18:01:48 +08:00
|
|
|
setTarget(target) {
|
|
|
|
this.target = target;
|
|
|
|
},
|
2022-07-06 17:43:30 +08:00
|
|
|
cancel() {
|
|
|
|
$(this.$refs.modal).modal('hide');
|
|
|
|
},
|
|
|
|
appendImage(item) {
|
|
|
|
let imageBlob = item.getAsFile();
|
|
|
|
if (imageBlob) {
|
2023-10-26 18:01:48 +08:00
|
|
|
var canvas = this.$refs.preview;
|
2022-07-06 17:43:30 +08:00
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
var img = new Image();
|
|
|
|
img.onload = function() {
|
|
|
|
canvas.width = this.width;
|
|
|
|
canvas.height = this.height;
|
|
|
|
ctx.drawImage(img, 0, 0);
|
|
|
|
};
|
|
|
|
let URLObj = window.URL || window.webkitURL;
|
|
|
|
img.src = URLObj.createObjectURL(imageBlob);
|
|
|
|
let extension = imageBlob.name.slice(
|
|
|
|
(Math.max(0, imageBlob.name.lastIndexOf('.')) || Infinity) + 1
|
|
|
|
);
|
2023-10-26 18:01:48 +08:00
|
|
|
this.extension = extension;
|
2022-07-06 17:43:30 +08:00
|
|
|
this.imageBlob = imageBlob;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
uploadImage() {
|
2023-10-26 18:01:48 +08:00
|
|
|
let newName = this.fileName;
|
2022-07-06 17:43:30 +08:00
|
|
|
let imageBlog = this.imageBlob;
|
|
|
|
// check if the name is set
|
|
|
|
if (newName && newName.length > 0) {
|
|
|
|
let extension = imageBlog.name.slice(
|
|
|
|
(Math.max(0, imageBlog.name.lastIndexOf('.')) || Infinity) + 1
|
|
|
|
);
|
|
|
|
// hack to inject custom name in File object
|
|
|
|
let name = newName + '.' + extension;
|
|
|
|
let 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');
|
2023-10-26 18:01:48 +08:00
|
|
|
this.$emit('files', this.imageBlob, this.target);
|
2022-07-06 17:43:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|