mirror of
https://github.com/motajs/template.git
synced 2026-12-02 20:24:31 +08:00
refactor: TextContentTyper 删除 eventemitter3 依赖,换用 Hook 接口
This commit is contained in:
1 parent
800a005ada
commit
9c6693d4b7
2 files changed
+53
-27
No files matched your search
@@ -11,7 +11,8 @@ import { isNil } from 'lodash-es';
|
||||
import {
|
||||
TextContentTyper,
|
||||
TyperRenderable,
|
||||
TextContentType
|
||||
TextContentType,
|
||||
ITextContentTyperHooks
|
||||
} from './textboxTyper';
|
||||
import { SetupComponentOptions } from '@motajs/system';
|
||||
import { DefaultProps, lwh, useRendererRoot, xywh } from '@motajs/render-vue';
|
||||
@@ -116,6 +117,15 @@ export const TextContent = defineComponent<
|
||||
const renderable = shallowRef<TyperRenderable[]>([]);
|
||||
let needUpdate = false;
|
||||
|
||||
const hook: ITextContentTyperHooks = {
|
||||
onTypeStart() {
|
||||
emit('typeStart');
|
||||
},
|
||||
onTypeEnd() {
|
||||
emit('typeEnd');
|
||||
}
|
||||
};
|
||||
|
||||
watch(
|
||||
root,
|
||||
(value, old) => {
|
||||
@@ -125,15 +135,6 @@ export const TextContent = defineComponent<
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
watch(
|
||||
typer,
|
||||
(value, old) => {
|
||||
if (old) root.value?.excitation.remove(old);
|
||||
root.value?.excitation.add(value);
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
/** 从头开始重新打字 */
|
||||
const retype = () => {
|
||||
if (needUpdate || props.hidden) return;
|
||||
@@ -211,13 +212,20 @@ export const TextContent = defineComponent<
|
||||
renderable.value = data;
|
||||
};
|
||||
|
||||
typer.value.setRender(renderFunc);
|
||||
typer.value.on('typeStart', () => {
|
||||
emit('typeStart');
|
||||
});
|
||||
typer.value.on('typeEnd', () => {
|
||||
emit('typeEnd');
|
||||
});
|
||||
watch(
|
||||
typer,
|
||||
(value, old) => {
|
||||
if (old) {
|
||||
root.value?.excitation.remove(old);
|
||||
old.removeHook(hook);
|
||||
old.setRender(() => {});
|
||||
}
|
||||
root.value?.excitation.add(value);
|
||||
value.addHook(hook);
|
||||
value.setRender(renderFunc);
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
onMounted(retype);
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { logger } from '@motajs/common';
|
||||
import {
|
||||
Hookable,
|
||||
HookController,
|
||||
IHookBase,
|
||||
IHookController,
|
||||
logger
|
||||
} from '@motajs/common';
|
||||
import { Font, MotaOffscreenCanvas2D } from '@motajs/render';
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { IExcitable } from '@motajs/animate';
|
||||
import { IMaterialFramedData, ITextureManager } from '../material';
|
||||
@@ -238,15 +243,22 @@ export type TyperRenderable =
|
||||
| ITyperIconRenderable
|
||||
| ITyperWaitRenderable;
|
||||
|
||||
interface TextContentTyperEvent {
|
||||
typeStart: [];
|
||||
typeEnd: [];
|
||||
export interface ITextContentTyperHooks extends IHookBase {
|
||||
/**
|
||||
* 当开始打字时触发
|
||||
*/
|
||||
onTypeStart?(): void;
|
||||
|
||||
/**
|
||||
* 当结束打字时触发
|
||||
*/
|
||||
onTypeEnd?(): void;
|
||||
}
|
||||
|
||||
type TyperFunction = (data: TyperRenderable[], typing: boolean) => void;
|
||||
|
||||
export class TextContentTyper
|
||||
extends EventEmitter<TextContentTyperEvent>
|
||||
extends Hookable<ITextContentTyperHooks>
|
||||
implements IExcitable<number>
|
||||
{
|
||||
/** 文字配置信息 */
|
||||
@@ -316,6 +328,12 @@ export class TextContentTyper
|
||||
);
|
||||
}
|
||||
|
||||
protected createController(
|
||||
hook: Partial<ITextContentTyperHooks>
|
||||
): IHookController<ITextContentTyperHooks> {
|
||||
return new HookController(this, hook);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取这段文字的总高度
|
||||
*/
|
||||
@@ -506,7 +524,7 @@ export class TextContentTyper
|
||||
this.render?.(this.renderData, !end);
|
||||
if (end) {
|
||||
this.typing = false;
|
||||
this.emit('typeEnd');
|
||||
this.forEachHook(hook => hook.onTypeEnd?.());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -517,7 +535,7 @@ export class TextContentTyper
|
||||
if (this.typing) return;
|
||||
this.processing = 0;
|
||||
if (this.config.interval === 0) {
|
||||
this.emit('typeStart');
|
||||
this.forEachHook(hook => hook.onTypeStart?.());
|
||||
this.renderData.forEach(data => {
|
||||
data.cut = false;
|
||||
switch (data.type) {
|
||||
@@ -530,10 +548,10 @@ export class TextContentTyper
|
||||
}
|
||||
});
|
||||
this.render?.(this.renderData, false);
|
||||
this.emit('typeEnd');
|
||||
this.forEachHook(hook => hook.onTypeEnd?.());
|
||||
return;
|
||||
}
|
||||
this.emit('typeStart');
|
||||
this.forEachHook(hook => hook.onTypeStart?.());
|
||||
// 减去间隔是为了第一个字可以立刻打出来,不然看起来有延迟
|
||||
this.lastTypeTime = Date.now() - this.config.interval - 1;
|
||||
this.typing = true;
|
||||
|
||||
Reference in new issue
Block a user