| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1 | import { css, html, LitElement, PropertyValues } from "lit"; |
| Philip Zeyliger | 73db605 | 2025-04-23 13:09:07 -0700 | [diff] [blame] | 2 | import { customElement, property, state, query } from "lit/decorators.js"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 3 | |
| 4 | @customElement("sketch-chat-input") |
| 5 | export class SketchChatInput extends LitElement { |
| Philip Zeyliger | 73db605 | 2025-04-23 13:09:07 -0700 | [diff] [blame] | 6 | @state() |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 7 | content: string = ""; |
| 8 | |
| 9 | // See https://lit.dev/docs/components/styles/ for how lit-element handles CSS. |
| 10 | // Note that these styles only apply to the scope of this web component's |
| 11 | // shadow DOM node, so they won't leak out or collide with CSS declared in |
| 12 | // other components or the containing web page (...unless you want it to do that). |
| 13 | static styles = css` |
| 14 | /* Chat styles - exactly matching timeline.css */ |
| 15 | .chat-container { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 16 | width: 100%; |
| 17 | background: #f0f0f0; |
| 18 | padding: 15px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 19 | min-height: 40px; /* Ensure minimum height */ |
| 20 | } |
| 21 | |
| 22 | .chat-input-wrapper { |
| 23 | display: flex; |
| 24 | max-width: 1200px; |
| 25 | margin: 0 auto; |
| 26 | gap: 10px; |
| 27 | } |
| 28 | |
| 29 | #chatInput { |
| 30 | flex: 1; |
| 31 | padding: 12px; |
| 32 | border: 1px solid #ddd; |
| 33 | border-radius: 4px; |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 34 | resize: vertical; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 35 | font-family: monospace; |
| 36 | font-size: 12px; |
| 37 | min-height: 40px; |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 38 | max-height: 300px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 39 | background: #f7f7f7; |
| Sean McCullough | 5164eee | 2025-04-21 18:20:23 -0700 | [diff] [blame] | 40 | overflow-y: auto; |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 41 | box-sizing: border-box; /* Ensure padding is included in height calculation */ |
| 42 | line-height: 1.4; /* Consistent line height for better height calculation */ |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | #sendChatButton { |
| 46 | background-color: #2196f3; |
| 47 | color: white; |
| 48 | border: none; |
| 49 | border-radius: 4px; |
| 50 | padding: 0 20px; |
| 51 | cursor: pointer; |
| 52 | font-weight: 600; |
| Pokey Rule | 97188fc | 2025-04-23 15:50:04 +0100 | [diff] [blame] | 53 | align-self: center; |
| 54 | height: 40px; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | #sendChatButton:hover { |
| 58 | background-color: #0d8bf2; |
| 59 | } |
| 60 | `; |
| 61 | |
| 62 | constructor() { |
| 63 | super(); |
| Philip Zeyliger | 73db605 | 2025-04-23 13:09:07 -0700 | [diff] [blame] | 64 | this._handleDiffComment = this._handleDiffComment.bind(this); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 67 | connectedCallback() { |
| 68 | super.connectedCallback(); |
| Philip Zeyliger | 73db605 | 2025-04-23 13:09:07 -0700 | [diff] [blame] | 69 | window.addEventListener("diff-comment", this._handleDiffComment); |
| 70 | } |
| 71 | |
| 72 | private _handleDiffComment(event: CustomEvent) { |
| 73 | const { comment } = event.detail; |
| 74 | if (!comment) return; |
| 75 | |
| 76 | if (this.content != "") { |
| 77 | this.content += "\n\n"; |
| 78 | } |
| 79 | this.content += comment; |
| 80 | requestAnimationFrame(() => this.adjustChatSpacing()); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // See https://lit.dev/docs/components/lifecycle/ |
| 84 | disconnectedCallback() { |
| 85 | super.disconnectedCallback(); |
| Philip Zeyliger | 73db605 | 2025-04-23 13:09:07 -0700 | [diff] [blame] | 86 | window.removeEventListener("diff-comment", this._handleDiffComment); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | sendChatMessage() { |
| 90 | const event = new CustomEvent("send-chat", { |
| 91 | detail: { message: this.content }, |
| 92 | bubbles: true, |
| 93 | composed: true, |
| 94 | }); |
| 95 | this.dispatchEvent(event); |
| Philip Zeyliger | 73db605 | 2025-04-23 13:09:07 -0700 | [diff] [blame] | 96 | |
| 97 | // TODO(philip?): Ideally we only clear the content if the send is successful. |
| 98 | this.content = ""; // Clear content after sending |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | adjustChatSpacing() { |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 102 | if (!this.chatInput) return; |
| Sean McCullough | 5164eee | 2025-04-21 18:20:23 -0700 | [diff] [blame] | 103 | |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 104 | // Reset height to minimal value to correctly calculate scrollHeight |
| Sean McCullough | 5164eee | 2025-04-21 18:20:23 -0700 | [diff] [blame] | 105 | this.chatInput.style.height = "auto"; |
| 106 | |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 107 | // Get the scroll height (content height) |
| 108 | const scrollHeight = this.chatInput.scrollHeight; |
| Sean McCullough | 5164eee | 2025-04-21 18:20:23 -0700 | [diff] [blame] | 109 | |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 110 | // Set the height to match content (up to max-height which is handled by CSS) |
| 111 | this.chatInput.style.height = `${scrollHeight}px`; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| Philip Zeyliger | 73db605 | 2025-04-23 13:09:07 -0700 | [diff] [blame] | 114 | async _sendChatClicked() { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 115 | this.sendChatMessage(); |
| 116 | this.chatInput.focus(); // Refocus the input after sending |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 117 | // Reset height after sending a message |
| 118 | requestAnimationFrame(() => this.adjustChatSpacing()); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | _chatInputKeyDown(event: KeyboardEvent) { |
| 122 | // Send message if Enter is pressed without Shift key |
| 123 | if (event.key === "Enter" && !event.shiftKey) { |
| 124 | event.preventDefault(); // Prevent default newline |
| 125 | this.sendChatMessage(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | _chatInputChanged(event) { |
| 130 | this.content = event.target.value; |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 131 | // Use requestAnimationFrame to ensure DOM updates have completed |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 132 | requestAnimationFrame(() => this.adjustChatSpacing()); |
| 133 | } |
| 134 | |
| 135 | @query("#chatInput") |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 136 | chatInput: HTMLTextAreaElement; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 137 | |
| 138 | protected firstUpdated(): void { |
| 139 | if (this.chatInput) { |
| 140 | this.chatInput.focus(); |
| Sean McCullough | 07b3e39 | 2025-04-21 22:51:14 +0000 | [diff] [blame] | 141 | // Initialize the input height |
| 142 | this.adjustChatSpacing(); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 143 | } |
| Josh Bleecher Snyder | e2c7f72 | 2025-05-01 21:58:41 +0000 | [diff] [blame] | 144 | |
| 145 | // Add window.onload handler to ensure the input is focused when the page fully loads |
| 146 | window.addEventListener( |
| 147 | "load", |
| 148 | () => { |
| 149 | if (this.chatInput) { |
| 150 | this.chatInput.focus(); |
| 151 | } |
| 152 | }, |
| 153 | { once: true }, |
| 154 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | render() { |
| 158 | return html` |
| 159 | <div class="chat-container"> |
| 160 | <div class="chat-input-wrapper"> |
| 161 | <textarea |
| 162 | id="chatInput" |
| 163 | placeholder="Type your message here and press Enter to send..." |
| 164 | autofocus |
| 165 | @keydown="${this._chatInputKeyDown}" |
| 166 | @input="${this._chatInputChanged}" |
| 167 | .value=${this.content || ""} |
| 168 | ></textarea> |
| 169 | <button @click="${this._sendChatClicked}" id="sendChatButton"> |
| 170 | Send |
| 171 | </button> |
| 172 | </div> |
| 173 | </div> |
| 174 | `; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | declare global { |
| 179 | interface HTMLElementTagNameMap { |
| 180 | "sketch-chat-input": SketchChatInput; |
| 181 | } |
| 182 | } |