soma-core 1.0.0
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/app/api/page/route.ts +97 -0
- package/app/globals.css +3 -0
- package/app/layout.tsx +18 -0
- package/app/page.tsx +129 -0
- package/bin/soma.cjs +2 -0
- package/core/base/SomaApi.ts +14 -0
- package/core/base/SomaDict.ts +168 -0
- package/core/base/SomaEntity.ts +41 -0
- package/core/base/SomaHttpClient.ts +126 -0
- package/core/base/SomaLocaleOnlyPageRoot.ts +4 -0
- package/core/base/SomaPageRootBase.ts +54 -0
- package/core/base/SomaValidator.ts +147 -0
- package/core/base/index.ts +7 -0
- package/core/compiler/full-manifest-builder.ts +43 -0
- package/core/compiler/manifest-compiler.ts +237 -0
- package/core/compiler/route-builder.ts +31 -0
- package/core/compiler/soma-precompile.ts +48 -0
- package/core/compiler/soma-webpack-loader.js +35 -0
- package/core/compiler/vite-plugin-soma.ts +73 -0
- package/core/container/SomaContainerAdapter.ts +169 -0
- package/core/diagnostics/SomaDiagnostics.ts +186 -0
- package/core/engine/asset-resolver.ts +67 -0
- package/core/engine/dom-renderer.ts +366 -0
- package/core/engine/event-engine.ts +287 -0
- package/core/engine/lis.ts +60 -0
- package/core/engine/reactive-proxy.ts +225 -0
- package/core/engine/render-engine.ts +264 -0
- package/core/engine/runtime-builder.ts +100 -0
- package/core/engine/scope-resolver.ts +252 -0
- package/core/engine/soma-bootstrap.ts +244 -0
- package/core/index.ts +6 -0
- package/core/infra/auth/IAuthService.ts +13 -0
- package/core/infra/auth/TokenAuthService.ts +98 -0
- package/core/infra/cache/ICache.ts +13 -0
- package/core/infra/cache/MemoryCache.ts +95 -0
- package/core/infra/cache/ServerFileCache.ts +123 -0
- package/core/infra/config/IConfig.ts +9 -0
- package/core/infra/config/SomaConfig.ts +85 -0
- package/core/infra/event-bus/IEventBus.ts +9 -0
- package/core/infra/event-bus/SomaEventBus.ts +53 -0
- package/core/infra/http/IHttpClient.ts +21 -0
- package/core/infra/http/SomaHttpService.ts +141 -0
- package/core/infra/i18n/DefaultI18n.ts +41 -0
- package/core/infra/i18n/II18n.ts +7 -0
- package/core/infra/logger/ConsoleLogger.ts +81 -0
- package/core/infra/logger/ILogger.ts +19 -0
- package/core/infra/notify/DefaultNotifyService.ts +31 -0
- package/core/infra/notify/INotifyService.ts +14 -0
- package/core/infra/permission/IPermission.ts +10 -0
- package/core/infra/permission/SomaPermission.ts +48 -0
- package/core/infra/router/IRouter.ts +19 -0
- package/core/infra/router/SomaRouter.ts +66 -0
- package/core/infra/storage/IStorageService.ts +7 -0
- package/core/infra/storage/MemoryStorageService.ts +53 -0
- package/core/infra/timer/ITimer.ts +9 -0
- package/core/infra/timer/SomaTimer.ts +118 -0
- package/core/middleware/SomaMiddlewareChain.ts +29 -0
- package/core/parser/pipeline-parser.ts +276 -0
- package/core/parser/ui-config-parser.ts +336 -0
- package/core/parser/yaml-loader.ts +36 -0
- package/core/router/RouteHandler.ts +93 -0
- package/core/runtime/SomaSnapshotStore.ts +98 -0
- package/core/scheduler/SomaScheduler.ts +190 -0
- package/core/types/domain.types.ts +88 -0
- package/core/types/engine.types.ts +35 -0
- package/core/types/event.types.ts +57 -0
- package/core/types/keyboard.types.ts +18 -0
- package/core/types/manifest.types.ts +30 -0
- package/core/types/scheduler.types.ts +10 -0
- package/core/types/ui-config.types.ts +89 -0
- package/core/validation/contract-validator.ts +465 -0
- package/dist/cli/soma.js +49 -0
- package/global.d.ts +14 -0
- package/next.config.js +61 -0
- package/package.json +77 -0
- package/postcss.config.js +6 -0
- package/tailwind.config.ts +21 -0
- package/tsconfig.json +35 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { INotifyService, NotifyOptions } from './INotifyService';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* DefaultNotifyService — headless / testable notification service.
|
|
5
|
+
*
|
|
6
|
+
* Collects toasts into an in-memory queue. confirm() and alert() resolve
|
|
7
|
+
* immediately so tests and headless environments never block.
|
|
8
|
+
*/
|
|
9
|
+
export class DefaultNotifyService implements INotifyService {
|
|
10
|
+
/** All toasts received, newest last. */
|
|
11
|
+
readonly notifications: NotifyOptions[] = [];
|
|
12
|
+
|
|
13
|
+
/** Default return value for confirm(). Can be changed per-test. */
|
|
14
|
+
confirmResult = true;
|
|
15
|
+
|
|
16
|
+
toast(options: NotifyOptions): void {
|
|
17
|
+
this.notifications.push({
|
|
18
|
+
level: 'info',
|
|
19
|
+
duration: 3000,
|
|
20
|
+
...options,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async confirm(_message: string, _title?: string): Promise<boolean> {
|
|
25
|
+
return this.confirmResult;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async alert(_message: string, _title?: string): Promise<void> {
|
|
29
|
+
// nothing to do in headless mode
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type NotifyLevel = 'info' | 'success' | 'warning' | 'error';
|
|
2
|
+
|
|
3
|
+
export interface NotifyOptions {
|
|
4
|
+
message: string;
|
|
5
|
+
level?: NotifyLevel;
|
|
6
|
+
duration?: number; // ms, 0 = sticky
|
|
7
|
+
title?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface INotifyService {
|
|
11
|
+
toast(options: NotifyOptions): void;
|
|
12
|
+
confirm(message: string, title?: string): Promise<boolean>;
|
|
13
|
+
alert(message: string, title?: string): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface IPermission {
|
|
2
|
+
setRoles(roles: string[]): void;
|
|
3
|
+
setPermissions(permissions: string[]): void;
|
|
4
|
+
hasRole(role: string): boolean;
|
|
5
|
+
hasPermission(permission: string): boolean;
|
|
6
|
+
hasAnyRole(roles: string[]): boolean;
|
|
7
|
+
hasAllPermissions(permissions: string[]): boolean;
|
|
8
|
+
check(required: string | string[]): boolean;
|
|
9
|
+
reset(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { IPermission } from './IPermission';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SomaPermission — stores roles and permissions as Sets.
|
|
5
|
+
*
|
|
6
|
+
* check() accepts a single string (single permission required) or
|
|
7
|
+
* an array of strings (all required).
|
|
8
|
+
*/
|
|
9
|
+
export class SomaPermission implements IPermission {
|
|
10
|
+
private roles = new Set<string>();
|
|
11
|
+
private permissions = new Set<string>();
|
|
12
|
+
|
|
13
|
+
setRoles(roles: string[]): void {
|
|
14
|
+
this.roles = new Set(roles);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
setPermissions(permissions: string[]): void {
|
|
18
|
+
this.permissions = new Set(permissions);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
hasRole(role: string): boolean {
|
|
22
|
+
return this.roles.has(role);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
hasPermission(permission: string): boolean {
|
|
26
|
+
return this.permissions.has(permission);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
hasAnyRole(roles: string[]): boolean {
|
|
30
|
+
return roles.some((r) => this.roles.has(r));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
hasAllPermissions(permissions: string[]): boolean {
|
|
34
|
+
return permissions.every((p) => this.permissions.has(p));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
check(required: string | string[]): boolean {
|
|
38
|
+
if (typeof required === 'string') {
|
|
39
|
+
return this.permissions.has(required);
|
|
40
|
+
}
|
|
41
|
+
return required.every((p) => this.permissions.has(p));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
reset(): void {
|
|
45
|
+
this.roles.clear();
|
|
46
|
+
this.permissions.clear();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface RouteParams {
|
|
2
|
+
[key: string]: string;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface NavigateOptions {
|
|
6
|
+
replace?: boolean;
|
|
7
|
+
params?: RouteParams;
|
|
8
|
+
query?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type RouteGuard = (to: string, from: string) => boolean | Promise<boolean>;
|
|
12
|
+
|
|
13
|
+
export interface IRouter {
|
|
14
|
+
navigate(path: string, options?: NavigateOptions): void | Promise<void>;
|
|
15
|
+
getCurrentPath(): string;
|
|
16
|
+
addGuard(guard: RouteGuard): () => void;
|
|
17
|
+
buildUrl(path: string, query?: Record<string, string>): string;
|
|
18
|
+
onNavigate(callback: (path: string) => void): () => void;
|
|
19
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { IRouter, NavigateOptions, RouteGuard } from './IRouter';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SomaRouter — in-memory router implementation.
|
|
5
|
+
*
|
|
6
|
+
* Maintains currentPath in memory, supports guards (before-navigate checks),
|
|
7
|
+
* and emits callbacks on navigate. Does NOT use window.location.
|
|
8
|
+
*/
|
|
9
|
+
export class SomaRouter implements IRouter {
|
|
10
|
+
private currentPath: string;
|
|
11
|
+
private guards: Set<RouteGuard> = new Set();
|
|
12
|
+
private listeners: Set<(path: string) => void> = new Set();
|
|
13
|
+
|
|
14
|
+
constructor(initialPath: string = '/') {
|
|
15
|
+
this.currentPath = initialPath;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async navigate(path: string, options?: NavigateOptions): Promise<void> {
|
|
19
|
+
const from = this.currentPath;
|
|
20
|
+
|
|
21
|
+
if (options?.query) {
|
|
22
|
+
path = this.buildUrl(path, options.query);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Run all guards — if any rejects, cancel navigation
|
|
26
|
+
for (const guard of this.guards) {
|
|
27
|
+
const allowed = await guard(path, from);
|
|
28
|
+
if (!allowed) return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.currentPath = path;
|
|
32
|
+
|
|
33
|
+
// Notify listeners
|
|
34
|
+
for (const cb of this.listeners) {
|
|
35
|
+
cb(path);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
getCurrentPath(): string {
|
|
40
|
+
return this.currentPath;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
addGuard(guard: RouteGuard): () => void {
|
|
44
|
+
this.guards.add(guard);
|
|
45
|
+
return () => {
|
|
46
|
+
this.guards.delete(guard);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
buildUrl(path: string, query?: Record<string, string>): string {
|
|
51
|
+
if (!query || Object.keys(query).length === 0) return path;
|
|
52
|
+
|
|
53
|
+
const params = Object.entries(query)
|
|
54
|
+
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
|
|
55
|
+
.join('&');
|
|
56
|
+
|
|
57
|
+
return `${path}?${params}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
onNavigate(callback: (path: string) => void): () => void {
|
|
61
|
+
this.listeners.add(callback);
|
|
62
|
+
return () => {
|
|
63
|
+
this.listeners.delete(callback);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { IStorageService } from './IStorageService';
|
|
2
|
+
|
|
3
|
+
interface StorageEntry<T = any> {
|
|
4
|
+
value: T;
|
|
5
|
+
expiresAt: number | null; // epoch ms, null = no expiry
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* MemoryStorageService — in-memory implementation of IStorageService.
|
|
10
|
+
*
|
|
11
|
+
* Works in any JS runtime (Node.js, browser, etc.).
|
|
12
|
+
* Supports optional TTL (time-to-live) in milliseconds.
|
|
13
|
+
*/
|
|
14
|
+
export class MemoryStorageService implements IStorageService {
|
|
15
|
+
private store = new Map<string, StorageEntry>();
|
|
16
|
+
|
|
17
|
+
get<T>(key: string): T | null {
|
|
18
|
+
const entry = this.store.get(key);
|
|
19
|
+
if (!entry) return null;
|
|
20
|
+
|
|
21
|
+
if (entry.expiresAt !== null && Date.now() > entry.expiresAt) {
|
|
22
|
+
this.store.delete(key);
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return entry.value as T;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
set<T>(key: string, value: T, ttl?: number): void {
|
|
30
|
+
const expiresAt = ttl !== undefined && ttl > 0 ? Date.now() + ttl : null;
|
|
31
|
+
this.store.set(key, { value, expiresAt });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
remove(key: string): void {
|
|
35
|
+
this.store.delete(key);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
clear(): void {
|
|
39
|
+
this.store.clear();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
has(key: string): boolean {
|
|
43
|
+
const entry = this.store.get(key);
|
|
44
|
+
if (!entry) return false;
|
|
45
|
+
|
|
46
|
+
if (entry.expiresAt !== null && Date.now() > entry.expiresAt) {
|
|
47
|
+
this.store.delete(key);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface ITimer {
|
|
2
|
+
setTimeout(callback: () => void, ms: number): string;
|
|
3
|
+
setInterval(callback: () => void, ms: number): string;
|
|
4
|
+
clearTimeout(id: string): void;
|
|
5
|
+
clearInterval(id: string): void;
|
|
6
|
+
clearAll(): void;
|
|
7
|
+
debounce<T extends (...args: any[]) => void>(fn: T, ms: number): T & { cancel(): void };
|
|
8
|
+
throttle<T extends (...args: any[]) => void>(fn: T, ms: number): T & { cancel(): void };
|
|
9
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ITimer } from './ITimer';
|
|
2
|
+
|
|
3
|
+
let idCounter = 0;
|
|
4
|
+
function generateId(): string {
|
|
5
|
+
return `timer-${++idCounter}-${Date.now()}`;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* SomaTimer — wraps native setTimeout/setInterval with string IDs for tracking.
|
|
10
|
+
*
|
|
11
|
+
* clearAll cancels everything. debounce/throttle return wrapped functions with cancel().
|
|
12
|
+
*/
|
|
13
|
+
export class SomaTimer implements ITimer {
|
|
14
|
+
private timeouts = new Map<string, ReturnType<typeof globalThis.setTimeout>>();
|
|
15
|
+
private intervals = new Map<string, ReturnType<typeof globalThis.setInterval>>();
|
|
16
|
+
|
|
17
|
+
setTimeout(callback: () => void, ms: number): string {
|
|
18
|
+
const id = generateId();
|
|
19
|
+
const handle = globalThis.setTimeout(() => {
|
|
20
|
+
this.timeouts.delete(id);
|
|
21
|
+
callback();
|
|
22
|
+
}, ms);
|
|
23
|
+
this.timeouts.set(id, handle);
|
|
24
|
+
return id;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
setInterval(callback: () => void, ms: number): string {
|
|
28
|
+
const id = generateId();
|
|
29
|
+
const handle = globalThis.setInterval(callback, ms);
|
|
30
|
+
this.intervals.set(id, handle);
|
|
31
|
+
return id;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
clearTimeout(id: string): void {
|
|
35
|
+
const handle = this.timeouts.get(id);
|
|
36
|
+
if (handle !== undefined) {
|
|
37
|
+
globalThis.clearTimeout(handle);
|
|
38
|
+
this.timeouts.delete(id);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
clearInterval(id: string): void {
|
|
43
|
+
const handle = this.intervals.get(id);
|
|
44
|
+
if (handle !== undefined) {
|
|
45
|
+
globalThis.clearInterval(handle);
|
|
46
|
+
this.intervals.delete(id);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
clearAll(): void {
|
|
51
|
+
for (const handle of this.timeouts.values()) {
|
|
52
|
+
globalThis.clearTimeout(handle);
|
|
53
|
+
}
|
|
54
|
+
this.timeouts.clear();
|
|
55
|
+
|
|
56
|
+
for (const handle of this.intervals.values()) {
|
|
57
|
+
globalThis.clearInterval(handle);
|
|
58
|
+
}
|
|
59
|
+
this.intervals.clear();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
debounce<T extends (...args: any[]) => void>(fn: T, ms: number): T & { cancel(): void } {
|
|
63
|
+
let timerId: string | null = null;
|
|
64
|
+
|
|
65
|
+
const debounced = ((...args: any[]) => {
|
|
66
|
+
if (timerId !== null) {
|
|
67
|
+
this.clearTimeout(timerId);
|
|
68
|
+
}
|
|
69
|
+
timerId = this.setTimeout(() => {
|
|
70
|
+
timerId = null;
|
|
71
|
+
fn(...args);
|
|
72
|
+
}, ms);
|
|
73
|
+
}) as T & { cancel(): void };
|
|
74
|
+
|
|
75
|
+
debounced.cancel = () => {
|
|
76
|
+
if (timerId !== null) {
|
|
77
|
+
this.clearTimeout(timerId);
|
|
78
|
+
timerId = null;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return debounced;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
throttle<T extends (...args: any[]) => void>(fn: T, ms: number): T & { cancel(): void } {
|
|
86
|
+
let timerId: string | null = null;
|
|
87
|
+
let lastArgs: any[] | null = null;
|
|
88
|
+
|
|
89
|
+
const throttled = ((...args: any[]) => {
|
|
90
|
+
if (timerId !== null) {
|
|
91
|
+
// Already throttled — save latest args for trailing call
|
|
92
|
+
lastArgs = args;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fn(...args);
|
|
97
|
+
|
|
98
|
+
timerId = this.setTimeout(() => {
|
|
99
|
+
timerId = null;
|
|
100
|
+
if (lastArgs !== null) {
|
|
101
|
+
const saved = lastArgs;
|
|
102
|
+
lastArgs = null;
|
|
103
|
+
throttled(...saved);
|
|
104
|
+
}
|
|
105
|
+
}, ms);
|
|
106
|
+
}) as T & { cancel(): void };
|
|
107
|
+
|
|
108
|
+
throttled.cancel = () => {
|
|
109
|
+
if (timerId !== null) {
|
|
110
|
+
this.clearTimeout(timerId);
|
|
111
|
+
timerId = null;
|
|
112
|
+
}
|
|
113
|
+
lastArgs = null;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return throttled;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type SomaMiddleware<TContext> = (
|
|
2
|
+
context: TContext,
|
|
3
|
+
next: () => Promise<void>,
|
|
4
|
+
) => Promise<void> | void;
|
|
5
|
+
|
|
6
|
+
export class SomaMiddlewareChain<TContext> {
|
|
7
|
+
private readonly middlewares: SomaMiddleware<TContext>[] = [];
|
|
8
|
+
|
|
9
|
+
use(middleware: SomaMiddleware<TContext>): void {
|
|
10
|
+
this.middlewares.push(middleware);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async execute(
|
|
14
|
+
context: TContext,
|
|
15
|
+
terminal: (context: TContext) => Promise<void> | void,
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
const dispatch = async (index: number): Promise<void> => {
|
|
18
|
+
if (index === this.middlewares.length) {
|
|
19
|
+
await terminal(context);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const middleware = this.middlewares[index];
|
|
24
|
+
await middleware(context, () => dispatch(index + 1));
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
await dispatch(0);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
// core/parser/pipeline-parser.ts — SOMA Pipeline DSL 解析器
|
|
2
|
+
// 负责解析 _event.yaml 中的 Pipeline 表达式,将其转换为类型安全的 ParsedEvent 结构
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
ParsedEvent,
|
|
6
|
+
EventType,
|
|
7
|
+
InteractAction,
|
|
8
|
+
EventConfigRaw,
|
|
9
|
+
} from '../types/event.types';
|
|
10
|
+
|
|
11
|
+
// ─── 错误类 ───────────────────────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Pipeline DSL 解析错误
|
|
15
|
+
* 携带原始表达式以便调试
|
|
16
|
+
*/
|
|
17
|
+
export class SomaEventParseError extends Error {
|
|
18
|
+
public readonly rawExpression: string;
|
|
19
|
+
|
|
20
|
+
constructor(message: string, rawExpression: string) {
|
|
21
|
+
super(`[SOMA EventParser] ${message} — 原始表达式: "${rawExpression}"`);
|
|
22
|
+
this.name = 'SomaEventParseError';
|
|
23
|
+
this.rawExpression = rawExpression;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ─── 常量 ─────────────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
/** 支持的事件类型 */
|
|
30
|
+
const VALID_TYPES = new Set<EventType>([
|
|
31
|
+
'NAV_STATIC',
|
|
32
|
+
'NAV_ACTION',
|
|
33
|
+
'UI_REFRESH',
|
|
34
|
+
'UI_INTERACT',
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
/** 支持的 DOM 事件 */
|
|
38
|
+
const VALID_DOM_EVENTS = new Set<string>([
|
|
39
|
+
'click', 'dblclick', 'input', 'change', 'blur',
|
|
40
|
+
'focus', 'mouseenter', 'mouseleave', 'submit',
|
|
41
|
+
'keydown', 'keyup', 'scroll',
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
/** 支持的 UI 交互动作 */
|
|
45
|
+
const VALID_ACTIONS = new Set<InteractAction>(['open', 'close', 'toggle']);
|
|
46
|
+
|
|
47
|
+
import { VALID_KEY_MODIFIER_NAMES } from '../types/keyboard.types';
|
|
48
|
+
|
|
49
|
+
/** 支持的键盘修饰符(从统一定义导入) */
|
|
50
|
+
const VALID_KEY_MODIFIERS = VALID_KEY_MODIFIER_NAMES;
|
|
51
|
+
|
|
52
|
+
// ─── 选择器解析 ───────────────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 解析事件选择器字符串
|
|
56
|
+
*
|
|
57
|
+
* 支持两种格式:
|
|
58
|
+
* - 两段式:`btn.click`(somaId.domEvent)
|
|
59
|
+
* - 三段式:`input.keydown.enter`(somaId.domEvent.keyModifier)
|
|
60
|
+
*
|
|
61
|
+
* @param selector - 选择器字符串
|
|
62
|
+
* @returns 解析后的 somaId、domEvent 和可选的 keyModifier
|
|
63
|
+
* @throws SomaEventParseError 选择器格式不合法时抛出
|
|
64
|
+
*/
|
|
65
|
+
export function parseSelector(
|
|
66
|
+
selector: string,
|
|
67
|
+
): { somaId: string; domEvent: string; keyModifier?: string } {
|
|
68
|
+
const parts = selector.split('.');
|
|
69
|
+
|
|
70
|
+
if (parts.length < 2) {
|
|
71
|
+
throw new SomaEventParseError(
|
|
72
|
+
`选择器格式错误,缺少 DOM 事件: "${selector}"`,
|
|
73
|
+
selector,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (parts.length === 2) {
|
|
78
|
+
// 标准格式:somaId.event(如 btn_refresh.click)
|
|
79
|
+
const [somaId, domEvent] = parts;
|
|
80
|
+
|
|
81
|
+
if (!somaId) {
|
|
82
|
+
throw new SomaEventParseError(`选择器的 somaId 部分为空`, selector);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!VALID_DOM_EVENTS.has(domEvent)) {
|
|
86
|
+
throw new SomaEventParseError(
|
|
87
|
+
`不支持的 DOM 事件 "${domEvent}",` +
|
|
88
|
+
`支持的事件: ${[...VALID_DOM_EVENTS].join(', ')}`,
|
|
89
|
+
selector,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { somaId, domEvent };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (parts.length === 3) {
|
|
97
|
+
// 三段式:somaId.event.modifier(如 search_input.keydown.enter)
|
|
98
|
+
const [somaId, domEvent, keyModifier] = parts;
|
|
99
|
+
|
|
100
|
+
if (!somaId) {
|
|
101
|
+
throw new SomaEventParseError(`选择器的 somaId 部分为空`, selector);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!VALID_DOM_EVENTS.has(domEvent)) {
|
|
105
|
+
throw new SomaEventParseError(
|
|
106
|
+
`不支持的 DOM 事件 "${domEvent}",` +
|
|
107
|
+
`支持的事件: ${[...VALID_DOM_EVENTS].join(', ')}`,
|
|
108
|
+
selector,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!VALID_KEY_MODIFIERS.has(keyModifier)) {
|
|
113
|
+
throw new SomaEventParseError(
|
|
114
|
+
`不支持的键盘修饰符 "${keyModifier}",` +
|
|
115
|
+
`支持的修饰符: ${[...VALID_KEY_MODIFIERS].join(', ')}`,
|
|
116
|
+
selector,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return { somaId, domEvent, keyModifier };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
throw new SomaEventParseError(
|
|
124
|
+
`选择器格式错误,段数过多: "${selector}"`,
|
|
125
|
+
selector,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ─── Pipeline 表达式解析 ──────────────────────────────────────────────────────
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 解析单行 Pipeline 表达式
|
|
133
|
+
*
|
|
134
|
+
* 格式:`[节点ID].[DOM事件]: [枚举类型] | [参数1] | [参数2] | [参数3]`
|
|
135
|
+
*
|
|
136
|
+
* 各类型管道签名:
|
|
137
|
+
* - NAV_STATIC | targetUrl
|
|
138
|
+
* - NAV_ACTION | funcPath | targetUrl
|
|
139
|
+
* - UI_REFRESH | funcPath
|
|
140
|
+
* - UI_INTERACT | targetId | funcPath | action
|
|
141
|
+
*
|
|
142
|
+
* @param selector - 事件选择器(如 "btn.click")
|
|
143
|
+
* @param expression - Pipeline 表达式(如 "NAV_STATIC | /home.html")
|
|
144
|
+
* @returns 类型安全的 ParsedEvent 对象
|
|
145
|
+
* @throws SomaEventParseError 表达式不合法时抛出
|
|
146
|
+
*/
|
|
147
|
+
export function parsePipeline(
|
|
148
|
+
selector: string,
|
|
149
|
+
expression: string,
|
|
150
|
+
): ParsedEvent {
|
|
151
|
+
const { somaId, domEvent, keyModifier } = parseSelector(selector);
|
|
152
|
+
|
|
153
|
+
// 按管道符拆分,去除每段前后空格
|
|
154
|
+
const parts = expression.split('|').map((s) => s.trim());
|
|
155
|
+
const type = parts[0] as EventType;
|
|
156
|
+
|
|
157
|
+
if (!VALID_TYPES.has(type)) {
|
|
158
|
+
throw new SomaEventParseError(
|
|
159
|
+
`未知事件类型 "${type}",` +
|
|
160
|
+
`支持的类型: ${[...VALID_TYPES].join(', ')}`,
|
|
161
|
+
expression,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const result: ParsedEvent = { somaId, domEvent, type };
|
|
166
|
+
|
|
167
|
+
if (keyModifier) {
|
|
168
|
+
result.keyModifier = keyModifier;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
switch (type) {
|
|
172
|
+
case 'NAV_STATIC':
|
|
173
|
+
if (!parts[1]) {
|
|
174
|
+
throw new SomaEventParseError(
|
|
175
|
+
`NAV_STATIC 缺少必填参数 targetUrl`,
|
|
176
|
+
expression,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
result.targetUrl = parts[1];
|
|
180
|
+
break;
|
|
181
|
+
|
|
182
|
+
case 'NAV_ACTION':
|
|
183
|
+
if (!parts[1]) {
|
|
184
|
+
throw new SomaEventParseError(
|
|
185
|
+
`NAV_ACTION 缺少必填参数 funcPath`,
|
|
186
|
+
expression,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
if (!parts[2]) {
|
|
190
|
+
throw new SomaEventParseError(
|
|
191
|
+
`NAV_ACTION 缺少必填参数 targetUrl`,
|
|
192
|
+
expression,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
result.funcPath = parts[1];
|
|
196
|
+
result.targetUrl = parts[2];
|
|
197
|
+
break;
|
|
198
|
+
|
|
199
|
+
case 'UI_REFRESH':
|
|
200
|
+
if (!parts[1]) {
|
|
201
|
+
throw new SomaEventParseError(
|
|
202
|
+
`UI_REFRESH 缺少必填参数 funcPath`,
|
|
203
|
+
expression,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
result.funcPath = parts[1];
|
|
207
|
+
break;
|
|
208
|
+
|
|
209
|
+
case 'UI_INTERACT':
|
|
210
|
+
result.targetId = parts[1] || undefined; // 空字符串 → undefined(默认 self)
|
|
211
|
+
result.funcPath = parts[2] || null; // 空字符串 → null(不执行逻辑)
|
|
212
|
+
{
|
|
213
|
+
const action = parts[3] || 'toggle';
|
|
214
|
+
if (!VALID_ACTIONS.has(action as InteractAction)) {
|
|
215
|
+
throw new SomaEventParseError(
|
|
216
|
+
`不支持的交互动作 "${action}",` +
|
|
217
|
+
`支持的动作: ${[...VALID_ACTIONS].join(', ')}`,
|
|
218
|
+
expression,
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
result.action = action as InteractAction;
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return result;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ─── 事件配置递归解析 ─────────────────────────────────────────────────────────
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 递归解析事件配置(YAML 反序列化后的对象)
|
|
233
|
+
*
|
|
234
|
+
* 处理嵌套层级结构:
|
|
235
|
+
* - 叶节点(值为 string):视为事件声明,调用 parsePipeline 解析
|
|
236
|
+
* - 嵌套节点(值为 object):视为嵌套层级,递归解析
|
|
237
|
+
*
|
|
238
|
+
* 嵌套层级与 _ui.yaml 的 bind 层级一一对应。
|
|
239
|
+
*
|
|
240
|
+
* @param config - YAML 反序列化后的原始对象
|
|
241
|
+
* @param parentPath - 当前嵌套路径(用于调试和错误追踪)
|
|
242
|
+
* @returns 扁平化的 ParsedEvent 数组
|
|
243
|
+
* @throws SomaEventParseError 表达式不合法时抛出
|
|
244
|
+
*/
|
|
245
|
+
export function parseEventConfig(
|
|
246
|
+
config: EventConfigRaw,
|
|
247
|
+
parentPath: string[] = [],
|
|
248
|
+
): ParsedEvent[] {
|
|
249
|
+
const events: ParsedEvent[] = [];
|
|
250
|
+
|
|
251
|
+
for (const [key, value] of Object.entries(config)) {
|
|
252
|
+
if (typeof value === 'string') {
|
|
253
|
+
// 叶节点:事件声明
|
|
254
|
+
try {
|
|
255
|
+
events.push(parsePipeline(key, value));
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (error instanceof SomaEventParseError) {
|
|
258
|
+
// 追加嵌套路径信息以便定位
|
|
259
|
+
const path = [...parentPath, key].join(' > ');
|
|
260
|
+
throw new SomaEventParseError(
|
|
261
|
+
`${error.message} (路径: ${path})`,
|
|
262
|
+
error.rawExpression,
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
throw error;
|
|
266
|
+
}
|
|
267
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
268
|
+
// 嵌套层级:递归解析
|
|
269
|
+
events.push(
|
|
270
|
+
...parseEventConfig(value as EventConfigRaw, [...parentPath, key]),
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return events;
|
|
276
|
+
}
|