From 6599cf836b9108f4e32de4ce1604abfbd70f9162 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Fri, 13 Nov 2026 21:15:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=20use=20=E4=B8=AD?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=A0=B9=E5=85=83=E7=B4=A0=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/render-vue/src/use.ts | 257 ++++++++++++++++++++++-------- packages/render/src/core/item.ts | 51 +++++- packages/render/src/core/types.ts | 38 ++++- 3 files changed, 271 insertions(+), 75 deletions(-) diff --git a/packages/render-vue/src/use.ts b/packages/render-vue/src/use.ts index a77ddab..c7f225d 100644 --- a/packages/render-vue/src/use.ts +++ b/packages/render-vue/src/use.ts @@ -1,11 +1,21 @@ -import { IRenderTreeRoot, MotaRenderer, RenderItem } from '@motajs/render'; import { + IRenderItemBaseHooks, + IRenderTreeRoot, + MotaRenderer, + RenderItem +} from '@motajs/render'; +import { + ComponentInternalInstance, computed, ComputedRef, getCurrentInstance, + onMounted, onUnmounted, + onUpdated, Ref, - ref + ref, + shallowRef, + watch } from 'vue'; import { IExcitable, @@ -22,6 +32,33 @@ import { ITransitionedController } from './types'; //#region 函数功能接口 +/** + * 尝试从不同的路径获取当前挂载至的根元素 + * @param instance 组件实例对象 + */ +function tryGetRoot( + instance: ComponentInternalInstance +): IRenderTreeRoot | null { + const root = instance.root.vnode.el; + if (root instanceof RenderItem) { + if (root.parent instanceof MotaRenderer) { + return root.parent; + } else if (root.root) { + return root.root; + } + } + const currEl = instance.proxy?.$el; + if (currEl instanceof RenderItem) { + return currEl.root; + } + const appEl = instance.appContext.app._container; + if (appEl instanceof MotaRenderer) { + return appEl; + } + + return null; +} + /** * 尝试获取当前组件的根渲染元素,若无法找到可能存在的根元素,则返回 `null`。 * 仅允许在组件的 `setup` 或其生命周期钩子中调用,否则一定返回 `null`。 @@ -31,23 +68,28 @@ import { ITransitionedController } from './types'; * const ex = root.excitation; // 例如从根元素获取激励源 * ``` */ -export function useRendererRoot(): IRenderTreeRoot | null { +export function useRendererRoot(): Readonly> { + const ret = shallowRef(null); const instance = getCurrentInstance(); - if (!instance) return null; - // 尝试从几个路径获取根元素 - const root = instance.root.vnode.el; - if (root instanceof RenderItem) { - if (root.parent instanceof MotaRenderer) { - return root.parent; - } else { - return null; + if (!instance) { + ret.value = null; + return ret; + } + + ret.value = tryGetRoot(instance); + + if (!instance.isUnmounted) { + onUpdated(() => { + ret.value = tryGetRoot(getCurrentInstance()!); + }); + if (!instance.isMounted) { + onMounted(() => { + ret.value = tryGetRoot(getCurrentInstance()!); + }); } } - const currEl = instance.proxy?.$el; - if (currEl instanceof RenderItem) return currEl.root; - const appEl = instance.appContext.app._container; - if (appEl instanceof MotaRenderer) return appEl; - return null; + + return ret; } /** @@ -64,12 +106,25 @@ export function useRendererRoot(): IRenderTreeRoot | null { * @param excitable 要执行的被激励对象 */ export function onExcited(excitable: IExcitable): void { + const instance = getCurrentInstance(); + if (!instance) return; const renderer = useRendererRoot(); - if (!renderer) return; - const num = renderer.delegateExcitable(excitable); - onUnmounted(() => { - renderer.removeExcitable(num); - }); + let num: number = -1; + watch( + renderer, + (value, old) => { + old?.removeExcitable(num); + if (value) { + num = value.delegateExcitable(excitable); + } + }, + { immediate: true } + ); + if (!instance.isUnmounted) { + onUnmounted(() => { + renderer.value?.removeExcitable(num); + }); + } } /** @@ -104,15 +159,25 @@ export function useAnimater( anim?: IAnimater, excitation?: IExcitation ): IAnimater | null { + const instance = getCurrentInstance(); + if (!instance) return null; const renderer = useRendererRoot(); - if (!renderer) return null; - const ex = excitation ?? renderer.excitation; const animater = anim ?? new Animater(); - animater.bindExcitation(ex); - onUnmounted(() => { - animater.unbindExcitation(); - if (!anim) animater.destroy(); - }); + watch( + renderer, + value => { + const ex = excitation ?? value?.excitation; + if (ex) animater.bindExcitation(ex); + else animater.unbindExcitation(); + }, + { immediate: true } + ); + if (!instance.isUnmounted) { + onUnmounted(() => { + animater.unbindExcitation(); + if (!anim) animater.destroy(); + }); + } return animater; } @@ -133,20 +198,60 @@ export function useTransition( tran?: ITransition, excitation?: IExcitation ): ITransition | null { + const instance = getCurrentInstance(); + if (!instance) return null; const renderer = useRendererRoot(); - if (!renderer) return null; - const ex = excitation ?? renderer.excitation; const transition = tran ?? new Transition(); - transition.bindExcitation(ex); - onUnmounted(() => { - transition.unbindExcitation(); - if (!tran) transition.destroy(); - }); + watch( + renderer, + value => { + const ex = excitation ?? value?.excitation; + if (ex) transition.bindExcitation(ex); + else transition.unbindExcitation(); + }, + { immediate: true } + ); + if (!instance.isUnmounted) { + onUnmounted(() => { + transition.unbindExcitation(); + if (!tran) transition.destroy(); + }); + } return transition; } const transitionMap: Map = new Map(); +/** + * 获取专供 `transitioned` 和 `transitionedColor` 使用的渐变对象 + */ +function getGlobalTransition(): ComputedRef { + const renderer = useRendererRoot(); + const tran = computed(() => { + if (renderer.value) { + const transition = transitionMap.getOrInsertComputed( + renderer.value, + () => { + // 在这里添加副作用以在根元素被销毁时一并删除渐变对象 + const pin = renderer.value!; + const hook: IRenderItemBaseHooks = { + onDestroyed() { + transitionMap.delete(pin); + } + }; + pin.addHook(hook); + return new Transition(); + } + ); + transition.bindExcitation(renderer.value.excitation); + return transition; + } else { + return null; + } + }); + return tran; +} + /** * 创建一个渐变数值,当数值发生变化时会缓慢变化值目标值而非突变,只可以用于组件内,不可用于组件外 * ```tsx @@ -165,16 +270,9 @@ export function transitioned( time: number, curve: ExcitationCurve ): ITransitionedController | null { - const renderer = useRendererRoot(); - if (!renderer) return null; - const tran = transitionMap.getOrInsertComputed( - renderer, - () => new Transition() - ); - if (!tran.excitation) { - tran.bindExcitation(renderer.excitation); - } - return new RenderTransition(value, tran, time, curve); + const instance = getCurrentInstance(); + if (!instance) return null; + return new RenderTransition(value, getGlobalTransition(), time, curve); } /** @@ -198,14 +296,7 @@ export function transitionedColor( ): ITransitionedController | null { const renderer = useRendererRoot(); if (!renderer) return null; - const tran = transitionMap.getOrInsertComputed( - renderer, - () => new Transition() - ); - if (!tran.excitation) { - tran.bindExcitation(renderer.excitation); - } - return new RenderColorTransition(color, tran, time, curve); + return new RenderColorTransition(color, getGlobalTransition(), time, curve); } //#endregion @@ -215,6 +306,11 @@ export function transitionedColor( class RenderTransition implements ITransitionedController { readonly ref: Ref; + /** 在未绑定渐变器的时候,将要移动至的目标值 */ + private pendingValue: number = 0; + /** 在未绑定渐变器的时候,移动目标值将花费的时间 */ + private pendingTime: number = 0; + set value(v: number) { this.set(v); } @@ -224,15 +320,28 @@ class RenderTransition implements ITransitionedController { constructor( value: number, - readonly transition: ITransition, + readonly transition: ComputedRef, public time: number, public curve: ExcitationCurve ) { this.ref = ref(value); + + watch(transition, (value, old) => { + if (value && !old) { + this.set(this.pendingValue, this.pendingTime); + } + }); } set(value: number, time: number = this.time): void { - this.transition.curve(this.curve).transition(this.ref).to(value, time); + if (this.transition.value) { + this.transition.value + .curve(this.curve) + .transition(this.ref) + .to(value, time); + } else { + this.pendingValue = value; + } } mode(timing: ExcitationCurve): void { @@ -258,6 +367,11 @@ class RenderColorTransition implements ITransitionedController { readonly ref: ComputedRef; + /** 在未绑定渐变器的时候,将要移动至的目标值 */ + private pendingValue: ColorRGBA = [0, 0, 0, 0]; + /** 在未绑定渐变器的时候,移动目标值将花费的时间 */ + private pendingTime: number = 0; + set value(v: string) { this.set(v); } @@ -267,7 +381,7 @@ class RenderColorTransition implements ITransitionedController { constructor( value: string, - readonly transition: ITransition, + readonly transition: ComputedRef, public time: number, public curve: ExcitationCurve ) { @@ -277,6 +391,12 @@ class RenderColorTransition implements ITransitionedController { this.bValue = ref(b); this.aValue = ref(a); this.ref = computed(() => this.encodeColor()); + + watch(transition, (value, old) => { + if (value && !old) { + this.transitionColor(this.pendingValue, this.pendingTime); + } + }); } set(value: string, time: number = this.time): void { @@ -297,16 +417,21 @@ class RenderColorTransition implements ITransitionedController { * @param time 渐变时长 */ private transitionColor([r, g, b, a]: ColorRGBA, time: number) { - this.transition - .curve(this.curve) - .transition(this.rValue) - .to(r, time) - .transition(this.gValue) - .to(g, time) - .transition(this.bValue) - .to(b, time) - .transition(this.aValue) - .to(a, time); + if (this.transition.value) { + this.transition.value + .curve(this.curve) + .transition(this.rValue) + .to(r, time) + .transition(this.gValue) + .to(g, time) + .transition(this.bValue) + .to(b, time) + .transition(this.aValue) + .to(a, time); + } else { + this.pendingValue = [r, g, b, a]; + this.pendingTime = time; + } } /** diff --git a/packages/render/src/core/item.ts b/packages/render/src/core/item.ts index 6b79946..80de719 100644 --- a/packages/render/src/core/item.ts +++ b/packages/render/src/core/item.ts @@ -170,7 +170,13 @@ export abstract class RenderItem .add('onWheel') .add('onWheelCapture') .add('onResize') - .add('onTransform'); + .add('onTransform') + .add('onConnected') + .add('onDisconnected') + .add('onUpdatedChildren') + .add('onAppendTo') + .add('onRemove') + .add('onDestroyed'); this.addHook(this.getDefaultHook()); @@ -514,7 +520,7 @@ export abstract class RenderItem } this.x = transform.x; this.y = transform.y; - this.forEachHook(hook => hook.onTransform?.(transform)); + this.forEachHook(hook => hook.onTransform?.(this, transform)); } //#endregion @@ -530,12 +536,15 @@ export abstract class RenderItem item.connect(item); this.connected = true; this.onConnected(item); + this.forEachHook(hook => hook.onConnected?.(item)); } else { this.connected = false; if (before) { this.onDisconnected(before); + this.forEachHook(hook => hook.onDisconnected?.(before)); } } + this.children.forEach(v => v.setRoot(item)); } appendTo(parent: IRenderItem, anchor?: IRenderItem) { @@ -570,11 +579,10 @@ export abstract class RenderItem el.onResize(this.scale); el.forEachHook(hook => hook.onResize?.(el.width, el.height)); // 还需要深度遍历每个元素,给每个元素都设置根元素 - if (this.root) { - el.setRoot(this.root); - el.forEachChild(item => item.setRoot(this.root)); - } + el.setRoot(this.root); el.update(); + el.forEachHook(hook => hook.onAppendTo?.(this)); + this.forEachHook(hook => hook.onUpdateChildren?.(this.children)); } removeChild(child: IRenderItem): boolean { @@ -589,6 +597,8 @@ export abstract class RenderItem this.update(); el.transform.unbind(el); el.setRoot(null); + el.forEachHook(hook => hook.onRemove?.(this)); + this.forEachHook(hook => hook.onUpdateChildren?.(this.children)); return true; } @@ -1146,6 +1156,7 @@ export abstract class RenderItem this.touchId.clear(); this.children.length = 0; this.setRoot(null); + this.forEachHook(hook => hook.onDestroyed?.()); } } @@ -1204,7 +1215,31 @@ export class RenderItemDefaultHooks implements IRenderItemBaseHooks { this.list.get('onResize')?.(width, height); } - onTransform(transform: Transform): void { - this.list.get('onTransform')?.(transform); + onTransform(item: IRenderItem, transform: Transform): void { + this.list.get('onTransform')?.(item, transform); + } + + onConnected(root: IRenderTreeRoot): void { + this.list.get('onConnected')?.(root); + } + + onDisconnected(prevRoot: IRenderTreeRoot): void { + this.list.get('onDisconnected')?.(prevRoot); + } + + onUpdateChildren(children: readonly IRenderItem[]): void { + this.list.get('onUpdateChildren')?.(children); + } + + onAppendTo(parent: IRenderItem): void { + this.list.get('onAppendTo')?.(parent); + } + + onRemove(prevParent: IRenderItem): void { + this.list.get('onRemove')?.(prevParent); + } + + onDestroyed(): void { + this.list.get('onDestroyed')?.(); } } diff --git a/packages/render/src/core/types.ts b/packages/render/src/core/types.ts index 7e73153..05aff40 100644 --- a/packages/render/src/core/types.ts +++ b/packages/render/src/core/types.ts @@ -144,9 +144,45 @@ export interface IRenderItemBaseHooks extends IHookBase { /** * 当元素的变换矩阵发生变化时触发 + * @param item 触发此钩子的渲染元素 * @param transform 变换矩阵对象 */ - onTransform?(transform: Transform): void; + onTransform?(item: IRenderItem, transform: Transform): void; + + /** + * 当连接到根元素上时执行 + * @param root 根元素 + */ + onConnected?(root: IRenderTreeRoot): void; + + /** + * 当从根元素上取消连接时执行 + * @param prevRoot 先前连接至的根元素 + */ + onDisconnected?(prevRoot: IRenderTreeRoot): void; + + /** + * 当更新子元素列表时触发 + * @param children 当前的子元素列表 + */ + onUpdateChildren?(children: readonly IRenderItem[]): void; + + /** + * 当添加至任意父元素时触发 + * @param parent 添加至的父元素 + */ + onAppendTo?(parent: IRenderItem): void; + + /** + * 当从任意父元素上被移除时触发 + * @param prevParent 原先所属的父元素 + */ + onRemove?(prevParent: IRenderItem): void; + + /** + * 当此元素被销毁时触发 + */ + onDestroyed?(): void; } export interface IRenderItem