2023-06-23 10:52:03 +00:00
|
|
|
import debounce from "debounce";
|
|
|
|
import {Component, render} from "preact";
|
2023-06-26 10:10:08 +00:00
|
|
|
import {
|
|
|
|
type UserLabelsData,
|
|
|
|
createValueUserLabel,
|
|
|
|
saveUserLabels,
|
2023-06-27 11:51:04 +00:00
|
|
|
} from "../../storage/exports.js";
|
2020-10-10 23:32:27 +00:00
|
|
|
import {
|
|
|
|
createElementFromString,
|
|
|
|
isColorBright,
|
|
|
|
log,
|
|
|
|
querySelectorAll,
|
2022-02-23 13:52:06 +00:00
|
|
|
themeColors,
|
2023-06-23 10:52:03 +00:00
|
|
|
} from "../../utilities/exports.js";
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-06-23 10:52:03 +00:00
|
|
|
anonymizeUsernamesEnabled: boolean;
|
2023-06-24 12:00:27 +00:00
|
|
|
userLabels: UserLabelsData;
|
2020-10-10 23:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
color: string;
|
|
|
|
selectedColor: string;
|
|
|
|
hidden: boolean;
|
2022-02-23 13:52:06 +00:00
|
|
|
id: number | undefined;
|
2020-10-10 23:32:27 +00:00
|
|
|
priority: number;
|
2022-02-23 13:52:06 +00:00
|
|
|
target: HTMLElement | undefined;
|
2020-10-10 23:32:27 +00:00
|
|
|
text: string;
|
|
|
|
username: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class UserLabelsFeature extends Component<Props, State> {
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
const selectedColor = window
|
|
|
|
.getComputedStyle(document.body)
|
|
|
|
.getPropertyValue(themeColors[1].value)
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
color: selectedColor,
|
|
|
|
hidden: true,
|
2022-02-23 13:52:06 +00:00
|
|
|
id: undefined,
|
2023-06-23 10:52:03 +00:00
|
|
|
text: "",
|
2020-10-10 23:32:27 +00:00
|
|
|
priority: 0,
|
|
|
|
selectedColor,
|
2022-02-23 13:52:06 +00:00
|
|
|
target: undefined,
|
2023-06-23 10:52:03 +00:00
|
|
|
username: "",
|
2020-10-10 23:32:27 +00:00
|
|
|
};
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
const count = this.addLabelsToUsernames(querySelectorAll(".link-user"));
|
2020-10-10 23:32:27 +00:00
|
|
|
log(`User Labels: Initialized for ${count} user links.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
hide = () => {
|
|
|
|
this.setState({hidden: true});
|
|
|
|
};
|
|
|
|
|
2022-02-23 17:17:22 +00:00
|
|
|
addLabelsToUsernames = (elements: HTMLElement[], onlyID?: number): number => {
|
2023-06-23 10:52:03 +00:00
|
|
|
const {userLabels} = this.props;
|
|
|
|
const inTopicListing = document.querySelector(".topic-listing") !== null;
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
// Sort the labels by priority or alphabetically, so 2 labels with the same
|
|
|
|
// priority will be sorted alphabetically.
|
2023-06-24 12:00:27 +00:00
|
|
|
const sortedLabels = userLabels.sort((a, b): number => {
|
2020-10-10 23:32:27 +00:00
|
|
|
if (inTopicListing) {
|
|
|
|
// If we're in the topic listing sort with highest priority first.
|
2023-06-26 10:10:08 +00:00
|
|
|
if (a.value.priority !== b.value.priority) {
|
|
|
|
return b.value.priority - a.value.priority;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
2023-06-26 10:10:08 +00:00
|
|
|
} else if (a.value.priority !== b.value.priority) {
|
2020-10-10 23:32:27 +00:00
|
|
|
// If we're not in the topic listing, sort with lowest priority first.
|
|
|
|
// We will add elements backwards, so the first label will be
|
|
|
|
// behind all the other labels.
|
2023-06-26 10:10:08 +00:00
|
|
|
return a.value.priority - b.value.priority;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 10:10:08 +00:00
|
|
|
return b.value.text.localeCompare(a.value.text);
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const element of elements) {
|
2022-02-23 17:17:22 +00:00
|
|
|
let username: string = element
|
2023-06-23 10:52:03 +00:00
|
|
|
.textContent!.replace(/@/g, "")
|
2020-10-10 23:32:27 +00:00
|
|
|
.toLowerCase();
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
if (this.props.anonymizeUsernamesEnabled) {
|
2022-02-23 17:17:22 +00:00
|
|
|
username = element.dataset.trxUsername ?? username;
|
|
|
|
}
|
|
|
|
|
2020-10-10 23:32:27 +00:00
|
|
|
const userLabels = sortedLabels.filter(
|
2023-06-26 10:10:08 +00:00
|
|
|
({value}) =>
|
2023-06-24 12:00:27 +00:00
|
|
|
value.username.toLowerCase() === username &&
|
2022-02-23 13:52:06 +00:00
|
|
|
(onlyID === undefined ? true : value.id === onlyID),
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
const addLabel = (
|
2020-10-10 23:32:27 +00:00
|
|
|
<span
|
|
|
|
class="trx-user-label-add"
|
2023-06-23 10:52:03 +00:00
|
|
|
onClick={(event: MouseEvent) => {
|
2020-12-16 16:46:20 +00:00
|
|
|
this.addLabelHandler(event, username);
|
|
|
|
}}
|
2020-10-10 23:32:27 +00:00
|
|
|
>
|
|
|
|
[+]
|
|
|
|
</span>
|
2023-06-23 10:52:03 +00:00
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
if (!inTopicListing && onlyID === undefined) {
|
2023-06-23 10:52:03 +00:00
|
|
|
const addLabelPlaceholder = document.createElement("span");
|
2020-10-10 23:32:27 +00:00
|
|
|
element.after(addLabelPlaceholder);
|
|
|
|
render(addLabel, element.parentElement!, addLabelPlaceholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userLabels.length === 0 && onlyID === undefined) {
|
|
|
|
if (
|
|
|
|
inTopicListing &&
|
|
|
|
(element.nextElementSibling === null ||
|
2023-06-23 10:52:03 +00:00
|
|
|
!element.nextElementSibling.className.includes("trx-user-label"))
|
2020-10-10 23:32:27 +00:00
|
|
|
) {
|
2023-06-23 10:52:03 +00:00
|
|
|
const addLabelPlaceholder = document.createElement("span");
|
2020-10-10 23:32:27 +00:00
|
|
|
element.after(addLabelPlaceholder);
|
|
|
|
render(addLabel, element.parentElement!, addLabelPlaceholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const userLabel of userLabels) {
|
2023-06-26 10:10:08 +00:00
|
|
|
const bright = isColorBright(userLabel.value.color.trim())
|
2023-06-23 10:52:03 +00:00
|
|
|
? "trx-bright"
|
|
|
|
: "";
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
const label = createElementFromString<HTMLSpanElement>(`<span
|
2023-06-26 10:10:08 +00:00
|
|
|
data-trx-label-id="${userLabel.value.id}"
|
2020-10-10 23:32:27 +00:00
|
|
|
class="trx-user-label ${bright}"
|
|
|
|
>
|
2023-06-26 10:10:08 +00:00
|
|
|
${userLabel.value.text}
|
2020-10-10 23:32:27 +00:00
|
|
|
</span>`);
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
label.addEventListener("click", (event: MouseEvent) => {
|
2023-06-26 10:10:08 +00:00
|
|
|
this.editLabelHandler(event, userLabel.value.id);
|
2020-12-16 16:46:20 +00:00
|
|
|
});
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
element.after(label);
|
2023-06-26 10:10:08 +00:00
|
|
|
label.setAttribute(
|
|
|
|
"style",
|
|
|
|
`background-color: ${userLabel.value.color};`,
|
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
// If we're in the topic listing, stop after adding 1 label.
|
|
|
|
if (inTopicListing) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return elements.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
addLabelHandler = (event: MouseEvent, username: string) => {
|
|
|
|
event.preventDefault();
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
|
|
if (this.state.target === target && !this.state.hidden) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
const selectedColor = window
|
|
|
|
.getComputedStyle(document.body)
|
|
|
|
.getPropertyValue(themeColors[1].value)
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
hidden: false,
|
|
|
|
target,
|
|
|
|
username,
|
|
|
|
color: selectedColor,
|
2022-02-23 13:52:06 +00:00
|
|
|
id: undefined,
|
2023-06-23 10:52:03 +00:00
|
|
|
text: "",
|
2020-10-10 23:32:27 +00:00
|
|
|
priority: 0,
|
2022-02-23 13:52:06 +00:00
|
|
|
selectedColor,
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
editLabelHandler = (event: MouseEvent, id: number) => {
|
|
|
|
event.preventDefault();
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
|
|
|
if (this.state.target === target && !this.state.hidden) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
2023-06-26 10:10:08 +00:00
|
|
|
const label = this.props.userLabels.find(({value}) => value.id === id);
|
2020-10-10 23:32:27 +00:00
|
|
|
if (label === undefined) {
|
|
|
|
log(
|
2023-06-23 10:52:03 +00:00
|
|
|
"User Labels: Tried to edit label with ID that could not be found.",
|
2022-02-23 13:52:06 +00:00
|
|
|
true,
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
hidden: false,
|
|
|
|
target,
|
2023-06-26 10:10:08 +00:00
|
|
|
color: label.value.color,
|
|
|
|
id: label.value.id,
|
|
|
|
priority: label.value.priority,
|
|
|
|
text: label.value.text,
|
|
|
|
username: label.value.username,
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
colorChange = (event: Event) => {
|
2023-06-25 10:19:18 +00:00
|
|
|
const color = (event.target as HTMLInputElement).value.toLowerCase();
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
// If the color was changed through the preset values, also change the
|
|
|
|
// selected color state.
|
2023-06-23 10:52:03 +00:00
|
|
|
if ((event.target as HTMLElement).tagName === "SELECT") {
|
2020-10-10 23:32:27 +00:00
|
|
|
this.setState({color, selectedColor: color});
|
|
|
|
} else {
|
|
|
|
this.setState({color});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
labelChange = (event: Event) => {
|
|
|
|
this.setState({text: (event.target as HTMLInputElement).value});
|
|
|
|
};
|
|
|
|
|
|
|
|
priorityChange = (event: Event) => {
|
|
|
|
this.setState({
|
2022-02-23 13:52:06 +00:00
|
|
|
priority: Number((event.target as HTMLInputElement).value),
|
2020-10-10 23:32:27 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-23 13:52:06 +00:00
|
|
|
save = async (event: MouseEvent) => {
|
2020-10-10 23:32:27 +00:00
|
|
|
event.preventDefault();
|
|
|
|
const {color, id, text, priority, username} = this.state;
|
2023-06-23 10:52:03 +00:00
|
|
|
if (color === "" || username === "") {
|
|
|
|
log("Cannot save user label without all values present.");
|
2020-10-10 23:32:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
const {userLabels} = this.props;
|
2020-10-10 23:32:27 +00:00
|
|
|
// If no ID is present then save a new label otherwise edit the existing one.
|
2022-02-23 13:52:06 +00:00
|
|
|
if (id === undefined) {
|
2023-06-23 10:52:03 +00:00
|
|
|
let newId = 1;
|
2023-06-24 12:00:27 +00:00
|
|
|
if (userLabels.length > 0) {
|
2023-06-26 10:10:08 +00:00
|
|
|
newId =
|
|
|
|
userLabels.sort((a, b) => b.value.id - a.value.id)[0].value.id + 1;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 10:10:08 +00:00
|
|
|
userLabels.push(
|
|
|
|
await createValueUserLabel({
|
|
|
|
color,
|
|
|
|
id: newId,
|
|
|
|
priority,
|
|
|
|
text,
|
|
|
|
username,
|
|
|
|
}),
|
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
this.addLabelsToUsernames(querySelectorAll(".link-user"), newId);
|
2020-10-10 23:32:27 +00:00
|
|
|
} else {
|
2023-06-26 10:10:08 +00:00
|
|
|
const index = userLabels.findIndex(({value}) => value.id === id);
|
2023-06-24 12:00:27 +00:00
|
|
|
userLabels.splice(index, 1);
|
2023-06-26 10:10:08 +00:00
|
|
|
userLabels.push(
|
|
|
|
await createValueUserLabel({
|
|
|
|
id,
|
|
|
|
color,
|
|
|
|
priority,
|
|
|
|
text,
|
|
|
|
username,
|
|
|
|
}),
|
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
|
|
|
|
const elements = querySelectorAll(`[data-trx-label-id="${id}"]`);
|
|
|
|
const bright = isColorBright(color);
|
|
|
|
for (const element of elements) {
|
|
|
|
element.textContent = text;
|
2023-06-23 10:52:03 +00:00
|
|
|
element.setAttribute("style", `background-color: ${color};`);
|
2020-10-10 23:32:27 +00:00
|
|
|
if (bright) {
|
2023-06-23 10:52:03 +00:00
|
|
|
element.classList.add("trx-bright");
|
2020-10-10 23:32:27 +00:00
|
|
|
} else {
|
2023-06-23 10:52:03 +00:00
|
|
|
element.classList.remove("trx-bright");
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 12:00:27 +00:00
|
|
|
await saveUserLabels(userLabels);
|
2023-06-23 10:52:03 +00:00
|
|
|
this.props.userLabels = userLabels;
|
2020-10-10 23:32:27 +00:00
|
|
|
this.hide();
|
|
|
|
};
|
|
|
|
|
2022-02-23 13:52:06 +00:00
|
|
|
remove = async (event: MouseEvent) => {
|
2020-10-10 23:32:27 +00:00
|
|
|
event.preventDefault();
|
|
|
|
const {id} = this.state;
|
2022-02-23 13:52:06 +00:00
|
|
|
if (id === undefined) {
|
2023-06-23 10:52:03 +00:00
|
|
|
log("User Labels: Tried remove label when ID was undefined.");
|
2020-10-10 23:32:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
const {userLabels} = this.props;
|
2023-06-26 10:10:08 +00:00
|
|
|
const index = userLabels.findIndex(({value}) => value.id === id);
|
2020-10-10 23:32:27 +00:00
|
|
|
if (index === undefined) {
|
|
|
|
log(
|
|
|
|
`User Labels: Tried to remove label with ID ${id} that could not be found.`,
|
2022-02-23 13:52:06 +00:00
|
|
|
true,
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:16:55 +00:00
|
|
|
for (const value of querySelectorAll(`[data-trx-label-id="${id}"]`)) {
|
2020-12-16 16:46:20 +00:00
|
|
|
value.remove();
|
2021-02-23 20:16:55 +00:00
|
|
|
}
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2023-06-28 13:03:35 +00:00
|
|
|
for (const userLabel of userLabels.splice(index, 1)) {
|
|
|
|
await userLabel.remove();
|
|
|
|
}
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
this.props.userLabels = userLabels;
|
2020-10-10 23:32:27 +00:00
|
|
|
this.hide();
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const bodyStyle = window.getComputedStyle(document.body);
|
2023-06-23 10:52:03 +00:00
|
|
|
const themeSelectOptions = themeColors.map(({name, value}) => (
|
|
|
|
<option value={bodyStyle.getPropertyValue(value).trim()}>{name}</option>
|
|
|
|
));
|
2020-10-10 23:32:27 +00:00
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
const bright = isColorBright(this.state.color) ? "trx-bright" : "";
|
|
|
|
const hidden = this.state.hidden ? "trx-hidden" : "";
|
2020-10-10 23:32:27 +00:00
|
|
|
const {color, text: label, priority, selectedColor, username} = this.state;
|
|
|
|
|
|
|
|
let top = 0;
|
|
|
|
let left = 0;
|
|
|
|
|
|
|
|
const target = this.state.target;
|
2022-02-23 13:52:06 +00:00
|
|
|
if (target !== undefined) {
|
2020-10-10 23:32:27 +00:00
|
|
|
const bounds = target.getBoundingClientRect();
|
|
|
|
top = bounds.y + bounds.height + 4 + window.scrollY;
|
|
|
|
left = bounds.x + window.scrollX;
|
|
|
|
}
|
|
|
|
|
|
|
|
const position = `left: ${left}px; top: ${top}px;`;
|
|
|
|
const previewStyle = `background-color: ${color}`;
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
return (
|
|
|
|
<form class={`trx-user-label-form ${hidden}`} style={position}>
|
2022-02-23 13:52:06 +00:00
|
|
|
<div class="trx-label-username-priority">
|
|
|
|
<label class="trx-label-username">
|
|
|
|
Add New Label
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
class="form-input"
|
|
|
|
placeholder="Username"
|
2023-06-23 10:52:03 +00:00
|
|
|
value={username}
|
2022-02-23 13:52:06 +00:00
|
|
|
required
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<label class="trx-label-priority">
|
|
|
|
Priority
|
|
|
|
<input
|
|
|
|
type="number"
|
|
|
|
class="form-input"
|
2023-06-23 10:52:03 +00:00
|
|
|
value={priority}
|
|
|
|
onChange={this.priorityChange}
|
2022-02-23 13:52:06 +00:00
|
|
|
required
|
|
|
|
/>
|
|
|
|
</label>
|
2020-10-10 23:32:27 +00:00
|
|
|
</div>
|
2022-02-23 13:52:06 +00:00
|
|
|
|
|
|
|
<div>
|
|
|
|
<label for="trx-label-color-input">Pick A Color</label>
|
|
|
|
|
|
|
|
<div class="trx-label-grid">
|
|
|
|
<input
|
|
|
|
id="trx-label-color-input"
|
|
|
|
type="text"
|
|
|
|
class="form-input"
|
|
|
|
placeholder="Color"
|
2023-06-23 10:52:03 +00:00
|
|
|
value={color}
|
|
|
|
onInput={debounce(this.colorChange, 250)}
|
2022-02-23 13:52:06 +00:00
|
|
|
required
|
|
|
|
/>
|
|
|
|
|
|
|
|
<select
|
|
|
|
class="form-select"
|
2023-06-23 10:52:03 +00:00
|
|
|
value={selectedColor}
|
|
|
|
onChange={this.colorChange}
|
2022-02-23 13:52:06 +00:00
|
|
|
>
|
2023-06-23 10:52:03 +00:00
|
|
|
{themeSelectOptions}
|
2022-02-23 13:52:06 +00:00
|
|
|
</select>
|
2020-10-10 23:32:27 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-02-23 13:52:06 +00:00
|
|
|
|
|
|
|
<div>
|
|
|
|
<label for="trx-label-input">Label</label>
|
|
|
|
|
|
|
|
<div class="trx-label-grid">
|
|
|
|
<input
|
|
|
|
id="trx-label-input"
|
|
|
|
type="text"
|
|
|
|
class="form-input"
|
|
|
|
placeholder="Text"
|
2023-06-23 10:52:03 +00:00
|
|
|
value={label}
|
|
|
|
onInput={debounce(this.labelChange, 250)}
|
2022-02-23 13:52:06 +00:00
|
|
|
/>
|
|
|
|
|
2023-06-23 10:52:03 +00:00
|
|
|
<div class={`trx-label-preview ${bright}`} style={previewStyle}>
|
|
|
|
<p>{label}</p>
|
2022-02-23 13:52:06 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="trx-label-actions">
|
2023-06-23 10:52:03 +00:00
|
|
|
<a class="btn-post-action" onClick={this.save}>
|
|
|
|
Save
|
|
|
|
</a>
|
|
|
|
<a class="btn-post-action" onClick={this.hide}>
|
|
|
|
Close
|
|
|
|
</a>
|
|
|
|
<a class="btn-post-action" onClick={this.remove}>
|
|
|
|
Remove
|
|
|
|
</a>
|
2022-02-23 13:52:06 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
2023-06-23 10:52:03 +00:00
|
|
|
);
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
}
|