2023-06-23 10:52:03 +00:00
|
|
|
import {offset, type Offset} from "caret-pos";
|
|
|
|
import {Component} from "preact";
|
2023-06-27 11:51:04 +00:00
|
|
|
import {type UserLabelsData} from "../../storage/exports.js";
|
2023-06-23 10:52:03 +00:00
|
|
|
import {log, querySelectorAll} from "../../utilities/exports.js";
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-06-23 10:52:03 +00:00
|
|
|
anonymizeUsernamesEnabled: boolean;
|
|
|
|
knownGroups: Set<string>;
|
|
|
|
userLabels: UserLabelsData;
|
2020-10-10 23:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
groups: Set<string>;
|
|
|
|
groupsHidden: boolean;
|
|
|
|
groupsMatches: Set<string>;
|
2022-02-23 13:52:06 +00:00
|
|
|
groupsPosition: Offset | undefined;
|
2020-10-10 23:32:27 +00:00
|
|
|
usernames: Set<string>;
|
|
|
|
usernamesHidden: boolean;
|
|
|
|
usernamesMatches: Set<string>;
|
2022-02-23 13:52:06 +00:00
|
|
|
usernamesPosition: Offset | undefined;
|
2020-10-10 23:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export class AutocompleteFeature extends Component<Props, State> {
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
// Get all the groups without their leading tildes.
|
2023-06-23 10:52:03 +00:00
|
|
|
const groups = Array.from(props.knownGroups).map((value) =>
|
|
|
|
value.startsWith("~") ? value.slice(1) : value,
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Get all the usernames on the page without their leading @s, and get
|
2022-02-24 12:46:51 +00:00
|
|
|
// all the usernames from the saved user labels.
|
2023-06-23 10:52:03 +00:00
|
|
|
const usernameElements = querySelectorAll<HTMLElement>(".link-user");
|
2020-10-10 23:32:27 +00:00
|
|
|
const usernames = [
|
2022-02-24 12:46:51 +00:00
|
|
|
...usernameElements.map((value) => {
|
2023-06-23 10:52:03 +00:00
|
|
|
if (props.anonymizeUsernamesEnabled) {
|
|
|
|
return (value.dataset.trxUsername ?? "<unknown>").toLowerCase();
|
2022-02-24 12:46:51 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
return value.textContent!.replace(/^@/, "").toLowerCase();
|
2022-02-24 12:46:51 +00:00
|
|
|
}),
|
2023-06-26 10:10:08 +00:00
|
|
|
...props.userLabels.map(({value}) => value.username),
|
2020-10-10 23:32:27 +00:00
|
|
|
].sort((a, b) => a.localeCompare(b));
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
groups: new Set(groups),
|
|
|
|
groupsHidden: true,
|
|
|
|
groupsMatches: new Set(groups),
|
2022-02-23 13:52:06 +00:00
|
|
|
groupsPosition: undefined,
|
2020-10-10 23:32:27 +00:00
|
|
|
usernames: new Set(usernames),
|
|
|
|
usernamesHidden: true,
|
|
|
|
usernamesMatches: new Set(usernames),
|
2022-02-23 13:52:06 +00:00
|
|
|
usernamesPosition: undefined,
|
2020-10-10 23:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add a keydown listener for the entire page.
|
2023-06-23 10:52:03 +00:00
|
|
|
document.addEventListener("keydown", this.globalInputHandler);
|
2023-08-14 13:09:32 +00:00
|
|
|
document.addEventListener("compositionupdate", this.globalInputHandler);
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
log(
|
|
|
|
`Autocomplete: Initialized with ${this.state.groups.size} groups and ` +
|
2022-02-23 13:52:06 +00:00
|
|
|
`${this.state.usernames.size} usernames.`,
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-14 13:09:32 +00:00
|
|
|
globalInputHandler = (event: CompositionEvent | KeyboardEvent) => {
|
2020-10-10 23:32:27 +00:00
|
|
|
const activeElement = document.activeElement as HTMLElement;
|
|
|
|
// Only add the autocompletes to textareas.
|
2023-06-23 10:52:03 +00:00
|
|
|
if (activeElement.tagName !== "TEXTAREA") {
|
2020-10-10 23:32:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to create autocompletes with.
|
|
|
|
const createHandler = (
|
|
|
|
prefix: string,
|
|
|
|
target: string,
|
2022-02-23 13:52:06 +00:00
|
|
|
values: Set<string>,
|
2020-10-10 23:32:27 +00:00
|
|
|
) => {
|
|
|
|
const dataAttribute = `data-trx-autocomplete-${target}`;
|
|
|
|
|
2023-08-14 13:09:32 +00:00
|
|
|
const key = event instanceof KeyboardEvent ? event.key : event.data;
|
|
|
|
if (key === prefix && !activeElement.getAttribute(dataAttribute)) {
|
2023-06-23 10:52:03 +00:00
|
|
|
activeElement.setAttribute(dataAttribute, "true");
|
|
|
|
activeElement.addEventListener("keyup", (event) => {
|
2020-12-16 16:46:20 +00:00
|
|
|
this.textareaInputHandler(event, prefix, target, values);
|
|
|
|
});
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
this.textareaInputHandler(event, prefix, target, values);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
createHandler("~", "groups", this.state.groups);
|
|
|
|
createHandler("@", "usernames", this.state.usernames);
|
2020-10-10 23:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
textareaInputHandler = (
|
2023-08-14 13:09:32 +00:00
|
|
|
event: CompositionEvent | KeyboardEvent,
|
2020-10-10 23:32:27 +00:00
|
|
|
prefix: string,
|
|
|
|
target: string,
|
2022-02-23 13:52:06 +00:00
|
|
|
values: Set<string>,
|
2020-10-10 23:32:27 +00:00
|
|
|
) => {
|
|
|
|
const textarea = event.target as HTMLTextAreaElement;
|
|
|
|
const text = textarea.value;
|
|
|
|
|
|
|
|
// If the prefix isn't in the textarea, return early.
|
|
|
|
if (!text.includes(prefix)) {
|
|
|
|
this.hide(target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the starting position of the caret (text cursor).
|
|
|
|
const position = textarea.selectionStart;
|
|
|
|
|
|
|
|
// Grab the last index of the prefix inside the beginning of the textarea
|
|
|
|
// and the starting position of the caret.
|
|
|
|
const prefixIndex = text.slice(0, position).lastIndexOf(prefix);
|
|
|
|
|
|
|
|
// Grab the input between the prefix and the caret position, which will be
|
|
|
|
// what the user is currently typing.
|
|
|
|
const input = text.slice(prefixIndex + prefix.length, position);
|
|
|
|
|
|
|
|
// If there is any whitespace in the input or there is no input at all,
|
|
|
|
// return early. Usernames cannot have whitespace in them.
|
2023-06-23 10:52:03 +00:00
|
|
|
if (/\s/.test(input) || input === "") {
|
2020-10-10 23:32:27 +00:00
|
|
|
this.hide(target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all the values that match the input using `includes`.
|
|
|
|
const matches = new Set<string>(
|
2022-02-23 13:52:06 +00:00
|
|
|
[...values].filter((value) => value.includes(input.toLowerCase())),
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// If there are no matches, return early.
|
|
|
|
if (matches.size === 0) {
|
|
|
|
this.hide(target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise make sure the list is shown in the correct place and also
|
|
|
|
// has all the new matches.
|
|
|
|
this.show(target, offset(textarea));
|
|
|
|
this.update(target, matches);
|
|
|
|
};
|
|
|
|
|
|
|
|
update = (target: string, matches: Set<string>) => {
|
2023-06-23 10:52:03 +00:00
|
|
|
if (target === "groups") {
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({
|
2022-02-23 13:52:06 +00:00
|
|
|
groupsMatches: matches,
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
2023-06-23 10:52:03 +00:00
|
|
|
} else if (target === "usernames") {
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({
|
2022-02-23 13:52:06 +00:00
|
|
|
usernamesMatches: matches,
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
show = (target: string, position: Offset) => {
|
2023-06-23 10:52:03 +00:00
|
|
|
if (target === "groups") {
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({
|
|
|
|
groupsHidden: false,
|
2022-02-23 13:52:06 +00:00
|
|
|
groupsPosition: position,
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
2023-06-23 10:52:03 +00:00
|
|
|
} else if (target === "usernames") {
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({
|
|
|
|
usernamesHidden: false,
|
2022-02-23 13:52:06 +00:00
|
|
|
usernamesPosition: position,
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
hide = (target: string) => {
|
2023-06-23 10:52:03 +00:00
|
|
|
if (target === "groups") {
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({groupsHidden: true});
|
2023-06-23 10:52:03 +00:00
|
|
|
} else if (target === "usernames") {
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({usernamesHidden: true});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
// Create the list of groups and usernames.
|
2023-06-23 10:52:03 +00:00
|
|
|
const groups = [...this.state.groupsMatches].map((value) => (
|
|
|
|
<li>~{value}</li>
|
|
|
|
));
|
|
|
|
const usernames = [...this.state.usernamesMatches].map((value) => (
|
|
|
|
<li>@{value}</li>
|
|
|
|
));
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
// Create the CSS class whether or not to hide the autocomplete.
|
2023-06-23 10:52:03 +00:00
|
|
|
const groupsHidden = this.state.groupsHidden ? "trx-hidden" : "";
|
|
|
|
const usernamesHidden = this.state.usernamesHidden ? "trx-hidden" : "";
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
// Create the position for the group and usernames autocomplete.
|
|
|
|
const groupsLeft = this.state.groupsPosition?.left ?? 0;
|
|
|
|
const groupsTop =
|
|
|
|
(this.state.groupsPosition?.top ?? 0) +
|
|
|
|
(this.state.groupsPosition?.height ?? 0);
|
|
|
|
|
|
|
|
const usernamesLeft = this.state.usernamesPosition?.left ?? 0;
|
|
|
|
const usernamesTop =
|
|
|
|
(this.state.usernamesPosition?.top ?? 0) +
|
|
|
|
(this.state.usernamesPosition?.height ?? 0);
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ul
|
|
|
|
id="trx-autocomplete-usernames"
|
|
|
|
class={`trx-autocomplete ${usernamesHidden}`}
|
|
|
|
style={`left: ${usernamesLeft}px; top: ${usernamesTop}px`}
|
|
|
|
>
|
|
|
|
{usernames}
|
|
|
|
</ul>
|
|
|
|
<ul
|
|
|
|
id="trx-autocomplete-groups"
|
|
|
|
class={`trx-autocomplete ${groupsHidden}`}
|
|
|
|
style={`left: ${groupsLeft}px; top: ${groupsTop}px`}
|
|
|
|
>
|
|
|
|
{groups}
|
|
|
|
</ul>
|
|
|
|
</>
|
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
}
|