schlechtenburg/src/components/user/Image/edit.tsx

94 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-05-24 20:52:33 +00:00
import {
defineComponent,
reactive,
ref,
Ref,
watch,
PropType,
} from '@vue/composition-api';
import {
model,
blockProps,
} from '@components/TreeElement';
2020-05-25 18:07:34 +00:00
import SbToolbar from '@internal/Toolbar';
2020-05-24 20:52:33 +00:00
import {
getDefaultData,
ImageData,
ImageProps,
} from './util';
import './style.scss';
export default defineComponent({
name: 'sb-image-edit',
model,
props: {
...blockProps,
data: {
type: (null as unknown) as PropType<ImageData>,
default: getDefaultData,
},
},
2020-05-27 13:57:57 +00:00
setup(props: ImageProps, context) {
2020-05-24 20:52:33 +00:00
const localData = reactive({
src: props.data.src,
alt: props.data.alt,
});
const fileInput: Ref<null|HTMLInputElement> = ref(null);
watch(() => props.data, () => {
localData.src = props.data.src;
localData.alt = props.data.alt;
});
const selectImage = () => {
if (fileInput.value) {
fileInput.value.click();
}
};
const onImageSelect = () => {
if (fileInput.value && fileInput.value.files && fileInput.value.files.length) {
2020-05-27 13:57:57 +00:00
context.emit('update', {
src: window.URL.createObjectURL(fileInput.value.files[0]),
});
2020-05-24 20:52:33 +00:00
}
};
2020-05-25 21:10:21 +00:00
return () => (
2020-05-24 20:52:33 +00:00
<div class="sb-image">
<SbToolbar>
Image Edit
<input
type="file"
ref="fileInput"
style="display: none;"
{...{
on: {
2020-05-25 21:10:21 +00:00
input: onImageSelect,
2020-05-24 20:52:33 +00:00
},
}}
/>
</SbToolbar>
2020-05-25 21:10:21 +00:00
{localData.src
? <img src={localData.src} alt={localData.alt} />
2020-05-25 18:07:34 +00:00
: <button
2020-05-24 20:52:33 +00:00
{...{
on: {
2020-05-25 21:10:21 +00:00
click: selectImage,
2020-05-24 20:52:33 +00:00
},
}}
>Select Image</button>
}
</div>
);
},
});