From 7506f5b666fe42b91acf28aa48c5cf1f73b18715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=A4dorf?= Date: Sun, 24 May 2020 22:52:33 +0200 Subject: [PATCH] Basic image block --- src/components/user/Image/edit.tsx | 109 +++++++++++++++++++++++++++ src/components/user/Image/style.scss | 4 + src/components/user/Image/util.ts | 16 +++- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/components/user/Image/style.scss diff --git a/src/components/user/Image/edit.tsx b/src/components/user/Image/edit.tsx index e69de29..045be1b 100644 --- a/src/components/user/Image/edit.tsx +++ b/src/components/user/Image/edit.tsx @@ -0,0 +1,109 @@ +import { + defineComponent, + reactive, + computed, + ref, + Ref, + onMounted, + watch, + PropType, +} from '@vue/composition-api'; +import { + model, + blockProps, + useActivation, +} from '@components/TreeElement'; + +import { + getDefaultData, + ImageData, + ImageProps, +} from './util'; + +import SbToolbar from '@internal/Toolbar'; + +import './style.scss'; + +export default defineComponent({ + name: 'sb-image-edit', + + model, + + props: { + ...blockProps, + data: { + type: (null as unknown) as PropType, + default: getDefaultData, + }, + }, + + setup(props: ImageProps, context) { + const localData = reactive({ + src: props.data.src, + alt: props.data.alt, + }); + + console.log(localData); + + const fileInput: Ref = ref(null); + + const { isActive } = useActivation(props.blockId); + + watch(() => props.data, () => { + console.log('props update image'); + localData.src = props.data.src; + localData.alt = props.data.alt; + }); + + const selectImage = () => { + console.log(fileInput); + if (fileInput.value) { + fileInput.value.click(); + } + }; + + const onImageSelect = () => { + console.log('select image'); + if (fileInput.value && fileInput.value.files && fileInput.value.files.length) { + localData.src = window.URL.createObjectURL(fileInput.value.files[0]); + } + }; + + return { + localData, + fileInput, + selectImage, + onImageSelect, + }; + }, + + render() { + return ( +
+ + Image Edit + + + {this.localData.src ? + {this.localData.alt} : + + } +
+ ); + }, +}); diff --git a/src/components/user/Image/style.scss b/src/components/user/Image/style.scss new file mode 100644 index 0000000..ab3533b --- /dev/null +++ b/src/components/user/Image/style.scss @@ -0,0 +1,4 @@ +.sb-image { + max-width: 100%; + max-height: 100%; +} diff --git a/src/components/user/Image/util.ts b/src/components/user/Image/util.ts index 03e73c5..764f09b 100644 --- a/src/components/user/Image/util.ts +++ b/src/components/user/Image/util.ts @@ -1 +1,15 @@ -export const getDefaultData = () => ({}); +import { BlockProps } from '@components/TreeElement'; + +export interface ImageData { + src: string; + alt: string; +} + +export interface ImageProps extends BlockProps { + data: ImageData; +} + +export const getDefaultData: () => ImageData = () => ({ + src: '', + alt: '', +});