ExcalidrawView.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <div ref="canvasContainer" class="excalidraw-wrapper" @click="showCanvasTop" />
  3. <div ref="canvasFloating" v-if="showFullView" class="floating-canvas" @mousedown="onMouseDown" @wheel.prevent="onWheel" @click="onCloseFloating" />
  4. </template>
  5. <script setup lang="ts">
  6. import { onMounted, ref, nextTick, onUnmounted } from 'vue'
  7. import { withBase } from 'vitepress';
  8. import { exportToCanvas } from '@excalidraw/excalidraw'
  9. import { ExportedDataState } from '@excalidraw/excalidraw/data/types'
  10. interface Props {
  11. filePath: string;
  12. }
  13. const props = defineProps<Props>()
  14. const canvasContainer = ref<HTMLCanvasElement | null>(null)
  15. const canvasFloating = ref<HTMLCanvasElement | null>(null)
  16. const showFullView = ref(false);
  17. let excalidrawJson: ExportedDataState | undefined = undefined
  18. let topCanvas: HTMLCanvasElement | undefined = undefined
  19. let scale = 1;
  20. let origin = { x: 0, y: 0 };
  21. let lastMouse = { x: 0, y: 0 };
  22. let dragging = false;
  23. let hasMoved = false; // 标记是否拖动过
  24. const dblclickCanvasHandler = () => {
  25. origin = { x: 0, y: 0 };
  26. scale = 1;
  27. updateTransform();
  28. };
  29. function showCanvasTop() {
  30. showFullView.value = true;
  31. // 等待 DOM 渲染后复制内容
  32. nextTick(async () => {
  33. if (canvasFloating.value && excalidrawJson) {
  34. const canvas = await drawCanvas(
  35. excalidrawJson,
  36. canvasFloating.value.clientWidth,
  37. canvasFloating.value.clientHeight
  38. )
  39. if (canvas) {
  40. topCanvas = canvas
  41. canvas.style.transform = `translate(0px, 0px) scale(1)`;
  42. origin = { x: 0, y: 0 };
  43. scale = 1;
  44. canvasFloating.value.appendChild(canvas)
  45. // 双击重置
  46. canvas.addEventListener('dblclick', dblclickCanvasHandler);
  47. }
  48. }
  49. });
  50. }
  51. function closeFullView() {
  52. showFullView.value = false;
  53. if (topCanvas) {
  54. topCanvas.removeEventListener('dblclick', dblclickCanvasHandler);
  55. }
  56. if (canvasFloating.value) {
  57. canvasFloating.value.innerHTML = ''; // 清空 canvas
  58. }
  59. }
  60. function onCloseFloating() {
  61. if (hasMoved) {
  62. hasMoved = false;
  63. return; // 用户拖动过,不关闭
  64. }
  65. closeFullView()
  66. }
  67. function onMouseDown(e: MouseEvent) {
  68. // 如果按的是 canvas,就拖动
  69. if (topCanvas && topCanvas.contains(e.target as Node)) {
  70. dragging = true;
  71. hasMoved = false; // 重置状态
  72. lastMouse = { x: e.clientX, y: e.clientY };
  73. const onMouseMove = (e: MouseEvent) => {
  74. if (!dragging) return;
  75. const dx = e.clientX - lastMouse.x;
  76. const dy = e.clientY - lastMouse.y;
  77. // 如果移动距离大于阈值,认为已经拖动过
  78. if (Math.abs(dx) > 2 || Math.abs(dy) > 2) {
  79. hasMoved = true;
  80. }
  81. origin.x += dx;
  82. origin.y += dy;
  83. lastMouse = { x: e.clientX, y: e.clientY };
  84. updateTransform();
  85. };
  86. const onMouseUp = () => {
  87. dragging = false;
  88. window.removeEventListener('mousemove', onMouseMove);
  89. window.removeEventListener('mouseup', onMouseUp);
  90. };
  91. // 添加鼠标事件监控
  92. window.addEventListener('mousemove', onMouseMove);
  93. window.addEventListener('mouseup', onMouseUp);
  94. }
  95. }
  96. function updateTransform() {
  97. if (topCanvas) {
  98. topCanvas.style.transform = `translate(${origin.x}px, ${origin.y}px) scale(${scale})`;
  99. }
  100. }
  101. function onWheel(e: WheelEvent) {
  102. const delta = -e.deltaY;
  103. const factor = delta > 0 ? 1.1 : 0.9;
  104. scale *= factor;
  105. scale = Math.min(Math.max(0.2, scale), 10); // 限制缩放范围
  106. updateTransform();
  107. }
  108. const getExcalidraw = async (path: string): Promise<ExportedDataState | undefined> => {
  109. const res = await fetch(withBase(path)).catch((err) => {
  110. console.log("excalidraw file err: ", err)
  111. })
  112. if (!res) return undefined
  113. const json = await res.json()
  114. return json
  115. }
  116. const drawCanvas = async (data: ExportedDataState, width: number, height: number): Promise<HTMLCanvasElement> => {
  117. // const maxWidthOrHeight = width > height ? height : width
  118. // 改为只限制宽度,因为渲染前父窗口高度可能为0,导致maxWidthOrHeight不生效
  119. const maxWidthOrHeight = width
  120. const canvas = await exportToCanvas({
  121. ...data,
  122. maxWidthOrHeight,// 绘制的最大高度或宽度,设置maxWidthOrHeight后getDimensions则不生效
  123. // getDimensions: (width: number, height: number) => {
  124. // return {
  125. // width,
  126. // height,
  127. // scale: 1
  128. // }
  129. // }, // 参数为原始绘制尺寸,返回尺寸和比例 {width: number; height: number; scale: number}
  130. exportPadding: 10 // 添加边距,默认10
  131. })
  132. return canvas
  133. }
  134. function onKeyDown(e: KeyboardEvent) {
  135. if (e.key === 'Escape') {
  136. closeFullView()
  137. }
  138. }
  139. onMounted(async () => {
  140. console.log("excalidrawJson: ", props.filePath)
  141. excalidrawJson = await getExcalidraw(props.filePath)
  142. if (excalidrawJson && canvasContainer.value) {
  143. let excalidrawCanvas = await drawCanvas(excalidrawJson, canvasContainer.value.clientWidth, canvasContainer.value.clientHeight)
  144. if (excalidrawCanvas) {
  145. canvasContainer.value.appendChild(excalidrawCanvas)
  146. }
  147. // 使用 ResizeObserver 监听父容器宽度变化
  148. const resizeObserver = new ResizeObserver(async (entries: ResizeObserverEntry[]) => {
  149. for (const entry of entries) {
  150. const el = entry.target
  151. if (el === canvasContainer.value && excalidrawJson) {
  152. excalidrawCanvas = await drawCanvas(excalidrawJson, el.clientWidth, el.clientHeight)
  153. }
  154. }
  155. });
  156. resizeObserver.observe(canvasContainer.value!);
  157. }
  158. window.addEventListener('keydown', onKeyDown);
  159. })
  160. onUnmounted(() => {
  161. window.removeEventListener('keydown', onKeyDown);
  162. })
  163. </script>
  164. <style scoped>
  165. .excalidraw-wrapper {
  166. width: 100%;
  167. border: 1px solid #ddd;
  168. }
  169. .floating-canvas {
  170. position: fixed;
  171. top: 0;
  172. left: 0;
  173. width: 100vw;
  174. height: 100vh;
  175. background: rgba(0, 0, 0, 0.8);
  176. z-index: 9999;
  177. display: flex;
  178. justify-content: center;
  179. align-items: center;
  180. overflow: hidden;
  181. cursor: grab;
  182. }
  183. .floating-canvas:active {
  184. cursor: grabbing;
  185. }
  186. .floating-canvas canvas {
  187. /* max-width: 90vw; */
  188. max-width: none;
  189. /* max-height: 90vh; */
  190. max-height: none;
  191. box-shadow: 0 0 12px #fff;
  192. transition: transform 0.05s;
  193. }
  194. </style>