` and getting the coordinates to position the popup:
+
+```js
+// Copy relevant styles from textarea to measuring div
+function copyStyles() {
+ const styles = window.getComputedStyle(textarea);
+ const relevantStyles = [...];
+ relevantStyles.forEach(style => {
+ measuringDiv.style[style] = styles[style];
+ });
+ ensureWordWrapMatches();
+}
+
+// Use Case 1
+
+function getCaretCoordinates() {
+ const text = textarea.value;
+ const caretPos = textarea.selectionStart;
+ // Create a copy of the content up to the caret
+ const textBeforeCaret = text.substring(0, caretPos);
+ // Copy styles before measuring
+ copyStyles();
+ // Set content and create a range
+ measuringDiv.textContent = textBeforeCaret;
+ // Add a span where the caret would be
+ const caretSpan = document.createElement('span');
+ caretSpan.textContent = '|';
+ measuringDiv.appendChild(caretSpan);
+ // Position the div over the textarea to measure
+ measuringDiv.style.visibility = 'hidden';
+ measuringDiv.style.position = 'fixed';
+ document.body.appendChild(measuringDiv);
+ // Get the position of the caret span
+ const caretRect = caretSpan.getBoundingClientRect();
+ const textareaRect = textarea.getBoundingClientRect();
+ // Clean up
+ measuringDiv.textContent = '';
+ // Return coordinates relative to the viewport
+ return {
+ left: textareaRect.left + (caretRect.left - measuringDiv.getBoundingClientRect().left),
+ top: textareaRect.top + (caretRect.top - measuringDiv.getBoundingClientRect().top),
+ height: caretRect.height
+ };
+}
+
+// Use Case 2
+
+function highlightSyntax(start_index, end_index) {
+ const text = textarea.value;
+ // Copy styles
+ copyStyles();
+ // Set content
+ measuringDIv.textContent = text;
+
+ // Create range
+ const range = document.createRange();
+ range.setStart(measuringDiv.firstChild, start_index);
+ range.setStart(measuringDiv.firstChild, end_index);
+
+ // Add highlight
+ highlight.add(range);
+
+ // Apply highlight
+ CSS.highlights.set('syntax-highlight', highlight);
+}
+
+textarea.addEventListener('input', (e) => {
+ const selectionStart = textarea.selectionStart;
+ const text = textarea.value;
+ // Check if the last character typed was @ for Use Case 1
+ if (text[selectionStart - 1] === '@') {
+ const coords = getCaretCoordinates();
+ // Position and show the user list
+ userList.style.left = `${coords.left}px`;
+ userList.style.top = `${coords.top + coords.height}px`;
+ userList.style.display = 'block';
+ populateUserList();
+ } else {
+ userList.style.display = 'none';
+ }
+
+ // Check if the last character types was ' ' for Use Case 2
+ if (text[selectionStart - 1] === ' ' ) {
+ if(!dictionary.has(previousWord)){
+ highlightSyntax(selectionStart, selectionStart-previousWord.length);
+ previousWord = '';
+ }
+ }
+ else {
+ previousWord += text[selectionStart - 1];
+ }
+});
+
+
+// Initial style copy
+copyStyles();
+// Initial word setup for highlight
+var previousWord = '';
+// Dictionary for syntax check
+const dictionary = Set();
+// Create highlight object
+const highlight = new Highlight();
+// Handle window resize
+window.addEventListener('resize',copyStyles);
+```
+
+### Option 2: Using `contenteditable` `
`
+
+Using a `
` for direct text handling can be challenging. Web authors may need to implement behaviors for form integration, cross-browser consistency, and accessibility to match `