-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathchat-input.ts
More file actions
187 lines (162 loc) · 5.13 KB
/
chat-input.ts
File metadata and controls
187 lines (162 loc) · 5.13 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
187
import { LitElement, html } from 'lit';
import { property, query, state } from 'lit/decorators.js';
import IgcIconButtonComponent from '../button/icon-button.js';
import IgcChipComponent from '../chip/chip.js';
import { registerComponent } from '../common/definitions/register.js';
import IgcFileInputComponent from '../file-input/file-input.js';
import IgcIconComponent from '../icon/icon.js';
import { registerIconFromText } from '../icon/icon.registry.js';
import IgcTextareaComponent from '../textarea/textarea.js';
import { styles } from './themes/input.base.css.js';
import {
type IgcMessageAttachment,
attachmentIcon,
sendButtonIcon,
} from './types.js';
/**
*
* @element igc-chat
*
*/
export default class IgcChatInputComponent extends LitElement {
/** @private */
public static readonly tagName = 'igc-chat-input';
public static override styles = styles;
/* blazorSuppress */
public static register() {
registerComponent(
IgcChatInputComponent,
IgcTextareaComponent,
IgcIconButtonComponent,
IgcChipComponent,
IgcFileInputComponent,
IgcIconComponent
);
}
@property({ type: Boolean, attribute: 'disable-attachments' })
public disableAttachments = false;
@property({ type: Boolean })
public isAiResponding = false;
@query('textarea')
private textInputElement!: HTMLTextAreaElement;
@state()
private inputValue = '';
@state()
private attachments: IgcMessageAttachment[] = [];
constructor() {
super();
registerIconFromText('attachment', attachmentIcon, 'material');
registerIconFromText('send-message', sendButtonIcon, 'material');
}
private handleInput(e: Event) {
const target = e.target as HTMLTextAreaElement;
this.inputValue = target.value;
this.adjustTextareaHeight();
}
private handleKeyDown(e: KeyboardEvent) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
this.sendMessage();
}
}
private adjustTextareaHeight() {
const textarea = this.textInputElement;
if (!textarea) return;
textarea.style.height = 'auto';
const newHeight = Math.min(textarea.scrollHeight, 120);
textarea.style.height = `${newHeight}px`;
}
private sendMessage() {
if (!this.inputValue.trim() && this.attachments.length === 0) return;
const messageEvent = new CustomEvent('message-send', {
detail: { text: this.inputValue, attachments: this.attachments },
});
this.dispatchEvent(messageEvent);
this.inputValue = '';
this.attachments = [];
if (this.textInputElement) {
this.textInputElement.style.height = 'auto';
}
setTimeout(() => {
this.textInputElement?.focus();
}, 0);
}
private handleFileUpload(e: Event) {
const input = (e.target as any).input as HTMLInputElement;
if (!input.files || input.files.length === 0) return;
const files = Array.from(input.files);
const newAttachments: IgcMessageAttachment[] = [];
files.forEach((file) => {
const isImage = file.type.startsWith('image/');
newAttachments.push({
id: `attach_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
type: isImage ? 'image' : 'file',
url: URL.createObjectURL(file),
name: file.name,
size: file.size,
thumbnail: isImage ? URL.createObjectURL(file) : undefined,
});
});
this.attachments = [...this.attachments, ...newAttachments];
}
private removeAttachment(index: number) {
this.attachments = this.attachments.filter((_, i) => i !== index);
}
protected override render() {
return html`
<div class="input-container">
${this.disableAttachments
? ''
: html`
<igc-file-input multiple @igcChange=${this.handleFileUpload}>
<igc-icon
slot="file-selector-text"
name="attachment"
collection="material"
></igc-icon>
</igc-file-input>
`}
<div class="input-wrapper">
<igc-textarea
class="text-input"
placeholder="Type a message..."
rows="1"
.value=${this.inputValue}
@input=${this.handleInput}
@keydown=${this.handleKeyDown}
></igc-textarea>
</div>
<div class="buttons-container">
<igc-icon-button
name="send-message"
collection="material"
variant="contained"
class="small"
?disabled=${!this.inputValue.trim() &&
this.attachments.length === 0}
@click=${this.sendMessage}
></igc-icon-button>
</div>
</div>
<div>
${this.attachments?.map(
(attachment, index) => html`
<div class="attachment-wrapper">
<igc-chip
removable
@igcRemove=${() => this.removeAttachment(index)}
>
<span class="attachment-name">${attachment.name}</span>
</igc-chip>
</div>
`
)}
</div>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-chat-input': IgcChatInputComponent;
}
}