Add the Show Topic Author miscellaneous feature.
This commit is contained in:
parent
c7758eb9f0
commit
3008bdc9ab
|
@ -8,6 +8,7 @@ export * from "./markdown-toolbar.js";
|
||||||
export * from "./miscellaneous/comment-anchor-fix.js";
|
export * from "./miscellaneous/comment-anchor-fix.js";
|
||||||
export * from "./miscellaneous/group-list-subscribe-button.js";
|
export * from "./miscellaneous/group-list-subscribe-button.js";
|
||||||
export * from "./miscellaneous/hide-own-username.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/topic-info-ignore.js";
|
||||||
export * from "./miscellaneous/unignore-all-button.js";
|
export * from "./miscellaneous/unignore-all-button.js";
|
||||||
export * from "./themed-logo.js";
|
export * from "./themed-logo.js";
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -24,6 +24,7 @@ import {
|
||||||
runHideVotesFeature,
|
runHideVotesFeature,
|
||||||
runMarkdownToolbarFeature,
|
runMarkdownToolbarFeature,
|
||||||
runThemedLogoFeature,
|
runThemedLogoFeature,
|
||||||
|
runShowTopicAuthorFeature,
|
||||||
runTopicInfoIgnore,
|
runTopicInfoIgnore,
|
||||||
runUnignoreAllButtonFeature,
|
runUnignoreAllButtonFeature,
|
||||||
runUsernameColorsFeature,
|
runUsernameColorsFeature,
|
||||||
|
@ -182,6 +183,10 @@ async function initialize() {
|
||||||
runHideOwnUsernameFeature();
|
runHideOwnUsernameFeature();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (miscEnabled.value.has(MiscellaneousFeature.ShowTopicAuthor)) {
|
||||||
|
runShowTopicAuthorFeature();
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore) &&
|
miscEnabled.value.has(MiscellaneousFeature.TopicInfoIgnore) &&
|
||||||
isLoggedIn
|
isLoggedIn
|
||||||
|
|
|
@ -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) {
|
if (feature === MiscellaneousFeature.TopicInfoIgnore) {
|
||||||
return (
|
return (
|
||||||
<p class="description">
|
<p class="description">
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
@import "scripts/hide-topics";
|
@import "scripts/hide-topics";
|
||||||
@import "scripts/jump-to-new-comment";
|
@import "scripts/jump-to-new-comment";
|
||||||
@import "scripts/markdown-toolbar";
|
@import "scripts/markdown-toolbar";
|
||||||
|
@import "scripts/show-topic-author";
|
||||||
@import "scripts/topic-info-ignore";
|
@import "scripts/topic-info-ignore";
|
||||||
@import "scripts/user-labels";
|
@import "scripts/user-labels";
|
||||||
@import "scripts/username-colors";
|
@import "scripts/username-colors";
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ export enum MiscellaneousFeature {
|
||||||
CommentAnchorFix = "comment-anchor-fix",
|
CommentAnchorFix = "comment-anchor-fix",
|
||||||
GroupListSubscribeButtons = "group-list-subscribe-buttons",
|
GroupListSubscribeButtons = "group-list-subscribe-buttons",
|
||||||
HideOwnUsername = "hide-own-username",
|
HideOwnUsername = "hide-own-username",
|
||||||
|
ShowTopicAuthor = "show-topic-author",
|
||||||
TopicInfoIgnore = "topic-info-ignore",
|
TopicInfoIgnore = "topic-info-ignore",
|
||||||
UnignoreAllButton = "unignore-all-button",
|
UnignoreAllButton = "unignore-all-button",
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue