schlechtenburg/packages/image/lib/edit.tsx

112 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

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