SModal2.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. const n = `<template>
  2. <!-- v-bind处理a-table 传递过来的参数,你们开发的时候不用再子组件中接收这么ant原有的参数-->
  3. <a-modal
  4. :open="isOpen"
  5. :body-style="computeBodyStyle"
  6. :width="modalWidth"
  7. :centered="centered"
  8. @cancel="closeModal"
  9. :afterClose="handleAfterClose"
  10. :destroyOnClose="destroyOnClose"
  11. :keyboard="keyboardOpen"
  12. :style="topStyle"
  13. :maskClosable="maskClosable"
  14. :mask="mask"
  15. :wrap-class-name="'s-modal ' + fullModal"
  16. v-bind="attrs"
  17. >
  18. <template #title>
  19. <div class="modalHeader">
  20. <!-- 标题的ref用于控制是否可以拖拽-->
  21. <div :ref="setTitleRef" class="draggableTitle">{{ title }}</div>
  22. <!-- 弹框头部的标题,以及全屏的按钮-->
  23. <ModalHeader
  24. :width="modalWidth"
  25. :showFullScreen="showFullScreen"
  26. @fullScreen="handleFullScreen"
  27. @reduction="handleReduction"
  28. />
  29. </div>
  30. </template>
  31. <a-spin :spinning="loading">
  32. <!-- 弹框内容区域-->
  33. <div ref="bodySlot">
  34. <div class="tabs-wrap" v-if="tabs && tabs.length > 0">
  35. <a-tabs :active-key="activeTabKey" @change="handTabChange">
  36. <a-tab-pane
  37. :tab="tab[tabKey.label]"
  38. v-for="tab in tabs"
  39. :key="tab[tabKeyValue]"
  40. ></a-tab-pane>
  41. </a-tabs>
  42. </div>
  43. <div class="step-wrap" ref="stepRef" v-if="steps && steps.length">
  44. <a-steps :current="stepActive" type="navigation" size="small">
  45. <a-step :title="step" v-for="step in steps" :key="step" :disabled="true" />
  46. </a-steps>
  47. </div>
  48. <div class="modal-body-container">
  49. <slot></slot>
  50. </div>
  51. </div>
  52. </a-spin>
  53. <!-- 弹框底部按钮区域-->
  54. <template #footer>
  55. <div class="footer-top-border"></div>
  56. <div ref="footerSlot" v-if="$slots.footer">
  57. <a-space :size="spaceSize">
  58. <slot name="footer"></slot>
  59. </a-space>
  60. </div>
  61. <div ref="footerSlot" v-if="!$slots.footer && (!steps || !steps.length)">
  62. <a-space :size="spaceSize">
  63. <a-button @click="closeModal"><poweroff-outlined />{{ $l('关闭') }} </a-button>
  64. <a-button type="primary" @click="handSave">
  65. <save-outlined />
  66. {{ $l(saveBtnText) }}
  67. </a-button>
  68. </a-space>
  69. </div>
  70. <div ref="footerSlot" v-if="steps && steps.length > 0">
  71. <a-space :size="spaceSize">
  72. <a-button @click="closeModal"><poweroff-outlined />{{ $l('关闭') }} </a-button>
  73. <a-button
  74. v-if="steps.length"
  75. :disabled="stepActive === stepDefaultActive"
  76. @click="handPrev"
  77. >
  78. <arrow-left-outlined />{{ $l('上一步') }}
  79. </a-button>
  80. <a-button
  81. type="primary"
  82. v-if="steps.length"
  83. :disabled="stepActive === steps.length - 1"
  84. @click="handNext"
  85. >
  86. <arrow-right-outlined />{{ $l('下一步') }}
  87. </a-button>
  88. <a-button v-if="isShowSave" style="margin-right: 8px" type="primary" @click="handSave">
  89. <save-outlined />
  90. {{ $l(saveBtnText) }}
  91. </a-button>
  92. </a-space>
  93. </div>
  94. </template>
  95. <!-- 实现是否拖拽弹框计算的核心代码-->
  96. <template #modalRender="{ originVNode }">
  97. <div :style="transformStyle">
  98. <component :is="originVNode" />
  99. </div>
  100. </template>
  101. </a-modal>
  102. </template>
  103. <script setup>
  104. import { ref, computed, watch, watchEffect, useAttrs, nextTick, onBeforeUnmount } from 'vue'
  105. import { useDraggable } from '@vueuse/core'
  106. import ModalHeader from './components/ModalHeader.vue'
  107. defineOptions({ name: 'SModal' })
  108. const props = defineProps({
  109. // 确定按钮 loading,保存中禁止关闭
  110. loading: {
  111. type: Boolean,
  112. default: false
  113. },
  114. // 是否垂直居中显示
  115. centered: {
  116. type: Boolean,
  117. default: true
  118. },
  119. // 点遮罩是否关闭
  120. maskClosable: {
  121. type: Boolean,
  122. default: true
  123. },
  124. // 是否显示遮罩
  125. mask: {
  126. type: Boolean,
  127. default: true
  128. },
  129. // 非居中时的距顶距离
  130. topStyle: {
  131. type: String,
  132. default: ''
  133. },
  134. // 是否显示右上角关闭按钮
  135. closable: {
  136. type: Boolean,
  137. default: true
  138. },
  139. // 关闭后是否销毁内容
  140. destroyOnClose: {
  141. type: Boolean,
  142. default: false
  143. },
  144. // 是否支持 Esc 关闭
  145. keyboardOpen: {
  146. type: Boolean,
  147. default: true
  148. },
  149. // 弹窗标题
  150. title: {
  151. type: String,
  152. default: ''
  153. },
  154. // 显隐(v-model:value)
  155. value: {
  156. type: Boolean,
  157. default: false
  158. },
  159. // 弹窗宽度,默认 60vw
  160. width: {
  161. type: [String, Number],
  162. default: '60vw'
  163. },
  164. // 内容区高度占视口的百分比(60 = 60vh)
  165. boxHeight: {
  166. type: [String, Number],
  167. default: 60
  168. },
  169. // 内容区样式,默认纵向滚动、隐藏横向滚动
  170. bodyStyle: {
  171. type: Object,
  172. default() {
  173. return {
  174. overflowX: 'hidden',
  175. overflowY: 'auto'
  176. }
  177. }
  178. },
  179. // 是否显示全屏按钮
  180. showFullScreen: {
  181. type: Boolean,
  182. default: true
  183. },
  184. // 页签配置,传了才显示页签区
  185. tabs: {
  186. type: Array,
  187. default: () => []
  188. },
  189. // 页签字段映射:{ label, value }
  190. tabKey: {
  191. type: Object,
  192. default: () => ({
  193. label: 'label',
  194. value: 'value'
  195. })
  196. },
  197. // 步骤条配置,传了才显示步骤区
  198. steps: {
  199. type: Array,
  200. default: () => []
  201. },
  202. // 步骤条默认选中下标
  203. stepDefaultActive: {
  204. type: Number,
  205. default: 0
  206. },
  207. // 切换步骤前的校验函数,返回 false 停在当前步
  208. stepValid: {
  209. type: Function,
  210. default: undefined
  211. },
  212. // 确定按钮文案
  213. saveBtnText: {
  214. type: String,
  215. default: window.$l('保存')
  216. }
  217. })
  218. const emit = defineEmits([
  219. 'update:value',
  220. 'handCancel',
  221. 'afterClose',
  222. 'tabsChange',
  223. 'stepCallback',
  224. 'handSave'
  225. ])
  226. const attrs = useAttrs()
  227. /*****拖拽相关*****/
  228. const modalTitleRef = ref(null)
  229. const titleEl = ref()
  230. const { x, y, isDragging } = useDraggable(modalTitleRef)
  231. const startX = ref(0)
  232. const startY = ref(0)
  233. const startedDrag = ref(false)
  234. const transformX = ref(0)
  235. const transformY = ref(0)
  236. const preTransformX = ref(0)
  237. const preTransformY = ref(0)
  238. const dragRect = ref({ left: 0, right: 0, top: 0, bottom: 0 })
  239. /*****************/
  240. const bodySlot = ref(null)
  241. const footerSlot = ref(null)
  242. const modalWidth = ref(props.width)
  243. const fullModal = ref()
  244. const isOpen = ref(props.value)
  245. const spaceSize = ref(8)
  246. // 页签取值字段:tabKey.value 未传时回退到 label,避免所有 pane 的 key 都是 undefined
  247. const tabKeyValue = computed(() => (props.tabKey && props.tabKey.value) || 'value')
  248. // 当前页签受控维护,保证首屏高亮项与 pane 的 key 是同一个值(原来取 label 而 key 取 value,两者不同则首屏不高亮)
  249. const activeTabKey = ref('')
  250. const syncActiveTab = () => {
  251. const list = props.tabs || []
  252. if (!list.length) {
  253. activeTabKey.value = ''
  254. return
  255. }
  256. const field = tabKeyValue.value
  257. if (!list.some((tab) => tab && tab[field] === activeTabKey.value)) {
  258. activeTabKey.value = list[0][field]
  259. }
  260. }
  261. watch(() => props.tabs, syncActiveTab, { immediate: true, deep: true })
  262. const stepActive = ref(0)
  263. const isShowSave = computed(() => {
  264. return stepActive.value === props.steps.length - 1
  265. })
  266. const transformStyle = computed(() => {
  267. return {
  268. transform: \`translate(\${transformX.value}px, \${transformY.value}px)\`
  269. }
  270. })
  271. // 初始不给写死高度,先让弹窗按内容自然高度渲染,测量后再收敛;
  272. // 传 undefined 会让 a-modal 在首帧没有 body 样式,出现高度跳变。
  273. const computeBodyStyle = ref({ ...props.bodyStyle })
  274. // 最近一次写入的高度,测量结果没变就不重复写,防止 ResizeObserver 自激死循环
  275. let lastMeasuredHeight = -1
  276. let rafId = null
  277. let ro = null
  278. // 全屏时把标题 DOM 从拖拽目标上摘掉,实现「全屏不可拖动」
  279. const setTitleRef = (el) => {
  280. titleEl.value = el
  281. modalTitleRef.value = fullModal.value ? null : el
  282. }
  283. /**
  284. * 测量弹窗内容高度并写回 bodyStyle
  285. * 关键点:
  286. * 1) 用 scrollHeight 而不是 clientHeight —— 我们自己写死了 height 后 clientHeight 会被自己夹住,
  287. * 只有 scrollHeight 才是内容的真实高度;
  288. * 2) 高度为 0 时直接跳过 —— a-modal 首次打开时内容还没布局完,clientHeight 为 0,
  289. * 此时若写回 height:0px 会把弹窗压成一片空白(内容其实在 DOM 里,只是被压没了)。
  290. */
  291. const measureBody = () => {
  292. const bodyDom = bodySlot.value
  293. if (!bodyDom) return
  294. const headerDom = modalTitleRef.value
  295. const footerDom = footerSlot.value
  296. const bodyRealHeight = bodyDom.scrollHeight || bodyDom.clientHeight
  297. if (!bodyRealHeight) return
  298. const headerRealHeight = headerDom?.clientHeight || 0
  299. const footerRealHeight = footerDom?.clientHeight || 0
  300. const windowHeight = document.body.offsetHeight
  301. //后面增加数值的构成 model padding 上下20 + 20 header和body之间 25 footer和body之间 20
  302. const realHeight =
  303. bodyRealHeight + headerRealHeight + footerRealHeight + 85 >= windowHeight
  304. ? windowHeight - headerRealHeight - footerRealHeight - 82
  305. : bodyRealHeight
  306. if (realHeight <= 0 || realHeight === lastMeasuredHeight) return
  307. lastMeasuredHeight = realHeight
  308. computeBodyStyle.value = Object.assign({ height: \`\${realHeight}px\` }, props.bodyStyle)
  309. }
  310. // 等一帧再量:DOM 更新完 + 浏览器完成一次布局,clientHeight/scrollHeight 才是准的
  311. const scheduleMeasure = () => {
  312. nextTick(() => {
  313. if (rafId) return
  314. rafId = requestAnimationFrame(() => {
  315. rafId = null
  316. measureBody()
  317. })
  318. })
  319. }
  320. // 全屏
  321. const handleFullScreen = () => {
  322. modalWidth.value = '100vw'
  323. fullModal.value = 's-full-modal'
  324. modalTitleRef.value = null
  325. transformX.value = 0
  326. transformY.value = 0
  327. }
  328. // 还原
  329. const handleReduction = () => {
  330. modalWidth.value = props.width
  331. fullModal.value = ''
  332. modalTitleRef.value = titleEl.value
  333. }
  334. const closeModal = () => {
  335. emit('update:value', false)
  336. emit('handCancel')
  337. }
  338. /**
  339. * @description: Modal 完全关闭后的回调
  340. */
  341. const handleAfterClose = () => {
  342. fullModal.value = ''
  343. modalTitleRef.value = titleEl.value
  344. modalWidth.value = props.width
  345. emit('afterClose')
  346. }
  347. const handTabChange = (e) => {
  348. activeTabKey.value = e
  349. emit('tabsChange', e)
  350. }
  351. function handPrev() {
  352. stepActive.value--
  353. emit('stepCallback', 'prev', stepActive.value)
  354. }
  355. function handNext() {
  356. if (!window.$utils.isFunction(props.stepValid)) {
  357. stepActive.value++
  358. emit('stepCallback', 'next', stepActive.value)
  359. return
  360. }
  361. props.stepValid(stepActive.value).then((res) => {
  362. if (res) {
  363. stepActive.value++
  364. emit('stepCallback', 'next', stepActive.value)
  365. }
  366. })
  367. }
  368. function handSave() {
  369. emit('handSave', 'save')
  370. }
  371. watchEffect(() => {
  372. if (startedDrag.value) {
  373. transformX.value =
  374. preTransformX.value +
  375. Math.min(Math.max(dragRect.value.left, x.value), dragRect.value.right) -
  376. startX.value
  377. transformY.value =
  378. preTransformY.value +
  379. Math.min(Math.max(dragRect.value.top, y.value), dragRect.value.bottom) -
  380. startY.value
  381. }
  382. })
  383. watch([x, y], () => {
  384. if (!startedDrag.value) {
  385. startX.value = x.value
  386. startY.value = y.value
  387. const bodyRect = document.body.getBoundingClientRect()
  388. const titleRect = modalTitleRef.value?.getBoundingClientRect() ?? { width: 0, height: 0 }
  389. dragRect.value.right = bodyRect.width - titleRect.width
  390. dragRect.value.bottom = bodyRect.height - titleRect.height
  391. preTransformX.value = transformX.value
  392. preTransformY.value = transformY.value
  393. }
  394. startedDrag.value = true
  395. })
  396. watch(isDragging, () => {
  397. if (!isDragging.value) {
  398. startedDrag.value = false
  399. }
  400. })
  401. // 内容异步变高(如 s-tree-select 拉到数据后展开)时重新测量
  402. watch(bodySlot, (el) => {
  403. if (ro) {
  404. ro.disconnect()
  405. ro = null
  406. }
  407. if (el && typeof ResizeObserver !== 'undefined') {
  408. ro = new ResizeObserver(() => scheduleMeasure())
  409. ro.observe(el)
  410. }
  411. scheduleMeasure()
  412. })
  413. watch(
  414. () => props.value,
  415. (newVal) => {
  416. if (newVal) {
  417. isOpen.value = true
  418. // 打开时先按上一次的高度渲染,内容稳定后再测量,避免首帧压成 0 高度空白
  419. scheduleMeasure()
  420. } else {
  421. isOpen.value = false
  422. }
  423. },
  424. { deep: true }
  425. )
  426. // 初始 props.value 已为 true 时(非 immediate,靠下面的 immediate 分支兜底)
  427. watch(
  428. () => isOpen.value,
  429. (val) => {
  430. if (val) scheduleMeasure()
  431. }
  432. )
  433. // 组件挂载时 props.value 就已是 true(父组件用 v-if 挂载 + 立即置 true),
  434. // 上面的 watch 没有变化可监听,这里用 immediate 主动测一次
  435. watch(
  436. () => props.value,
  437. () => {
  438. if (props.value) scheduleMeasure()
  439. },
  440. { immediate: true }
  441. )
  442. onBeforeUnmount(() => {
  443. if (ro) {
  444. ro.disconnect()
  445. ro = null
  446. }
  447. if (rafId) {
  448. cancelAnimationFrame(rafId)
  449. rafId = null
  450. }
  451. })
  452. <\/script>
  453. <style lang="less">
  454. @import url('./common.less');
  455. .s-modal {
  456. .ant-modal-content {
  457. padding: 10px 20px;
  458. position: relative;
  459. }
  460. .ant-modal-close {
  461. margin-top: -5px;
  462. }
  463. .ant-modal-header {
  464. margin-bottom: 0;
  465. padding-bottom: 12px;
  466. &::after {
  467. content: '';
  468. display: block;
  469. width: 100%;
  470. border-bottom: 1px solid var(--s-border-color-split);
  471. position: absolute;
  472. top: 44px;
  473. left: 0;
  474. z-index: 2;
  475. }
  476. }
  477. .ant-modal-footer {
  478. padding-top: 4px;
  479. margin-top: 10px;
  480. &::before {
  481. content: '';
  482. display: block;
  483. width: 100%;
  484. border-bottom: 1px solid var(--s-border-color-split);
  485. position: absolute;
  486. bottom: 54px;
  487. left: 0;
  488. z-index: 2;
  489. }
  490. }
  491. }
  492. </style>
  493. <style scoped lang="less">
  494. .draggableTitle {
  495. width: 100%;
  496. cursor: move;
  497. }
  498. :global(.ant-modal-root .ant-modal-wrap) {
  499. overflow: hidden;
  500. }
  501. :deep(.ant-modal-body)::-webkit-scrollbar {
  502. width: 0px;
  503. height: 0px;
  504. padding: 0px;
  505. }
  506. :deep(.ant-modal-body)::-webkit-scrollbar-thumb {
  507. background: var(--s-border-color-base);
  508. border-radius: 3px;
  509. }
  510. :deep(.ant-modal-body)::-webkit-scrollbar-track {
  511. border-radius: 3px;
  512. }
  513. .step-wrap {
  514. width: 100%;
  515. flex: 0 0 auto;
  516. background-color: var(--s-bg-color-container);
  517. border-bottom: 1px solid var(--s-border-color-split);
  518. margin-bottom: 16px;
  519. }
  520. .tabs-wrap {
  521. flex: 0 0 auto;
  522. }
  523. .modal-body-container {
  524. max-height: 75vh;
  525. overflow: hidden auto;
  526. padding-bottom: 10px;
  527. }
  528. </style>
  529. `;
  530. export {
  531. n as default
  532. };