-
-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathhomeConfig.js
212 lines (189 loc) · 5.56 KB
/
homeConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
SpicetifyHomeConfig = {};
(async () => {
// Status enum
const NORMAL = 0;
const STICKY = 1;
const LOWERED = 2;
// List of sections' metadata
let list;
// Store sections' statuses
const statusDic = {};
let mounted = false;
SpicetifyHomeConfig.arrange = (sections) => {
mounted = true;
if (list) {
return list;
}
const stickList = (localStorage.getItem("spicetify-home-config:stick") || "").split(",");
const lowList = (localStorage.getItem("spicetify-home-config:low") || "").split(",");
const stickSections = [];
const lowSections = [];
for (const uri of stickList) {
const index = sections.findIndex((a) => a?.uri === uri || a?.item?.uri === uri);
if (index !== -1) {
const item = sections[index];
const uri = item.item.uri || item.uri;
statusDic[uri] = STICKY;
stickSections.push(item);
sections[index] = undefined;
}
}
for (const uri of lowList) {
const index = sections.findIndex((a) => a?.uri === uri || a?.item?.uri === uri);
if (index !== -1) {
const item = sections[index];
const uri = item.item.uri || item.uri;
statusDic[uri] = LOWERED;
lowSections.push(item);
sections[index] = undefined;
}
}
list = [...stickSections, ...sections.filter(Boolean), ...lowSections];
return list;
};
const up = document.createElement("button");
up.innerText = "Up";
const down = document.createElement("button");
down.innerText = "Down";
const lower = document.createElement("button");
const stick = document.createElement("button");
const sectionStyle = document.createElement("style");
sectionStyle.innerHTML = `
.main-home-content section {
order: 0 !important;
}
`;
const containerStyle = document.createElement("style");
containerStyle.innerHTML = `
#spicetify-home-config {
position: relative;
width: 100%;
height: 0;
display: flex;
justify-content: center;
align-items: flex-start;
gap: 5px;
z-index: 9999;
}
#spicetify-home-config button {
min-width: 60px;
height: 40px;
border-radius: 3px;
background-color: var(--spice-main);
color: var(--spice-text);
border: 1px solid var(--spice-text);
}
#spicetify-home-config button:disabled {
color: var(--spice-button-disabled);
}
`;
const container = document.createElement("div");
container.id = "spicetify-home-config";
container.append(containerStyle, up, down, lower, stick);
document.head.append(sectionStyle);
let elem = [];
function injectInteraction() {
const main = document.querySelector(".main-home-content");
elem = [...main.querySelectorAll("section")];
for (const [index, item] of elem.entries()) {
item.dataset.uri = list[index]?.uri ?? list[index].item?.uri;
}
function appendItems() {
const stick = [];
const low = [];
const normal = [];
for (const el of elem) {
if (statusDic[el.dataset.uri] === STICKY) stick.push(el);
else if (statusDic[el.dataset.uri] === LOWERED) low.push(el);
else normal.push(el);
}
localStorage.setItem(
"spicetify-home-config:stick",
stick.map((a) => a.dataset.uri)
);
localStorage.setItem(
"spicetify-home-config:low",
low.map((a) => a.dataset.uri)
);
elem = [...stick, ...normal, ...low];
main.append(...elem);
}
function onSwap(item, dir) {
container.remove();
const curPos = elem.findIndex((e) => e === item);
const newPos = curPos + dir;
if (newPos < 0 || newPos > elem.length - 1) return;
[elem[curPos], elem[newPos]] = [elem[newPos], elem[curPos]];
[list[curPos], list[newPos]] = [list[newPos], list[curPos]];
appendItems();
}
function onChangeStatus(item, status) {
container.remove();
const isToggle = statusDic[item.dataset.uri] === status;
statusDic[item.dataset.uri] = isToggle ? NORMAL : status;
appendItems();
}
for (const el of elem) {
el.onmouseover = () => {
const status = statusDic[el.dataset.uri];
const index = elem.findIndex((a) => a === el);
if (!status || index === 0 || status !== statusDic[elem[index - 1]?.dataset.uri]) {
up.disabled = true;
} else {
up.disabled = false;
up.onclick = () => onSwap(el, -1);
}
if (!status || index === elem.length - 1 || status !== statusDic[elem[index + 1]?.dataset.uri]) {
down.disabled = true;
} else {
down.disabled = false;
down.onclick = () => onSwap(el, 1);
}
stick.innerText = status === STICKY ? "Unstick" : "Stick";
lower.innerText = status === LOWERED ? "Unlower" : "Lower";
lower.onclick = () => onChangeStatus(el, LOWERED);
stick.onclick = () => onChangeStatus(el, STICKY);
el.prepend(container);
};
}
}
function removeInteraction() {
container.remove();
for (const a of elem) {
a.onmouseover = undefined;
}
}
await new Promise((res) => Spicetify.Events.webpackLoaded.on(res));
SpicetifyHomeConfig.menu = new Spicetify.Menu.Item(
"Home config",
false,
(self) => {
self.setState(!self.isEnabled);
if (self.isEnabled) {
injectInteraction();
} else {
removeInteraction();
}
},
Spicetify.SVGIcons["grid-view"]
);
SpicetifyHomeConfig.addToMenu = () => {
SpicetifyHomeConfig.menu.register();
};
SpicetifyHomeConfig.removeMenu = () => {
SpicetifyHomeConfig.menu.setState(false);
SpicetifyHomeConfig.menu.deregister();
};
await new Promise((res) => Spicetify.Events.platformLoaded.on(res));
// Init
if (Spicetify.Platform.History.location.pathname === "/") {
SpicetifyHomeConfig.addToMenu();
}
Spicetify.Platform.History.listen(({ pathname }) => {
if (pathname === "/") {
SpicetifyHomeConfig.addToMenu();
} else {
SpicetifyHomeConfig.removeMenu();
}
});
})();