@bbq-chat/widgets-angular 1.0.1 → 1.0.3
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/package.json +25 -46
- package/.eslintrc.json +0 -23
- package/.prettierrc.json +0 -8
- package/EXAMPLES.md +0 -468
- package/ng-package.json +0 -10
- package/scripts/build.mjs +0 -63
- package/src/custom-widget-renderer.types.ts +0 -120
- package/src/index.ts +0 -75
- package/src/public_api.ts +0 -75
- package/src/widget-di.tokens.ts +0 -63
- package/src/widget-registry.service.ts +0 -128
- package/src/widget-renderer.component.ts +0 -400
- package/tsconfig.json +0 -34
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { Type, TemplateRef } from '@angular/core';
|
|
2
|
-
import { ChatWidget } from '@bbq-chat/widgets';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Context provided to template-based custom widget renderers
|
|
6
|
-
*/
|
|
7
|
-
export interface WidgetTemplateContext {
|
|
8
|
-
/**
|
|
9
|
-
* The widget instance being rendered
|
|
10
|
-
*/
|
|
11
|
-
$implicit: ChatWidget;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The widget instance (alternative access)
|
|
15
|
-
*/
|
|
16
|
-
widget: ChatWidget;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Emit a widget action
|
|
20
|
-
*/
|
|
21
|
-
emitAction: (actionName: string, payload: unknown) => void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Interface for component-based custom widget renderers
|
|
26
|
-
*/
|
|
27
|
-
export interface CustomWidgetComponent {
|
|
28
|
-
/**
|
|
29
|
-
* The widget instance to render
|
|
30
|
-
*/
|
|
31
|
-
widget: ChatWidget;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Event emitter for widget actions (optional, will be set by the renderer)
|
|
35
|
-
*/
|
|
36
|
-
widgetAction?: (actionName: string, payload: unknown) => void;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Type for custom widget renderer functions that return HTML strings
|
|
41
|
-
*/
|
|
42
|
-
export type CustomWidgetHtmlRenderer = (widget: ChatWidget) => string;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Type for custom widget renderer configurations
|
|
46
|
-
*/
|
|
47
|
-
export type CustomWidgetRenderer =
|
|
48
|
-
| CustomWidgetHtmlRenderer
|
|
49
|
-
| Type<CustomWidgetComponent>
|
|
50
|
-
| TemplateRef<WidgetTemplateContext>;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Configuration for registering a custom widget renderer
|
|
54
|
-
*/
|
|
55
|
-
export interface CustomWidgetRendererConfig {
|
|
56
|
-
/**
|
|
57
|
-
* The widget type identifier
|
|
58
|
-
*/
|
|
59
|
-
type: string;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* The renderer: can be a function returning HTML, an Angular Component class, or a TemplateRef
|
|
63
|
-
*/
|
|
64
|
-
renderer: CustomWidgetRenderer;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Type guard to check if a renderer is a TemplateRef
|
|
69
|
-
*/
|
|
70
|
-
export function isTemplateRenderer(
|
|
71
|
-
renderer: CustomWidgetRenderer
|
|
72
|
-
): renderer is TemplateRef<WidgetTemplateContext> {
|
|
73
|
-
return (
|
|
74
|
-
renderer !== null &&
|
|
75
|
-
typeof renderer === 'object' &&
|
|
76
|
-
'createEmbeddedView' in renderer
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Type guard to check if a renderer is an Angular Component
|
|
82
|
-
*
|
|
83
|
-
* Note: This uses a heuristic check based on the following assumptions:
|
|
84
|
-
* 1. Components are constructor functions
|
|
85
|
-
* 2. Components have a prototype with a constructor property
|
|
86
|
-
* 3. Components typically use dependency injection (no required constructor params)
|
|
87
|
-
*
|
|
88
|
-
* Limitation: This may not detect components with required constructor parameters.
|
|
89
|
-
* For edge cases, explicitly check your component's constructor signature.
|
|
90
|
-
*
|
|
91
|
-
* Alternative: You can always register a wrapper component that has no required params.
|
|
92
|
-
*/
|
|
93
|
-
export function isComponentRenderer(
|
|
94
|
-
renderer: CustomWidgetRenderer
|
|
95
|
-
): renderer is Type<CustomWidgetComponent> {
|
|
96
|
-
// Check if it's a function (constructor) but not a regular function renderer
|
|
97
|
-
if (typeof renderer !== 'function') {
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Check for Angular component characteristics
|
|
102
|
-
// Components typically have prototype with constructor property
|
|
103
|
-
return (
|
|
104
|
-
renderer.prototype !== undefined &&
|
|
105
|
-
renderer.prototype.constructor === renderer &&
|
|
106
|
-
renderer.length === 0 // Constructor with no required params (Angular DI)
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Type guard to check if a renderer is an HTML function
|
|
112
|
-
*
|
|
113
|
-
* Note: This should be checked AFTER checking for component and template renderers
|
|
114
|
-
* since components are also functions but with additional properties.
|
|
115
|
-
*/
|
|
116
|
-
export function isHtmlRenderer(
|
|
117
|
-
renderer: CustomWidgetRenderer
|
|
118
|
-
): renderer is CustomWidgetHtmlRenderer {
|
|
119
|
-
return typeof renderer === 'function';
|
|
120
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @bbq-chat/widgets-angular
|
|
3
|
-
*
|
|
4
|
-
* Angular components and services for BbQ ChatWidgets
|
|
5
|
-
*
|
|
6
|
-
* This package provides Angular-native components and services that wrap
|
|
7
|
-
* the core @bbq-chat/widgets library, making it easy to integrate chat
|
|
8
|
-
* widgets into Angular applications.
|
|
9
|
-
*
|
|
10
|
-
* @packageDocumentation
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
// Export components
|
|
14
|
-
export { WidgetRendererComponent } from './widget-renderer.component';
|
|
15
|
-
|
|
16
|
-
// Export services
|
|
17
|
-
export { WidgetRegistryService } from './widget-registry.service';
|
|
18
|
-
|
|
19
|
-
// Export DI tokens and factories
|
|
20
|
-
export {
|
|
21
|
-
WIDGET_EVENT_MANAGER_FACTORY,
|
|
22
|
-
SSR_WIDGET_RENDERER,
|
|
23
|
-
widgetEventManagerFactoryProvider,
|
|
24
|
-
ssrWidgetRendererFactory,
|
|
25
|
-
} from './widget-di.tokens';
|
|
26
|
-
|
|
27
|
-
export type { WidgetEventManagerFactory } from './widget-di.tokens';
|
|
28
|
-
|
|
29
|
-
// Export custom widget renderer types
|
|
30
|
-
export type {
|
|
31
|
-
CustomWidgetComponent,
|
|
32
|
-
CustomWidgetRenderer,
|
|
33
|
-
CustomWidgetHtmlRenderer,
|
|
34
|
-
CustomWidgetRendererConfig,
|
|
35
|
-
WidgetTemplateContext,
|
|
36
|
-
} from './custom-widget-renderer.types';
|
|
37
|
-
|
|
38
|
-
export {
|
|
39
|
-
isHtmlRenderer,
|
|
40
|
-
isComponentRenderer,
|
|
41
|
-
isTemplateRenderer,
|
|
42
|
-
} from './custom-widget-renderer.types';
|
|
43
|
-
|
|
44
|
-
// Re-export commonly used types and classes from core package
|
|
45
|
-
export {
|
|
46
|
-
ChatWidget,
|
|
47
|
-
} from '@bbq-chat/widgets';
|
|
48
|
-
|
|
49
|
-
export type {
|
|
50
|
-
ButtonWidget,
|
|
51
|
-
CardWidget,
|
|
52
|
-
FormWidget,
|
|
53
|
-
InputWidget,
|
|
54
|
-
TextAreaWidget,
|
|
55
|
-
DropdownWidget,
|
|
56
|
-
SliderWidget,
|
|
57
|
-
ToggleWidget,
|
|
58
|
-
FileUploadWidget,
|
|
59
|
-
DatePickerWidget,
|
|
60
|
-
MultiSelectWidget,
|
|
61
|
-
ProgressBarWidget,
|
|
62
|
-
ThemeSwitcherWidget,
|
|
63
|
-
ImageWidget,
|
|
64
|
-
ImageCollectionWidget,
|
|
65
|
-
} from '@bbq-chat/widgets';
|
|
66
|
-
|
|
67
|
-
// Re-export utilities
|
|
68
|
-
export {
|
|
69
|
-
SsrWidgetRenderer,
|
|
70
|
-
WidgetEventManager,
|
|
71
|
-
customWidgetRegistry,
|
|
72
|
-
} from '@bbq-chat/widgets';
|
|
73
|
-
|
|
74
|
-
// Version
|
|
75
|
-
export const VERSION = '1.0.1';
|
package/src/public_api.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @bbq-chat/widgets-angular
|
|
3
|
-
*
|
|
4
|
-
* Angular components and services for BbQ ChatWidgets
|
|
5
|
-
*
|
|
6
|
-
* This package provides Angular-native components and services that wrap
|
|
7
|
-
* the core @bbq-chat/widgets library, making it easy to integrate chat
|
|
8
|
-
* widgets into Angular applications.
|
|
9
|
-
*
|
|
10
|
-
* @packageDocumentation
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
// Export components
|
|
14
|
-
export { WidgetRendererComponent } from './widget-renderer.component';
|
|
15
|
-
|
|
16
|
-
// Export services
|
|
17
|
-
export { WidgetRegistryService } from './widget-registry.service';
|
|
18
|
-
|
|
19
|
-
// Export DI tokens and factories
|
|
20
|
-
export {
|
|
21
|
-
WIDGET_EVENT_MANAGER_FACTORY,
|
|
22
|
-
SSR_WIDGET_RENDERER,
|
|
23
|
-
widgetEventManagerFactoryProvider,
|
|
24
|
-
ssrWidgetRendererFactory,
|
|
25
|
-
} from './widget-di.tokens';
|
|
26
|
-
|
|
27
|
-
export type { WidgetEventManagerFactory } from './widget-di.tokens';
|
|
28
|
-
|
|
29
|
-
// Export custom widget renderer types
|
|
30
|
-
export type {
|
|
31
|
-
CustomWidgetComponent,
|
|
32
|
-
CustomWidgetRenderer,
|
|
33
|
-
CustomWidgetHtmlRenderer,
|
|
34
|
-
CustomWidgetRendererConfig,
|
|
35
|
-
WidgetTemplateContext,
|
|
36
|
-
} from './custom-widget-renderer.types';
|
|
37
|
-
|
|
38
|
-
export {
|
|
39
|
-
isHtmlRenderer,
|
|
40
|
-
isComponentRenderer,
|
|
41
|
-
isTemplateRenderer,
|
|
42
|
-
} from './custom-widget-renderer.types';
|
|
43
|
-
|
|
44
|
-
// Re-export commonly used types and classes from core package
|
|
45
|
-
export {
|
|
46
|
-
ChatWidget,
|
|
47
|
-
} from '@bbq-chat/widgets';
|
|
48
|
-
|
|
49
|
-
export type {
|
|
50
|
-
ButtonWidget,
|
|
51
|
-
CardWidget,
|
|
52
|
-
FormWidget,
|
|
53
|
-
InputWidget,
|
|
54
|
-
TextAreaWidget,
|
|
55
|
-
DropdownWidget,
|
|
56
|
-
SliderWidget,
|
|
57
|
-
ToggleWidget,
|
|
58
|
-
FileUploadWidget,
|
|
59
|
-
DatePickerWidget,
|
|
60
|
-
MultiSelectWidget,
|
|
61
|
-
ProgressBarWidget,
|
|
62
|
-
ThemeSwitcherWidget,
|
|
63
|
-
ImageWidget,
|
|
64
|
-
ImageCollectionWidget,
|
|
65
|
-
} from '@bbq-chat/widgets';
|
|
66
|
-
|
|
67
|
-
// Re-export utilities
|
|
68
|
-
export {
|
|
69
|
-
SsrWidgetRenderer,
|
|
70
|
-
WidgetEventManager,
|
|
71
|
-
customWidgetRegistry,
|
|
72
|
-
} from '@bbq-chat/widgets';
|
|
73
|
-
|
|
74
|
-
// Version
|
|
75
|
-
export const VERSION = '1.0.1';
|
package/src/widget-di.tokens.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { InjectionToken } from '@angular/core';
|
|
2
|
-
import { SsrWidgetRenderer, WidgetEventManager, IWidgetActionHandler } from '@bbq-chat/widgets';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Injection token for WidgetEventManager factory
|
|
6
|
-
*
|
|
7
|
-
* Use this token to inject a factory function that creates WidgetEventManager instances.
|
|
8
|
-
* The factory accepts an optional action handler to configure the manager.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* constructor(@Inject(WIDGET_EVENT_MANAGER_FACTORY) private eventManagerFactory: WidgetEventManagerFactory) {
|
|
13
|
-
* const actionHandler = { handle: async (action, payload) => { ... } };
|
|
14
|
-
* this.eventManager = this.eventManagerFactory(actionHandler);
|
|
15
|
-
* }
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export type WidgetEventManagerFactory = (actionHandler?: IWidgetActionHandler) => WidgetEventManager;
|
|
19
|
-
|
|
20
|
-
export const WIDGET_EVENT_MANAGER_FACTORY = new InjectionToken<WidgetEventManagerFactory>(
|
|
21
|
-
'WIDGET_EVENT_MANAGER_FACTORY'
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Injection token for SsrWidgetRenderer
|
|
26
|
-
*
|
|
27
|
-
* Use this token to inject a SsrWidgetRenderer instance in your components.
|
|
28
|
-
* By default, WidgetRendererComponent provides this token with a factory that creates
|
|
29
|
-
* a new instance for each component.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```typescript
|
|
33
|
-
* constructor(@Inject(SSR_WIDGET_RENDERER) private renderer: SsrWidgetRenderer) {}
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export const SSR_WIDGET_RENDERER = new InjectionToken<SsrWidgetRenderer>(
|
|
37
|
-
'SSR_WIDGET_RENDERER'
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Factory function for creating WidgetEventManager instances
|
|
42
|
-
*
|
|
43
|
-
* This factory is used by default in WidgetRendererComponent's providers array.
|
|
44
|
-
* You can override this in your own providers if you need custom initialization.
|
|
45
|
-
*
|
|
46
|
-
* @returns A factory function that creates WidgetEventManager instances
|
|
47
|
-
*/
|
|
48
|
-
export function widgetEventManagerFactoryProvider(): WidgetEventManagerFactory {
|
|
49
|
-
return (actionHandler?: IWidgetActionHandler) => new WidgetEventManager(actionHandler);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Factory function for creating SsrWidgetRenderer instances
|
|
54
|
-
*
|
|
55
|
-
* This factory is used by default in WidgetRendererComponent's providers array.
|
|
56
|
-
* You can override this in your own providers if you need custom initialization
|
|
57
|
-
* or custom rendering options.
|
|
58
|
-
*
|
|
59
|
-
* @returns A new SsrWidgetRenderer instance
|
|
60
|
-
*/
|
|
61
|
-
export function ssrWidgetRendererFactory(): SsrWidgetRenderer {
|
|
62
|
-
return new SsrWidgetRenderer();
|
|
63
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core';
|
|
2
|
-
import { customWidgetRegistry, ChatWidget } from '@bbq-chat/widgets';
|
|
3
|
-
import { CustomWidgetRenderer } from './custom-widget-renderer.types';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Service for registering custom widget factories and renderers
|
|
7
|
-
*
|
|
8
|
-
* This service provides a centralized way to register custom widget types
|
|
9
|
-
* that extend the base widget functionality, including support for
|
|
10
|
-
* Angular components and templates as custom renderers.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* constructor(private widgetRegistry: WidgetRegistryService) {
|
|
15
|
-
* // Register a widget factory
|
|
16
|
-
* this.widgetRegistry.registerFactory('myWidget', (obj) => {
|
|
17
|
-
* if (obj.type === 'myWidget') {
|
|
18
|
-
* return new MyCustomWidget(obj.label, obj.action);
|
|
19
|
-
* }
|
|
20
|
-
* return null;
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* // Register a component-based renderer
|
|
24
|
-
* this.widgetRegistry.registerRenderer('myWidget', MyWidgetComponent);
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
@Injectable({
|
|
29
|
-
providedIn: 'root',
|
|
30
|
-
})
|
|
31
|
-
export class WidgetRegistryService {
|
|
32
|
-
private readonly customRenderers = new Map<string, CustomWidgetRenderer>();
|
|
33
|
-
/**
|
|
34
|
-
* Register a custom widget factory function
|
|
35
|
-
*
|
|
36
|
-
* @param type - The widget type identifier
|
|
37
|
-
* @param factory - Factory function that creates widget instances from plain objects
|
|
38
|
-
*/
|
|
39
|
-
registerFactory(
|
|
40
|
-
type: string,
|
|
41
|
-
factory: (obj: unknown) => ChatWidget | null
|
|
42
|
-
): void {
|
|
43
|
-
customWidgetRegistry.registerFactory(type, factory);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Register a widget class with automatic factory creation
|
|
48
|
-
*
|
|
49
|
-
* @param type - The widget type identifier
|
|
50
|
-
* @param ctor - Widget class constructor
|
|
51
|
-
*/
|
|
52
|
-
registerClass(type: string, ctor: any): void {
|
|
53
|
-
customWidgetRegistry.registerClass(type, ctor);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Get a factory for a specific widget type
|
|
58
|
-
*
|
|
59
|
-
* @param type - The widget type identifier
|
|
60
|
-
* @returns The factory function if registered, undefined otherwise
|
|
61
|
-
*/
|
|
62
|
-
getFactory(type: string): ((obj: any) => ChatWidget | null) | undefined {
|
|
63
|
-
return customWidgetRegistry.getFactory(type);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Register a custom renderer for a specific widget type
|
|
68
|
-
*
|
|
69
|
-
* The renderer can be:
|
|
70
|
-
* - A function that returns HTML string
|
|
71
|
-
* - An Angular Component class
|
|
72
|
-
* - An Angular TemplateRef
|
|
73
|
-
*
|
|
74
|
-
* @param type - The widget type identifier
|
|
75
|
-
* @param renderer - The custom renderer (function, Component, or TemplateRef)
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```typescript
|
|
79
|
-
* // HTML function renderer
|
|
80
|
-
* widgetRegistry.registerRenderer('weather', (widget) => `<div>${widget.label}</div>`);
|
|
81
|
-
*
|
|
82
|
-
* // Component renderer
|
|
83
|
-
* widgetRegistry.registerRenderer('weather', WeatherWidgetComponent);
|
|
84
|
-
*
|
|
85
|
-
* // Template renderer (from @ViewChild or elsewhere)
|
|
86
|
-
* widgetRegistry.registerRenderer('weather', this.weatherTemplate);
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
registerRenderer(type: string, renderer: CustomWidgetRenderer): void {
|
|
90
|
-
if (!type || typeof type !== 'string') {
|
|
91
|
-
throw new Error('type must be a non-empty string');
|
|
92
|
-
}
|
|
93
|
-
if (!renderer) {
|
|
94
|
-
throw new Error('renderer is required');
|
|
95
|
-
}
|
|
96
|
-
this.customRenderers.set(type, renderer);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Get a custom renderer for a specific widget type
|
|
101
|
-
*
|
|
102
|
-
* @param type - The widget type identifier
|
|
103
|
-
* @returns The custom renderer if registered, undefined otherwise
|
|
104
|
-
*/
|
|
105
|
-
getRenderer(type: string): CustomWidgetRenderer | undefined {
|
|
106
|
-
return this.customRenderers.get(type);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Check if a custom renderer is registered for a widget type
|
|
111
|
-
*
|
|
112
|
-
* @param type - The widget type identifier
|
|
113
|
-
* @returns True if a custom renderer is registered, false otherwise
|
|
114
|
-
*/
|
|
115
|
-
hasRenderer(type: string): boolean {
|
|
116
|
-
return this.customRenderers.has(type);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Unregister a custom renderer for a widget type
|
|
121
|
-
*
|
|
122
|
-
* @param type - The widget type identifier
|
|
123
|
-
* @returns True if a renderer was removed, false if none was registered
|
|
124
|
-
*/
|
|
125
|
-
unregisterRenderer(type: string): boolean {
|
|
126
|
-
return this.customRenderers.delete(type);
|
|
127
|
-
}
|
|
128
|
-
}
|