Skip to content

A platform for community discussion. Free, open, simple.

DEV: Create wrapper API for `getCaretPosition()` jQuery (#23194)

Keegan George92b2e10ee848
diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.js
index a7d24da4ab9..2ec9a246c54 100644
--- a/app/assets/javascripts/discourse/app/lib/utilities.js
+++ b/app/assets/javascripts/discourse/app/lib/utilities.js
@@ -596,3 +596,19 @@ export function mergeSortedLists(list1, list2, comparator) {
}
return merged;
}
+
+export function getCaretPosition(element, options) {
+ const jqueryElement = $(element);
+ const position = jqueryElement.caretPosition(options);
+
+ // Get the position of the textarea on the page
+ const textareaRect = element.getBoundingClientRect();
+
+ // Calculate the x and y coordinates by adding the element's position
+ const adjustedPosition = {
+ x: position.left + textareaRect.left,
+ y: position.top + textareaRect.top,
+ };
+
+ return adjustedPosition;
+}