Commit 946f91f3 by yuhengshen Committed by GitHub

feat: optimize modal dragging range(#6414)

* 当弹窗指定了容器时,拖拽将被限制在容器范围内
1 parent 986eacae
...@@ -105,10 +105,17 @@ const shouldDraggable = computed( ...@@ -105,10 +105,17 @@ const shouldDraggable = computed(
() => draggable.value && !shouldFullscreen.value && header.value, () => draggable.value && !shouldFullscreen.value && header.value,
); );
const getAppendTo = computed(() => {
return appendToMain.value
? `#${ELEMENT_ID_MAIN_CONTENT}>div:not(.absolute)>div`
: undefined;
});
const { dragging, transform } = useModalDraggable( const { dragging, transform } = useModalDraggable(
dialogRef, dialogRef,
headerRef, headerRef,
shouldDraggable, shouldDraggable,
getAppendTo,
); );
const firstOpened = ref(false); const firstOpened = ref(false);
...@@ -198,11 +205,6 @@ function handleFocusOutside(e: Event) { ...@@ -198,11 +205,6 @@ function handleFocusOutside(e: Event) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
} }
const getAppendTo = computed(() => {
return appendToMain.value
? `#${ELEMENT_ID_MAIN_CONTENT}>div:not(.absolute)>div`
: undefined;
});
const getForceMount = computed(() => { const getForceMount = computed(() => {
return !unref(destroyOnClose) && unref(firstOpened); return !unref(destroyOnClose) && unref(firstOpened);
......
...@@ -13,6 +13,7 @@ export function useModalDraggable( ...@@ -13,6 +13,7 @@ export function useModalDraggable(
targetRef: Ref<HTMLElement | undefined>, targetRef: Ref<HTMLElement | undefined>,
dragRef: Ref<HTMLElement | undefined>, dragRef: Ref<HTMLElement | undefined>,
draggable: ComputedRef<boolean>, draggable: ComputedRef<boolean>,
containerSelector?: ComputedRef<string | undefined>,
) { ) {
const transform = reactive({ const transform = reactive({
offsetX: 0, offsetX: 0,
...@@ -30,20 +31,36 @@ export function useModalDraggable( ...@@ -30,20 +31,36 @@ export function useModalDraggable(
} }
const targetRect = targetRef.value.getBoundingClientRect(); const targetRect = targetRef.value.getBoundingClientRect();
const { offsetX, offsetY } = transform; const { offsetX, offsetY } = transform;
const targetLeft = targetRect.left; const targetLeft = targetRect.left;
const targetTop = targetRect.top; const targetTop = targetRect.top;
const targetWidth = targetRect.width; const targetWidth = targetRect.width;
const targetHeight = targetRect.height; const targetHeight = targetRect.height;
const docElement = document.documentElement;
const clientWidth = docElement.clientWidth; let containerRect: DOMRect | null = null;
const clientHeight = docElement.clientHeight;
if (containerSelector?.value) {
const minLeft = -targetLeft + offsetX; const container = document.querySelector(containerSelector.value);
const minTop = -targetTop + offsetY; if (container) {
const maxLeft = clientWidth - targetLeft - targetWidth + offsetX; containerRect = container.getBoundingClientRect();
const maxTop = clientHeight - targetTop - targetHeight + offsetY; }
}
let maxLeft, maxTop, minLeft, minTop;
if (containerRect) {
minLeft = containerRect.left - targetLeft + offsetX;
maxLeft = containerRect.right - targetLeft - targetWidth + offsetX;
minTop = containerRect.top - targetTop + offsetY;
maxTop = containerRect.bottom - targetTop - targetHeight + offsetY;
} else {
const docElement = document.documentElement;
const clientWidth = docElement.clientWidth;
const clientHeight = docElement.clientHeight;
minLeft = -targetLeft + offsetX;
minTop = -targetTop + offsetY;
maxLeft = clientWidth - targetLeft - targetWidth + offsetX;
maxTop = clientHeight - targetTop - targetHeight + offsetY;
}
const onMousemove = (e: MouseEvent) => { const onMousemove = (e: MouseEvent) => {
let moveX = offsetX + e.clientX - downX; let moveX = offsetX + e.clientX - downX;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!