-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskin.js
103 lines (82 loc) · 2.71 KB
/
skin.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
import { setCustomSkin, getCustomSkin } from "./store.js";
import { getCurrentSkinBackgroundColor } from "./utils.js";
const loadCustomSkinButton = document.getElementById("load-custom-skin");
const defaultStyleSheet = document.getElementById('default-stylesheet');
let customStyleSheet = null;
export async function reloadStoredCustomSkin() {
const previousSkin = await getCustomSkin();
if (previousSkin) {
await applyCustomSkin(previousSkin);
}
}
export async function loadCustomOrResetSkin() {
// If a custom skin already exists, revert to default.
// Otherwise, let the user pick a new custom skin.
const skin = await getCustomSkin();
if (skin) {
await revertToDefaultSkin();
return;
}
// Let the user choose a file from the file system.
let handles = [];
try {
handles = await window.showOpenFilePicker({
multiple: false
});
} catch (e) {
// Silent bail out, the user aborted the file picker.
return;
}
if (!handles.length) {
return;
}
const file = await handles[0].getFile();
if (file.type !== 'text/css') {
console.error(`${file.name} is not a CSS file`);
return;
}
const text = await file.text();
await applyCustomSkin(text);
}
export async function applyCustomSkin(skin) {
// Remove the default stylesheet.
defaultStyleSheet.remove();
// Load the CSS file into the page. Replacing the default one.
if (!customStyleSheet) {
customStyleSheet = createInlineStyleSheet();
}
document.head.appendChild(customStyleSheet);
customStyleSheet.textContent = skin;
// Load the skin into the store.
await setCustomSkin(skin);
setThemeColorMeta();
updateSkinButton();
}
async function revertToDefaultSkin() {
// Remove the custom CSS.
customStyleSheet.remove();
// Load the default CSS again.
document.head.appendChild(defaultStyleSheet);
// Remove the custom skin from the store.
await setCustomSkin(null);
setThemeColorMeta();
updateSkinButton();
}
function createInlineStyleSheet() {
const style = document.createElement('style');
return style;
}
function setThemeColorMeta() {
// Set the theme meta to make the titlebar match the skin.
const color = getCurrentSkinBackgroundColor();
document.querySelector('meta[name="theme-color"]').setAttribute('content', `rgb(${color[0]}, ${color[1]}, ${color[2]})`);
}
function hasCustomSkinApplied() {
return !!(customStyleSheet && customStyleSheet.parentNode);
}
function updateSkinButton() {
// Update the load skin button so it says the right thing depending on the state.
const label = hasCustomSkinApplied() ? 'Reset to default skin' : 'Apply a custom skin';
loadCustomSkinButton.title = label;
loadCustomSkinButton.querySelector('span').textContent = label;
}