Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit d21a0fd

Browse files
committed
Use Swift Package Manager resource bundling
1 parent add500f commit d21a0fd

File tree

6 files changed

+92
-8
lines changed

6 files changed

+92
-8
lines changed

.node/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"not dead"
66
],
77
"scripts": {
8-
"watch": "concurrently \"postcss -w ../Assets/**/*.css -o ../Resources/all.min.css --config ./postcss.config.js\" \"tsc -w\"",
9-
"build": "tsc && postcss ../Assets/**/*.css -o ../Resources/all.min.css --config ./postcss.config.js"
8+
"watch": "concurrently \"postcss -w ../Assets/**/*.css -o ../Sources/swift-doc/Resources/all.min.css --config ./postcss.config.js\" \"tsc -w\"",
9+
"build": "tsc && postcss ../Assets/**/*.css -o ../Sources/swift-doc/Resources/all.min.css --config ./postcss.config.js"
1010
},
1111
"devDependencies": {
1212
"concurrently": "^5.2.0",

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
"repositoryURL": "https://github.com/SwiftDocOrg/swift-cmark.git",
5252
"state": {
5353
"branch": null,
54-
"revision": "2a766030bee955b4806044fd7aca1b6884475138",
55-
"version": "0.28.3+20200110.2a76603"
54+
"revision": "1168665f6b36be747ffe6b7b90bc54cfc17f42b7",
55+
"version": "0.28.3+20200207.1168665"
5656
}
5757
},
5858
{

Package.swift

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ let package = Package(
4343
.product(name: "SwiftSyntaxHighlighter", package: "SwiftSyntaxHighlighter"),
4444
.product(name: "Logging", package: "swift-log"),
4545
.product(name: "LoggingGitHubActions", package: "LoggingGitHubActions")
46+
],
47+
resources: [
48+
.copy("Resources/all.js"),
49+
.copy("Resources/all.min.css"),
4650
]
4751
),
4852
.target(

Sources/swift-doc/Resources/all.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var _a;
2+
var themeQuery = window.matchMedia('(prefers-color-scheme: dark)');
3+
var localStorageKey = "picked-theme";
4+
var autoTheme = false;
5+
// load initial theme before load event to prevent flashing of background color on navigation
6+
var initialTheme = (_a = window.localStorage.getItem(localStorageKey)) !== null && _a !== void 0 ? _a : "auto";
7+
setPickedTheme(initialTheme);
8+
window.addEventListener("load", function () {
9+
var _a;
10+
var themeSwitcher = window.document.getElementById("theme-switcher");
11+
themeSwitcher.addEventListener("change", function (themePickedEvent) {
12+
var newValue = themePickedEvent.target.value;
13+
setPickedTheme(newValue);
14+
});
15+
(_a = themeQuery.addEventListener) === null || _a === void 0 ? void 0 : _a.call(themeQuery, "change", function (systemThemeChangeEvent) {
16+
var systemTheme = systemThemeChangeEvent.matches ? "dark" : "light";
17+
updateDropdownLabel(systemTheme);
18+
if (autoTheme) {
19+
applyTheme(systemTheme);
20+
}
21+
});
22+
setInitialTheme(themeSwitcher);
23+
checkThemingSupport();
24+
});
25+
/**
26+
* Sets the correct theme based on what's in storage, and updates the theme switcher dropdown with the correct initial value.
27+
*/
28+
function setInitialTheme(themeSwitcher) {
29+
var _a;
30+
var initialTheme = (_a = window.localStorage.getItem(localStorageKey)) !== null && _a !== void 0 ? _a : "auto";
31+
themeSwitcher.value = initialTheme;
32+
setPickedTheme(initialTheme);
33+
var systemTheme = themeQuery.matches ? "dark" : "light";
34+
updateDropdownLabel(systemTheme);
35+
}
36+
/**
37+
* Updates the styles of the page to reflect a new theme
38+
*/
39+
function applyTheme(newTheme) {
40+
var otherTheme = newTheme == "light" ? "dark" : "light";
41+
window.document.documentElement.classList.remove(otherTheme + "-theme");
42+
window.document.documentElement.classList.add(newTheme + "-theme");
43+
}
44+
/**
45+
* Saves a newly picked theme to storage and applies the theme.
46+
* If the new theme is "auto", the correct theme will be applied based on the system settings.
47+
*/
48+
function setPickedTheme(newTheme) {
49+
window.localStorage.setItem(localStorageKey, newTheme);
50+
autoTheme = newTheme === "auto";
51+
if (newTheme === "auto") {
52+
var systemTheme = themeQuery.matches ? "dark" : "light";
53+
applyTheme(systemTheme);
54+
}
55+
else {
56+
applyTheme(newTheme);
57+
}
58+
}
59+
/**
60+
* Updates the "Auto" choice of the theme dropdown to reflect the current system theme - either "Auto (light)" or "Auto (dark)"
61+
*/
62+
function updateDropdownLabel(systemTheme) {
63+
window.document.getElementById('theme-option-auto').innerText = "Auto (" + systemTheme + ")";
64+
}
65+
66+
/**
67+
* Checks whether color-scheme is a supported feature of the browser.
68+
* If it is not, removes the auto option from the dropdown.
69+
*/
70+
function checkThemingSupport() {
71+
var darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
72+
var lightQuery = window.matchMedia('(prefers-color-scheme: light)');
73+
// If neither query matches, we know that the browser doesn't support theming
74+
if (!darkQuery.matches && !lightQuery.matches) {
75+
var themeOptionAuto = window.document.getElementById('theme-option-auto');
76+
// IE doesn't support element.remove()
77+
themeOptionAuto.parentNode.removeChild(themeOptionAuto);
78+
}
79+
}

Sources/swift-doc/Resources/all.min.css

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/swift-doc/Subcommands/Generate.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ extension SwiftDoc {
133133
}
134134

135135
func fetchRemoteCSS() throws -> Data {
136-
let url = URL(string: "https://raw.githubusercontent.com/literalpie/swift-doc/5165f782d33d437f7bccf3c2fa5f89b039134f73/Resources/all.min.css")!
137-
return try Data(contentsOf: url)
136+
let cssURL = Bundle.module.url(forResource: "all.min", withExtension: "css")!
137+
return try Data(contentsOf: cssURL)
138138
}
139139

140140
func fetchRemoteJS() throws -> Data {
141-
let url = URL(string: "https://raw.githubusercontent.com/literalpie/swift-doc/5165f782d33d437f7bccf3c2fa5f89b039134f73/Resources/all.js")!
142-
return try Data(contentsOf: url)
141+
let jsURL = Bundle.module.url(forResource: "all", withExtension: "js")!
142+
return try Data(contentsOf: jsURL)
143143
}

0 commit comments

Comments
 (0)