Modal.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <a-modal ref="modalRef" width="850px" :visible="props.visible" :wrap-style="{ overflow: 'hidden' }" @ok="handleOk"
  3. :mask-closable="maskClosable" :centered="props.centered" :footer="props.footer" @cancel="handleCancel"
  4. :confirm-loading="props.confirmLoading" :destroyOnClose="props.destroyOnClose">
  5. <slot></slot>
  6. <template #title>
  7. <div ref="modalTitleRef" style="width: 100%; cursor: move">{{ props.title }}</div>
  8. </template>
  9. <template #modalRender="{ originVNode }">
  10. <div :style="transformStyle">
  11. <component :is="originVNode" />
  12. </div>
  13. </template>
  14. </a-modal>
  15. </template>
  16. <script lang="ts" setup>
  17. import { computed, CSSProperties, ref, watch, watchEffect } from "vue";
  18. import { useDraggable } from "@vueuse/core";
  19. // 定义Props接口,描述组件的属性
  20. interface Props {
  21. title?: string, // 对话框的标题,默认为"提示"
  22. footer?: null, // 对话框的页脚,默认为null
  23. visible: boolean, // 对话框是否可见
  24. confirmLoading?: boolean, // 确认按钮是否处于加载状态,默认为false
  25. destroyOnClose?: boolean, // 对话框关闭时是否销毁组件,默认为false
  26. maskClosable?: boolean, // 点击遮罩层是否可关闭对话框,默认为false
  27. centered?: boolean, // 对话框是否居中显示,默认为false
  28. }
  29. // 设置props的默认值
  30. const props = withDefaults(defineProps<Props>(), { title: '摄像头信息', visible: false, destroyOnClose: false, maskClosable: true })
  31. // 创建ref引用modalTitleRef,用于引用组件中的标题元素
  32. const modalTitleRef = ref<HTMLElement | null | any>(null);
  33. // 使用useDraggable钩子,获取拖拽相关属性
  34. const { x, y, isDragging } = useDraggable(modalTitleRef);
  35. // 创建emit函数,用于触发自定义事件
  36. const emit = defineEmits(['ok', 'update:visible', 'cancel'])
  37. // 处理ok事件的函数
  38. const handleOk = (e: MouseEvent) => {
  39. emit('ok')
  40. };
  41. // 处理cancel事件的函数
  42. const handleCancel = () => {
  43. emit('update:visible', false)
  44. emit('cancel')
  45. }
  46. // 创建各种响应式数据
  47. const startX = ref<number>(0); // 记录起始点的 x 坐标
  48. const startY = ref<number>(0); // 记录起始点的 y 坐标
  49. const startedDrag = ref(false); // 标志位,表示是否开始拖拽,默认为 false
  50. const transformX = ref(0); // x 偏移量
  51. const transformY = ref(0); // y 偏移量
  52. const preTransformX = ref(0); // 拖拽前的 x 偏移量
  53. const preTransformY = ref(0); // 拖拽前的 y 偏移量
  54. const dragRect = ref({ left: 0, right: 0, top: 0, bottom: 0 }); // 可拖拽的边界
  55. // 监听x和y的变化
  56. watch([x, y], () => { // 监听鼠标移动事件的函数
  57. if (!startedDrag.value) { // 如果尚未开始拖拽
  58. startX.value = x.value; // 记录起始点的 x 坐标
  59. startY.value = y.value; // 记录起始点的 y 坐标
  60. const bodyRect = document.body.getBoundingClientRect(); // 获取页面 body 元素的边界信息
  61. const titleRect = modalTitleRef.value.getBoundingClientRect(); // 获取 modalTitle 元素的边界信息
  62. dragRect.value.right = bodyRect.width - titleRect.width; // 计算可拖拽的最大右边界
  63. dragRect.value.bottom = bodyRect.height - titleRect.height; // 计算可拖拽的最大下边界
  64. preTransformX.value = transformX.value; // 记录拖拽前的 x 偏移量
  65. preTransformY.value = transformY.value; // 记录拖拽前的 y 偏移量
  66. }
  67. startedDrag.value = true; // 设置已开始拖拽标志
  68. });
  69. // 监听isDragging的变化
  70. watch(isDragging, () => { // 监听 isDragging 变量的改变
  71. if (!isDragging) { // 如果 isDragging 变为 false
  72. startedDrag.value = false; // 将 startedDrag.value 设置为 false,表示拖拽操作结束
  73. }
  74. });
  75. // 使用watchEffect监听响应式数据的变化
  76. watchEffect(() => { // 响应 startedDrag.value 的变化
  77. if (startedDrag.value) { // 如果 startedDrag.value 为 true,表示正在进行拖拽操作
  78. transformX.value = // 计算 x 偏移量
  79. preTransformX.value +
  80. Math.min(Math.max(dragRect.value.left, x.value), dragRect.value.right) -
  81. startX.value;
  82. transformY.value = // 计算 y 偏移量
  83. preTransformY.value +
  84. Math.min(Math.max(dragRect.value.top, y.value), dragRect.value.bottom) -
  85. startY.value;
  86. }
  87. });
  88. // 计算transformStyle,用于动态设置对话框的位置
  89. const transformStyle = computed<CSSProperties>(() => {
  90. return {
  91. transform: `translate(${transformX.value}px, ${transformY.value}px)`,
  92. };
  93. });
  94. </script>