浏览代码

增加导出功能

liushengqiang 2 年之前
父节点
当前提交
d4c0043c62
共有 2 个文件被更改,包括 111 次插入21 次删除
  1. 13 6
      src/views/coursewarePlay/component/tools/pen.module.less
  2. 98 15
      src/views/coursewarePlay/component/tools/pen.tsx

+ 13 - 6
src/views/coursewarePlay/component/tools/pen.module.less

@@ -23,13 +23,20 @@
 }
 .rightItem{
     position: absolute;
-    right: 15Px;
-    bottom: 0;
-    bottom: constant(safe-area-inset-bottom);
-    bottom: env(safe-area-inset-bottom);
-    width: 50Px;
-    height: 54Px;
+    right: 4px;
+    top: 4px;
     display: flex;
     justify-content: center;
     align-items: center;
+    background-color: rgba(0, 0, 0, .5);
+    border-radius: 4px;
+    padding: 4px;
+}
+.img{
+    position: absolute;
+    left: 0;
+    top: 0;
+    width: 100%;
+    height: 100%;
+    display: block;
 }

+ 98 - 15
src/views/coursewarePlay/component/tools/pen.tsx

@@ -1,4 +1,7 @@
-import { defineComponent, toRefs, ref } from 'vue'
+import { promisefiyPostMessage } from '@/helpers/native-message'
+import html2canvas from 'html2canvas'
+import { closeToast, Icon, showFailToast, showLoadingToast, showSuccessToast } from 'vant'
+import { defineComponent, toRefs, ref, reactive, onMounted, onUnmounted, nextTick } from 'vue'
 import styles from './pen.module.less'
 
 export default defineComponent({
@@ -8,17 +11,91 @@ export default defineComponent({
       type: Boolean,
       default: false
     },
-    close:{
-        type: Function,
-        default: () => {}
+    close: {
+      type: Function,
+      default: () => {}
     }
   },
   setup(props) {
     const { show } = toRefs(props)
     const firstRender = ref(true)
     const src = /(localhost|192)/.test(location.host)
-    ? 'https://test.lexiaoya.cn/whiteboard-noCollab'
-    : `${location.origin}/whiteboard-noCollab`
+      ? 'http://192.168.3.114:3000/' // 'https://test.lexiaoya.cn/whiteboard-noCollab'
+      : `${location.origin}/whiteboard-noCollab`
+
+    const exportImg = (event: MessageEvent) => {
+      const data = event.data
+      console.log('🚀 ~ event:', event)
+      if (data.api === 'excalidraw_exportImg') {
+        imgs.base64 = data.base64
+        imgs.exported = true
+        nextTick(() => {
+          onSaveImg()
+        })
+      }
+    }
+    onMounted(() => {
+      window.addEventListener('message', exportImg)
+    })
+    onUnmounted(() => {
+      window.removeEventListener('message', exportImg)
+    })
+
+    const imgs = reactive({
+      exported: false,
+      saveLoading: false,
+      base64: '',
+      image: ''
+    })
+
+    const saveImg = async () => {
+      showLoadingToast({ message: '图片生成中...', forbidClick: true })
+      setTimeout(() => {
+        imgs.saveLoading = false
+      }, 100)
+      const res = await promisefiyPostMessage({
+        api: 'savePicture',
+        content: {
+          base64: imgs.image
+        }
+      })
+      if (res?.content?.status === 'success') {
+        showSuccessToast('保存成功')
+      } else {
+        showFailToast('保存失败')
+      }
+      imgs.exported = false
+    }
+
+    const onSaveImg = async () => {
+      // 判断是否在保存中...
+      if (imgs.saveLoading) {
+        return
+      }
+      imgs.saveLoading = true
+      // 判断是否已经生成图片
+      if (imgs.image) {
+        saveImg()
+      } else {
+        const container: any = document.getElementById(`app`)
+        html2canvas(container, {
+          allowTaint: true,
+          useCORS: true,
+          backgroundColor: null
+        })
+          .then(async (canvas) => {
+            const url = canvas.toDataURL('image/png')
+            imgs.image = url
+            saveImg()
+          })
+          .catch(() => {
+            closeToast()
+            imgs.saveLoading = false
+            imgs.exported = false
+          })
+      }
+    }
+
     return () => (
       <div
         class={[
@@ -30,20 +107,26 @@ export default defineComponent({
         <iframe
           class={styles.iframe}
           frameborder="0"
+          width="100vw"
+          height="100vh"
           src={src}
           onLoad={() => {
             firstRender.value = false
           }}
         ></iframe>
-        <div class={styles.rightItem} onClick={() => props.close()}>
-          <svg width="22px" height="20px" viewBox="0 0 22 20">
-            <path
-              transform="translate(-1.000000, -2.000000)"
-              fill="#FFFFFF"
-              d="M13,2 C13.5522847,2 14,2.44771525 14,3 C14,3.51283584 13.6139598,3.93550716 13.1166211,3.99327227 L13,4 L3,4 L3,20 L13,20 C13.5128358,20 13.9355072,20.3860402 13.9932723,20.8833789 L14,21 C14,21.5128358 13.6139598,21.9355072 13.1166211,21.9932723 L13,22 L2,22 C1.48716416,22 1.06449284,21.6139598 1.00672773,21.1166211 L1,21 L1,3 C1,2.48716416 1.38604019,2.06449284 1.88337887,2.00672773 L2,2 L13,2 Z M17.7071068,7.05025253 L21.9497475,11.2928932 L21.9497475,11.2928932 C22.3402718,11.6834175 22.3402718,12.3165825 21.9497475,12.7071068 L17.7071068,16.9497475 C17.3165825,17.3402718 16.6834175,17.3402718 16.2928932,16.9497475 C15.9023689,16.5592232 15.9023689,15.9260582 16.2928932,15.5355339 L18.828,12.999 L9.29368112,13 C8.74139637,13 8.29368112,12.5522847 8.29368112,12 C8.29368112,11.4871642 8.67972131,11.0644928 9.17706,11.0067277 L9.29368112,11 L18.827,10.999 L16.2928932,8.46446609 C15.9023689,8.0739418 15.9023689,7.44077682 16.2928932,7.05025253 C16.6834175,6.65972824 17.3165825,6.65972824 17.7071068,7.05025253 Z"
-            ></path>
-          </svg>
-        </div>
+        {imgs.exported ? (
+          <img class={styles.img} src={imgs.base64} />
+        ) : (
+          <div
+            class={styles.rightItem}
+            onClick={() => {
+              // onSaveImg()
+              // props.close()
+            }}
+          >
+            <Icon name='close' size={30} color="#fff" />
+          </div>
+        )}
       </div>
     )
   }