@object-ui/core 0.3.1 → 0.5.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/.turbo/turbo-build.log +4 -0
- package/dist/actions/index.d.ts +1 -1
- package/dist/actions/index.js +1 -1
- package/dist/evaluator/ExpressionCache.d.ts +101 -0
- package/dist/evaluator/ExpressionCache.js +135 -0
- package/dist/evaluator/ExpressionEvaluator.d.ts +20 -2
- package/dist/evaluator/ExpressionEvaluator.js +34 -14
- package/dist/evaluator/index.d.ts +3 -2
- package/dist/evaluator/index.js +3 -2
- package/dist/index.d.ts +10 -7
- package/dist/index.js +9 -7
- package/dist/query/index.d.ts +6 -0
- package/dist/query/index.js +6 -0
- package/dist/query/query-ast.d.ts +32 -0
- package/dist/query/query-ast.js +268 -0
- package/dist/registry/PluginScopeImpl.d.ts +80 -0
- package/dist/registry/PluginScopeImpl.js +243 -0
- package/dist/registry/PluginSystem.d.ts +66 -0
- package/dist/registry/PluginSystem.js +142 -0
- package/dist/registry/Registry.d.ts +73 -4
- package/dist/registry/Registry.js +112 -7
- package/dist/validation/index.d.ts +9 -0
- package/dist/validation/index.js +9 -0
- package/dist/validation/validation-engine.d.ts +70 -0
- package/dist/validation/validation-engine.js +363 -0
- package/dist/validation/validators/index.d.ts +16 -0
- package/dist/validation/validators/index.js +16 -0
- package/dist/validation/validators/object-validation-engine.d.ts +118 -0
- package/dist/validation/validators/object-validation-engine.js +538 -0
- package/package.json +13 -5
- package/src/actions/index.ts +1 -1
- package/src/evaluator/ExpressionCache.ts +192 -0
- package/src/evaluator/ExpressionEvaluator.ts +33 -14
- package/src/evaluator/__tests__/ExpressionCache.test.ts +135 -0
- package/src/evaluator/index.ts +3 -2
- package/src/index.ts +10 -7
- package/src/query/__tests__/query-ast.test.ts +211 -0
- package/src/query/__tests__/window-functions.test.ts +275 -0
- package/src/query/index.ts +7 -0
- package/src/query/query-ast.ts +341 -0
- package/src/registry/PluginScopeImpl.ts +259 -0
- package/src/registry/PluginSystem.ts +161 -0
- package/src/registry/Registry.ts +125 -8
- package/src/registry/__tests__/PluginSystem.test.ts +226 -0
- package/src/registry/__tests__/Registry.test.ts +293 -0
- package/src/registry/__tests__/plugin-scope-integration.test.ts +283 -0
- package/src/validation/__tests__/object-validation-engine.test.ts +567 -0
- package/src/validation/__tests__/validation-engine.test.ts +102 -0
- package/src/validation/index.ts +10 -0
- package/src/validation/validation-engine.ts +461 -0
- package/src/validation/validators/index.ts +25 -0
- package/src/validation/validators/object-validation-engine.ts +722 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.config.ts +2 -0
- package/src/adapters/index.d.ts +0 -8
- package/src/adapters/index.js +0 -10
- package/src/builder/schema-builder.d.ts +0 -294
- package/src/builder/schema-builder.js +0 -503
- package/src/index.d.ts +0 -13
- package/src/index.js +0 -16
- package/src/registry/Registry.d.ts +0 -56
- package/src/registry/Registry.js +0 -43
- package/src/types/index.d.ts +0 -19
- package/src/types/index.js +0 -8
- package/src/utils/filter-converter.d.ts +0 -57
- package/src/utils/filter-converter.js +0 -100
- package/src/validation/schema-validator.d.ts +0 -94
- package/src/validation/schema-validator.js +0 -278
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @object-ui/core - Plugin Scope Implementation
|
|
11
|
+
*
|
|
12
|
+
* Section 3.3: Implementation of scoped plugin system to prevent conflicts.
|
|
13
|
+
* Provides isolated component registration, state management, and event bus.
|
|
14
|
+
*
|
|
15
|
+
* @module plugin-scope-impl
|
|
16
|
+
* @packageDocumentation
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type {
|
|
20
|
+
PluginScope,
|
|
21
|
+
PluginScopeConfig,
|
|
22
|
+
PluginEventHandler
|
|
23
|
+
} from '@object-ui/types';
|
|
24
|
+
import type { Registry, ComponentMeta as RegistryComponentMeta } from './Registry.js';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Event Bus for scoped plugin events
|
|
28
|
+
*/
|
|
29
|
+
class EventBus {
|
|
30
|
+
private listeners = new Map<string, Set<PluginEventHandler>>();
|
|
31
|
+
|
|
32
|
+
on(event: string, handler: PluginEventHandler): () => void {
|
|
33
|
+
if (!this.listeners.has(event)) {
|
|
34
|
+
this.listeners.set(event, new Set());
|
|
35
|
+
}
|
|
36
|
+
this.listeners.get(event)!.add(handler);
|
|
37
|
+
|
|
38
|
+
// Return unsubscribe function
|
|
39
|
+
return () => {
|
|
40
|
+
this.listeners.get(event)?.delete(handler);
|
|
41
|
+
if (this.listeners.get(event)?.size === 0) {
|
|
42
|
+
this.listeners.delete(event);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
emit(event: string, data?: any): void {
|
|
48
|
+
const handlers = this.listeners.get(event);
|
|
49
|
+
if (handlers) {
|
|
50
|
+
handlers.forEach(handler => {
|
|
51
|
+
try {
|
|
52
|
+
handler(data);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error(`Error in event handler for "${event}":`, error);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
cleanup(): void {
|
|
61
|
+
this.listeners.clear();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Global event bus for cross-plugin communication
|
|
67
|
+
*/
|
|
68
|
+
const globalEventBus = new EventBus();
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Plugin Scope Implementation
|
|
72
|
+
*
|
|
73
|
+
* Provides isolated access to registry, state, and events for each plugin.
|
|
74
|
+
*/
|
|
75
|
+
export class PluginScopeImpl implements PluginScope {
|
|
76
|
+
public readonly name: string;
|
|
77
|
+
public readonly version: string;
|
|
78
|
+
|
|
79
|
+
private registry: Registry;
|
|
80
|
+
private state = new Map<string, any>();
|
|
81
|
+
private eventBus = new EventBus();
|
|
82
|
+
private config: Required<PluginScopeConfig>;
|
|
83
|
+
|
|
84
|
+
constructor(
|
|
85
|
+
name: string,
|
|
86
|
+
version: string,
|
|
87
|
+
registry: Registry,
|
|
88
|
+
config?: PluginScopeConfig
|
|
89
|
+
) {
|
|
90
|
+
this.name = name;
|
|
91
|
+
this.version = version;
|
|
92
|
+
this.registry = registry;
|
|
93
|
+
this.config = {
|
|
94
|
+
enableStateIsolation: config?.enableStateIsolation ?? true,
|
|
95
|
+
enableEventIsolation: config?.enableEventIsolation ?? true,
|
|
96
|
+
allowGlobalEvents: config?.allowGlobalEvents ?? true,
|
|
97
|
+
maxStateSize: config?.maxStateSize ?? 5 * 1024 * 1024, // 5MB
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Register a component in the scoped namespace
|
|
103
|
+
*/
|
|
104
|
+
registerComponent(type: string, component: any, meta?: any): void {
|
|
105
|
+
// Components are registered as "pluginName:type"
|
|
106
|
+
const registryMeta: RegistryComponentMeta = {
|
|
107
|
+
...meta,
|
|
108
|
+
namespace: this.name,
|
|
109
|
+
};
|
|
110
|
+
this.registry.register(type, component, registryMeta);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Get a component from the scoped namespace
|
|
115
|
+
*/
|
|
116
|
+
getComponent(type: string): any | undefined {
|
|
117
|
+
// First try scoped lookup
|
|
118
|
+
const scoped = this.registry.get(`${this.name}:${type}`);
|
|
119
|
+
if (scoped) {
|
|
120
|
+
return scoped;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Fall back to global lookup
|
|
124
|
+
return this.registry.get(type);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Scoped state management
|
|
129
|
+
*/
|
|
130
|
+
useState<T>(key: string, initialValue: T): [T, (value: T | ((prev: T) => T)) => void] {
|
|
131
|
+
if (!this.config.enableStateIsolation) {
|
|
132
|
+
throw new Error('State isolation is disabled for this plugin');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Initialize state if not present
|
|
136
|
+
if (!this.state.has(key)) {
|
|
137
|
+
this.setState(key, initialValue);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const currentValue = this.getState<T>(key) ?? initialValue;
|
|
141
|
+
|
|
142
|
+
const setValue = (value: T | ((prev: T) => T)) => {
|
|
143
|
+
const newValue = typeof value === 'function'
|
|
144
|
+
? (value as (prev: T) => T)(this.getState<T>(key) ?? initialValue)
|
|
145
|
+
: value;
|
|
146
|
+
this.setState(key, newValue);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return [currentValue, setValue];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Get scoped state value
|
|
154
|
+
*/
|
|
155
|
+
getState<T>(key: string): T | undefined {
|
|
156
|
+
return this.state.get(key);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Set scoped state value
|
|
161
|
+
*/
|
|
162
|
+
setState<T>(key: string, value: T): void {
|
|
163
|
+
if (!this.config.enableStateIsolation) {
|
|
164
|
+
throw new Error('State isolation is disabled for this plugin');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Check state size limit
|
|
168
|
+
const stateSize = this.estimateStateSize();
|
|
169
|
+
const valueSize = this.estimateValueSize(value);
|
|
170
|
+
|
|
171
|
+
if (stateSize + valueSize > this.config.maxStateSize) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
`Plugin "${this.name}" exceeded maximum state size of ${this.config.maxStateSize} bytes`
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
this.state.set(key, value);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Subscribe to scoped events
|
|
182
|
+
*/
|
|
183
|
+
on(event: string, handler: PluginEventHandler): () => void {
|
|
184
|
+
if (!this.config.enableEventIsolation) {
|
|
185
|
+
// If isolation is disabled, use global event bus
|
|
186
|
+
return this.onGlobal(event, handler);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Scoped event: prefix with plugin name
|
|
190
|
+
const scopedEvent = `${this.name}:${event}`;
|
|
191
|
+
return this.eventBus.on(scopedEvent, handler);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Emit a scoped event
|
|
196
|
+
*/
|
|
197
|
+
emit(event: string, data?: any): void {
|
|
198
|
+
if (!this.config.enableEventIsolation) {
|
|
199
|
+
// If isolation is disabled, emit globally
|
|
200
|
+
this.emitGlobal(event, data);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Scoped event: prefix with plugin name
|
|
205
|
+
const scopedEvent = `${this.name}:${event}`;
|
|
206
|
+
this.eventBus.emit(scopedEvent, data);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Emit a global event
|
|
211
|
+
*/
|
|
212
|
+
emitGlobal(event: string, data?: any): void {
|
|
213
|
+
if (!this.config.allowGlobalEvents) {
|
|
214
|
+
throw new Error('Global events are disabled for this plugin');
|
|
215
|
+
}
|
|
216
|
+
globalEventBus.emit(event, data);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Subscribe to global events
|
|
221
|
+
*/
|
|
222
|
+
onGlobal(event: string, handler: PluginEventHandler): () => void {
|
|
223
|
+
if (!this.config.allowGlobalEvents) {
|
|
224
|
+
throw new Error('Global events are disabled for this plugin');
|
|
225
|
+
}
|
|
226
|
+
return globalEventBus.on(event, handler);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Clean up all plugin resources
|
|
231
|
+
*/
|
|
232
|
+
cleanup(): void {
|
|
233
|
+
this.state.clear();
|
|
234
|
+
this.eventBus.cleanup();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Estimate total state size in bytes
|
|
239
|
+
*/
|
|
240
|
+
private estimateStateSize(): number {
|
|
241
|
+
let size = 0;
|
|
242
|
+
for (const value of this.state.values()) {
|
|
243
|
+
size += this.estimateValueSize(value);
|
|
244
|
+
}
|
|
245
|
+
return size;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Estimate size of a value in bytes
|
|
250
|
+
*/
|
|
251
|
+
private estimateValueSize(value: any): number {
|
|
252
|
+
try {
|
|
253
|
+
return JSON.stringify(value).length * 2; // UTF-16 encoding
|
|
254
|
+
} catch {
|
|
255
|
+
// If not serializable, use rough estimate
|
|
256
|
+
return 1024; // 1KB default
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { Registry } from './Registry.js';
|
|
10
|
+
import type { PluginScope, PluginScopeConfig } from '@object-ui/types';
|
|
11
|
+
import { PluginScopeImpl } from './PluginScopeImpl.js';
|
|
12
|
+
|
|
13
|
+
export interface PluginDefinition {
|
|
14
|
+
name: string;
|
|
15
|
+
version: string;
|
|
16
|
+
dependencies?: string[]; // Dependencies on other plugins
|
|
17
|
+
peerDependencies?: string[]; // Peer dependencies
|
|
18
|
+
register: (registry: Registry | PluginScope) => void; // Support both legacy and scoped registration
|
|
19
|
+
onLoad?: () => void | Promise<void>; // Lifecycle hook: called after registration
|
|
20
|
+
onUnload?: () => void | Promise<void>; // Lifecycle hook: called before unload
|
|
21
|
+
scopeConfig?: PluginScopeConfig; // Optional scope configuration
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class PluginSystem {
|
|
25
|
+
private plugins = new Map<string, PluginDefinition>();
|
|
26
|
+
private loaded = new Set<string>();
|
|
27
|
+
private scopes = new Map<string, PluginScopeImpl>();
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Load a plugin into the system with optional scope isolation
|
|
31
|
+
* @param plugin The plugin definition to load
|
|
32
|
+
* @param registry The component registry to use for registration
|
|
33
|
+
* @param useScope Whether to use scoped loading (default: true for better isolation)
|
|
34
|
+
* @throws Error if dependencies are missing
|
|
35
|
+
*/
|
|
36
|
+
async loadPlugin(plugin: PluginDefinition, registry: Registry, useScope: boolean = true): Promise<void> {
|
|
37
|
+
// Check if already loaded
|
|
38
|
+
if (this.loaded.has(plugin.name)) {
|
|
39
|
+
console.warn(`Plugin "${plugin.name}" is already loaded. Skipping.`);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Check dependencies
|
|
44
|
+
for (const dep of plugin.dependencies || []) {
|
|
45
|
+
if (!this.loaded.has(dep)) {
|
|
46
|
+
throw new Error(`Missing dependency: ${dep} required by ${plugin.name}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
if (useScope) {
|
|
52
|
+
// Create scoped environment for plugin
|
|
53
|
+
const scope = new PluginScopeImpl(
|
|
54
|
+
plugin.name,
|
|
55
|
+
plugin.version,
|
|
56
|
+
registry,
|
|
57
|
+
plugin.scopeConfig
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// Store scope for cleanup
|
|
61
|
+
this.scopes.set(plugin.name, scope);
|
|
62
|
+
|
|
63
|
+
// Execute registration with scope
|
|
64
|
+
plugin.register(scope);
|
|
65
|
+
} else {
|
|
66
|
+
// Legacy mode: direct registry access
|
|
67
|
+
plugin.register(registry);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Store plugin definition
|
|
71
|
+
this.plugins.set(plugin.name, plugin);
|
|
72
|
+
|
|
73
|
+
// Execute lifecycle hook
|
|
74
|
+
await plugin.onLoad?.();
|
|
75
|
+
|
|
76
|
+
// Mark as loaded
|
|
77
|
+
this.loaded.add(plugin.name);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
// Clean up on failure
|
|
80
|
+
this.plugins.delete(plugin.name);
|
|
81
|
+
this.scopes.delete(plugin.name);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Unload a plugin from the system
|
|
88
|
+
* @param name The name of the plugin to unload
|
|
89
|
+
* @throws Error if other plugins depend on this plugin
|
|
90
|
+
*/
|
|
91
|
+
async unloadPlugin(name: string): Promise<void> {
|
|
92
|
+
const plugin = this.plugins.get(name);
|
|
93
|
+
if (!plugin) {
|
|
94
|
+
throw new Error(`Plugin "${name}" is not loaded`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Check if any loaded plugins depend on this one
|
|
98
|
+
for (const [pluginName, pluginDef] of this.plugins.entries()) {
|
|
99
|
+
if (this.loaded.has(pluginName) && pluginDef.dependencies?.includes(name)) {
|
|
100
|
+
throw new Error(`Cannot unload plugin "${name}" - plugin "${pluginName}" depends on it`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Execute lifecycle hook
|
|
105
|
+
await plugin.onUnload?.();
|
|
106
|
+
|
|
107
|
+
// Clean up scope if exists
|
|
108
|
+
const scope = this.scopes.get(name);
|
|
109
|
+
if (scope) {
|
|
110
|
+
scope.cleanup();
|
|
111
|
+
this.scopes.delete(name);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Remove from loaded set
|
|
115
|
+
this.loaded.delete(name);
|
|
116
|
+
this.plugins.delete(name);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Get the scope for a loaded plugin
|
|
121
|
+
* @param name The name of the plugin
|
|
122
|
+
* @returns The plugin scope or undefined
|
|
123
|
+
*/
|
|
124
|
+
getScope(name: string): PluginScope | undefined {
|
|
125
|
+
return this.scopes.get(name);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Check if a plugin is loaded
|
|
130
|
+
* @param name The name of the plugin
|
|
131
|
+
* @returns true if the plugin is loaded
|
|
132
|
+
*/
|
|
133
|
+
isLoaded(name: string): boolean {
|
|
134
|
+
return this.loaded.has(name);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Get a loaded plugin definition
|
|
139
|
+
* @param name The name of the plugin
|
|
140
|
+
* @returns The plugin definition or undefined
|
|
141
|
+
*/
|
|
142
|
+
getPlugin(name: string): PluginDefinition | undefined {
|
|
143
|
+
return this.plugins.get(name);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Get all loaded plugin names
|
|
148
|
+
* @returns Array of loaded plugin names
|
|
149
|
+
*/
|
|
150
|
+
getLoadedPlugins(): string[] {
|
|
151
|
+
return Array.from(this.loaded);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Get all plugin definitions
|
|
156
|
+
* @returns Array of all plugin definitions
|
|
157
|
+
*/
|
|
158
|
+
getAllPlugins(): PluginDefinition[] {
|
|
159
|
+
return Array.from(this.plugins.values());
|
|
160
|
+
}
|
|
161
|
+
}
|
package/src/registry/Registry.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { SchemaNode } from '../types';
|
|
9
|
+
import type { SchemaNode } from '../types/index.js';
|
|
10
10
|
|
|
11
11
|
export type ComponentRenderer<T = any> = T;
|
|
12
12
|
|
|
@@ -26,6 +26,7 @@ export type ComponentMeta = {
|
|
|
26
26
|
label?: string; // Display name in designer
|
|
27
27
|
icon?: string; // Icon name or svg string
|
|
28
28
|
category?: string; // Grouping category
|
|
29
|
+
namespace?: string; // Component namespace (e.g., 'ui', 'plugin-grid', 'field')
|
|
29
30
|
inputs?: ComponentInput[];
|
|
30
31
|
defaultProps?: Record<string, any>; // Default props when dropped
|
|
31
32
|
defaultChildren?: SchemaNode[]; // Default children when dropped
|
|
@@ -50,36 +51,152 @@ export type ComponentConfig<T = any> = ComponentMeta & {
|
|
|
50
51
|
export class Registry<T = any> {
|
|
51
52
|
private components = new Map<string, ComponentConfig<T>>();
|
|
52
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Register a component with optional namespace support.
|
|
56
|
+
* If namespace is provided in meta, the component will be registered as "namespace:type".
|
|
57
|
+
*
|
|
58
|
+
* @param type - Component type identifier
|
|
59
|
+
* @param component - Component renderer
|
|
60
|
+
* @param meta - Component metadata (including optional namespace)
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Register with namespace
|
|
64
|
+
* registry.register('button', ButtonComponent, { namespace: 'ui' });
|
|
65
|
+
* // Accessible as 'ui:button' or 'button' (fallback)
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* // Register without namespace (backward compatible)
|
|
69
|
+
* registry.register('button', ButtonComponent);
|
|
70
|
+
* // Accessible as 'button'
|
|
71
|
+
*/
|
|
53
72
|
register(type: string, component: ComponentRenderer<T>, meta?: ComponentMeta) {
|
|
54
|
-
|
|
55
|
-
|
|
73
|
+
const fullType = meta?.namespace ? `${meta.namespace}:${type}` : type;
|
|
74
|
+
|
|
75
|
+
// Warn if registering without namespace (deprecated pattern)
|
|
76
|
+
if (!meta?.namespace) {
|
|
77
|
+
console.warn(
|
|
78
|
+
`Registering component "${type}" without a namespace is deprecated. ` +
|
|
79
|
+
`Please provide a namespace in the meta parameter.`
|
|
80
|
+
);
|
|
56
81
|
}
|
|
57
|
-
|
|
58
|
-
|
|
82
|
+
|
|
83
|
+
if (this.components.has(fullType)) {
|
|
84
|
+
// console.warn(`Component type "${fullType}" is already registered. Overwriting.`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.components.set(fullType, {
|
|
88
|
+
type: fullType,
|
|
59
89
|
component,
|
|
60
90
|
...meta
|
|
61
91
|
});
|
|
92
|
+
|
|
93
|
+
// Also register without namespace for backward compatibility
|
|
94
|
+
// This allows "button" to work even when registered as "ui:button"
|
|
95
|
+
// Note: If multiple namespaced components share the same short name,
|
|
96
|
+
// the last registration wins for non-namespaced lookups
|
|
97
|
+
if (meta?.namespace) {
|
|
98
|
+
this.components.set(type, {
|
|
99
|
+
type: fullType, // Keep reference to namespaced type
|
|
100
|
+
component,
|
|
101
|
+
...meta
|
|
102
|
+
});
|
|
103
|
+
}
|
|
62
104
|
}
|
|
63
105
|
|
|
64
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Get a component by type. Supports both namespaced and non-namespaced lookups.
|
|
108
|
+
*
|
|
109
|
+
* @param type - Component type (e.g., 'button' or 'ui:button')
|
|
110
|
+
* @param namespace - Optional namespace for lookup priority
|
|
111
|
+
* @returns Component renderer or undefined
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* // Direct lookup
|
|
115
|
+
* registry.get('ui:button') // Gets ui:button
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* // Fallback lookup
|
|
119
|
+
* registry.get('button') // Gets first registered button
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* // Namespaced lookup with priority
|
|
123
|
+
* registry.get('button', 'ui') // Tries 'ui:button' first, then 'button'
|
|
124
|
+
*/
|
|
125
|
+
get(type: string, namespace?: string): ComponentRenderer<T> | undefined {
|
|
126
|
+
// If namespace is explicitly provided, ONLY look in that namespace (no fallback)
|
|
127
|
+
if (namespace) {
|
|
128
|
+
const namespacedType = `${namespace}:${type}`;
|
|
129
|
+
return this.components.get(namespacedType)?.component;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// When no namespace provided, use backward compatibility lookup
|
|
65
133
|
return this.components.get(type)?.component;
|
|
66
134
|
}
|
|
67
135
|
|
|
68
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Get component configuration by type with namespace support.
|
|
138
|
+
*
|
|
139
|
+
* @param type - Component type (e.g., 'button' or 'ui:button')
|
|
140
|
+
* @param namespace - Optional namespace for lookup priority
|
|
141
|
+
* @returns Component configuration or undefined
|
|
142
|
+
*/
|
|
143
|
+
getConfig(type: string, namespace?: string): ComponentConfig<T> | undefined {
|
|
144
|
+
// If namespace is explicitly provided, ONLY look in that namespace (no fallback)
|
|
145
|
+
if (namespace) {
|
|
146
|
+
const namespacedType = `${namespace}:${type}`;
|
|
147
|
+
return this.components.get(namespacedType);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// When no namespace provided, use backward compatibility lookup
|
|
69
151
|
return this.components.get(type);
|
|
70
152
|
}
|
|
71
153
|
|
|
72
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Check if a component type is registered.
|
|
156
|
+
*
|
|
157
|
+
* @param type - Component type (e.g., 'button' or 'ui:button')
|
|
158
|
+
* @param namespace - Optional namespace for lookup
|
|
159
|
+
* @returns True if component is registered
|
|
160
|
+
*/
|
|
161
|
+
has(type: string, namespace?: string): boolean {
|
|
162
|
+
// If namespace is explicitly provided, ONLY look in that namespace (no fallback)
|
|
163
|
+
if (namespace) {
|
|
164
|
+
const namespacedType = `${namespace}:${type}`;
|
|
165
|
+
return this.components.has(namespacedType);
|
|
166
|
+
}
|
|
167
|
+
// When no namespace provided, use backward compatibility lookup
|
|
73
168
|
return this.components.has(type);
|
|
74
169
|
}
|
|
75
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Get all registered component types.
|
|
173
|
+
*
|
|
174
|
+
* @returns Array of all component type identifiers
|
|
175
|
+
*/
|
|
76
176
|
getAllTypes(): string[] {
|
|
77
177
|
return Array.from(this.components.keys());
|
|
78
178
|
}
|
|
79
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Get all registered component configurations.
|
|
182
|
+
*
|
|
183
|
+
* @returns Array of all component configurations
|
|
184
|
+
*/
|
|
80
185
|
getAllConfigs(): ComponentConfig<T>[] {
|
|
81
186
|
return Array.from(this.components.values());
|
|
82
187
|
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Get all components in a specific namespace.
|
|
191
|
+
*
|
|
192
|
+
* @param namespace - Namespace to filter by
|
|
193
|
+
* @returns Array of component configurations in the namespace
|
|
194
|
+
*/
|
|
195
|
+
getNamespaceComponents(namespace: string): ComponentConfig<T>[] {
|
|
196
|
+
return Array.from(this.components.values()).filter(
|
|
197
|
+
config => config.namespace === namespace
|
|
198
|
+
);
|
|
199
|
+
}
|
|
83
200
|
}
|
|
84
201
|
|
|
85
202
|
export const ComponentRegistry = new Registry<any>();
|