@vnejs/plugins.text.smooth 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +16 -0
- package/dist/modules/cycle.d.ts +16 -0
- package/dist/modules/cycle.js +60 -0
- package/dist/modules/emit.end.d.ts +9 -0
- package/dist/modules/emit.end.js +26 -0
- package/dist/modules/init.d.ts +6 -0
- package/dist/modules/init.js +7 -0
- package/dist/modules/process.d.ts +10 -0
- package/dist/modules/process.js +26 -0
- package/dist/modules/state.d.ts +12 -0
- package/dist/modules/state.js +9 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.js +1 -0
- package/dist/utils/smooth.d.ts +3 -0
- package/dist/utils/smooth.js +1 -0
- package/package.json +45 -0
- package/src/index.ts +19 -0
- package/src/modules/cycle.ts +98 -0
- package/src/modules/emit.end.ts +38 -0
- package/src/modules/init.ts +11 -0
- package/src/modules/process.ts +39 -0
- package/src/modules/state.ts +26 -0
- package/src/types.ts +13 -0
- package/src/utils/smooth.ts +3 -0
- package/tsconfig.json +9 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import "@vnejs/contracts.text";
|
|
2
|
+
import "@vnejs/contracts.text.smooth";
|
|
3
|
+
import { regPlugin } from "@vnejs/shared";
|
|
4
|
+
import { CONSTANTS, PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.text.smooth";
|
|
5
|
+
import { TextSmoothCycle } from "./modules/cycle.js";
|
|
6
|
+
import { TextSmoothEmitEnd } from "./modules/emit.end.js";
|
|
7
|
+
import { TextSmoothInit } from "./modules/init.js";
|
|
8
|
+
import { TextSmoothProcess } from "./modules/process.js";
|
|
9
|
+
import { TextSmoothState } from "./modules/state.js";
|
|
10
|
+
regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS }, [
|
|
11
|
+
TextSmoothInit,
|
|
12
|
+
TextSmoothState,
|
|
13
|
+
TextSmoothProcess,
|
|
14
|
+
TextSmoothCycle,
|
|
15
|
+
TextSmoothEmitEnd,
|
|
16
|
+
]);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { TextSmoothPayload } from "@vnejs/contracts.text.smooth";
|
|
3
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
4
|
+
import type { TextSmoothCycleState } from "./state.js";
|
|
5
|
+
export declare class TextSmoothCycle extends ModuleCore<TextSmoothPluginEvents, TextSmoothPluginConstants, TextSmoothPluginSettings, TextSmoothPluginParams, TextSmoothCycleState> {
|
|
6
|
+
name: string;
|
|
7
|
+
textUpdateRaf: number | null;
|
|
8
|
+
subscribe: () => void;
|
|
9
|
+
init: () => Promise<unknown[]> | undefined;
|
|
10
|
+
getSmoothState: () => import("@vnejs/contracts.text.smooth").ModuleGlobalStateTextSmooth;
|
|
11
|
+
onSmooth: ({ tokensVisible }?: TextSmoothPayload) => Promise<void>;
|
|
12
|
+
setUpdateTextRaf: (tokens?: TextSmoothPayload["tokens"]) => void;
|
|
13
|
+
createUpdateTextFunc: (tokens?: TextSmoothPayload["tokens"]) => Promise<void | unknown[]>;
|
|
14
|
+
cancelRaf: () => void;
|
|
15
|
+
onCancel: () => Promise<unknown[]> | undefined;
|
|
16
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
export class TextSmoothCycle extends ModuleCore {
|
|
3
|
+
name = "text.smooth.cycle";
|
|
4
|
+
textUpdateRaf = null;
|
|
5
|
+
subscribe = () => {
|
|
6
|
+
this.on(this.EVENTS.TEXT_SMOOTH.SMOOTH, this.onSmooth);
|
|
7
|
+
this.on(this.EVENTS.TEXT.PROCESS_CANCEL, this.onCancel);
|
|
8
|
+
this.on(this.EVENTS.TEXT.EMIT_END, this.cancelRaf);
|
|
9
|
+
this.on(this.EVENTS.STATE.CLEAR, this.cancelRaf);
|
|
10
|
+
};
|
|
11
|
+
init = () => this.emit(this.EVENTS.SETTINGS.INIT, {
|
|
12
|
+
name: this.SETTINGS.TEXT_SMOOTH.TOKEN_RENDER_SPEED,
|
|
13
|
+
value: this.PARAMS.TEXT_SMOOTH.DEFAULT_TOKEN_RENDER_SPEED,
|
|
14
|
+
});
|
|
15
|
+
getSmoothState = () => this.globalState["text.smooth"];
|
|
16
|
+
onSmooth = async ({ tokensVisible = 0 } = {}) => {
|
|
17
|
+
const { tokens = [], uid = "" } = this.globalState.text;
|
|
18
|
+
const smoothState = this.getSmoothState();
|
|
19
|
+
smoothState.tokensVisible = tokensVisible;
|
|
20
|
+
smoothState.uid = uid;
|
|
21
|
+
await this.emit(this.EVENTS.TEXT_SMOOTH.CHANGED, { tokensVisible, uid });
|
|
22
|
+
this.setState({ lastTs: new Date().getTime() });
|
|
23
|
+
this.setUpdateTextRaf(tokens);
|
|
24
|
+
};
|
|
25
|
+
setUpdateTextRaf = (tokens = []) => {
|
|
26
|
+
this.textUpdateRaf = requestAnimationFrame(() => void this.createUpdateTextFunc(tokens));
|
|
27
|
+
};
|
|
28
|
+
createUpdateTextFunc = async (tokens = []) => {
|
|
29
|
+
const { lastTs = 0 } = this.state;
|
|
30
|
+
const ts = new Date().getTime();
|
|
31
|
+
const diff = ts - lastTs;
|
|
32
|
+
const delayForOneChar = this.PARAMS.TEXT_SMOOTH.TOKEN_RENDER_DELAY / this.shared.settings[this.SETTINGS.TEXT_SMOOTH.TOKEN_RENDER_SPEED];
|
|
33
|
+
const smoothState = this.getSmoothState();
|
|
34
|
+
if (diff < delayForOneChar)
|
|
35
|
+
return this.setUpdateTextRaf(tokens);
|
|
36
|
+
smoothState.tokensVisible += Math.floor(diff / delayForOneChar);
|
|
37
|
+
const { tokens: currentTokens, uid = "" } = this.globalState.text;
|
|
38
|
+
const { tokensVisible = 0 } = smoothState;
|
|
39
|
+
if (currentTokens !== tokens) {
|
|
40
|
+
this.setDefaultState();
|
|
41
|
+
return this.emit(this.EVENTS.TEXT_SMOOTH.DONE);
|
|
42
|
+
}
|
|
43
|
+
if (tokensVisible >= (tokens?.length ?? 0)) {
|
|
44
|
+
this.setDefaultState();
|
|
45
|
+
return this.emit(this.EVENTS.TEXT_SMOOTH.DONE);
|
|
46
|
+
}
|
|
47
|
+
await this.emit(this.EVENTS.TEXT_SMOOTH.CHANGED, { tokensVisible, uid });
|
|
48
|
+
this.updateState({ lastTs: ts - (diff % delayForOneChar) });
|
|
49
|
+
return this.setUpdateTextRaf(tokens);
|
|
50
|
+
};
|
|
51
|
+
cancelRaf = () => {
|
|
52
|
+
if (this.textUpdateRaf !== null)
|
|
53
|
+
cancelAnimationFrame(this.textUpdateRaf);
|
|
54
|
+
this.textUpdateRaf = null;
|
|
55
|
+
};
|
|
56
|
+
onCancel = () => {
|
|
57
|
+
this.cancelRaf();
|
|
58
|
+
return this.emit(this.EVENTS.TEXT_SMOOTH.DONE);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
3
|
+
import type { TextEmitEndPayload } from "../utils/smooth.js";
|
|
4
|
+
export declare class TextSmoothEmitEnd extends ModuleCore<TextSmoothPluginEvents, TextSmoothPluginConstants, TextSmoothPluginSettings, TextSmoothPluginParams> {
|
|
5
|
+
name: string;
|
|
6
|
+
subscribe: () => void;
|
|
7
|
+
onTextEmitEnd: ({ isForced }?: TextEmitEndPayload) => Promise<void>;
|
|
8
|
+
onTextRecalcDone: () => Promise<unknown[] | undefined>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
export class TextSmoothEmitEnd extends ModuleCore {
|
|
3
|
+
name = "text.smooth.emit.end";
|
|
4
|
+
subscribe = () => {
|
|
5
|
+
this.on(this.EVENTS.TEXT.EMIT_END, this.onTextEmitEnd);
|
|
6
|
+
this.on(this.EVENTS.TEXT.RECALC_DONE, this.onTextRecalcDone);
|
|
7
|
+
};
|
|
8
|
+
onTextEmitEnd = async ({ isForced = true } = {}) => {
|
|
9
|
+
const { tokens = [], uid = "" } = this.globalState.text;
|
|
10
|
+
const smoothState = this.globalState["text.smooth"];
|
|
11
|
+
const tokensVisible = isForced ? this.CONST.TEXT_SMOOTH.ALL_TOKENS_VISIBLE : tokens.length;
|
|
12
|
+
smoothState.tokensVisible = tokensVisible;
|
|
13
|
+
smoothState.tokensLength = tokens.length;
|
|
14
|
+
smoothState.uid = uid;
|
|
15
|
+
await this.emit(this.EVENTS.TEXT_SMOOTH.CHANGED, { tokensVisible, uid });
|
|
16
|
+
this.shared.isTextSmoothInProcess = false;
|
|
17
|
+
};
|
|
18
|
+
onTextRecalcDone = async () => {
|
|
19
|
+
const { tokens = [], uid = "" } = this.globalState.text;
|
|
20
|
+
const smoothState = this.globalState["text.smooth"];
|
|
21
|
+
smoothState.tokensVisible = tokens.length;
|
|
22
|
+
smoothState.tokensLength = tokens.length;
|
|
23
|
+
smoothState.uid = uid;
|
|
24
|
+
return this.emit(this.EVENTS.TEXT_SMOOTH.CHANGED, { tokensVisible: tokens.length, uid });
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
3
|
+
export declare class TextSmoothInit extends ModuleCore<TextSmoothPluginEvents, TextSmoothPluginConstants, TextSmoothPluginSettings, TextSmoothPluginParams> {
|
|
4
|
+
name: string;
|
|
5
|
+
init: () => void;
|
|
6
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { TextEmitProcessPayload } from "@vnejs/contracts.text";
|
|
3
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
4
|
+
export declare class TextSmoothProcess extends ModuleCore<TextSmoothPluginEvents, TextSmoothPluginConstants, TextSmoothPluginSettings, TextSmoothPluginParams> {
|
|
5
|
+
name: string;
|
|
6
|
+
doneResolve: (() => void) | null;
|
|
7
|
+
subscribe: () => void;
|
|
8
|
+
onSmoothDone: () => void;
|
|
9
|
+
onEmitProcess: ({ isExtend }?: TextEmitProcessPayload) => Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createPromiseResolve } from "@vnejs/helpers";
|
|
2
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
3
|
+
export class TextSmoothProcess extends ModuleCore {
|
|
4
|
+
name = "text.smooth.process";
|
|
5
|
+
doneResolve = null;
|
|
6
|
+
subscribe = () => {
|
|
7
|
+
this.on(this.EVENTS.TEXT_SMOOTH.DONE, this.onSmoothDone);
|
|
8
|
+
this.on(this.EVENTS.TEXT.EMIT_PROCESS, this.onEmitProcess);
|
|
9
|
+
};
|
|
10
|
+
onSmoothDone = () => {
|
|
11
|
+
this.doneResolve?.();
|
|
12
|
+
this.doneResolve = null;
|
|
13
|
+
};
|
|
14
|
+
onEmitProcess = async ({ isExtend = false } = {}) => {
|
|
15
|
+
const { uid = "" } = this.globalState.text;
|
|
16
|
+
const smoothState = this.globalState["text.smooth"];
|
|
17
|
+
const { promise, resolve } = createPromiseResolve();
|
|
18
|
+
this.doneResolve = resolve;
|
|
19
|
+
const tokensVisible = isExtend ? smoothState.tokensLength : 0;
|
|
20
|
+
this.shared.isTextSmoothInProcess = true;
|
|
21
|
+
smoothState.tokensVisible = tokensVisible;
|
|
22
|
+
smoothState.uid = uid;
|
|
23
|
+
await this.emit(this.EVENTS.TEXT_SMOOTH.SMOOTH, { tokensVisible });
|
|
24
|
+
await promise;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { ModuleGlobalStateTextSmooth } from "@vnejs/contracts.text.smooth";
|
|
3
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
4
|
+
export declare class TextSmoothState extends ModuleCore<TextSmoothPluginEvents, TextSmoothPluginConstants, TextSmoothPluginSettings, TextSmoothPluginParams, ModuleGlobalStateTextSmooth> {
|
|
5
|
+
name: string;
|
|
6
|
+
subscribe: () => void;
|
|
7
|
+
onStateClear: () => void;
|
|
8
|
+
getDefaultState: () => ModuleGlobalStateTextSmooth;
|
|
9
|
+
}
|
|
10
|
+
export type TextSmoothCycleState = {
|
|
11
|
+
lastTs: number;
|
|
12
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
export class TextSmoothState extends ModuleCore {
|
|
3
|
+
name = "text.smooth";
|
|
4
|
+
subscribe = () => {
|
|
5
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
|
|
6
|
+
};
|
|
7
|
+
onStateClear = () => this.setState(this.getDefaultState());
|
|
8
|
+
getDefaultState = () => ({ tokensVisible: 0, tokensLength: 0, uid: "" });
|
|
9
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type { PluginName as TextPluginName, SubscribeEvents as TextSubscribeEvents } from "@vnejs/contracts.text";
|
|
3
|
+
import type { Constants, Params, PluginName, SettingsKeys, SubscribeEvents } from "@vnejs/contracts.text.smooth";
|
|
4
|
+
export type TextSmoothPluginEvents = ModuleCoreEvents & Record<PluginName, SubscribeEvents> & Record<TextPluginName, TextSubscribeEvents>;
|
|
5
|
+
export type TextSmoothPluginConstants = ModuleCoreConstants & Record<PluginName, Constants>;
|
|
6
|
+
export type TextSmoothPluginSettings = ModuleCoreSettings & Record<PluginName, SettingsKeys>;
|
|
7
|
+
export type TextSmoothPluginParams = ModuleCoreParams & Record<PluginName, Params>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.text.smooth",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major:plugin": "npm run publish:major",
|
|
24
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
25
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
26
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
27
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
28
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
29
|
+
},
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@vnejs/contracts.text.smooth": "~0.1.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@vnejs/contracts.text": "~0.1.0",
|
|
37
|
+
"@vnejs/helpers": "~0.1.0",
|
|
38
|
+
"@vnejs/module.core": "~0.1.0",
|
|
39
|
+
"@vnejs/shared": "~0.1.0",
|
|
40
|
+
"@vnejs/sources-set": "~0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@vnejs/configs.ts-common": "~0.1.0"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import "@vnejs/contracts.text";
|
|
2
|
+
import "@vnejs/contracts.text.smooth";
|
|
3
|
+
|
|
4
|
+
import { regPlugin } from "@vnejs/shared";
|
|
5
|
+
import { CONSTANTS, PARAMS, PLUGIN_NAME, SETTINGS_KEYS, SUBSCRIBE_EVENTS } from "@vnejs/contracts.text.smooth";
|
|
6
|
+
|
|
7
|
+
import { TextSmoothCycle } from "./modules/cycle.js";
|
|
8
|
+
import { TextSmoothEmitEnd } from "./modules/emit.end.js";
|
|
9
|
+
import { TextSmoothInit } from "./modules/init.js";
|
|
10
|
+
import { TextSmoothProcess } from "./modules/process.js";
|
|
11
|
+
import { TextSmoothState } from "./modules/state.js";
|
|
12
|
+
|
|
13
|
+
regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params: PARAMS }, [
|
|
14
|
+
TextSmoothInit,
|
|
15
|
+
TextSmoothState,
|
|
16
|
+
TextSmoothProcess,
|
|
17
|
+
TextSmoothCycle,
|
|
18
|
+
TextSmoothEmitEnd,
|
|
19
|
+
]);
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { TextSmoothPayload } from "@vnejs/contracts.text.smooth";
|
|
3
|
+
|
|
4
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
5
|
+
import type { TextSmoothCycleState } from "./state.js";
|
|
6
|
+
|
|
7
|
+
export class TextSmoothCycle extends ModuleCore<
|
|
8
|
+
TextSmoothPluginEvents,
|
|
9
|
+
TextSmoothPluginConstants,
|
|
10
|
+
TextSmoothPluginSettings,
|
|
11
|
+
TextSmoothPluginParams,
|
|
12
|
+
TextSmoothCycleState
|
|
13
|
+
> {
|
|
14
|
+
name = "text.smooth.cycle";
|
|
15
|
+
|
|
16
|
+
textUpdateRaf: number | null = null;
|
|
17
|
+
|
|
18
|
+
subscribe = () => {
|
|
19
|
+
this.on(this.EVENTS.TEXT_SMOOTH.SMOOTH, this.onSmooth);
|
|
20
|
+
|
|
21
|
+
this.on(this.EVENTS.TEXT.PROCESS_CANCEL, this.onCancel);
|
|
22
|
+
this.on(this.EVENTS.TEXT.EMIT_END, this.cancelRaf);
|
|
23
|
+
|
|
24
|
+
this.on(this.EVENTS.STATE.CLEAR, this.cancelRaf);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
init = () =>
|
|
28
|
+
this.emit(this.EVENTS.SETTINGS.INIT, {
|
|
29
|
+
name: this.SETTINGS.TEXT_SMOOTH.TOKEN_RENDER_SPEED,
|
|
30
|
+
value: this.PARAMS.TEXT_SMOOTH.DEFAULT_TOKEN_RENDER_SPEED,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
getSmoothState = () => this.globalState["text.smooth"];
|
|
34
|
+
|
|
35
|
+
onSmooth = async ({ tokensVisible = 0 }: TextSmoothPayload = {}) => {
|
|
36
|
+
const { tokens = [], uid = "" } = this.globalState.text;
|
|
37
|
+
const smoothState = this.getSmoothState();
|
|
38
|
+
|
|
39
|
+
smoothState.tokensVisible = tokensVisible;
|
|
40
|
+
smoothState.uid = uid;
|
|
41
|
+
|
|
42
|
+
await this.emit(this.EVENTS.TEXT_SMOOTH.CHANGED, { tokensVisible, uid });
|
|
43
|
+
|
|
44
|
+
this.setState({ lastTs: new Date().getTime() });
|
|
45
|
+
this.setUpdateTextRaf(tokens);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
setUpdateTextRaf = (tokens: TextSmoothPayload["tokens"] = []) => {
|
|
49
|
+
this.textUpdateRaf = requestAnimationFrame(() => void this.createUpdateTextFunc(tokens));
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
createUpdateTextFunc = async (tokens: TextSmoothPayload["tokens"] = []) => {
|
|
53
|
+
const { lastTs = 0 } = this.state;
|
|
54
|
+
|
|
55
|
+
const ts = new Date().getTime();
|
|
56
|
+
const diff = ts - lastTs;
|
|
57
|
+
|
|
58
|
+
const delayForOneChar = this.PARAMS.TEXT_SMOOTH.TOKEN_RENDER_DELAY / this.shared.settings[this.SETTINGS.TEXT_SMOOTH.TOKEN_RENDER_SPEED];
|
|
59
|
+
const smoothState = this.getSmoothState();
|
|
60
|
+
|
|
61
|
+
if (diff < delayForOneChar) return this.setUpdateTextRaf(tokens);
|
|
62
|
+
|
|
63
|
+
smoothState.tokensVisible += Math.floor(diff / delayForOneChar);
|
|
64
|
+
|
|
65
|
+
const { tokens: currentTokens, uid = "" } = this.globalState.text;
|
|
66
|
+
const { tokensVisible = 0 } = smoothState;
|
|
67
|
+
|
|
68
|
+
if (currentTokens !== tokens) {
|
|
69
|
+
this.setDefaultState();
|
|
70
|
+
|
|
71
|
+
return this.emit(this.EVENTS.TEXT_SMOOTH.DONE);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (tokensVisible >= (tokens?.length ?? 0)) {
|
|
75
|
+
this.setDefaultState();
|
|
76
|
+
|
|
77
|
+
return this.emit(this.EVENTS.TEXT_SMOOTH.DONE);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
await this.emit(this.EVENTS.TEXT_SMOOTH.CHANGED, { tokensVisible, uid });
|
|
81
|
+
|
|
82
|
+
this.updateState({ lastTs: ts - (diff % delayForOneChar) });
|
|
83
|
+
|
|
84
|
+
return this.setUpdateTextRaf(tokens);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
cancelRaf = () => {
|
|
88
|
+
if (this.textUpdateRaf !== null) cancelAnimationFrame(this.textUpdateRaf);
|
|
89
|
+
|
|
90
|
+
this.textUpdateRaf = null;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
onCancel = () => {
|
|
94
|
+
this.cancelRaf();
|
|
95
|
+
|
|
96
|
+
return this.emit(this.EVENTS.TEXT_SMOOTH.DONE);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
|
|
3
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
4
|
+
import type { TextEmitEndPayload } from "../utils/smooth.js";
|
|
5
|
+
|
|
6
|
+
export class TextSmoothEmitEnd extends ModuleCore<TextSmoothPluginEvents, TextSmoothPluginConstants, TextSmoothPluginSettings, TextSmoothPluginParams> {
|
|
7
|
+
name = "text.smooth.emit.end";
|
|
8
|
+
|
|
9
|
+
subscribe = () => {
|
|
10
|
+
this.on(this.EVENTS.TEXT.EMIT_END, this.onTextEmitEnd);
|
|
11
|
+
this.on(this.EVENTS.TEXT.RECALC_DONE, this.onTextRecalcDone);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
onTextEmitEnd = async ({ isForced = true }: TextEmitEndPayload = {}) => {
|
|
15
|
+
const { tokens = [], uid = "" } = this.globalState.text;
|
|
16
|
+
const smoothState = this.globalState["text.smooth"];
|
|
17
|
+
const tokensVisible = isForced ? this.CONST.TEXT_SMOOTH.ALL_TOKENS_VISIBLE : tokens.length;
|
|
18
|
+
|
|
19
|
+
smoothState.tokensVisible = tokensVisible;
|
|
20
|
+
smoothState.tokensLength = tokens.length;
|
|
21
|
+
smoothState.uid = uid;
|
|
22
|
+
|
|
23
|
+
await this.emit(this.EVENTS.TEXT_SMOOTH.CHANGED, { tokensVisible, uid });
|
|
24
|
+
|
|
25
|
+
this.shared.isTextSmoothInProcess = false;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
onTextRecalcDone = async () => {
|
|
29
|
+
const { tokens = [], uid = "" } = this.globalState.text;
|
|
30
|
+
const smoothState = this.globalState["text.smooth"];
|
|
31
|
+
|
|
32
|
+
smoothState.tokensVisible = tokens.length;
|
|
33
|
+
smoothState.tokensLength = tokens.length;
|
|
34
|
+
smoothState.uid = uid;
|
|
35
|
+
|
|
36
|
+
return this.emit(this.EVENTS.TEXT_SMOOTH.CHANGED, { tokensVisible: tokens.length, uid });
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
|
|
3
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
4
|
+
|
|
5
|
+
export class TextSmoothInit extends ModuleCore<TextSmoothPluginEvents, TextSmoothPluginConstants, TextSmoothPluginSettings, TextSmoothPluginParams> {
|
|
6
|
+
name = "text.smooth.init";
|
|
7
|
+
|
|
8
|
+
init = () => {
|
|
9
|
+
this.shared.isTextSmoothInProcess = false;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createPromiseResolve } from "@vnejs/helpers";
|
|
2
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
3
|
+
import type { TextEmitProcessPayload } from "@vnejs/contracts.text";
|
|
4
|
+
|
|
5
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
6
|
+
|
|
7
|
+
export class TextSmoothProcess extends ModuleCore<TextSmoothPluginEvents, TextSmoothPluginConstants, TextSmoothPluginSettings, TextSmoothPluginParams> {
|
|
8
|
+
name = "text.smooth.process";
|
|
9
|
+
|
|
10
|
+
doneResolve: (() => void) | null = null;
|
|
11
|
+
|
|
12
|
+
subscribe = () => {
|
|
13
|
+
this.on(this.EVENTS.TEXT_SMOOTH.DONE, this.onSmoothDone);
|
|
14
|
+
|
|
15
|
+
this.on(this.EVENTS.TEXT.EMIT_PROCESS, this.onEmitProcess);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
onSmoothDone = () => {
|
|
19
|
+
this.doneResolve?.();
|
|
20
|
+
this.doneResolve = null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
onEmitProcess = async ({ isExtend = false }: TextEmitProcessPayload = {}) => {
|
|
24
|
+
const { uid = "" } = this.globalState.text;
|
|
25
|
+
const smoothState = this.globalState["text.smooth"];
|
|
26
|
+
const { promise, resolve } = createPromiseResolve<void>();
|
|
27
|
+
|
|
28
|
+
this.doneResolve = resolve;
|
|
29
|
+
|
|
30
|
+
const tokensVisible = isExtend ? smoothState.tokensLength : 0;
|
|
31
|
+
|
|
32
|
+
this.shared.isTextSmoothInProcess = true;
|
|
33
|
+
smoothState.tokensVisible = tokensVisible;
|
|
34
|
+
smoothState.uid = uid;
|
|
35
|
+
|
|
36
|
+
await this.emit(this.EVENTS.TEXT_SMOOTH.SMOOTH, { tokensVisible });
|
|
37
|
+
await promise;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
2
|
+
import type { ModuleGlobalStateTextSmooth } from "@vnejs/contracts.text.smooth";
|
|
3
|
+
|
|
4
|
+
import type { TextSmoothPluginConstants, TextSmoothPluginEvents, TextSmoothPluginParams, TextSmoothPluginSettings } from "../types.js";
|
|
5
|
+
|
|
6
|
+
export class TextSmoothState extends ModuleCore<
|
|
7
|
+
TextSmoothPluginEvents,
|
|
8
|
+
TextSmoothPluginConstants,
|
|
9
|
+
TextSmoothPluginSettings,
|
|
10
|
+
TextSmoothPluginParams,
|
|
11
|
+
ModuleGlobalStateTextSmooth
|
|
12
|
+
> {
|
|
13
|
+
name = "text.smooth";
|
|
14
|
+
|
|
15
|
+
subscribe = () => {
|
|
16
|
+
this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
onStateClear = () => this.setState(this.getDefaultState());
|
|
20
|
+
|
|
21
|
+
getDefaultState = (): ModuleGlobalStateTextSmooth => ({ tokensVisible: 0, tokensLength: 0, uid: "" });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type TextSmoothCycleState = {
|
|
25
|
+
lastTs: number;
|
|
26
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type { PluginName as TextPluginName, SubscribeEvents as TextSubscribeEvents } from "@vnejs/contracts.text";
|
|
3
|
+
import type { Constants, Params, PluginName, SettingsKeys, SubscribeEvents } from "@vnejs/contracts.text.smooth";
|
|
4
|
+
|
|
5
|
+
export type TextSmoothPluginEvents = ModuleCoreEvents &
|
|
6
|
+
Record<PluginName, SubscribeEvents> &
|
|
7
|
+
Record<TextPluginName, TextSubscribeEvents>;
|
|
8
|
+
|
|
9
|
+
export type TextSmoothPluginConstants = ModuleCoreConstants & Record<PluginName, Constants>;
|
|
10
|
+
|
|
11
|
+
export type TextSmoothPluginSettings = ModuleCoreSettings & Record<PluginName, SettingsKeys>;
|
|
12
|
+
|
|
13
|
+
export type TextSmoothPluginParams = ModuleCoreParams & Record<PluginName, Params>;
|