<script lang="ts" setup>
|
import { onMounted, ref, watch } from 'vue';
|
|
import { storeToRefs } from 'pinia';
|
|
import { useJnpfUniverStore } from '../../../store';
|
|
interface Data {
|
id: string;
|
piniaStoreId: string;
|
}
|
interface PropsType {
|
data: Data;
|
}
|
|
defineOptions({ name: 'JnpfUniverFloatImage' });
|
|
const props = defineProps<PropsType>();
|
|
const floatImageContainerRef = ref<HTMLDivElement | null>(null);
|
|
const jnpfUniverStore = useJnpfUniverStore(props?.data?.piniaStoreId);
|
const { floatImageToUniverImgDataCaches, focusedFloatImageDataCache, univerCreateModeCache } = storeToRefs(jnpfUniverStore);
|
|
// 初始化或更新图表
|
function initialize(option: any) {
|
if (!floatImageContainerRef.value) {
|
return;
|
}
|
|
const imageEle = floatImageContainerRef.value?.querySelector('#imageEle') as HTMLImageElement | null;
|
if (!imageEle) {
|
return;
|
}
|
|
const { alt = '', src = '' } = option ?? {};
|
imageEle.setAttribute('src', src);
|
imageEle.setAttribute('alt', alt);
|
|
// 非设计状态下,图片直接转成univer图片
|
if (univerCreateModeCache.value !== 'design') {
|
const { id: domId } = props.data ?? {};
|
|
if (domId) {
|
const caches = {
|
...floatImageToUniverImgDataCaches.value,
|
[domId]: src,
|
};
|
|
jnpfUniverStore?.setFloatImageToUniverImgDataCaches(caches);
|
}
|
}
|
}
|
|
onMounted(() => {
|
const { id: domId, piniaStoreId } = props.data ?? {};
|
|
if (!floatImageContainerRef.value || !domId || !piniaStoreId) {
|
return;
|
}
|
|
const floatImageDataCaches = jnpfUniverStore?.floatImageDataCaches ?? {};
|
|
const targetOption = Object.values(floatImageDataCaches as Record<string, { domId: string; option: any }>)?.find(item => item?.domId === domId)?.option;
|
initialize(targetOption);
|
});
|
|
watch(
|
() => focusedFloatImageDataCache.value,
|
value => {
|
const { domId, drawingId, option } = value;
|
|
if (!domId && !drawingId) {
|
// store置空的情况下,不响应
|
return;
|
}
|
|
const containerId = floatImageContainerRef.value?.getAttribute('id');
|
if (domId !== containerId) {
|
return;
|
}
|
|
initialize(option);
|
},
|
);
|
</script>
|
|
<template>
|
<div ref="floatImageContainerRef" :id="data.id" class="jnpf-univer-float-image-ele">
|
<img id="imageEle" src="" alt="" />
|
</div>
|
</template>
|
|
<style lang="scss" scoped>
|
.jnpf-univer-float-image-ele {
|
position: absolute;
|
width: 100%;
|
height: 100%;
|
background: #fff;
|
|
img {
|
display: block;
|
width: 100%;
|
height: 100%;
|
}
|
}
|
</style>
|