2025-08-21 00:24:36 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* @Author: RenXiaoDong
|
|
|
|
|
|
* @Date: 2025-08-20 23:17:49
|
2025-08-21 10:54:18 +08:00
|
|
|
|
* @Description: 气泡上下文管理
|
|
|
|
|
|
* 提供气泡组件间的通信机制,支持全局状态管理和组件间数据传递
|
2025-08-21 00:24:36 +08:00
|
|
|
|
*/
|
|
|
|
|
|
import { computed, defineComponent, inject, provide, shallowRef, triggerRef, unref, watch } from "vue";
|
|
|
|
|
|
import type { ComputedRef, InjectionKey } from "vue";
|
|
|
|
|
|
import { objectType } from "@/utils/type";
|
|
|
|
|
|
import type { BubbleContextProps } from "./types";
|
|
|
|
|
|
|
2025-08-21 10:54:18 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 气泡上下文注入键
|
|
|
|
|
|
* 用于Vue的依赖注入系统,确保上下文的唯一性
|
|
|
|
|
|
*/
|
2025-08-21 00:24:36 +08:00
|
|
|
|
const BubbleContextKey: InjectionKey<ComputedRef<BubbleContextProps>> =
|
|
|
|
|
|
Symbol('BubbleContext');
|
|
|
|
|
|
|
2025-08-21 10:54:18 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 全局气泡上下文API
|
|
|
|
|
|
* 提供全局访问气泡上下文的能力,用于跨组件通信
|
|
|
|
|
|
*/
|
2025-08-21 00:24:36 +08:00
|
|
|
|
export const globalBubbleContextApi = shallowRef<BubbleContextProps>();
|
|
|
|
|
|
|
2025-08-21 10:54:18 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 气泡上下文提供者Hook
|
|
|
|
|
|
* 向子组件提供气泡上下文,并同步更新全局API
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param value - 要提供的上下文值(计算属性)
|
|
|
|
|
|
*/
|
2025-08-21 00:24:36 +08:00
|
|
|
|
export const useBubbleContextProvider = (value: ComputedRef<BubbleContextProps>) => {
|
2025-08-21 10:54:18 +08:00
|
|
|
|
// 向子组件提供上下文
|
2025-08-21 00:24:36 +08:00
|
|
|
|
provide(BubbleContextKey, value);
|
2025-08-21 10:54:18 +08:00
|
|
|
|
|
|
|
|
|
|
// 监听上下文变化,同步更新全局API
|
2025-08-21 00:24:36 +08:00
|
|
|
|
watch(
|
|
|
|
|
|
value,
|
|
|
|
|
|
() => {
|
|
|
|
|
|
globalBubbleContextApi.value = unref(value);
|
2025-08-21 10:54:18 +08:00
|
|
|
|
// 触发响应式更新
|
2025-08-21 00:24:36 +08:00
|
|
|
|
triggerRef(globalBubbleContextApi);
|
|
|
|
|
|
},
|
2025-08-21 10:54:18 +08:00
|
|
|
|
{ immediate: true, deep: true }, // 立即执行,深度监听
|
2025-08-21 00:24:36 +08:00
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-21 10:54:18 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 气泡上下文注入Hook
|
|
|
|
|
|
* 从父组件或全局API获取气泡上下文
|
|
|
|
|
|
*
|
|
|
|
|
|
* @returns 气泡上下文(计算属性)
|
|
|
|
|
|
*/
|
2025-08-21 00:24:36 +08:00
|
|
|
|
export const useBubbleContextInject = () => {
|
|
|
|
|
|
return inject(
|
|
|
|
|
|
BubbleContextKey,
|
2025-08-21 10:54:18 +08:00
|
|
|
|
// 如果没有找到注入的上下文,使用全局API作为后备
|
2025-08-21 00:24:36 +08:00
|
|
|
|
computed(() => globalBubbleContextApi.value || {}),
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
2025-08-21 10:54:18 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 气泡上下文提供者组件
|
|
|
|
|
|
* 用于在模板中提供气泡上下文,简化使用方式
|
|
|
|
|
|
*/
|
2025-08-21 00:24:36 +08:00
|
|
|
|
export const BubbleContextProvider = defineComponent({
|
|
|
|
|
|
props: {
|
2025-08-21 10:54:18 +08:00
|
|
|
|
// 上下文值,支持对象类型验证
|
2025-08-21 00:24:36 +08:00
|
|
|
|
value: objectType<BubbleContextProps>(),
|
|
|
|
|
|
},
|
|
|
|
|
|
setup(props, { slots }) {
|
2025-08-21 10:54:18 +08:00
|
|
|
|
// 使用计算属性包装props.value,确保响应式
|
2025-08-21 00:24:36 +08:00
|
|
|
|
useBubbleContextProvider(computed(() => props.value));
|
2025-08-21 10:54:18 +08:00
|
|
|
|
|
|
|
|
|
|
// 渲染默认插槽内容
|
2025-08-21 00:24:36 +08:00
|
|
|
|
return () => {
|
|
|
|
|
|
return slots.default?.();
|
|
|
|
|
|
};
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export default BubbleContextProvider;
|