SModal2.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. const e = `<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. defaultFullScreen: {
  186. type: Boolean,
  187. default: false
  188. },
  189. // 页签配置,传了才显示页签区
  190. tabs: {
  191. type: Array,
  192. default: () => []
  193. },
  194. // 页签字段映射:{ label, value }
  195. tabKey: {
  196. type: Object,
  197. default: () => ({
  198. label: 'label',
  199. value: 'value'
  200. })
  201. },
  202. // 步骤条配置,传了才显示步骤区
  203. steps: {
  204. type: Array,
  205. default: () => []
  206. },
  207. // 步骤条默认选中下标
  208. stepDefaultActive: {
  209. type: Number,
  210. default: 0
  211. },
  212. // 切换步骤前的校验函数,返回 false 停在当前步
  213. stepValid: {
  214. type: Function,
  215. default: undefined
  216. },
  217. // 确定按钮文案
  218. saveBtnText: {
  219. type: String,
  220. default: window.$l('保存')
  221. }
  222. })
  223. const emit = defineEmits([
  224. 'update:value',
  225. 'handCancel',
  226. 'afterClose',
  227. 'tabsChange',
  228. 'stepCallback',
  229. 'handSave'
  230. ])
  231. const attrs = useAttrs()
  232. /*****拖拽相关*****/
  233. const modalTitleRef = ref(null)
  234. const titleEl = ref()
  235. const { x, y, isDragging } = useDraggable(modalTitleRef)
  236. const startX = ref(0)
  237. const startY = ref(0)
  238. const startedDrag = ref(false)
  239. const transformX = ref(0)
  240. const transformY = ref(0)
  241. const preTransformX = ref(0)
  242. const preTransformY = ref(0)
  243. const dragRect = ref({ left: 0, right: 0, top: 0, bottom: 0 })
  244. /*****************/
  245. const bodySlot = ref(null)
  246. const footerSlot = ref(null)
  247. const modalWidth = ref(props.defaultFullScreen ? '100vw' : props.width)
  248. const fullModal = ref(props.defaultFullScreen ? 's-full-modal' : '')
  249. const isOpen = ref(props.value)
  250. const spaceSize = ref(8)
  251. // 页签取值字段:tabKey.value 未传时回退到 label,避免所有 pane 的 key 都是 undefined
  252. const tabKeyValue = computed(() => (props.tabKey && props.tabKey.value) || 'value')
  253. // 当前页签受控维护,保证首屏高亮项与 pane 的 key 是同一个值(原来取 label 而 key 取 value,两者不同则首屏不高亮)
  254. const activeTabKey = ref('')
  255. const syncActiveTab = () => {
  256. const list = props.tabs || []
  257. if (!list.length) {
  258. activeTabKey.value = ''
  259. return
  260. }
  261. const field = tabKeyValue.value
  262. if (!list.some((tab) => tab && tab[field] === activeTabKey.value)) {
  263. activeTabKey.value = list[0][field]
  264. }
  265. }
  266. watch(() => props.tabs, syncActiveTab, { immediate: true, deep: true })
  267. const stepActive = ref(0)
  268. const isShowSave = computed(() => {
  269. return stepActive.value === props.steps.length - 1
  270. })
  271. const transformStyle = computed(() => {
  272. return {
  273. transform: \`translate(\${transformX.value}px, \${transformY.value}px)\`
  274. }
  275. })
  276. // 初始不给写死高度,先让弹窗按内容自然高度渲染,测量后再收敛;
  277. // 传 undefined 会让 a-modal 在首帧没有 body 样式,出现高度跳变。
  278. const computeBodyStyle = ref({ ...props.bodyStyle })
  279. // 最近一次写入的高度,测量结果没变就不重复写,防止 ResizeObserver 自激死循环
  280. let lastMeasuredHeight = -1
  281. let rafId = null
  282. let ro = null
  283. // 全屏时把标题 DOM 从拖拽目标上摘掉,实现「全屏不可拖动」
  284. const setTitleRef = (el) => {
  285. titleEl.value = el
  286. modalTitleRef.value = fullModal.value ? null : el
  287. }
  288. /**
  289. * 测量弹窗内容高度并写回 bodyStyle
  290. * 关键点:
  291. * 1) 用 scrollHeight 而不是 clientHeight —— 我们自己写死了 height 后 clientHeight 会被自己夹住,
  292. * 只有 scrollHeight 才是内容的真实高度;
  293. * 2) 高度为 0 时直接跳过 —— a-modal 首次打开时内容还没布局完,clientHeight 为 0,
  294. * 此时若写回 height:0px 会把弹窗压成一片空白(内容其实在 DOM 里,只是被压没了)。
  295. */
  296. const measureBody = () => {
  297. const bodyDom = bodySlot.value
  298. if (!bodyDom) return
  299. const headerDom = modalTitleRef.value
  300. const footerDom = footerSlot.value
  301. const bodyRealHeight = bodyDom.scrollHeight || bodyDom.clientHeight
  302. if (!bodyRealHeight) return
  303. const headerRealHeight = headerDom?.clientHeight || 0
  304. const footerRealHeight = footerDom?.clientHeight || 0
  305. const windowHeight = document.body.offsetHeight
  306. //后面增加数值的构成 model padding 上下20 + 20 header和body之间 25 footer和body之间 20
  307. const realHeight =
  308. bodyRealHeight + headerRealHeight + footerRealHeight + 85 >= windowHeight
  309. ? windowHeight - headerRealHeight - footerRealHeight - 82
  310. : bodyRealHeight
  311. if (realHeight <= 0 || realHeight === lastMeasuredHeight) return
  312. lastMeasuredHeight = realHeight
  313. computeBodyStyle.value = Object.assign({ height: \`\${realHeight}px\` }, props.bodyStyle)
  314. }
  315. // 等一帧再量:DOM 更新完 + 浏览器完成一次布局,clientHeight/scrollHeight 才是准的
  316. const scheduleMeasure = () => {
  317. nextTick(() => {
  318. if (rafId) return
  319. rafId = requestAnimationFrame(() => {
  320. rafId = null
  321. measureBody()
  322. })
  323. })
  324. }
  325. // 全屏 / 还原 状态统一切换,保证两处入口行为一致
  326. const setFullScreenState = (full) => {
  327. if (full) {
  328. modalWidth.value = '100vw'
  329. fullModal.value = 's-full-modal'
  330. modalTitleRef.value = null
  331. transformX.value = 0
  332. transformY.value = 0
  333. } else {
  334. modalWidth.value = props.width
  335. fullModal.value = ''
  336. modalTitleRef.value = titleEl.value
  337. }
  338. }
  339. // 全屏
  340. const handleFullScreen = () => {
  341. setFullScreenState(true)
  342. }
  343. // 还原
  344. const handleReduction = () => {
  345. setFullScreenState(false)
  346. }
  347. const closeModal = () => {
  348. emit('update:value', false)
  349. emit('handCancel')
  350. }
  351. /**
  352. * @description: Modal 完全关闭后的回调
  353. */
  354. const handleAfterClose = () => {
  355. // 关闭后回到「默认状态」:配了 defaultFullScreen 就还是全屏
  356. setFullScreenState(props.defaultFullScreen)
  357. emit('afterClose')
  358. }
  359. // 属性运行中变化时同步当前展示状态
  360. watch(
  361. () => props.defaultFullScreen,
  362. (val) => setFullScreenState(val)
  363. )
  364. const handTabChange = (e) => {
  365. activeTabKey.value = e
  366. emit('tabsChange', e)
  367. }
  368. function handPrev() {
  369. stepActive.value--
  370. emit('stepCallback', 'prev', stepActive.value)
  371. }
  372. function handNext() {
  373. if (!window.$utils.isFunction(props.stepValid)) {
  374. stepActive.value++
  375. emit('stepCallback', 'next', stepActive.value)
  376. return
  377. }
  378. props.stepValid(stepActive.value).then((res) => {
  379. if (res) {
  380. stepActive.value++
  381. emit('stepCallback', 'next', stepActive.value)
  382. }
  383. })
  384. }
  385. function handSave() {
  386. emit('handSave', 'save')
  387. }
  388. watchEffect(() => {
  389. if (startedDrag.value) {
  390. transformX.value =
  391. preTransformX.value +
  392. Math.min(Math.max(dragRect.value.left, x.value), dragRect.value.right) -
  393. startX.value
  394. transformY.value =
  395. preTransformY.value +
  396. Math.min(Math.max(dragRect.value.top, y.value), dragRect.value.bottom) -
  397. startY.value
  398. }
  399. })
  400. watch([x, y], () => {
  401. if (!startedDrag.value) {
  402. startX.value = x.value
  403. startY.value = y.value
  404. const bodyRect = document.body.getBoundingClientRect()
  405. const titleRect = modalTitleRef.value?.getBoundingClientRect() ?? { width: 0, height: 0 }
  406. dragRect.value.right = bodyRect.width - titleRect.width
  407. dragRect.value.bottom = bodyRect.height - titleRect.height
  408. preTransformX.value = transformX.value
  409. preTransformY.value = transformY.value
  410. }
  411. startedDrag.value = true
  412. })
  413. watch(isDragging, () => {
  414. if (!isDragging.value) {
  415. startedDrag.value = false
  416. }
  417. })
  418. // 内容异步变高(如 s-tree-select 拉到数据后展开)时重新测量
  419. watch(bodySlot, (el) => {
  420. if (ro) {
  421. ro.disconnect()
  422. ro = null
  423. }
  424. if (el && typeof ResizeObserver !== 'undefined') {
  425. ro = new ResizeObserver(() => scheduleMeasure())
  426. ro.observe(el)
  427. }
  428. scheduleMeasure()
  429. })
  430. watch(
  431. () => props.value,
  432. (newVal) => {
  433. if (newVal) {
  434. isOpen.value = true
  435. // 打开时先按上一次的高度渲染,内容稳定后再测量,避免首帧压成 0 高度空白
  436. scheduleMeasure()
  437. } else {
  438. isOpen.value = false
  439. }
  440. },
  441. { deep: true }
  442. )
  443. // 初始 props.value 已为 true 时(非 immediate,靠下面的 immediate 分支兜底)
  444. watch(
  445. () => isOpen.value,
  446. (val) => {
  447. if (val) scheduleMeasure()
  448. }
  449. )
  450. // 组件挂载时 props.value 就已是 true(父组件用 v-if 挂载 + 立即置 true),
  451. // 上面的 watch 没有变化可监听,这里用 immediate 主动测一次
  452. watch(
  453. () => props.value,
  454. () => {
  455. if (props.value) scheduleMeasure()
  456. },
  457. { immediate: true }
  458. )
  459. onBeforeUnmount(() => {
  460. if (ro) {
  461. ro.disconnect()
  462. ro = null
  463. }
  464. if (rafId) {
  465. cancelAnimationFrame(rafId)
  466. rafId = null
  467. }
  468. })
  469. <\/script>
  470. <style lang="less">
  471. @import url('./common.less');
  472. .s-modal {
  473. .ant-modal-content {
  474. padding: 10px 20px;
  475. position: relative;
  476. }
  477. .ant-modal-close {
  478. margin-top: -5px;
  479. }
  480. .ant-modal-header {
  481. margin-bottom: 0;
  482. padding-bottom: 12px;
  483. &::after {
  484. content: '';
  485. display: block;
  486. width: 100%;
  487. border-bottom: 1px solid var(--s-border-color-split);
  488. position: absolute;
  489. top: 44px;
  490. left: 0;
  491. z-index: 2;
  492. }
  493. }
  494. .ant-modal-footer {
  495. padding-top: 4px;
  496. margin-top: 10px;
  497. &::before {
  498. content: '';
  499. display: block;
  500. width: 100%;
  501. border-bottom: 1px solid var(--s-border-color-split);
  502. position: absolute;
  503. bottom: 54px;
  504. left: 0;
  505. z-index: 2;
  506. }
  507. }
  508. }
  509. </style>
  510. <style scoped lang="less">
  511. .draggableTitle {
  512. width: 100%;
  513. cursor: move;
  514. }
  515. :global(.ant-modal-root .ant-modal-wrap) {
  516. overflow: hidden;
  517. }
  518. :deep(.ant-modal-body)::-webkit-scrollbar {
  519. width: 0px;
  520. height: 0px;
  521. padding: 0px;
  522. }
  523. :deep(.ant-modal-body)::-webkit-scrollbar-thumb {
  524. background: var(--s-border-color-base);
  525. border-radius: 3px;
  526. }
  527. :deep(.ant-modal-body)::-webkit-scrollbar-track {
  528. border-radius: 3px;
  529. }
  530. .step-wrap {
  531. width: 100%;
  532. flex: 0 0 auto;
  533. background-color: var(--s-bg-color-container);
  534. border-bottom: 1px solid var(--s-border-color-split);
  535. margin-bottom: 16px;
  536. }
  537. .tabs-wrap {
  538. flex: 0 0 auto;
  539. }
  540. .modal-body-container {
  541. max-height: 75vh;
  542. overflow: hidden auto;
  543. padding-bottom: 10px;
  544. }
  545. </style>
  546. `;
  547. export {
  548. e as default
  549. };