| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- const n = `<template>
- <!-- v-bind处理a-table 传递过来的参数,你们开发的时候不用再子组件中接收这么ant原有的参数-->
- <a-modal
- :open="isOpen"
- :body-style="computeBodyStyle"
- :width="modalWidth"
- :centered="centered"
- @cancel="closeModal"
- :afterClose="handleAfterClose"
- :destroyOnClose="destroyOnClose"
- :keyboard="keyboardOpen"
- :style="topStyle"
- :maskClosable="maskClosable"
- :mask="mask"
- :wrap-class-name="'s-modal ' + fullModal"
- v-bind="attrs"
- >
- <template #title>
- <div class="modalHeader">
- <!-- 标题的ref用于控制是否可以拖拽-->
- <div :ref="setTitleRef" class="draggableTitle">{{ title }}</div>
- <!-- 弹框头部的标题,以及全屏的按钮-->
- <ModalHeader
- :width="modalWidth"
- :showFullScreen="showFullScreen"
- @fullScreen="handleFullScreen"
- @reduction="handleReduction"
- />
- </div>
- </template>
- <a-spin :spinning="loading">
- <!-- 弹框内容区域-->
- <div ref="bodySlot">
- <div class="tabs-wrap" v-if="tabs && tabs.length > 0">
- <a-tabs :active-key="activeTabKey" @change="handTabChange">
- <a-tab-pane
- :tab="tab[tabKey.label]"
- v-for="tab in tabs"
- :key="tab[tabKeyValue]"
- ></a-tab-pane>
- </a-tabs>
- </div>
- <div class="step-wrap" ref="stepRef" v-if="steps && steps.length">
- <a-steps :current="stepActive" type="navigation" size="small">
- <a-step :title="step" v-for="step in steps" :key="step" :disabled="true" />
- </a-steps>
- </div>
- <div class="modal-body-container">
- <slot></slot>
- </div>
- </div>
- </a-spin>
- <!-- 弹框底部按钮区域-->
- <template #footer>
- <div class="footer-top-border"></div>
- <div ref="footerSlot" v-if="$slots.footer">
- <a-space :size="spaceSize">
- <slot name="footer"></slot>
- </a-space>
- </div>
- <div ref="footerSlot" v-if="!$slots.footer && (!steps || !steps.length)">
- <a-space :size="spaceSize">
- <a-button @click="closeModal"><poweroff-outlined />{{ $l('关闭') }} </a-button>
- <a-button type="primary" @click="handSave">
- <save-outlined />
- {{ $l(saveBtnText) }}
- </a-button>
- </a-space>
- </div>
- <div ref="footerSlot" v-if="steps && steps.length > 0">
- <a-space :size="spaceSize">
- <a-button @click="closeModal"><poweroff-outlined />{{ $l('关闭') }} </a-button>
- <a-button
- v-if="steps.length"
- :disabled="stepActive === stepDefaultActive"
- @click="handPrev"
- >
- <arrow-left-outlined />{{ $l('上一步') }}
- </a-button>
- <a-button
- type="primary"
- v-if="steps.length"
- :disabled="stepActive === steps.length - 1"
- @click="handNext"
- >
- <arrow-right-outlined />{{ $l('下一步') }}
- </a-button>
- <a-button v-if="isShowSave" style="margin-right: 8px" type="primary" @click="handSave">
- <save-outlined />
- {{ $l(saveBtnText) }}
- </a-button>
- </a-space>
- </div>
- </template>
- <!-- 实现是否拖拽弹框计算的核心代码-->
- <template #modalRender="{ originVNode }">
- <div :style="transformStyle">
- <component :is="originVNode" />
- </div>
- </template>
- </a-modal>
- </template>
- <script setup>
- import { ref, computed, watch, watchEffect, useAttrs, nextTick, onBeforeUnmount } from 'vue'
- import { useDraggable } from '@vueuse/core'
- import ModalHeader from './components/ModalHeader.vue'
- defineOptions({ name: 'SModal' })
- const props = defineProps({
- // 确定按钮 loading,保存中禁止关闭
- loading: {
- type: Boolean,
- default: false
- },
- // 是否垂直居中显示
- centered: {
- type: Boolean,
- default: true
- },
- // 点遮罩是否关闭
- maskClosable: {
- type: Boolean,
- default: true
- },
- // 是否显示遮罩
- mask: {
- type: Boolean,
- default: true
- },
- // 非居中时的距顶距离
- topStyle: {
- type: String,
- default: ''
- },
- // 是否显示右上角关闭按钮
- closable: {
- type: Boolean,
- default: true
- },
- // 关闭后是否销毁内容
- destroyOnClose: {
- type: Boolean,
- default: false
- },
- // 是否支持 Esc 关闭
- keyboardOpen: {
- type: Boolean,
- default: true
- },
- // 弹窗标题
- title: {
- type: String,
- default: ''
- },
- // 显隐(v-model:value)
- value: {
- type: Boolean,
- default: false
- },
- // 弹窗宽度,默认 60vw
- width: {
- type: [String, Number],
- default: '60vw'
- },
- // 内容区高度占视口的百分比(60 = 60vh)
- boxHeight: {
- type: [String, Number],
- default: 60
- },
- // 内容区样式,默认纵向滚动、隐藏横向滚动
- bodyStyle: {
- type: Object,
- default() {
- return {
- overflowX: 'hidden',
- overflowY: 'auto'
- }
- }
- },
- // 是否显示全屏按钮
- showFullScreen: {
- type: Boolean,
- default: true
- },
- // 页签配置,传了才显示页签区
- tabs: {
- type: Array,
- default: () => []
- },
- // 页签字段映射:{ label, value }
- tabKey: {
- type: Object,
- default: () => ({
- label: 'label',
- value: 'value'
- })
- },
- // 步骤条配置,传了才显示步骤区
- steps: {
- type: Array,
- default: () => []
- },
- // 步骤条默认选中下标
- stepDefaultActive: {
- type: Number,
- default: 0
- },
- // 切换步骤前的校验函数,返回 false 停在当前步
- stepValid: {
- type: Function,
- default: undefined
- },
- // 确定按钮文案
- saveBtnText: {
- type: String,
- default: window.$l('保存')
- }
- })
- const emit = defineEmits([
- 'update:value',
- 'handCancel',
- 'afterClose',
- 'tabsChange',
- 'stepCallback',
- 'handSave'
- ])
- const attrs = useAttrs()
- /*****拖拽相关*****/
- const modalTitleRef = ref(null)
- const titleEl = ref()
- const { x, y, isDragging } = useDraggable(modalTitleRef)
- const startX = ref(0)
- const startY = ref(0)
- const startedDrag = ref(false)
- const transformX = ref(0)
- const transformY = ref(0)
- const preTransformX = ref(0)
- const preTransformY = ref(0)
- const dragRect = ref({ left: 0, right: 0, top: 0, bottom: 0 })
- /*****************/
- const bodySlot = ref(null)
- const footerSlot = ref(null)
- const modalWidth = ref(props.width)
- const fullModal = ref()
- const isOpen = ref(props.value)
- const spaceSize = ref(8)
- // 页签取值字段:tabKey.value 未传时回退到 label,避免所有 pane 的 key 都是 undefined
- const tabKeyValue = computed(() => (props.tabKey && props.tabKey.value) || 'value')
- // 当前页签受控维护,保证首屏高亮项与 pane 的 key 是同一个值(原来取 label 而 key 取 value,两者不同则首屏不高亮)
- const activeTabKey = ref('')
- const syncActiveTab = () => {
- const list = props.tabs || []
- if (!list.length) {
- activeTabKey.value = ''
- return
- }
- const field = tabKeyValue.value
- if (!list.some((tab) => tab && tab[field] === activeTabKey.value)) {
- activeTabKey.value = list[0][field]
- }
- }
- watch(() => props.tabs, syncActiveTab, { immediate: true, deep: true })
- const stepActive = ref(0)
- const isShowSave = computed(() => {
- return stepActive.value === props.steps.length - 1
- })
- const transformStyle = computed(() => {
- return {
- transform: \`translate(\${transformX.value}px, \${transformY.value}px)\`
- }
- })
- // 初始不给写死高度,先让弹窗按内容自然高度渲染,测量后再收敛;
- // 传 undefined 会让 a-modal 在首帧没有 body 样式,出现高度跳变。
- const computeBodyStyle = ref({ ...props.bodyStyle })
- // 最近一次写入的高度,测量结果没变就不重复写,防止 ResizeObserver 自激死循环
- let lastMeasuredHeight = -1
- let rafId = null
- let ro = null
- // 全屏时把标题 DOM 从拖拽目标上摘掉,实现「全屏不可拖动」
- const setTitleRef = (el) => {
- titleEl.value = el
- modalTitleRef.value = fullModal.value ? null : el
- }
- /**
- * 测量弹窗内容高度并写回 bodyStyle
- * 关键点:
- * 1) 用 scrollHeight 而不是 clientHeight —— 我们自己写死了 height 后 clientHeight 会被自己夹住,
- * 只有 scrollHeight 才是内容的真实高度;
- * 2) 高度为 0 时直接跳过 —— a-modal 首次打开时内容还没布局完,clientHeight 为 0,
- * 此时若写回 height:0px 会把弹窗压成一片空白(内容其实在 DOM 里,只是被压没了)。
- */
- const measureBody = () => {
- const bodyDom = bodySlot.value
- if (!bodyDom) return
- const headerDom = modalTitleRef.value
- const footerDom = footerSlot.value
- const bodyRealHeight = bodyDom.scrollHeight || bodyDom.clientHeight
- if (!bodyRealHeight) return
- const headerRealHeight = headerDom?.clientHeight || 0
- const footerRealHeight = footerDom?.clientHeight || 0
- const windowHeight = document.body.offsetHeight
- //后面增加数值的构成 model padding 上下20 + 20 header和body之间 25 footer和body之间 20
- const realHeight =
- bodyRealHeight + headerRealHeight + footerRealHeight + 85 >= windowHeight
- ? windowHeight - headerRealHeight - footerRealHeight - 82
- : bodyRealHeight
- if (realHeight <= 0 || realHeight === lastMeasuredHeight) return
- lastMeasuredHeight = realHeight
- computeBodyStyle.value = Object.assign({ height: \`\${realHeight}px\` }, props.bodyStyle)
- }
- // 等一帧再量:DOM 更新完 + 浏览器完成一次布局,clientHeight/scrollHeight 才是准的
- const scheduleMeasure = () => {
- nextTick(() => {
- if (rafId) return
- rafId = requestAnimationFrame(() => {
- rafId = null
- measureBody()
- })
- })
- }
- // 全屏
- const handleFullScreen = () => {
- modalWidth.value = '100vw'
- fullModal.value = 's-full-modal'
- modalTitleRef.value = null
- transformX.value = 0
- transformY.value = 0
- }
- // 还原
- const handleReduction = () => {
- modalWidth.value = props.width
- fullModal.value = ''
- modalTitleRef.value = titleEl.value
- }
- const closeModal = () => {
- emit('update:value', false)
- emit('handCancel')
- }
- /**
- * @description: Modal 完全关闭后的回调
- */
- const handleAfterClose = () => {
- fullModal.value = ''
- modalTitleRef.value = titleEl.value
- modalWidth.value = props.width
- emit('afterClose')
- }
- const handTabChange = (e) => {
- activeTabKey.value = e
- emit('tabsChange', e)
- }
- function handPrev() {
- stepActive.value--
- emit('stepCallback', 'prev', stepActive.value)
- }
- function handNext() {
- if (!window.$utils.isFunction(props.stepValid)) {
- stepActive.value++
- emit('stepCallback', 'next', stepActive.value)
- return
- }
- props.stepValid(stepActive.value).then((res) => {
- if (res) {
- stepActive.value++
- emit('stepCallback', 'next', stepActive.value)
- }
- })
- }
- function handSave() {
- emit('handSave', 'save')
- }
- watchEffect(() => {
- if (startedDrag.value) {
- transformX.value =
- preTransformX.value +
- Math.min(Math.max(dragRect.value.left, x.value), dragRect.value.right) -
- startX.value
- transformY.value =
- preTransformY.value +
- Math.min(Math.max(dragRect.value.top, y.value), dragRect.value.bottom) -
- startY.value
- }
- })
- watch([x, y], () => {
- if (!startedDrag.value) {
- startX.value = x.value
- startY.value = y.value
- const bodyRect = document.body.getBoundingClientRect()
- const titleRect = modalTitleRef.value?.getBoundingClientRect() ?? { width: 0, height: 0 }
- dragRect.value.right = bodyRect.width - titleRect.width
- dragRect.value.bottom = bodyRect.height - titleRect.height
- preTransformX.value = transformX.value
- preTransformY.value = transformY.value
- }
- startedDrag.value = true
- })
- watch(isDragging, () => {
- if (!isDragging.value) {
- startedDrag.value = false
- }
- })
- // 内容异步变高(如 s-tree-select 拉到数据后展开)时重新测量
- watch(bodySlot, (el) => {
- if (ro) {
- ro.disconnect()
- ro = null
- }
- if (el && typeof ResizeObserver !== 'undefined') {
- ro = new ResizeObserver(() => scheduleMeasure())
- ro.observe(el)
- }
- scheduleMeasure()
- })
- watch(
- () => props.value,
- (newVal) => {
- if (newVal) {
- isOpen.value = true
- // 打开时先按上一次的高度渲染,内容稳定后再测量,避免首帧压成 0 高度空白
- scheduleMeasure()
- } else {
- isOpen.value = false
- }
- },
- { deep: true }
- )
- // 初始 props.value 已为 true 时(非 immediate,靠下面的 immediate 分支兜底)
- watch(
- () => isOpen.value,
- (val) => {
- if (val) scheduleMeasure()
- }
- )
- // 组件挂载时 props.value 就已是 true(父组件用 v-if 挂载 + 立即置 true),
- // 上面的 watch 没有变化可监听,这里用 immediate 主动测一次
- watch(
- () => props.value,
- () => {
- if (props.value) scheduleMeasure()
- },
- { immediate: true }
- )
- onBeforeUnmount(() => {
- if (ro) {
- ro.disconnect()
- ro = null
- }
- if (rafId) {
- cancelAnimationFrame(rafId)
- rafId = null
- }
- })
- <\/script>
- <style lang="less">
- @import url('./common.less');
- .s-modal {
- .ant-modal-content {
- padding: 10px 20px;
- position: relative;
- }
- .ant-modal-close {
- margin-top: -5px;
- }
- .ant-modal-header {
- margin-bottom: 0;
- padding-bottom: 12px;
- &::after {
- content: '';
- display: block;
- width: 100%;
- border-bottom: 1px solid var(--s-border-color-split);
- position: absolute;
- top: 44px;
- left: 0;
- z-index: 2;
- }
- }
- .ant-modal-footer {
- padding-top: 4px;
- margin-top: 10px;
- &::before {
- content: '';
- display: block;
- width: 100%;
- border-bottom: 1px solid var(--s-border-color-split);
- position: absolute;
- bottom: 54px;
- left: 0;
- z-index: 2;
- }
- }
- }
- </style>
- <style scoped lang="less">
- .draggableTitle {
- width: 100%;
- cursor: move;
- }
- :global(.ant-modal-root .ant-modal-wrap) {
- overflow: hidden;
- }
- :deep(.ant-modal-body)::-webkit-scrollbar {
- width: 0px;
- height: 0px;
- padding: 0px;
- }
- :deep(.ant-modal-body)::-webkit-scrollbar-thumb {
- background: var(--s-border-color-base);
- border-radius: 3px;
- }
- :deep(.ant-modal-body)::-webkit-scrollbar-track {
- border-radius: 3px;
- }
- .step-wrap {
- width: 100%;
- flex: 0 0 auto;
- background-color: var(--s-bg-color-container);
- border-bottom: 1px solid var(--s-border-color-split);
- margin-bottom: 16px;
- }
- .tabs-wrap {
- flex: 0 0 auto;
- }
- .modal-body-container {
- max-height: 75vh;
- overflow: hidden auto;
- padding-bottom: 10px;
- }
- </style>
- `;
- export {
- n as default
- };
|