schlechtenburg/packages/core/lib/components/user/Image/edit.tsx

92 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-05-24 20:52:33 +00:00
import {
2020-12-27 21:32:43 +00:00
defineAsyncComponent,
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-27 21:32:43 +00:00
} from '/@components/TreeElement';
2020-05-24 20:52:33 +00:00
2020-12-27 21:32:43 +00:00
import SbToolbar from '/@internal/Toolbar';
import SbButton from '/@internal/Button';
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-27 21:32:43 +00:00
export default defineAsyncComponent({
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,
});
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 15:32:35 +00:00
const reader = new FileReader();
reader.addEventListener('load', () => {
props.eventUpdate({
src: reader.result,
alt: props.data.alt,
});
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-05-25 21:10:21 +00:00
return () => (
2020-05-24 20:52:33 +00:00
<div class="sb-image">
<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"
ref="fileInput"
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
? <img src={localData.src} alt={localData.alt} />
2020-05-27 18:36:46 +00:00
: <SbButton onClick={selectImage}>Select Image</SbButton>}
2020-05-24 20:52:33 +00:00
</div>
);
},
});