[client] Fix mention autocomplete

This commit is contained in:
Laura Hausmann 2023-10-12 22:12:25 +02:00
parent da5939d1d6
commit bab61d2a32
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 20 additions and 9 deletions

View file

@ -102,6 +102,7 @@ import { defaultStore } from "@/store";
import { addSkinTone, emojilist } from "@/scripts/emojilist"; import { addSkinTone, emojilist } from "@/scripts/emojilist";
import { instance } from "@/instance"; import { instance } from "@/instance";
import { i18n } from "@/i18n"; import { i18n } from "@/i18n";
import * as Acct from "iceshrimp-js/built/acct";
interface EmojiDef { interface EmojiDef {
emoji: string; emoji: string;
@ -254,8 +255,10 @@ function exec() {
users.value = JSON.parse(cache); users.value = JSON.parse(cache);
fetching.value = false; fetching.value = false;
} else { } else {
const acct = Acct.parse(props.q);
os.api("users/search-by-username-and-host", { os.api("users/search-by-username-and-host", {
username: props.q, username: acct.username,
host: acct.host ?? undefined,
limit: 10, limit: 10,
detail: false, detail: false,
}).then((searchedUsers) => { }).then((searchedUsers) => {

View file

@ -3,6 +3,8 @@ import getCaretCoordinates from "textarea-caret";
import { toASCII } from "punycode/"; import { toASCII } from "punycode/";
import { popup } from "@/os"; import { popup } from "@/os";
const mentionRegex = /@(?<user>[a-zA-Z0-9_]+|$)@?(?<host>[a-zA-Z0-9-.]+)?/g;
export class Autocomplete { export class Autocomplete {
private suggestion: { private suggestion: {
x: Ref<number>; x: Ref<number>;
@ -70,7 +72,9 @@ export class Autocomplete {
const caretPos = this.textarea.selectionStart; const caretPos = this.textarea.selectionStart;
const text = this.text.substring(0, caretPos).split("\n").pop()!; const text = this.text.substring(0, caretPos).split("\n").pop()!;
const mentionIndex = text.lastIndexOf("@"); const mentionArray = [...text.matchAll(mentionRegex)];
const mention = mentionArray.at(-1);
const mentionIndex = mention?.index ?? -1;
const hashtagIndex = text.lastIndexOf("#"); const hashtagIndex = text.lastIndexOf("#");
const emojiIndex = text.lastIndexOf(":"); const emojiIndex = text.lastIndexOf(":");
const mfmTagIndex = text.lastIndexOf("$"); const mfmTagIndex = text.lastIndexOf("$");
@ -82,7 +86,6 @@ export class Autocomplete {
return; return;
} }
const isMention = mentionIndex !== -1;
const isHashtag = hashtagIndex !== -1; const isHashtag = hashtagIndex !== -1;
const isMfmTag = mfmTagIndex !== -1; const isMfmTag = mfmTagIndex !== -1;
const isEmoji = const isEmoji =
@ -90,12 +93,14 @@ export class Autocomplete {
let opened = false; let opened = false;
if (isMention) { if (mention?.groups) {
const username = text.substring(mentionIndex + 1); const username = mention.groups.user;
if (username !== "" && username.match(/^[a-zA-Z0-9_]+$/)) { const host = mention.groups.host;
this.open("user", username); const acct = host ? `${username}@${host}` : username;
if (acct !== "") {
this.open("user", acct);
opened = true; opened = true;
} else if (username === "") { } else {
this.open("user", null); this.open("user", null);
opened = true; opened = true;
} }
@ -216,7 +221,10 @@ export class Autocomplete {
const source = this.text; const source = this.text;
const before = source.substring(0, caret); const before = source.substring(0, caret);
const trimmedBefore = before.substring(0, before.lastIndexOf("@")); const mentionArray = [...before.matchAll(mentionRegex)];
const mention = mentionArray.at(-1);
const mentionIndex = mention?.index ?? -1;
const trimmedBefore = before.substring(0, mentionIndex);
const after = source.substring(caret); const after = source.substring(caret);
const acct = const acct =