@object-ui/core 3.1.5 → 3.3.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.
Files changed (110) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +20 -1
  3. package/dist/actions/ActionRunner.d.ts +9 -0
  4. package/dist/actions/ActionRunner.js +41 -4
  5. package/dist/adapters/ValueDataSource.d.ts +5 -1
  6. package/dist/adapters/ValueDataSource.js +30 -1
  7. package/dist/errors/index.js +2 -3
  8. package/dist/evaluator/ExpressionCache.d.ts +9 -10
  9. package/dist/evaluator/ExpressionCache.js +29 -8
  10. package/dist/evaluator/SafeExpressionParser.d.ts +131 -0
  11. package/dist/evaluator/SafeExpressionParser.js +851 -0
  12. package/dist/evaluator/index.d.ts +1 -0
  13. package/dist/evaluator/index.js +1 -0
  14. package/dist/protocols/DndProtocol.js +2 -14
  15. package/dist/protocols/KeyboardProtocol.js +1 -4
  16. package/dist/protocols/NotificationProtocol.js +3 -13
  17. package/dist/utils/debug.js +2 -1
  18. package/dist/utils/filter-converter.js +25 -5
  19. package/package.json +33 -9
  20. package/.turbo/turbo-build.log +0 -4
  21. package/src/__benchmarks__/core.bench.ts +0 -64
  22. package/src/__tests__/protocols/DndProtocol.test.ts +0 -186
  23. package/src/__tests__/protocols/KeyboardProtocol.test.ts +0 -177
  24. package/src/__tests__/protocols/NotificationProtocol.test.ts +0 -142
  25. package/src/__tests__/protocols/ResponsiveProtocol.test.ts +0 -176
  26. package/src/__tests__/protocols/SharingProtocol.test.ts +0 -188
  27. package/src/actions/ActionEngine.ts +0 -268
  28. package/src/actions/ActionRunner.ts +0 -717
  29. package/src/actions/TransactionManager.ts +0 -521
  30. package/src/actions/UndoManager.ts +0 -215
  31. package/src/actions/__tests__/ActionEngine.test.ts +0 -206
  32. package/src/actions/__tests__/ActionRunner.params.test.ts +0 -134
  33. package/src/actions/__tests__/ActionRunner.test.ts +0 -711
  34. package/src/actions/__tests__/TransactionManager.test.ts +0 -447
  35. package/src/actions/__tests__/UndoManager.test.ts +0 -320
  36. package/src/actions/index.ts +0 -12
  37. package/src/adapters/ApiDataSource.ts +0 -376
  38. package/src/adapters/README.md +0 -180
  39. package/src/adapters/ValueDataSource.ts +0 -438
  40. package/src/adapters/__tests__/ApiDataSource.test.ts +0 -418
  41. package/src/adapters/__tests__/ValueDataSource.test.ts +0 -472
  42. package/src/adapters/__tests__/resolveDataSource.test.ts +0 -144
  43. package/src/adapters/index.ts +0 -15
  44. package/src/adapters/resolveDataSource.ts +0 -79
  45. package/src/builder/__tests__/schema-builder.test.ts +0 -235
  46. package/src/builder/schema-builder.ts +0 -584
  47. package/src/data-scope/DataScopeManager.ts +0 -269
  48. package/src/data-scope/ViewDataProvider.ts +0 -282
  49. package/src/data-scope/__tests__/DataScopeManager.test.ts +0 -211
  50. package/src/data-scope/__tests__/ViewDataProvider.test.ts +0 -270
  51. package/src/data-scope/index.ts +0 -24
  52. package/src/errors/__tests__/errors.test.ts +0 -292
  53. package/src/errors/index.ts +0 -270
  54. package/src/evaluator/ExpressionCache.ts +0 -192
  55. package/src/evaluator/ExpressionContext.ts +0 -118
  56. package/src/evaluator/ExpressionEvaluator.ts +0 -315
  57. package/src/evaluator/FormulaFunctions.ts +0 -398
  58. package/src/evaluator/__tests__/ExpressionCache.test.ts +0 -135
  59. package/src/evaluator/__tests__/ExpressionContext.test.ts +0 -110
  60. package/src/evaluator/__tests__/ExpressionEvaluator.test.ts +0 -131
  61. package/src/evaluator/__tests__/FormulaFunctions.test.ts +0 -447
  62. package/src/evaluator/index.ts +0 -12
  63. package/src/index.ts +0 -38
  64. package/src/protocols/DndProtocol.ts +0 -184
  65. package/src/protocols/KeyboardProtocol.ts +0 -185
  66. package/src/protocols/NotificationProtocol.ts +0 -159
  67. package/src/protocols/ResponsiveProtocol.ts +0 -210
  68. package/src/protocols/SharingProtocol.ts +0 -185
  69. package/src/protocols/index.ts +0 -13
  70. package/src/query/__tests__/query-ast.test.ts +0 -211
  71. package/src/query/__tests__/window-functions.test.ts +0 -275
  72. package/src/query/index.ts +0 -7
  73. package/src/query/query-ast.ts +0 -341
  74. package/src/registry/PluginScopeImpl.ts +0 -259
  75. package/src/registry/PluginSystem.ts +0 -206
  76. package/src/registry/Registry.ts +0 -219
  77. package/src/registry/WidgetRegistry.ts +0 -316
  78. package/src/registry/__tests__/PluginSystem.test.ts +0 -309
  79. package/src/registry/__tests__/Registry.test.ts +0 -293
  80. package/src/registry/__tests__/WidgetRegistry.test.ts +0 -321
  81. package/src/registry/__tests__/plugin-scope-integration.test.ts +0 -283
  82. package/src/theme/ThemeEngine.ts +0 -530
  83. package/src/theme/__tests__/ThemeEngine.test.ts +0 -668
  84. package/src/theme/index.ts +0 -24
  85. package/src/types/index.ts +0 -21
  86. package/src/utils/__tests__/debug-collector.test.ts +0 -102
  87. package/src/utils/__tests__/debug.test.ts +0 -134
  88. package/src/utils/__tests__/expand-fields.test.ts +0 -120
  89. package/src/utils/__tests__/extract-records.test.ts +0 -50
  90. package/src/utils/__tests__/filter-converter.test.ts +0 -118
  91. package/src/utils/__tests__/merge-views-into-objects.test.ts +0 -110
  92. package/src/utils/__tests__/normalize-quick-filter.test.ts +0 -123
  93. package/src/utils/debug-collector.ts +0 -100
  94. package/src/utils/debug.ts +0 -147
  95. package/src/utils/expand-fields.ts +0 -76
  96. package/src/utils/extract-records.ts +0 -33
  97. package/src/utils/filter-converter.ts +0 -133
  98. package/src/utils/merge-views-into-objects.ts +0 -36
  99. package/src/utils/normalize-quick-filter.ts +0 -78
  100. package/src/validation/__tests__/object-validation-engine.test.ts +0 -567
  101. package/src/validation/__tests__/schema-validator.test.ts +0 -118
  102. package/src/validation/__tests__/validation-engine.test.ts +0 -102
  103. package/src/validation/index.ts +0 -10
  104. package/src/validation/schema-validator.ts +0 -344
  105. package/src/validation/validation-engine.ts +0 -528
  106. package/src/validation/validators/index.ts +0 -25
  107. package/src/validation/validators/object-validation-engine.ts +0 -722
  108. package/tsconfig.json +0 -15
  109. package/tsconfig.tsbuildinfo +0 -1
  110. package/vitest.config.ts +0 -2
@@ -1,259 +0,0 @@
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
- }
@@ -1,206 +0,0 @@
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, AppMetadataPlugin, AppPluginContext } 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
-
162
- /**
163
- * Install an AppMetadataPlugin at runtime.
164
- *
165
- * Wraps the plugin as a PluginDefinition, calls its `init()` and `start()`
166
- * lifecycle hooks, and loads it into the system.
167
- *
168
- * @param plugin - An AppMetadataPlugin instance
169
- * @param registry - The component registry
170
- * @param ctx - Optional context passed to the plugin's start() hook
171
- */
172
- async install(plugin: AppMetadataPlugin, registry: Registry, ctx?: AppPluginContext): Promise<void> {
173
- if (this.loaded.has(plugin.name)) {
174
- console.warn(`Plugin "${plugin.name}" is already installed. Skipping.`);
175
- return;
176
- }
177
-
178
- await plugin.init();
179
-
180
- const definition: PluginDefinition = {
181
- name: plugin.name,
182
- version: plugin.version,
183
- register: () => {},
184
- onLoad: async () => {
185
- await plugin.start(ctx ?? { logger: console });
186
- },
187
- onUnload: async () => {
188
- await plugin.stop();
189
- },
190
- };
191
-
192
- await this.loadPlugin(definition, registry);
193
- }
194
-
195
- /**
196
- * Uninstall an AppMetadataPlugin at runtime.
197
- *
198
- * Calls the plugin's `stop()` lifecycle hook (via onUnload) and
199
- * removes it from the system.
200
- *
201
- * @param pluginName - Name of the plugin to uninstall
202
- */
203
- async uninstall(pluginName: string): Promise<void> {
204
- await this.unloadPlugin(pluginName);
205
- }
206
- }
@@ -1,219 +0,0 @@
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 { SchemaNode } from '../types/index.js';
10
-
11
- export type ComponentRenderer<T = any> = T;
12
-
13
- export type ComponentInput = {
14
- name: string;
15
- type: 'string' | 'number' | 'boolean' | 'enum' | 'array' | 'object' | 'color' | 'date' | 'code' | 'file' | 'slot';
16
- label?: string;
17
- defaultValue?: any;
18
- required?: boolean;
19
- enum?: string[] | { label: string; value: any }[];
20
- description?: string;
21
- advanced?: boolean;
22
- inputType?: string;
23
- };
24
-
25
- export type ComponentMeta = {
26
- label?: string; // Display name in designer
27
- icon?: string; // Icon name or svg string
28
- category?: string; // Grouping category
29
- namespace?: string; // Component namespace (e.g., 'ui', 'plugin-grid', 'field')
30
- /**
31
- * When true, prevents the component from being registered with a non-namespaced fallback.
32
- * Use this when a component should only be accessible via its full namespaced key.
33
- * This avoids conflicts with other components that share the same base name.
34
- *
35
- * @example
36
- * // Register as 'view:form' only, don't overwrite 'form'
37
- * registry.register('form', FormView, { namespace: 'view', skipFallback: true });
38
- */
39
- skipFallback?: boolean;
40
- inputs?: ComponentInput[];
41
- defaultProps?: Record<string, any>; // Default props when dropped
42
- defaultChildren?: SchemaNode[]; // Default children when dropped
43
- examples?: Record<string, any>; // Example configurations
44
- isContainer?: boolean; // Whether the component can have children
45
- resizable?: boolean; // Whether the component can be resized in the designer
46
- resizeConstraints?: {
47
- width?: boolean;
48
- height?: boolean;
49
- minWidth?: number;
50
- maxWidth?: number;
51
- minHeight?: number;
52
- maxHeight?: number;
53
- };
54
- };
55
-
56
- export type ComponentConfig<T = any> = ComponentMeta & {
57
- type: string;
58
- component: ComponentRenderer<T>;
59
- };
60
-
61
- export class Registry<T = any> {
62
- private components = new Map<string, ComponentConfig<T>>();
63
-
64
- /**
65
- * Register a component with optional namespace support.
66
- * If namespace is provided in meta, the component will be registered as "namespace:type".
67
- *
68
- * @param type - Component type identifier
69
- * @param component - Component renderer
70
- * @param meta - Component metadata (including optional namespace)
71
- *
72
- * @example
73
- * // Register with namespace
74
- * registry.register('button', ButtonComponent, { namespace: 'ui' });
75
- * // Accessible as 'ui:button' or 'button' (fallback)
76
- *
77
- * @example
78
- * // Register without namespace (backward compatible)
79
- * registry.register('button', ButtonComponent);
80
- * // Accessible as 'button'
81
- */
82
- register(type: string, component: ComponentRenderer<T>, meta?: ComponentMeta) {
83
- const fullType = meta?.namespace ? `${meta.namespace}:${type}` : type;
84
-
85
- // Warn if registering without namespace (deprecated pattern)
86
- if (!meta?.namespace) {
87
- console.warn(
88
- `Registering component "${type}" without a namespace is deprecated. ` +
89
- `Please provide a namespace in the meta parameter.\n\n` +
90
- ` Migration:\n` +
91
- ` // Before (deprecated):\n` +
92
- ` registry.register('${type}', MyComponent);\n\n` +
93
- ` // After:\n` +
94
- ` registry.register('${type}', MyComponent, { namespace: 'my-plugin' });\n\n` +
95
- ` See: https://github.com/objectstack-ai/objectui/blob/main/MIGRATION_GUIDE.md`
96
- );
97
- }
98
-
99
- if (this.components.has(fullType)) {
100
- // console.warn(`Component type "${fullType}" is already registered. Overwriting.`);
101
- }
102
-
103
- this.components.set(fullType, {
104
- type: fullType,
105
- component,
106
- ...meta
107
- });
108
-
109
- // Also register without namespace for backward compatibility
110
- // This allows "button" to work even when registered as "ui:button"
111
- // Note: If multiple namespaced components share the same short name,
112
- // the last registration wins for non-namespaced lookups
113
- // Skip this if skipFallback is true to avoid overwriting other components
114
- if (meta?.namespace && !meta?.skipFallback) {
115
- this.components.set(type, {
116
- type: fullType, // Keep reference to namespaced type
117
- component,
118
- ...meta
119
- });
120
- }
121
- }
122
-
123
- /**
124
- * Get a component by type. Supports both namespaced and non-namespaced lookups.
125
- *
126
- * @param type - Component type (e.g., 'button' or 'ui:button')
127
- * @param namespace - Optional namespace for lookup priority
128
- * @returns Component renderer or undefined
129
- *
130
- * @example
131
- * // Direct lookup
132
- * registry.get('ui:button') // Gets ui:button
133
- *
134
- * @example
135
- * // Fallback lookup
136
- * registry.get('button') // Gets first registered button
137
- *
138
- * @example
139
- * // Namespaced lookup with priority
140
- * registry.get('button', 'ui') // Tries 'ui:button' first, then 'button'
141
- */
142
- get(type: string, namespace?: string): ComponentRenderer<T> | undefined {
143
- // If namespace is explicitly provided, ONLY look in that namespace (no fallback)
144
- if (namespace) {
145
- const namespacedType = `${namespace}:${type}`;
146
- return this.components.get(namespacedType)?.component;
147
- }
148
-
149
- // When no namespace provided, use backward compatibility lookup
150
- return this.components.get(type)?.component;
151
- }
152
-
153
- /**
154
- * Get component configuration by type with namespace support.
155
- *
156
- * @param type - Component type (e.g., 'button' or 'ui:button')
157
- * @param namespace - Optional namespace for lookup priority
158
- * @returns Component configuration or undefined
159
- */
160
- getConfig(type: string, namespace?: string): ComponentConfig<T> | undefined {
161
- // If namespace is explicitly provided, ONLY look in that namespace (no fallback)
162
- if (namespace) {
163
- const namespacedType = `${namespace}:${type}`;
164
- return this.components.get(namespacedType);
165
- }
166
-
167
- // When no namespace provided, use backward compatibility lookup
168
- return this.components.get(type);
169
- }
170
-
171
- /**
172
- * Check if a component type is registered.
173
- *
174
- * @param type - Component type (e.g., 'button' or 'ui:button')
175
- * @param namespace - Optional namespace for lookup
176
- * @returns True if component is registered
177
- */
178
- has(type: string, namespace?: string): boolean {
179
- // If namespace is explicitly provided, ONLY look in that namespace (no fallback)
180
- if (namespace) {
181
- const namespacedType = `${namespace}:${type}`;
182
- return this.components.has(namespacedType);
183
- }
184
- // When no namespace provided, use backward compatibility lookup
185
- return this.components.has(type);
186
- }
187
-
188
- /**
189
- * Get all registered component types.
190
- *
191
- * @returns Array of all component type identifiers
192
- */
193
- getAllTypes(): string[] {
194
- return Array.from(this.components.keys());
195
- }
196
-
197
- /**
198
- * Get all registered component configurations.
199
- *
200
- * @returns Array of all component configurations
201
- */
202
- getAllConfigs(): ComponentConfig<T>[] {
203
- return Array.from(this.components.values());
204
- }
205
-
206
- /**
207
- * Get all components in a specific namespace.
208
- *
209
- * @param namespace - Namespace to filter by
210
- * @returns Array of component configurations in the namespace
211
- */
212
- getNamespaceComponents(namespace: string): ComponentConfig<T>[] {
213
- return Array.from(this.components.values()).filter(
214
- config => config.namespace === namespace
215
- );
216
- }
217
- }
218
-
219
- export const ComponentRegistry = new Registry<any>();