Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3382849
D5674.id18948.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D5674.id18948.diff
View Options
diff --git a/lib/shared/mention-utils.js b/lib/shared/mention-utils.js
--- a/lib/shared/mention-utils.js
+++ b/lib/shared/mention-utils.js
@@ -5,6 +5,10 @@
import SearchIndex from './search-index';
import { stringForUserExplicit } from './user-utils';
+const mentionRegex: RegExp = new RegExp(
+ `(?<textPrefix>(?:^(?:.|\n)*\\s+)|^)@(?<username>${oldValidUsernameRegexString})?$`,
+);
+
function getTypeaheadUserSuggestions(
userSearchIndex: SearchIndex,
usersInThread: $ReadOnlyArray<RelativeMemberInfo>,
@@ -13,17 +17,51 @@
const userIDs = userSearchIndex.getSearchResults(typedPrefix);
return usersInThread
- .filter(
- (user: RelativeMemberInfo) =>
- typedPrefix.length === 0 || userIDs.includes(user.id),
- )
+ .filter(user => typedPrefix.length === 0 || userIDs.includes(user.id))
.sort((userA, userB) =>
stringForUserExplicit(userA).localeCompare(stringForUserExplicit(userB)),
);
}
-const mentionRegex: RegExp = new RegExp(
- `(^|.* )@(${oldValidUsernameRegexString})?$`,
-);
+function getCaretOffsets(
+ textarea: ?HTMLTextAreaElement,
+ text: string,
+): { caretTopOffset: number, caretLeftOffset: number } {
+ if (!textarea) {
+ return { caretTopOffset: 0, caretLeftOffset: 0 };
+ }
+
+ // terribly hacky but it works I guess :D
+ // we had to use it, as it's hard to count lines in textarea
+ // and track cursor position within it as
+ // lines can be wrapped into new lines without \n character
+ // as result of overflow
+ const textareaStyle: CSSStyleDeclaration = window.getComputedStyle(
+ textarea,
+ null,
+ );
+ const div = document.createElement('div');
+
+ for (const styleName of textareaStyle) {
+ div.style.setProperty(styleName, textareaStyle.getPropertyValue(styleName));
+ }
+
+ div.style.display = 'inline-block';
+ div.style.position = 'absolute';
+ div.textContent = text;
+
+ const span = document.createElement('span');
+ span.textContent = textarea.value.slice(text.length);
+ div.appendChild(span);
+
+ document.body?.appendChild(div);
+ const { offsetTop, offsetLeft } = span;
+ document.body?.removeChild(div);
+
+ return {
+ caretTopOffset: offsetTop - textarea.scrollTop,
+ caretLeftOffset: offsetLeft,
+ };
+}
-export { getTypeaheadUserSuggestions, mentionRegex };
+export { mentionRegex, getTypeaheadUserSuggestions, getCaretOffsets };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Nov 29, 12:21 PM (21 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2595718
Default Alt Text
D5674.id18948.diff (2 KB)
Attached To
Mode
D5674: [lib] Added getTextsOffsets function. Refactored mention-utils.js.
Attached
Detach File
Event Timeline
Log In to Comment