1
Fork 0

Add the Show Topic Author miscellaneous feature.

This commit is contained in:
Bauke 2023-10-13 13:33:26 +02:00
parent c7758eb9f0
commit 3008bdc9ab
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
7 changed files with 76 additions and 0 deletions

View File

@ -8,6 +8,7 @@ export * from "./markdown-toolbar.js";
export * from "./miscellaneous/comment-anchor-fix.js";
export * from "./miscellaneous/group-list-subscribe-button.js";
export * from "./miscellaneous/hide-own-username.js";
export * from "./miscellaneous/show-topic-author.js";
export * from "./miscellaneous/topic-info-ignore.js";
export * from "./miscellaneous/unignore-all-button.js";
export * from "./themed-logo.js";

View File

@ -0,0 +1,52 @@
import {
createElementFromString,
log,
pluralize,
querySelectorAll,
} from "../../../utilities/exports.js";
export function runShowTopicAuthorFeature(): void {
const count = showTopicAuthor();
if (count > 0) {
const pluralized = `${count} ${pluralize(count, "topic")}`;
log(`Show Topic Author applied to ${pluralized}.`);
}
}
/**
* Add topic author links to topics that don't already have a user linked in
* their topic info source.
*/
function showTopicAuthor(): number {
const topics = querySelectorAll<HTMLElement>(
".topic-listing .topic:not([data-trx-show-topic-author])",
);
let count = 0;
for (const topic of topics) {
const topicInfoSource =
topic.querySelector<HTMLElement>(".topic-info-source");
if (topicInfoSource?.querySelector(".link-user") !== null) {
continue;
}
const author = topic.dataset.topicPostedBy;
if (author === undefined) {
// Technically this should never happen since deleted or removed topics
// don't show in the topic listing.
continue;
}
topicInfoSource.insertAdjacentElement(
"afterbegin",
createElementFromString(
`<a class="link-user" href="/user/${author}">${author}</a>`,
),
);
topic.dataset.trxShowTopicAuthor = "true";
count++;
}
return count;
}

View File

@ -24,6 +24,7 @@ import {
runHideVotesFeature,
runMarkdownToolbarFeature,
runThemedLogoFeature,
runShowTopicAuthorFeature,
runTopicInfoIgnore,
runUnignoreAllButtonFeature,
runUsernameColorsFeature,
@ -182,6 +183,10 @@ async function initialize() {
runHideOwnUsernameFeature();
}
if (miscEnabled.value.has(MiscellaneousFeature.ShowTopicAuthor)) {
runShowTopicAuthorFeature();
}
if (
miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore) &&
isLoggedIn

View File

@ -39,6 +39,14 @@ function FeatureDescription({
);
}
if (feature === MiscellaneousFeature.ShowTopicAuthor) {
return (
<p class="description">
Always show the topic author's username in the topic listing.
</p>
);
}
if (feature === MiscellaneousFeature.TopicInfoIgnore) {
return (
<p class="description">

View File

@ -5,6 +5,7 @@
@import "scripts/hide-topics";
@import "scripts/jump-to-new-comment";
@import "scripts/markdown-toolbar";
@import "scripts/show-topic-author";
@import "scripts/topic-info-ignore";
@import "scripts/user-labels";
@import "scripts/username-colors";

View File

@ -0,0 +1,8 @@
.topic[data-trx-show-topic-author] .topic-info-source {
.topic-icon {
// Instead of adding a margin-right to the user link we add it to the topic
// favicon so when there are user labels the margin still spaces things out
// as intended.
margin-left: 0.2rem;
}
}

View File

@ -23,6 +23,7 @@ export enum MiscellaneousFeature {
CommentAnchorFix = "comment-anchor-fix",
GroupListSubscribeButtons = "group-list-subscribe-buttons",
HideOwnUsername = "hide-own-username",
ShowTopicAuthor = "show-topic-author",
TopicInfoIgnore = "topic-info-ignore",
UnignoreAllButton = "unignore-all-button",
}