index.vue
3.57 KB
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
108
109
110
111
112
113
<script lang="ts" setup>
  import { computed } from 'vue';
  import { WidgetDataType } from '../../hooks/useDataSource';
  import { fetchViewComponent } from '../../../packages';
  import { ComponentMode, ConfigType } from '/@/views/visual/packages/index.type';
  import { ExtraDataSource } from '../../types';
  import { CSSProperties } from 'vue';
  import { toRaw } from 'vue';
  import { Spin } from 'ant-design-vue';
  import { componentMap, transformComponentKey } from '../../../packages/componentMap';
  import { onMounted } from 'vue';
  import { registerComponent } from '../../../packages/componentMap';
  import { useGetComponentConfig } from '../../../packages/hook/useGetComponetConfig';
  import { unref } from 'vue';
  import { isBoolean } from '/@/utils/is';
  const props = defineProps<{
    sourceInfo: WidgetDataType;
  }>();
  const getLayout = computed(() => {
    const { itemWidthRatio, itemHeightRatio } = props.sourceInfo || {};
    return { width: `${itemWidthRatio}%`, height: `${itemHeightRatio}%` } as CSSProperties;
  });
  const getCanRenderFlag = computed(() => {
    const { itemHeightRatio, itemWidthRatio } = props.sourceInfo;
    return [itemHeightRatio, itemWidthRatio].every(Boolean);
  });
  const getComponentConfig = computed(() => {
    const { widthPx, heightPx, itemWidthRatio, itemHeightRatio, frontId } = props.sourceInfo;
    return useGetComponentConfig(frontId, {
      widthPx,
      heightPx,
      itemWidthRatio,
      itemHeightRatio,
      mode: ComponentMode.BOUND_PREVIEW,
    });
  });
  const getBindValue = computed(() => {
    return (options: ExtraDataSource) => {
      const raw = toRaw(options);
      const config = unref(getComponentConfig);
      return {
        config: {
          ...config,
          option: { ...config.option, ...raw },
        },
      };
    };
  });
  const getIsMultipleDataSourceComponent = computed(() => {
    const { persetOption } = unref(getComponentConfig);
    const { multipleDataSourceComponent } = persetOption || {};
    return isBoolean(multipleDataSourceComponent) ? multipleDataSourceComponent : false;
  });
  const getBindMultipleDataSourceValue = computed(() => {
    const config = unref(getComponentConfig);
    const { dataSource } = props.sourceInfo;
    return {
      config: {
        ...config,
        option: { ...config.option, dataSource: toRaw(dataSource) },
      },
    };
  });
  onMounted(() => {
    const key = transformComponentKey(props.sourceInfo.frontId);
    const component = fetchViewComponent({ key } as ConfigType);
    registerComponent(key, component);
  });
</script>
<template>
  <Spin :spinning="!getCanRenderFlag" wrapperClassName="widget-distribute-spin">
    <!-- 多数据源组件 -->
    <template v-if="getCanRenderFlag">
      <template v-if="getIsMultipleDataSourceComponent">
        <component
          :is="componentMap.get(transformComponentKey(props.sourceInfo.frontId))"
          class="w-full h-full"
          v-bind="getBindMultipleDataSourceValue"
        />
      </template>
      <!-- 单一数据源组件 -->
      <template v-if="!getIsMultipleDataSourceComponent">
        <component
          v-for="item in props.sourceInfo.dataSource"
          :key="item.uuid"
          :is="componentMap.get(transformComponentKey(props.sourceInfo.frontId))"
          :style="getLayout"
          v-bind="getBindValue(item)"
        />
      </template>
    </template>
  </Spin>
</template>
<style lang="less" scoped>
  .widget-distribute-spin {
    @apply w-full h-full overflow-hidden;
    :deep(.ant-spin-container) {
      @apply w-full h-full flex flex-wrap;
    }
  }
</style>