@lingfan-engine/shell 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright (c) 2026 MoFeng-02
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "@lingfan-engine/shell",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "description": "灵泛叙事引擎宿主编排层:平台无关的宿主装配逻辑(提示条、事件落点、面板数据、叙事状态投影),消费方用自己的响应式系统落地",
6
+ "license": "Apache-2.0",
7
+ "exports": {
8
+ ".": "./src/index.ts"
9
+ },
10
+ "files": [
11
+ "src",
12
+ "!src/**/*.test.ts"
13
+ ],
14
+ "sideEffects": false,
15
+ "dependencies": {
16
+ "@lingfan-engine/core": "^0.1.1",
17
+ "@lingfan-engine/ui": "^0.1.1"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/MoFeng-02/LingFan.Engine.git",
22
+ "directory": "packages/shell"
23
+ },
24
+ "homepage": "https://github.com/MoFeng-02/LingFan.Engine#readme",
25
+ "bugs": {
26
+ "url": "https://github.com/MoFeng-02/LingFan.Engine/issues"
27
+ },
28
+ "keywords": [
29
+ "narrative",
30
+ "host",
31
+ "orchestration",
32
+ "stage",
33
+ "framework-agnostic"
34
+ ],
35
+ "engines": {
36
+ "node": ">=22.12.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ }
6
41
  }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * 响应式最小接口(Cell):宿主跨层传递可变值与只读值的最小形状。
3
+ *
4
+ * 宿主编排层不绑定任何响应式框架——它需要的只是「一个带 `value` 的盒子」。
5
+ * 任意框架的响应式单元(Vue 的 `ref`、Solid 的 signal 包装等)或一个普通的
6
+ * `{ value: T }` 对象都满足此形状,可直接赋给 `Cell`/`ReadonlyCell`,零包装。
7
+ */
8
+
9
+ /** 可变值盒子:持有方写 `value`,消费方读 `value` */
10
+ export interface Cell<T> {
11
+ value: T;
12
+ }
13
+
14
+ /** 只读值盒子:持有方维护,消费方只读 */
15
+ export interface ReadonlyCell<T> {
16
+ readonly value: T;
17
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 叙事事件域出口:提示条、读档诊断与错误横幅的落点实现。
3
+ */
4
+ export {
5
+ createNarrativeEvents,
6
+ type NarrativeEventOptions,
7
+ type NarrativeEvents,
8
+ } from "./narrativeEvents";
@@ -0,0 +1,99 @@
1
+ /**
2
+ * 叙事事件的落点:提示条、读档诊断与错误横幅。
3
+ *
4
+ * 事件怎么分流(哪个 `kind` 走哪条)留在组合根——分流顺序是接线契约;这里只收
5
+ * 「落下去做什么」。读档与回溯共用同一条收尾顺序:先清错误横幅,再把视图状态与
6
+ * 媒体渲染器对齐引擎当前状态,最后才提示——顺序反了会让玩家先看到提示、再看到
7
+ * 画面跳回存档坐标。写档不动画面,只清错误横幅:横幅报的是「上一次操作的结果」,
8
+ * 这一次既然成功,上一次的失败就已经过期,留着会让玩家以为仍有故障。
9
+ *
10
+ * 需要刷新渲染/媒体的分支把动作以具名回调注入,免得本模块反向依赖组合根手里的
11
+ * 渲染器与视图状态。
12
+ *
13
+ * 本模块平台无关:不触碰任何框架、DOM 或宿主全局。
14
+ */
15
+ import type { Notices } from "../notices";
16
+
17
+ /** 叙事事件落点需要的外部事实与出口 */
18
+ export interface NarrativeEventOptions {
19
+ /** 提示条出口(语气与驻留档位由它决定) */
20
+ notices: Notices;
21
+ /** 清错误横幅 */
22
+ clearError(): void;
23
+ /** 视图状态整体对齐引擎当前状态 */
24
+ syncFromEngine(): void;
25
+ /** 媒体渲染器对齐(音频位置 + 视频命令流) */
26
+ syncMedia(): void;
27
+ /** 错误横幅写入 */
28
+ reportError(message: string): void;
29
+ /** 错误横幅文案:把引擎错误码与消息拼成一句话;缺省 = `code: message` */
30
+ formatError?(code: string, message: string): string;
31
+ }
32
+
33
+ /** 叙事事件落点 */
34
+ export interface NarrativeEvents {
35
+ /** 通知:语气由 `notifyType` 归一,时长缺省走通知档 */
36
+ notify(
37
+ text: string,
38
+ notifyType: unknown,
39
+ durationMs: number | undefined,
40
+ ): void;
41
+ /** 写档成功(失败走错误分支,不提示成功;同时清掉上一次失败留下的错误横幅) */
42
+ saved(slot: string): void;
43
+ /** 读档完成:引擎已重放到存档坐标 */
44
+ loaded(slot: string): void;
45
+ /** 回溯完成:解除输入锁并同步渲染 */
46
+ rollbackDone(): void;
47
+ /** 读档诊断(非致命,宿主应知情) */
48
+ loadNotice(text: string): void;
49
+ /** 引擎错误:`code: message` 进错误横幅 */
50
+ failed(code: string, message: string): void;
51
+ }
52
+
53
+ /**
54
+ * 创建叙事事件落点。读档与回溯共用的收尾顺序在本模块内只写一次。
55
+ */
56
+ export function createNarrativeEvents(
57
+ options: NarrativeEventOptions,
58
+ ): NarrativeEvents {
59
+ const formatError = options.formatError ?? defaultFormatError;
60
+ /** 读档 / 回溯共用的收尾:清错误 → 视图与媒体对齐 → 提示 */
61
+ function realign(): void {
62
+ options.clearError();
63
+ options.syncFromEngine();
64
+ options.syncMedia();
65
+ }
66
+
67
+ return {
68
+ notify(
69
+ text: string,
70
+ notifyType: unknown,
71
+ durationMs: number | undefined,
72
+ ): void {
73
+ options.notices.notify(text, notifyType, durationMs);
74
+ },
75
+ saved(slot: string): void {
76
+ options.clearError();
77
+ options.notices.saved(slot);
78
+ },
79
+ loaded(slot: string): void {
80
+ realign();
81
+ options.notices.loaded(slot);
82
+ },
83
+ rollbackDone(): void {
84
+ realign();
85
+ options.notices.rollbackDone();
86
+ },
87
+ loadNotice(text: string): void {
88
+ options.notices.loadNotice(text);
89
+ },
90
+ failed(code: string, message: string): void {
91
+ options.reportError(formatError(code, message));
92
+ },
93
+ };
94
+ }
95
+
96
+ /** 错误横幅的缺省拼法:`code: message`(宿主可用 formatError 覆盖) */
97
+ function defaultFormatError(code: string, message: string): string {
98
+ return `${code}: ${message}`;
99
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * 宿主帧循环装配:把「每帧推进什么」串成一次连续续帧的循环,帧源由宿主注入。
3
+ *
4
+ * 时间线与每帧的推进顺序由共享实现固定(打字机推进 → 上交可见文本 → 媒体回写 → 帧驱动表现),
5
+ * 本模块只负责接上帧源与启动:浏览器宿主传 rAF 帧源,小程序宿主传画布帧源。
6
+ *
7
+ * `onText` 每帧无条件上交打字机的可见前缀,没有打字机时上交空串——整句直出时打字机可能已被丢弃,
8
+ * 所以调用方要自己用当前句文本兜底,不能把空串直接当成上屏内容。
9
+ */
10
+ import {
11
+ createFrameLoop,
12
+ type FrameLoop,
13
+ type FrameSource,
14
+ } from "@lingfan-engine/ui";
15
+
16
+ /** 宿主帧循环需要的帧源与回调 */
17
+ export interface HostFrameLoopOptions {
18
+ /** 帧源:宿主把平台的「下一帧」能力包成这两个方法(浏览器 rAF / 小程序画布) */
19
+ frame: FrameSource;
20
+ /** 取当前打字机;返回 null 表示本帧没有打字机 */
21
+ readTypewriter(): {
22
+ tick(dtSeconds: number): void;
23
+ readonly visible: string;
24
+ } | null;
25
+ /** 每帧上交打字机可见前缀(无打字机时为空串,由调用方决定怎么兜底) */
26
+ onText(visible: string): void;
27
+ /** 媒体位置回写(播放进度轮询等) */
28
+ onMediaTick(): void;
29
+ /** 帧驱动表现(元素动画 / 全屏转场 / 屏幕震动) */
30
+ onFrame(dt: number): void;
31
+ }
32
+
33
+ /**
34
+ * 起转帧循环:以注入的帧源排帧,返回的句柄可随时 `stop()`。
35
+ *
36
+ * 重复起转由共享实现挡住(已在运行则原样返回),调用方不必自己判重。
37
+ */
38
+ export function startHostFrameLoop(options: HostFrameLoopOptions): FrameLoop {
39
+ const loop = createFrameLoop({
40
+ frame: options.frame,
41
+ // 每帧顺序固定:打字机推进 → 上交可见文本 → 媒体回写 → 帧驱动表现
42
+ onTick: (dt) => {
43
+ const typewriter = options.readTypewriter();
44
+ typewriter?.tick(dt);
45
+ options.onText(typewriter?.visible ?? "");
46
+ options.onMediaTick();
47
+ options.onFrame(dt);
48
+ },
49
+ });
50
+ loop.start();
51
+ return loop;
52
+ }
@@ -0,0 +1,6 @@
1
+ /** 宿主帧循环域:帧源由宿主注入,推进顺序与收尾语义在这里固定。 */
2
+
3
+ export {
4
+ startHostFrameLoop,
5
+ type HostFrameLoopOptions,
6
+ } from "./appFrameLoop.ts";
package/src/index.ts ADDED
@@ -0,0 +1,69 @@
1
+ /**
2
+ * 宿主编排层出口(唯一出口):平台无关的宿主装配逻辑收在一处。
3
+ *
4
+ * 各域各管一段:cell 是跨层传值的最小接口;notices 是提示条编排;events 是
5
+ * 叙事事件落点;panel 是面板数据整形;state 是引擎状态键 → 视图状态的投影;
6
+ * frame 是宿主帧循环装配;stage 是舞台适配计算(视口 → 缩放与偏移)。平台宿主
7
+ * (浏览器/Tauri/小程序)从这里取编排逻辑,各自提供状态单元、帧源、视口尺寸与渲染层。
8
+ */
9
+
10
+ /** 响应式最小接口:`Cell`/`ReadonlyCell` */
11
+ export type { Cell, ReadonlyCell } from "./cell";
12
+ /** 提示条编排 */
13
+ export {
14
+ createNotices,
15
+ type Notice,
16
+ type NoticeOptions,
17
+ type NoticeTexts,
18
+ type Notices,
19
+ } from "./notices";
20
+ /** 叙事事件落点 */
21
+ export {
22
+ createNarrativeEvents,
23
+ type NarrativeEventOptions,
24
+ type NarrativeEvents,
25
+ } from "./events";
26
+ /** 面板数据:历史聚合与槽位视图 */
27
+ export {
28
+ buildHistoryBlocks,
29
+ canChooseSlot,
30
+ defaultSlotLabel,
31
+ filterHistoryEntries,
32
+ loadSlotViews,
33
+ parseSlotMeta,
34
+ withSlotLabels,
35
+ type HistoryBlock,
36
+ type HistoryEntry,
37
+ type SlotPanelMode,
38
+ type SlotPanelOptions,
39
+ type SlotView,
40
+ } from "./panel";
41
+ /** 叙事视图状态投影 */
42
+ export {
43
+ createNarrativeState,
44
+ type MenuChoice,
45
+ type NarrativeState,
46
+ type NarrativeStateOptions,
47
+ type StateCells,
48
+ } from "./state";
49
+ /** 宿主帧循环:帧源由宿主注入,覆盖层与各宿主共用一份实现 */
50
+ export { startHostFrameLoop, type HostFrameLoopOptions } from "./frame";
51
+ /** 舞台适配:设计基准 + 策略 → 缩放与偏移,视口尺寸由宿主读 */
52
+ export {
53
+ computeStageLayout,
54
+ DEFAULT_STAGE_FIT,
55
+ DEFAULT_STAGE_HEIGHT,
56
+ DEFAULT_STAGE_WIDTH,
57
+ resolveStagePreset,
58
+ stageTransform,
59
+ STAGE_BASELINES,
60
+ STAGE_PRESETS,
61
+ type StageAlign,
62
+ type StageBaseline,
63
+ type StageConfig,
64
+ type StageFit,
65
+ type StageLayout,
66
+ type StagePreset,
67
+ type StageScalePolicy,
68
+ type StageViewport,
69
+ } from "./stage";
@@ -0,0 +1,8 @@
1
+ /** 提示条域出口:提示条的装配、驻留与文案模板。 */
2
+ export {
3
+ createNotices,
4
+ type Notice,
5
+ type NoticeOptions,
6
+ type Notices,
7
+ type NoticeTexts,
8
+ } from "./noticeCenter";
@@ -0,0 +1,169 @@
1
+ /**
2
+ * 提示条编排:把「说什么、什么语气、停留多久」收成一处。
3
+ *
4
+ * 驻留时长是宿主策略而非引擎状态,四档默认值按用途分开(通知供阅读最长、操作反馈最短、
5
+ * 读档提示多留一档、回溯提示与反馈同档),由装配方注入——数值本身不在这里定死。
6
+ *
7
+ * 计时器到点即把对应条目从列表里摘掉,所以 `items` 里只留仍在驻留的提示;
8
+ * 模板按 `id` 做 key,摘除时不会误伤同文案的另一条。
9
+ *
10
+ * 存/读文案里的槽位名与槽位面板同一个来源(`labelFor`,缺省到面板的显示名规则):
11
+ * 面板上写「槽位 1」,提示条也必须写「槽位 1」,不能退回槽位 id。
12
+ *
13
+ * 本模块平台无关:状态放在调用方传入的 Cell 里,定时器走注入的 TimerPort,
14
+ * 不直接触碰任何框架或宿主全局。
15
+ */
16
+ import { hostTimer, type TimerPort } from "@lingfan-engine/core";
17
+ import {
18
+ DEFAULT_ACTION_TOAST_MS,
19
+ DEFAULT_LOAD_TOAST_MS,
20
+ DEFAULT_NOTIFY_MS,
21
+ DEFAULT_ROLLBACK_NOTICE_MS,
22
+ toNotifyTone,
23
+ type NotifyTone,
24
+ } from "@lingfan-engine/ui";
25
+ import type { Cell } from "../cell";
26
+ import { defaultSlotLabel } from "../panel/slots";
27
+
28
+ /**
29
+ * 提示文案缺省模板:宿主可用文案选项逐句覆盖(本地化/改写均走这里)。
30
+ *
31
+ * 槽位名一律经 `labelFor` 渲染,缺省与槽位面板同源(`defaultSlotLabel`)——
32
+ * 玩家在面板上点的是「槽位 1」,提示条就不该说 `slot_1`。
33
+ */
34
+ function defaultTexts(labelFor: (id: string) => string): Required<NoticeTexts> {
35
+ return {
36
+ /** 存档完成 */
37
+ saved: (slot: string): string => `已保存到 ${labelFor(slot)}`,
38
+ /** 读档完成(含状态说明) */
39
+ loaded: (slot: string): string =>
40
+ `已读取 ${labelFor(slot)}(回到存档时刻)`,
41
+ /** 回溯完成 */
42
+ rollbackDone: (): string => "已回溯",
43
+ };
44
+ }
45
+
46
+ /** 可覆盖的提示文案(逐句可选;未给的句子用缺省模板) */
47
+ export interface NoticeTexts {
48
+ /** 存档完成(参数 = 槽位 id) */
49
+ saved?(slot: string): string;
50
+ /** 读档完成(参数 = 槽位 id) */
51
+ loaded?(slot: string): string;
52
+ /** 回溯完成 */
53
+ rollbackDone?(): string;
54
+ }
55
+
56
+ /** 一条提示:`id` 唯一(同文案可并存多条),`tone` 决定皮肤类 */
57
+ export interface Notice {
58
+ id: number;
59
+ text: string;
60
+ tone: NotifyTone;
61
+ }
62
+
63
+ /** 提示条编排需要的外部事实:状态落点与定时能力 */
64
+ export interface NoticeOptions {
65
+ /** 提示列表落点(仍在驻留的条目,按 id 唯一);缺省 = 模块自持普通 Cell */
66
+ items?: Cell<Notice[]>;
67
+ /** 定时器(驻留到期摘除);缺省 = 宿主内建定时器 */
68
+ timer?: TimerPort;
69
+ /** `notify` 未自带 duration 时的驻留 */
70
+ notifyDurationMs?: number;
71
+ /** 操作反馈(存/读按钮等)的驻留 */
72
+ toastDurationMs?: number;
73
+ /** 读档完成提示的驻留 */
74
+ loadToastDurationMs?: number;
75
+ /** 「已回溯」轻提示的驻留 */
76
+ rollbackNoticeDurationMs?: number;
77
+ /**
78
+ * 槽位显示名:把提示条里的槽位 id 变成玩家看得懂的称呼。
79
+ * 缺省 = 槽位面板的显示名规则(`defaultSlotLabel`);宿主自定义命名时,
80
+ * 应与 `SlotPanelOptions.labelFor` 传同一个函数,好让两处叫法一致。
81
+ */
82
+ labelFor?(id: string): string;
83
+ /** 可覆盖的提示文案(本地化/改写入口);缺省 = 内置中文缺省模板 */
84
+ texts?: NoticeTexts;
85
+ }
86
+
87
+ /** 提示条出口:列表供模板渲染,其余方法各自负责一句话的装配与驻留 */
88
+ export interface Notices {
89
+ readonly items: Cell<Notice[]>;
90
+ /** 通用投递:显式给时长与语气 */
91
+ push(text: string, tone: NotifyTone, durationMs: number): void;
92
+ /** 引擎 `notify` 事件:语气由 `notifyType` 归一,时长缺省走通知档 */
93
+ notify(
94
+ text: string,
95
+ notifyType: unknown,
96
+ durationMs: number | undefined,
97
+ ): void;
98
+ /** 操作反馈:语气缺省 info,时长缺省走反馈档 */
99
+ toast(text: string, durationMs?: number, tone?: NotifyTone): void;
100
+ /** 存档完成 */
101
+ saved(slot: string): void;
102
+ /** 读档完成(文案含状态说明,故用读档档时长) */
103
+ loaded(slot: string): void;
104
+ /** 回溯完成 */
105
+ rollbackDone(): void;
106
+ /** 引擎侧的读档诊断(非致命,宿主应知情) */
107
+ loadNotice(text: string): void;
108
+ }
109
+
110
+ /**
111
+ * 创建提示条编排。`items` 由调用方持有——框架宿主传响应式 Cell,模板随写随渲染;
112
+ * 未传 `items` 也可独立使用(内部持有普通 Cell,供无渲染场景或测试)。
113
+ * 定时器经 TimerPort 注入,驻留摘除对调用方透明。
114
+ */
115
+ export function createNotices(options: NoticeOptions = {}): Notices {
116
+ const items: Cell<Notice[]> = options.items ?? { value: [] };
117
+ const notifyDurationMs = options.notifyDurationMs ?? DEFAULT_NOTIFY_MS;
118
+ const toastDurationMs = options.toastDurationMs ?? DEFAULT_ACTION_TOAST_MS;
119
+ const loadToastDurationMs =
120
+ options.loadToastDurationMs ?? DEFAULT_LOAD_TOAST_MS;
121
+ const rollbackNoticeDurationMs =
122
+ options.rollbackNoticeDurationMs ?? DEFAULT_ROLLBACK_NOTICE_MS;
123
+ const texts: Required<NoticeTexts> = {
124
+ ...defaultTexts(options.labelFor ?? defaultSlotLabel),
125
+ ...options.texts,
126
+ };
127
+ const timer: TimerPort = options.timer ?? hostTimer;
128
+
129
+ let seq = 0;
130
+
131
+ const push = (text: string, tone: NotifyTone, durationMs: number): void => {
132
+ const id = ++seq;
133
+ items.value = [...items.value, { id, text, tone }];
134
+ timer.set(() => {
135
+ items.value = items.value.filter((n) => n.id !== id);
136
+ }, durationMs);
137
+ };
138
+
139
+ return {
140
+ items,
141
+ push,
142
+ notify(
143
+ text: string,
144
+ notifyType: unknown,
145
+ durationMs: number | undefined,
146
+ ): void {
147
+ push(text, toNotifyTone(notifyType), durationMs ?? notifyDurationMs);
148
+ },
149
+ toast(
150
+ text: string,
151
+ durationMs: number = toastDurationMs,
152
+ tone: NotifyTone = "info",
153
+ ): void {
154
+ push(text, tone, durationMs);
155
+ },
156
+ saved(slot: string): void {
157
+ push(texts.saved(slot), "info", toastDurationMs);
158
+ },
159
+ loaded(slot: string): void {
160
+ push(texts.loaded(slot), "info", loadToastDurationMs);
161
+ },
162
+ rollbackDone(): void {
163
+ push(texts.rollbackDone(), "info", rollbackNoticeDurationMs);
164
+ },
165
+ loadNotice(text: string): void {
166
+ push(text, "warning", rollbackNoticeDurationMs);
167
+ },
168
+ };
169
+ }