2020-05-24 20:52:33 +00:00
|
|
|
import {
|
2020-12-30 01:32:46 +00:00
|
|
|
defineComponent,
|
2020-05-24 20:52:33 +00:00
|
|
|
reactive,
|
|
|
|
ref,
|
|
|
|
Ref,
|
|
|
|
watch,
|
|
|
|
PropType,
|
2020-12-27 21:32:43 +00:00
|
|
|
} from 'vue';
|
2020-05-24 20:52:33 +00:00
|
|
|
import {
|
|
|
|
model,
|
|
|
|
blockProps,
|
2020-12-30 13:34:23 +00:00
|
|
|
SbToolbar,
|
|
|
|
SbButton,
|
|
|
|
SbBlock
|
|
|
|
} from '@schlechtenburg/core';
|
2020-05-25 18:07:34 +00:00
|
|
|
|
2020-05-24 20:52:33 +00:00
|
|
|
import {
|
|
|
|
getDefaultData,
|
|
|
|
ImageData,
|
|
|
|
ImageProps,
|
|
|
|
} from './util';
|
|
|
|
|
|
|
|
import './style.scss';
|
|
|
|
|
2020-12-30 01:32:46 +00:00
|
|
|
export default defineComponent({
|
2020-05-24 20:52:33 +00:00
|
|
|
name: 'sb-image-edit',
|
|
|
|
|
|
|
|
model,
|
|
|
|
|
|
|
|
props: {
|
|
|
|
...blockProps,
|
2020-05-27 15:32:35 +00:00
|
|
|
eventUpdate: { type: Function, default: () => {} },
|
2020-05-24 20:52:33 +00:00
|
|
|
data: {
|
|
|
|
type: (null as unknown) as PropType<ImageData>,
|
|
|
|
default: getDefaultData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-05-27 15:32:35 +00:00
|
|
|
setup(props: ImageProps) {
|
2020-05-24 20:52:33 +00:00
|
|
|
const localData = reactive({
|
|
|
|
src: props.data.src,
|
|
|
|
alt: props.data.alt,
|
2020-12-30 01:32:46 +00:00
|
|
|
description: props.data.description,
|
2020-05-24 20:52:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const fileInput: Ref<null|HTMLInputElement> = ref(null);
|
|
|
|
|
|
|
|
watch(() => props.data, () => {
|
|
|
|
localData.src = props.data.src;
|
|
|
|
localData.alt = props.data.alt;
|
2020-12-30 01:32:46 +00:00
|
|
|
localData.description = props.data.description;
|
2020-05-24 20:52:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const selectImage = () => {
|
|
|
|
if (fileInput.value) {
|
|
|
|
fileInput.value.click();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onImageSelect = () => {
|
|
|
|
if (fileInput.value && fileInput.value.files && fileInput.value.files.length) {
|
2020-05-27 15:32:35 +00:00
|
|
|
const reader = new FileReader();
|
|
|
|
reader.addEventListener('load', () => {
|
|
|
|
props.eventUpdate({
|
|
|
|
src: reader.result,
|
|
|
|
alt: props.data.alt,
|
2020-12-30 01:32:46 +00:00
|
|
|
description: props.data.description,
|
2020-05-27 15:32:35 +00:00
|
|
|
});
|
2020-05-27 13:57:57 +00:00
|
|
|
});
|
2020-05-27 15:32:35 +00:00
|
|
|
|
|
|
|
reader.readAsDataURL(fileInput.value.files[0]);
|
2020-05-24 20:52:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-30 01:32:46 +00:00
|
|
|
const onDescriptionUpdate = (description) => {
|
|
|
|
props.eventUpdate({
|
|
|
|
...props.data,
|
|
|
|
description,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-05-25 21:10:21 +00:00
|
|
|
return () => (
|
2020-12-30 01:32:46 +00:00
|
|
|
<figure class="sb-image">
|
2020-05-24 20:52:33 +00:00
|
|
|
<SbToolbar>
|
2020-05-27 18:36:46 +00:00
|
|
|
{localData.src
|
|
|
|
? <SbButton onClick={selectImage}>Change Image</SbButton>
|
|
|
|
: null}
|
2020-05-24 20:52:33 +00:00
|
|
|
<input
|
|
|
|
type="file"
|
2020-12-30 01:32:46 +00:00
|
|
|
ref={fileInput}
|
2020-05-24 20:52:33 +00:00
|
|
|
style="display: none;"
|
2020-05-27 18:36:46 +00:00
|
|
|
onInput={onImageSelect}
|
2020-05-24 20:52:33 +00:00
|
|
|
/>
|
|
|
|
</SbToolbar>
|
2020-05-25 21:10:21 +00:00
|
|
|
{localData.src
|
2020-12-30 01:32:46 +00:00
|
|
|
? <>
|
|
|
|
<img
|
|
|
|
src={localData.src}
|
|
|
|
alt={localData.alt}
|
|
|
|
class="sb-image__content"
|
|
|
|
/>
|
|
|
|
<SbBlock
|
|
|
|
block={localData.description}
|
|
|
|
eventUpdate={(updated: Block) => onDescriptionUpdate(updated)}
|
|
|
|
/>
|
|
|
|
</>
|
2020-05-27 18:36:46 +00:00
|
|
|
: <SbButton onClick={selectImage}>Select Image</SbButton>}
|
2020-12-30 01:32:46 +00:00
|
|
|
</figure>
|
2020-05-24 20:52:33 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|