This commit is contained in:
dap 2025-06-30 19:35:24 +08:00
commit 190c8c586e
8 changed files with 168 additions and 51 deletions

View File

@ -23,6 +23,7 @@ const props = withDefaults(defineProps<TreeProps>(), {
defaultExpandedKeys: () => [], defaultExpandedKeys: () => [],
defaultExpandedLevel: 0, defaultExpandedLevel: 0,
disabled: false, disabled: false,
disabledField: 'disabled',
expanded: () => [], expanded: () => [],
iconField: 'icon', iconField: 'icon',
labelField: 'label', labelField: 'label',
@ -101,16 +102,37 @@ function updateTreeValue() {
if (val === undefined) { if (val === undefined) {
treeValue.value = undefined; treeValue.value = undefined;
} else { } else {
treeValue.value = Array.isArray(val) if (Array.isArray(val)) {
? val.map((v) => getItemByValue(v)) const filteredValues = val.filter((v) => {
: getItemByValue(val); const item = getItemByValue(v);
return item && !get(item, props.disabledField);
});
treeValue.value = filteredValues.map((v) => getItemByValue(v));
if (filteredValues.length !== val.length) {
modelValue.value = filteredValues;
}
} else {
const item = getItemByValue(val);
if (item && !get(item, props.disabledField)) {
treeValue.value = item;
} else {
treeValue.value = undefined;
modelValue.value = undefined;
}
}
} }
} }
function updateModelValue(val: Arrayable<Recordable<any>>) { function updateModelValue(val: Arrayable<Recordable<any>>) {
modelValue.value = Array.isArray(val) if (Array.isArray(val)) {
? val.map((v) => get(v, props.valueField)) const filteredVal = val.filter((v) => !get(v, props.disabledField));
: get(val, props.valueField); modelValue.value = filteredVal.map((v) => get(v, props.valueField));
} else {
if (val && !get(val, props.disabledField)) {
modelValue.value = get(val, props.valueField);
}
}
} }
function expandToLevel(level: number) { function expandToLevel(level: number) {
@ -149,10 +171,18 @@ function collapseAll() {
expanded.value = []; expanded.value = [];
} }
function isNodeDisabled(item: FlattenedItem<Recordable<any>>) {
return props.disabled || get(item.value, props.disabledField);
}
function onToggle(item: FlattenedItem<Recordable<any>>) { function onToggle(item: FlattenedItem<Recordable<any>>) {
emits('expand', item); emits('expand', item);
} }
function onSelect(item: FlattenedItem<Recordable<any>>, isSelected: boolean) { function onSelect(item: FlattenedItem<Recordable<any>>, isSelected: boolean) {
if (isNodeDisabled(item)) {
return;
}
if ( if (
!props.checkStrictly && !props.checkStrictly &&
props.multiple && props.multiple &&
@ -224,28 +254,34 @@ defineExpose({
:class=" :class="
cn('cursor-pointer', getNodeClass?.(item), { cn('cursor-pointer', getNodeClass?.(item), {
'data-[selected]:bg-accent': !multiple, 'data-[selected]:bg-accent': !multiple,
'cursor-not-allowed': disabled, 'cursor-not-allowed': isNodeDisabled(item),
}) })
" "
v-bind=" v-bind="
Object.assign(item.bind, { Object.assign(item.bind, {
onfocus: disabled ? 'this.blur()' : undefined, onfocus: isNodeDisabled(item) ? 'this.blur()' : undefined,
disabled: isNodeDisabled(item),
}) })
" "
@select=" @select="
(event) => { (event: any) => {
if (isNodeDisabled(item)) {
event.preventDefault();
event.stopPropagation();
return;
}
if (event.detail.originalEvent.type === 'click') { if (event.detail.originalEvent.type === 'click') {
event.preventDefault(); event.preventDefault();
} }
!disabled && onSelect(item, event.detail.isSelected); onSelect(item, event.detail.isSelected);
} }
" "
@toggle=" @toggle="
(event) => { (event: any) => {
if (event.detail.originalEvent.type === 'click') { if (event.detail.originalEvent.type === 'click') {
event.preventDefault(); event.preventDefault();
} }
!disabled && onToggle(item); !isNodeDisabled(item) && onToggle(item);
} }
" "
class="tree-node focus:ring-grass8 my-0.5 flex items-center rounded px-2 py-1 outline-none focus:ring-2" class="tree-node focus:ring-grass8 my-0.5 flex items-center rounded px-2 py-1 outline-none focus:ring-2"
@ -266,24 +302,32 @@ defineExpose({
</div> </div>
<Checkbox <Checkbox
v-if="multiple" v-if="multiple"
:checked="isSelected" :checked="isSelected && !isNodeDisabled(item)"
:disabled="disabled" :disabled="isNodeDisabled(item)"
:indeterminate="isIndeterminate" :indeterminate="isIndeterminate && !isNodeDisabled(item)"
@click=" @click="
() => { (event: MouseEvent) => {
!disabled && handleSelect(); if (isNodeDisabled(item)) {
// onSelect(item, !isSelected); event.preventDefault();
event.stopPropagation();
return;
}
handleSelect();
} }
" "
/> />
<div <div
class="flex items-center gap-1 pl-2" class="flex items-center gap-1 pl-2"
@click=" @click="
(_event) => { (event: MouseEvent) => {
// $event.stopPropagation(); if (isNodeDisabled(item)) {
// $event.preventDefault(); event.preventDefault();
!disabled && handleSelect(); event.stopPropagation();
// onSelect(item, !isSelected); return;
}
event.stopPropagation();
event.preventDefault();
handleSelect();
} }
" "
> >

View File

@ -22,6 +22,8 @@ export interface TreeProps {
defaultValue?: Arrayable<number | string>; defaultValue?: Arrayable<number | string>;
/** 禁用 */ /** 禁用 */
disabled?: boolean; disabled?: boolean;
/** 禁用字段名 */
disabledField?: string;
/** 自定义节点类名 */ /** 自定义节点类名 */
getNodeClass?: (item: FlattenedItem<Recordable<any>>) => string; getNodeClass?: (item: FlattenedItem<Recordable<any>>) => string;
iconField?: string; iconField?: string;

View File

@ -49,6 +49,7 @@
"@vueuse/core": "catalog:", "@vueuse/core": "catalog:",
"@vueuse/integrations": "catalog:", "@vueuse/integrations": "catalog:",
"codemirror": "6.0.1", "codemirror": "6.0.1",
"json-bigint": "catalog:",
"qrcode": "catalog:", "qrcode": "catalog:",
"tippy.js": "catalog:", "tippy.js": "catalog:",
"vditor": "3.10.9", "vditor": "3.10.9",

View File

@ -76,6 +76,12 @@ const keyword = ref('');
const keywordDebounce = refDebounced(keyword, 300); const keywordDebounce = refDebounced(keyword, 300);
const innerIcons = ref<string[]>([]); const innerIcons = ref<string[]>([]);
/* 当检索关键词变化时,重置分页 */
watch(keywordDebounce, () => {
currentPage.value = 1;
setCurrentPage(1);
});
watchDebounced( watchDebounced(
() => props.prefix, () => props.prefix,
async (prefix) => { async (prefix) => {

View File

@ -18,6 +18,9 @@ import { $t } from '@vben/locales';
import { isBoolean } from '@vben-core/shared/utils'; import { isBoolean } from '@vben-core/shared/utils';
// @ts-ignore
import JsonBigint from 'json-bigint';
defineOptions({ name: 'JsonViewer' }); defineOptions({ name: 'JsonViewer' });
const props = withDefaults(defineProps<JsonViewerProps>(), { const props = withDefaults(defineProps<JsonViewerProps>(), {
@ -68,6 +71,20 @@ function handleClick(event: MouseEvent) {
emit('click', event); emit('click', event);
} }
// bigint
const jsonData = computed<Record<string, any>>(() => {
if (typeof props.value !== 'string') {
return props.value || {};
}
try {
return JsonBigint({ storeAsString: true }).parse(props.value);
} catch (error) {
console.error('JSON parse error:', error);
return {};
}
});
const bindProps = computed<Recordable<any>>(() => { const bindProps = computed<Recordable<any>>(() => {
const copyable = { const copyable = {
copyText: $t('ui.jsonViewer.copy'), copyText: $t('ui.jsonViewer.copy'),
@ -79,6 +96,7 @@ const bindProps = computed<Recordable<any>>(() => {
return { return {
...props, ...props,
...attrs, ...attrs,
value: jsonData.value,
onCopied: (event: JsonViewerAction) => emit('copied', event), onCopied: (event: JsonViewerAction) => emit('copied', event),
onKeyclick: (key: string) => emit('keyClick', key), onKeyclick: (key: string) => emit('keyClick', key),
onClick: (event: MouseEvent) => handleClick(event), onClick: (event: MouseEvent) => handleClick(event),

View File

@ -3,6 +3,8 @@ import type { AuthenticationProps } from './types';
import { computed, watch } from 'vue'; import { computed, watch } from 'vue';
import { $t } from '@vben/locales';
import { useVbenModal } from '@vben-core/popup-ui'; import { useVbenModal } from '@vben-core/popup-ui';
import { Slot, VbenAvatar } from '@vben-core/shadcn-ui'; import { Slot, VbenAvatar } from '@vben-core/shadcn-ui';

View File

@ -2,7 +2,7 @@ import type { Arrayable, MaybeElementRef } from '@vueuse/core';
import type { Ref } from 'vue'; import type { Ref } from 'vue';
import { computed, onUnmounted, ref, unref, watch } from 'vue'; import { computed, effectScope, onUnmounted, ref, unref, watch } from 'vue';
import { isFunction } from '@vben/utils'; import { isFunction } from '@vben/utils';
@ -20,12 +20,12 @@ const DEFAULT_ENTER_DELAY = 0; // 鼠标进入延迟时间,默认为 0
/** /**
* true false * true false
* @param refElement true * @param refElement true
* @param delay / * @param delay /
* @returns ref enable disable * @returns ref enable disable
*/ */
export function useHoverToggle( export function useHoverToggle(
refElement: Arrayable<MaybeElementRef>, refElement: Arrayable<MaybeElementRef> | Ref<HTMLElement[] | null>,
delay: (() => number) | HoverDelayOptions | number = DEFAULT_LEAVE_DELAY, delay: (() => number) | HoverDelayOptions | number = DEFAULT_LEAVE_DELAY,
) { ) {
// 兼容旧版本API // 兼容旧版本API
@ -38,20 +38,58 @@ export function useHoverToggle(
...delay, ...delay,
}; };
const isHovers: Array<Ref<boolean>> = [];
const value = ref(false); const value = ref(false);
const enterTimer = ref<ReturnType<typeof setTimeout> | undefined>(); const enterTimer = ref<ReturnType<typeof setTimeout> | undefined>();
const leaveTimer = ref<ReturnType<typeof setTimeout> | undefined>(); const leaveTimer = ref<ReturnType<typeof setTimeout> | undefined>();
const refs = Array.isArray(refElement) ? refElement : [refElement]; const hoverScopes = ref<ReturnType<typeof effectScope>[]>([]);
refs.forEach((refEle) => {
const eleRef = computed(() => { // 使用计算属性包装 refElement使其响应式变化
const ele = unref(refEle); const refs = computed(() => {
return ele instanceof Element ? ele : (ele?.$el as Element); const raw = unref(refElement);
}); if (raw === null) return [];
const isHover = useElementHover(eleRef); return Array.isArray(raw) ? raw : [raw];
isHovers.push(isHover);
}); });
const isOutsideAll = computed(() => isHovers.every((v) => !v.value)); // 存储所有 hover 状态
const isHovers = ref<Array<Ref<boolean>>>([]);
// 更新 hover 监听的函数
function updateHovers() {
// 停止并清理之前的作用域
hoverScopes.value.forEach((scope) => scope.stop());
hoverScopes.value = [];
isHovers.value = refs.value.map((refEle) => {
if (!refEle) {
return ref(false);
}
const eleRef = computed(() => {
const ele = unref(refEle);
return ele instanceof Element ? ele : (ele?.$el as Element);
});
// 为每个元素创建独立的作用域
const scope = effectScope();
const hoverRef = scope.run(() => useElementHover(eleRef)) || ref(false);
hoverScopes.value.push(scope);
return hoverRef;
});
}
// 监听元素数量变化,避免过度执行
const elementsCount = computed(() => {
const raw = unref(refElement);
if (raw === null) return 0;
return Array.isArray(raw) ? raw.length : 1;
});
// 初始设置
updateHovers();
// 只在元素数量变化时重新设置监听器
const stopWatcher = watch(elementsCount, updateHovers, { deep: false });
const isOutsideAll = computed(() => isHovers.value.every((v) => !v.value));
function clearTimers() { function clearTimers() {
if (enterTimer.value) { if (enterTimer.value) {
@ -96,7 +134,7 @@ export function useHoverToggle(
} }
} }
const watcher = watch( const hoverWatcher = watch(
isOutsideAll, isOutsideAll,
(val) => { (val) => {
setValueDelay(!val); setValueDelay(!val);
@ -106,15 +144,19 @@ export function useHoverToggle(
const controller = { const controller = {
enable() { enable() {
watcher.resume(); hoverWatcher.resume();
}, },
disable() { disable() {
watcher.pause(); hoverWatcher.pause();
}, },
}; };
onUnmounted(() => { onUnmounted(() => {
clearTimers(); clearTimers();
// 停止监听器
stopWatcher();
// 停止所有剩余的作用域
hoverScopes.value.forEach((scope) => scope.stop());
}); });
return [value, controller] as [typeof value, typeof controller]; return [value, controller] as [typeof value, typeof controller];

View File

@ -62,21 +62,23 @@ const { authPanelCenter, authPanelLeft, authPanelRight, isDark } =
</template> </template>
</AuthenticationFormView> </AuthenticationFormView>
<!-- 头部 Logo 和应用名称 --> <slot name="logo">
<div <!-- 头部 Logo 和应用名称 -->
v-if="logo || appName"
class="absolute left-0 top-0 z-10 flex flex-1"
@click="clickLogo"
>
<div <div
class="text-foreground lg:text-foreground ml-4 mt-4 flex flex-1 items-center sm:left-6 sm:top-6" v-if="logo || appName"
class="absolute left-0 top-0 z-10 flex flex-1"
@click="clickLogo"
> >
<img v-if="logo" :alt="appName" :src="logo" class="mr-2" width="42" /> <div
<p v-if="appName" class="m-0 text-xl font-medium"> class="text-foreground lg:text-foreground ml-4 mt-4 flex flex-1 items-center sm:left-6 sm:top-6"
{{ appName }} >
</p> <img v-if="logo" :alt="appName" :src="logo" class="mr-2" width="42" />
<p v-if="appName" class="m-0 text-xl font-medium">
{{ appName }}
</p>
</div>
</div> </div>
</div> </slot>
<!-- 系统介绍 --> <!-- 系统介绍 -->
<div v-if="!authPanelCenter" class="relative hidden w-0 flex-1 lg:block"> <div v-if="!authPanelCenter" class="relative hidden w-0 flex-1 lg:block">