Skip to content

Commit 3deb72d

Browse files
committed
Implement #133
1 parent dc924f3 commit 3deb72d

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

src/features/base.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { store } from "../extensionPreferences"
1+
import { store } from "../extensionPreferences";
22
export class OLFeature {
33
moduleName = "unset";
44
moduleId = "unset";
@@ -33,28 +33,39 @@ export class SettingButton extends SettingOption {
3333
export class SettingToggle extends SettingOption {
3434
settingId: string;
3535
callback: (newState: boolean) => void;
36-
constructor(title: string, description: string, settingId: string, callback: (newState: boolean) => void) {
36+
constructor(
37+
title: string,
38+
description: string,
39+
settingId: string,
40+
callback: (newState: boolean) => void
41+
) {
3742
super(title, description);
3843
this.settingId = settingId;
3944
this.callback = callback;
4045
this.element.classList.add("ol-setting-toggle");
4146
this.element.innerHTML = `<span class="ol-set-inner"><p class="ol-set-title">${title}</p><p class="ol-set-desc">${description}</p></span><input type="checkbox" id="${settingId}" class="ol-setting-toggle-checkbox">`;
42-
this.element.addEventListener("click", async () => await this.toggleSetting());
43-
this.loadPD()
47+
this.element.addEventListener(
48+
"click",
49+
async () => await this.toggleSetting()
50+
);
51+
this.loadPD();
52+
}
53+
async getValue(): Promise<boolean> {
54+
const prefData = await store.get(this.settingId);
55+
return !!prefData;
4456
}
4557
async loadPD() {
46-
const prefData = await store.get(this.settingId)
47-
const value = !!prefData
58+
const value = await this.getValue();
4859
this.element.querySelector("input")!.checked = value;
4960
this.callback(value);
5061
}
5162
async toggleSetting() {
52-
const prefData = await store.get(this.settingId)
53-
const old_value = prefData
54-
const new_value = !old_value
63+
const prefData = await store.get(this.settingId);
64+
const old_value = prefData;
65+
const new_value = !old_value;
5566
await store.set(this.settingId, new_value);
5667
console.log("updating", this.settingId, old_value, new_value);
5768
this.element.querySelector("input")!.checked = !old_value;
58-
this.callback(new_value)
69+
this.callback(new_value);
5970
}
60-
}
71+
}

src/features/posts/css/votes.css renamed to src/features/posts/css/votes.scss

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@
5959
font-weight: bold;
6060
font-size: 125%;
6161
}
62-
.ol-votecount::after {
63-
content: " votes ";
64-
}
65-
.ol-votecount.ol-votecount-one::after {
66-
content: " vote " !important;
62+
html:not(.votesNearButtons) {
63+
.ol-votecount::after {
64+
content: " votes ";
65+
}
66+
.ol-votecount.ol-votecount-one::after {
67+
content: " vote " !important;
68+
}
6769
}

src/features/posts/reimplementVotes.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { OLFeature } from "../base";
2-
import "./css/votes.css";
1+
import { OLFeature, SettingToggle } from "../base";
2+
import "./css/votes.scss";
33

44
const getRedditConfig = `
55
setTimeout(function() {
@@ -18,7 +18,23 @@ export default class ReimplementVotes extends OLFeature {
1818
modHash: string;
1919
// @ts-ignore
2020
voteEventData: string;
21+
// @ts-ignore
22+
votesNearButtons: SettingToggle;
23+
2124
async init() {
25+
this.votesNearButtons = new SettingToggle(
26+
"Display votes between upvote and downvote buttons",
27+
"Reload required",
28+
"votesNearButtons",
29+
(value) => {
30+
document.documentElement.classList.toggle(
31+
"votesNearButtons",
32+
value
33+
);
34+
}
35+
);
36+
this.settingOptions.push(this.votesNearButtons);
37+
2238
this.voteHash = "";
2339
this.modHash = "";
2440
document.addEventListener("oldLanderConfigRequest", (e) => {
@@ -102,7 +118,15 @@ export default class ReimplementVotes extends OLFeature {
102118
if (postState === -1) {
103119
downVote.classList.add("act");
104120
}
105-
121+
if (
122+
(await this.votesNearButtons.getValue()) ||
123+
post.dataset.type === "comment"
124+
) {
125+
voteContainer.append(upVote, voteCount, downVote);
126+
} else {
127+
voteContainer.append(upVote, downVote);
128+
post.querySelector(".tagline")?.prepend(voteCount);
129+
}
106130
if (post.dataset.type === "comment") {
107131
voteContainer.append(upVote, voteCount, downVote);
108132
for (const button of [downVote, upVote]) {
@@ -129,8 +153,6 @@ export default class ReimplementVotes extends OLFeature {
129153
);
130154
}
131155
} else {
132-
voteContainer.append(upVote, downVote);
133-
post.querySelector(".tagline")?.prepend(voteCount);
134156
buttons?.prepend(voteContainer);
135157
}
136158
}

0 commit comments

Comments
 (0)