forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot-slot.ts
More file actions
150 lines (132 loc) · 4.05 KB
/
Copy pathcopilot-slot.ts
File metadata and controls
150 lines (132 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {
Component,
TemplateRef,
ViewContainerRef,
OnInit,
OnChanges,
SimpleChanges,
Inject,
ChangeDetectionStrategy,
ChangeDetectorRef,
input,
ViewChild,
} from "@angular/core";
import { CommonModule } from "@angular/common";
import { renderSlot } from "./slot.utils";
import { Type } from "@angular/core";
/**
* @internal - This component is for internal use only.
* Simple slot component for rendering custom content or defaults.
* Supports templates and components only.
*
* @example
* ```html
* <!-- With template -->
* <copilot-slot [slot]="sendButtonTemplate" [context]="buttonContext">
* <button class="default-btn">Default</button>
* </copilot-slot>
* ```
*/
@Component({
standalone: true,
selector: "copilot-slot",
imports: [CommonModule],
template: `
<!-- If slot template provided, render it -->
@if (slot() && isTemplate(slot)) {
<ng-container
[ngTemplateOutlet]="slot"
[ngTemplateOutletContext]="context || {}"
>
</ng-container>
}
<!-- If not a template, we'll handle in code -->
<ng-container #slotContainer></ng-container>
<!-- Default content (only shown if no slot) -->
@if (!slot && !defaultComponent) {
<ng-content></ng-content>
}
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CopilotSlot implements OnInit, OnChanges {
slot = input<TemplateRef<any> | Type<any> | undefined>(undefined);
context = input<any | undefined>(undefined);
defaultComponent = input<Type<any> | undefined>(undefined);
outputs = input<Record<string, (event: any) => void> | undefined>(undefined);
@ViewChild("slotContainer", { read: ViewContainerRef, static: true })
private slotContainer!: ViewContainerRef;
private componentRef?: any;
constructor(
@Inject(ViewContainerRef) private viewContainer: ViewContainerRef,
private cdr: ChangeDetectorRef,
) {}
ngOnInit(): void {
this.renderSlot();
}
ngOnChanges(changes: SimpleChanges): void {
if (changes["slot"]) {
// Slot changed, need to re-render completely
this.renderSlot();
} else if (changes["context"] && this.componentRef) {
// Just context changed, update existing component
this.updateComponentProps();
this.cdr.detectChanges();
} else if (changes["context"]) {
// No component ref yet, render the slot
this.renderSlot();
}
}
isTemplate(value: any): value is TemplateRef<any> {
return value instanceof TemplateRef;
}
private renderSlot(): void {
// Skip if it's a template (handled by ngTemplateOutlet)
if (this.slot() && this.isTemplate(this.slot())) {
this.componentRef = null;
return;
}
// Clear previous content
this.slotContainer.clear();
this.componentRef = null;
// Skip if no slot and no default component
if (!this.slot() && !this.defaultComponent()) {
return;
}
// Use the utility to render other slot types
if (this.slot() || this.defaultComponent()) {
this.componentRef = renderSlot(this.slotContainer, {
slot: this.slot(),
defaultComponent: this.defaultComponent()!,
props: this.context(),
outputs: this.outputs(),
});
}
}
private updateComponentProps(): void {
if (!this.componentRef || !this.componentRef.instance) {
return;
}
const props = this.context();
// Update props using setInput, only for declared inputs
if (props) {
const ctor = this.componentRef.instance.constructor as any;
const cmpDef: any = ctor?.ɵcmp;
const declaredInputs = new Set<string>(Object.keys(cmpDef?.inputs ?? {}));
if (declaredInputs.has("props")) {
this.componentRef.setInput("props", props);
} else {
for (const key in props) {
if (declaredInputs.has(key)) {
const value = props[key];
this.componentRef.setInput(key, value);
}
}
}
}
// Trigger change detection
if (this.componentRef.changeDetectorRef) {
this.componentRef.changeDetectorRef.detectChanges();
}
}
}