|
@@ -0,0 +1,87 @@
|
|
|
+<script setup lang="ts">
|
|
|
+import { onMounted, onUnmounted, useTemplateRef, watch } from 'vue';
|
|
|
+
|
|
|
+import { EnvAreaMsgType, getEnvAreaMsgType } from '@/utils/env-area';
|
|
|
+
|
|
|
+import type { IframeMsg, RegionsPointsItem } from '@/types';
|
|
|
+
|
|
|
+interface Props {
|
|
|
+ areaData?: RegionsPointsItem;
|
|
|
+}
|
|
|
+
|
|
|
+const props = defineProps<Props>();
|
|
|
+
|
|
|
+const emit = defineEmits<{
|
|
|
+ highlightEnvPoint: [id: number];
|
|
|
+}>();
|
|
|
+
|
|
|
+const iframeRef = useTemplateRef('previewIframe');
|
|
|
+let isIframeLoaded = false;
|
|
|
+
|
|
|
+onMounted(() => {
|
|
|
+ window.addEventListener('message', handleIframeMsg);
|
|
|
+});
|
|
|
+
|
|
|
+onUnmounted(() => {
|
|
|
+ window.removeEventListener('message', handleIframeMsg);
|
|
|
+});
|
|
|
+
|
|
|
+watch(
|
|
|
+ () => props.areaData,
|
|
|
+ () => {
|
|
|
+ if (isIframeLoaded) {
|
|
|
+ sendAreaData();
|
|
|
+ }
|
|
|
+ },
|
|
|
+);
|
|
|
+
|
|
|
+const handleIframeMsg = (e: MessageEvent<IframeMsg>) => {
|
|
|
+ const { msgType } = e.data;
|
|
|
+
|
|
|
+ if (msgType === getEnvAreaMsgType(EnvAreaMsgType.PreviewLoaded)) {
|
|
|
+ isIframeLoaded = true;
|
|
|
+ sendAreaData();
|
|
|
+ } else if (msgType === getEnvAreaMsgType(EnvAreaMsgType.HighlightEnvMonitorPoint)) {
|
|
|
+ emit('highlightEnvPoint', e.data.monitorPointId);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const sendAreaData = () => {
|
|
|
+ if (props.areaData) {
|
|
|
+ const msg: IframeMsg = {
|
|
|
+ msgType: getEnvAreaMsgType(EnvAreaMsgType.PreviewArea),
|
|
|
+ ...JSON.parse(JSON.stringify(props.areaData)),
|
|
|
+ };
|
|
|
+
|
|
|
+ iframeRef.value?.contentWindow?.postMessage(msg, '*');
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const highlightCanvasPoint = (id: number) => {
|
|
|
+ const msg: IframeMsg = {
|
|
|
+ msgType: getEnvAreaMsgType(EnvAreaMsgType.HighlightCanvasMonitorPoint),
|
|
|
+ monitorPointId: id,
|
|
|
+ };
|
|
|
+
|
|
|
+ iframeRef.value?.contentWindow?.postMessage(msg, '*');
|
|
|
+};
|
|
|
+
|
|
|
+defineExpose({
|
|
|
+ highlightCanvasPoint,
|
|
|
+});
|
|
|
+</script>
|
|
|
+
|
|
|
+<template>
|
|
|
+ <iframe class="env-area-preview" ref="previewIframe" src="http://localhost:88/env-area/preview"></iframe>
|
|
|
+</template>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+.env-area-preview {
|
|
|
+ width: 100%;
|
|
|
+ height: 100%;
|
|
|
+ overflow: hidden;
|
|
|
+ background: transparent;
|
|
|
+ border: none;
|
|
|
+ border-radius: 12px;
|
|
|
+}
|
|
|
+</style>
|