Skip to content

Commit e505034

Browse files
committed
vimhelp: Add offline light/dark mode support
Make light/dark mode completely offline, instead of relying on server. Currently the logic is already almost completely client-side via cookie, but it sends the requested mode to the server, which pre-generates the correct version to the client. Instead, we just have a simple JavaScript to set it on launch. It does mean this won't work on web browsers that turned off JS, but that's not a big deal. Also fixed up the way cookies work as it has some special casing to make sure neo.vimhelp.org and vimhelp.org could share the same cookies, but that's irrelevant to MacVim's use cases and should be removed.
1 parent 404cd67 commit e505034

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

vimhelp/static/vimhelp-v5.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ for (let theme of ["theme-native", "theme-light", "theme-dark"]) {
4040
document.getElementsByTagName("html")[0].className = className;
4141
document.querySelector('meta[name="color-scheme"]').content = meta;
4242

43-
const cookieDomain = location.hostname.replace(/^neo\./, "");
4443
const cookieExpiry = theme === "theme-native"
4544
? "Tue, 01 Jan 1970 00:00:00 GMT" // delete cookie
46-
: "Tue, 19 Jan 2038 04:14:07 GMT"; // set "permanent" cookie
45+
: "Tue, 19 Jan 2050 04:14:07 GMT"; // set "permanent" cookie
4746
document.cookie =
48-
`theme=${className}; Secure; Domain=${cookieDomain}; SameSite=Lax; Path=/; Expires=${cookieExpiry}`;
47+
`theme=${className}; Secure; SameSite=Strict; Path=/; Expires=${cookieExpiry}`;
4948
});
5049
}
5150

vimhelp/templates/page.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
<link rel="stylesheet" href="{{static_dir}}vimhelp-v5.css" type="text/css">
1919
<noscript><link rel="stylesheet" href="{{static_dir}}noscript.css" type="text/css"></noscript>
2020
<script defer src="{{static_dir}}vimhelp-v5.js"></script>
21+
{# MacVim extension: Offline light/dark mode selection using JavaScript #}
22+
{% if project.name == 'MacVim' %}
23+
<script>
24+
if (document.cookie.indexOf('theme=light') != -1) {
25+
document.getElementsByTagName("html")[0].className = 'light';
26+
document.querySelector('meta[name="color-scheme"]').content = 'only light';
27+
} else if (document.cookie.indexOf('theme=dark') != -1) {
28+
document.getElementsByTagName("html")[0].className = 'dark';
29+
document.querySelector('meta[name="color-scheme"]').content = 'only dark';
30+
}
31+
</script>
32+
{% endif %}
2133
</head>
2234
<body>
2335

0 commit comments

Comments
 (0)