forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot-chat-textarea.ts
More file actions
186 lines (157 loc) · 4.82 KB
/
Copy pathcopilot-chat-textarea.ts
File metadata and controls
186 lines (157 loc) · 4.82 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {
Component,
input,
output,
ElementRef,
AfterViewInit,
signal,
computed,
inject,
ChangeDetectionStrategy,
ViewEncapsulation,
} from "@angular/core";
import { cn } from "../../utils";
import { injectChatLabels } from "../../chat-config";
import { injectChatState } from "../../chat-state";
@Component({
selector: "textarea[copilotChatTextarea]",
standalone: true,
imports: [],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {
"[value]": "computedValue()",
"[placeholder]": "placeholder()",
"[disabled]": "disabled()",
"[class]": "computedClass()",
"[style.max-height.px]": "maxHeight()",
"[style.overflow]": "'auto'",
"[style.resize]": "'none'",
"(input)": "onInput($event)",
"(keydown)": "onKeyDown($event)",
"[attr.rows]": "1",
},
template: "",
styles: [],
})
export class CopilotChatTextarea implements AfterViewInit {
private elementRef = inject(ElementRef<HTMLTextAreaElement>);
get textareaRef() {
return this.elementRef;
}
inputValue = input<string | undefined>();
inputPlaceholder = input<string | undefined>();
inputMaxRows = input<number | undefined>();
inputAutoFocus = input<boolean | undefined>();
inputDisabled = input<boolean | undefined>();
inputClass = input<string | undefined>();
valueChange = output<string>();
keyDown = output<KeyboardEvent>();
readonly chatLabels = injectChatLabels();
readonly chatState = injectChatState();
// Internal signals
maxHeight = signal<number>(0);
// Computed values
computedValue = computed(
() => this.inputValue() ?? this.chatState.inputValue() ?? "",
);
placeholder = computed(
() => this.inputPlaceholder() || this.chatLabels.chatInputPlaceholder,
);
disabled = computed(() => this.inputDisabled() ?? false);
computedClass = computed(() => {
const baseClasses = cn(
// Layout and sizing
"w-full p-5 pb-0",
// Behavior
"outline-none resize-none",
// Background
"bg-transparent",
// Typography
"antialiased font-regular leading-relaxed text-[16px]",
// Placeholder styles
"placeholder:text-[#00000077] dark:placeholder:text-[#fffc]",
);
return cn(baseClasses, this.inputClass());
});
constructor() {}
ngAfterViewInit(): void {
this.calculateMaxHeight();
this.adjustHeight();
if (this.inputAutoFocus() ?? true) {
setTimeout(() => {
this.elementRef.nativeElement.focus();
});
}
}
onInput(event: Event): void {
const textarea = event.target as HTMLTextAreaElement;
const newValue = textarea.value;
this.valueChange.emit(newValue);
this.chatState.changeInput(newValue);
this.adjustHeight();
}
onKeyDown(event: KeyboardEvent): void {
// Check for Enter key without Shift
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
this.keyDown.emit(event);
} else {
this.keyDown.emit(event);
}
}
private calculateMaxHeight(): void {
const textarea = this.elementRef.nativeElement;
const maxRowsValue = this.inputMaxRows() ?? 5;
// Save current value
const currentValue = textarea.value;
// Clear content to measure single row height
textarea.value = "";
textarea.style.height = "auto";
// Get computed styles to account for padding
const computedStyle = window.getComputedStyle(textarea);
const paddingTop = parseFloat(computedStyle.paddingTop);
const paddingBottom = parseFloat(computedStyle.paddingBottom);
// Calculate actual content height (without padding)
const contentHeight = textarea.scrollHeight - paddingTop - paddingBottom;
// Calculate max height: content height for maxRows + padding
const calculatedMaxHeight =
contentHeight * maxRowsValue + paddingTop + paddingBottom;
this.maxHeight.set(calculatedMaxHeight);
// Restore original value
textarea.value = currentValue;
// Adjust height after calculating maxHeight
if (currentValue) {
this.adjustHeight();
}
}
private adjustHeight(): void {
const textarea = this.elementRef.nativeElement;
const maxHeightValue = this.maxHeight();
if (maxHeightValue > 0) {
textarea.style.height = "auto";
textarea.style.height = `${Math.min(textarea.scrollHeight, maxHeightValue)}px`;
}
}
/**
* Public method to focus the textarea
*/
focus(): void {
this.elementRef.nativeElement.focus();
}
/**
* Public method to get current value
*/
getValue(): string {
return this.elementRef.nativeElement.value;
}
/**
* Public method to set value programmatically
*/
setValue(value: string): void {
this.elementRef.nativeElement.value = value;
this.valueChange.emit(value);
this.chatState.changeInput(value);
setTimeout(() => this.adjustHeight());
}
}