pen.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { promisefiyPostMessage } from '@/helpers/native-message'
  2. import html2canvas from 'html2canvas'
  3. import { closeToast, Icon, showFailToast, showLoadingToast, showSuccessToast } from 'vant'
  4. import { defineComponent, toRefs, ref, reactive, onMounted, onUnmounted, nextTick } from 'vue'
  5. import styles from './pen.module.less'
  6. export default defineComponent({
  7. name: 'pen',
  8. props: {
  9. show: {
  10. type: Boolean,
  11. default: false
  12. },
  13. close: {
  14. type: Function,
  15. default: () => {}
  16. }
  17. },
  18. setup(props) {
  19. const { show } = toRefs(props)
  20. const firstRender = ref(true)
  21. const src = /(localhost|192)/.test(location.host)
  22. ? 'https://test.lexiaoya.cn/whiteboard-noCollab'
  23. : `${location.origin}/whiteboard-noCollab`
  24. const exportImg = (event: MessageEvent) => {
  25. const data = event.data
  26. // console.log('🚀 ~ event:', data)
  27. if (data.api === 'excalidraw_exportImg') {
  28. imgs.base64 = data.base64
  29. imgs.exported = true
  30. nextTick(() => {
  31. onSaveImg()
  32. })
  33. }
  34. }
  35. onMounted(() => {
  36. window.addEventListener('message', exportImg)
  37. })
  38. onUnmounted(() => {
  39. window.removeEventListener('message', exportImg)
  40. })
  41. const imgs = reactive({
  42. exported: false,
  43. saveLoading: false,
  44. base64: '',
  45. image: ''
  46. })
  47. const saveImg = async () => {
  48. showLoadingToast({ message: '图片生成中...', forbidClick: true })
  49. setTimeout(() => {
  50. imgs.saveLoading = false
  51. }, 100)
  52. const res = await promisefiyPostMessage({
  53. api: 'savePicture',
  54. content: {
  55. base64: imgs.image
  56. }
  57. })
  58. if (res?.content?.status === 'success') {
  59. showSuccessToast('已保存到相册')
  60. } else {
  61. showFailToast('保存失败')
  62. }
  63. imgs.exported = false
  64. }
  65. const onSaveImg = async () => {
  66. // 判断是否在保存中...
  67. if (imgs.saveLoading) {
  68. return
  69. }
  70. console.log('开始')
  71. imgs.saveLoading = true
  72. const container: any = document.getElementById(`app`)
  73. html2canvas(container, {
  74. allowTaint: true,
  75. useCORS: true,
  76. backgroundColor: null
  77. })
  78. .then(async (canvas) => {
  79. // console.log("🚀 ~ canvas:", canvas)
  80. // document.body.appendChild(canvas)
  81. // const url = await canvas.toDataURL()
  82. try {
  83. imgs.image = canvas.toDataURL()
  84. } catch (error) {
  85. console.log(error)
  86. }
  87. console.log("🚀 ~ imgs.image:", imgs.image)
  88. saveImg()
  89. })
  90. .catch((error) => {
  91. console.log("🚀 ~ error:", error)
  92. closeToast()
  93. imgs.saveLoading = false
  94. imgs.exported = false
  95. })
  96. }
  97. return () => (
  98. <div
  99. class={[
  100. styles.pen,
  101. firstRender.value ? styles.dely : '',
  102. show.value ? styles.open : styles.hide
  103. ]}
  104. >
  105. <iframe
  106. class={styles.iframe}
  107. frameborder="0"
  108. width="100vw"
  109. height="100vh"
  110. src={src}
  111. onLoad={() => {
  112. firstRender.value = false
  113. }}
  114. ></iframe>
  115. {imgs.exported ? (
  116. <img crossorigin="anonymous" class={styles.img} src={imgs.base64} />
  117. ) : (
  118. <div
  119. class={styles.rightItem}
  120. onClick={() => props.close()}
  121. >
  122. <svg width="22px" height="20px" viewBox="0 0 22 20">
  123. <path
  124. transform="translate(-1.000000, -2.000000)"
  125. fill="#FFFFFF"
  126. 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"
  127. ></path>
  128. </svg>
  129. </div>
  130. )}
  131. </div>
  132. )
  133. }
  134. })