master
dap 7 months ago
commit 383756c0aa

@ -150,8 +150,8 @@ export async function saveUserApi(user: UserInfo) {
```ts ```ts
import { requestClient } from '#/api/request'; import { requestClient } from '#/api/request';
export async function deleteUserApi(user: UserInfo) { export async function deleteUserApi(userId: number) {
return requestClient.delete<boolean>(`/user/${user.id}`, user); return requestClient.delete<boolean>(`/user/${userId}`);
} }
``` ```

@ -180,8 +180,8 @@ export async function saveUserApi(user: UserInfo) {
```ts ```ts
import { requestClient } from '#/api/request'; import { requestClient } from '#/api/request';
export async function deleteUserApi(user: UserInfo) { export async function deleteUserApi(userId: number) {
return requestClient.delete<boolean>(`/user/${user.id}`, user); return requestClient.delete<boolean>(`/user/${userId}`);
} }
``` ```

@ -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) {
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);
@ -224,7 +226,8 @@ function handleClosed() {
:append-to="getAppendTo" :append-to="getAppendTo"
:class=" :class="
cn( cn(
'left-0 right-0 top-[10vh] mx-auto flex max-h-[80%] w-[520px] flex-col p-0 sm:rounded-[var(--radius)]', 'left-0 right-0 top-[10vh] mx-auto flex max-h-[80%] w-[520px] flex-col p-0',
shouldFullscreen ? 'sm:rounded-none' : 'sm:rounded-[var(--radius)]',
modalClass, modalClass,
{ {
'border-border border': bordered, 'border-border border': bordered,

@ -12,6 +12,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,
@ -29,20 +30,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;

@ -3,11 +3,11 @@ import type { Component } from 'vue';
import type { AnyPromiseFunction } from '@vben/types'; import type { AnyPromiseFunction } from '@vben/types';
import { computed, ref, unref, useAttrs, watch } from 'vue'; import { computed, nextTick, ref, unref, useAttrs, watch } from 'vue';
import { LoaderCircle } from '@vben/icons'; import { LoaderCircle } from '@vben/icons';
import { get, isEqual, isFunction } from '@vben-core/shared/utils'; import { cloneDeep, get, isEqual, isFunction } from '@vben-core/shared/utils';
import { objectOmit } from '@vueuse/core'; import { objectOmit } from '@vueuse/core';
@ -104,6 +104,8 @@ const refOptions = ref<OptionsItem[]>([]);
const loading = ref(false); const loading = ref(false);
// //
const isFirstLoaded = ref(false); const isFirstLoaded = ref(false);
//
const hasPendingRequest = ref(false);
const getOptions = computed(() => { const getOptions = computed(() => {
const { labelField, valueField, childrenField, numberToString } = props; const { labelField, valueField, childrenField, numberToString } = props;
@ -146,18 +148,26 @@ const bindProps = computed(() => {
}); });
async function fetchApi() { async function fetchApi() {
let { api, beforeFetch, afterFetch, params, resultField } = props; const { api, beforeFetch, afterFetch, resultField } = props;
if (!api || !isFunction(api) || loading.value) { if (!api || !isFunction(api)) {
return; return;
} }
//
if (loading.value) {
hasPendingRequest.value = true;
return;
}
refOptions.value = []; refOptions.value = [];
try { try {
loading.value = true; loading.value = true;
let finalParams = unref(mergedParams);
if (beforeFetch && isFunction(beforeFetch)) { if (beforeFetch && isFunction(beforeFetch)) {
params = (await beforeFetch(params)) || params; finalParams = (await beforeFetch(cloneDeep(finalParams))) || finalParams;
} }
let res = await api(params); let res = await api(finalParams);
if (afterFetch && isFunction(afterFetch)) { if (afterFetch && isFunction(afterFetch)) {
res = (await afterFetch(res)) || res; res = (await afterFetch(res)) || res;
} }
@ -177,6 +187,13 @@ async function fetchApi() {
isFirstLoaded.value = false; isFirstLoaded.value = false;
} finally { } finally {
loading.value = false; loading.value = false;
//
if (hasPendingRequest.value) {
hasPendingRequest.value = false;
// 使 nextTick
await nextTick();
fetchApi();
}
} }
} }
@ -190,7 +207,7 @@ async function handleFetchForVisible(visible: boolean) {
} }
} }
const params = computed(() => { const mergedParams = computed(() => {
return { return {
...props.params, ...props.params,
...unref(innerParams), ...unref(innerParams),
@ -198,7 +215,7 @@ const params = computed(() => {
}); });
watch( watch(
params, mergedParams,
(value, oldValue) => { (value, oldValue) => {
if (isEqual(value, oldValue)) { if (isEqual(value, oldValue)) {
return; return;

Loading…
Cancel
Save