diff --git a/src/Utils/textEditor.ts b/src/Utils/textEditor.ts index f06253f58e2..a21a652ec03 100644 --- a/src/Utils/textEditor.ts +++ b/src/Utils/textEditor.ts @@ -20,29 +20,34 @@ export const getCaretInfo = (element: HTMLTextAreaElement) => { */ export const getCaretCoordinates = (element: HTMLTextAreaElement) => { const { start } = getCaretInfo(element); + const styles = window.getComputedStyle(element); + const textareaRect = element.getBoundingClientRect(); + const mirror = document.createElement("div"); + mirror.style.cssText = [ + "position: fixed", + "visibility: hidden", + `width: ${element.offsetWidth}px`, + `white-space: ${styles.whiteSpace}`, + `font-family: ${styles.fontFamily}`, + `font-size: ${styles.fontSize}`, + `line-height: ${styles.lineHeight}`, + `padding: ${styles.padding}`, + `top: ${window.scrollY + textareaRect.top}px`, + `left: ${window.scrollX + textareaRect.left}px`, + ].join(";"); - // Create a hidden div to measure text - const div = document.createElement("div"); - const span = document.createElement("span"); + mirror.textContent = element.value.substring(0, start); - // Copy textarea styles to div - const style = window.getComputedStyle(element); - div.style.cssText = style.cssText; - div.style.position = "absolute"; - div.style.visibility = "hidden"; - div.style.whiteSpace = "pre-wrap"; + const marker = document.createElement("span"); + marker.textContent = "I"; + mirror.appendChild(marker); + document.body.appendChild(mirror); - // Add text content - div.textContent = element.value.substring(0, start); - span.textContent = "."; // Marker for position - div.appendChild(span); - - document.body.appendChild(div); - const coordinates = span.getBoundingClientRect(); - document.body.removeChild(div); + const markerRect = marker.getBoundingClientRect(); + document.body.removeChild(mirror); return { - top: coordinates.top + window.scrollY, - left: coordinates.left + window.scrollX, + top: markerRect.top - textareaRect.top, + left: markerRect.left - textareaRect.left, }; }; diff --git a/src/components/Common/DiscussionNotesEditor.tsx b/src/components/Common/DiscussionNotesEditor.tsx index ab1eadbf8d1..1212af26366 100644 --- a/src/components/Common/DiscussionNotesEditor.tsx +++ b/src/components/Common/DiscussionNotesEditor.tsx @@ -95,8 +95,8 @@ const DiscussionNotesEditor: React.FC = ({ if (mentionText.includes(" ")) return; setMentionFilter(mentionText); - const { top, left } = getCaretCoordinates(event.target); - setMentionPosition({ top: top + 30, left: left + 10 }); + const coordinates = getCaretCoordinates(event.target); + setMentionPosition(coordinates); setShowMentions(true); } else { setShowMentions(false); @@ -109,7 +109,9 @@ const DiscussionNotesEditor: React.FC = ({ const cursorPosition = editorRef.current.selectionStart; const currentText = editorRef.current.value; - const newText = `${currentText.slice(0, cursorPosition)}@${currentText.slice(cursorPosition)}`; + const newText = `${currentText.slice(0, cursorPosition)}@${currentText.slice( + cursorPosition, + )}`; setText(newText); requestAnimationFrame(() => { @@ -119,8 +121,8 @@ const DiscussionNotesEditor: React.FC = ({ const newPosition = cursorPosition + 1; editorRef.current.setSelectionRange(newPosition, newPosition); - const { top, left } = getCaretCoordinates(editorRef.current); - setMentionPosition({ top: top + 20, left }); + const coordinates = getCaretCoordinates(editorRef.current); + setMentionPosition(coordinates); setShowMentions(true); }); }; diff --git a/src/components/Common/FilePreviewCard.tsx b/src/components/Common/FilePreviewCard.tsx index f72b42e1180..1d586bbe72a 100644 --- a/src/components/Common/FilePreviewCard.tsx +++ b/src/components/Common/FilePreviewCard.tsx @@ -61,7 +61,7 @@ export function FilePreviewCard({ e.stopPropagation(); onRemove(index); }} - className="absolute -right-1 -top-1 z-10 h-5 w-5 rounded-full bg-gray-300 text-gray-800 transition-colors duration-200 hover:bg-gray-400 hover:text-white" + className="absolute right-0 top-0 z-10 h-5 w-5 rounded-full bg-gray-300 text-gray-800 transition-colors duration-200 hover:bg-gray-400 hover:text-white" > ( -
-
- {user.first_name[0]} -
+
+

{user.first_name} {user.last_name}