刘光辉
8 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<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>