Skip to content

Commit da4f72b

Browse files
committed
adding the ability to set the theme with ?theme= in the url
1 parent 49b6d59 commit da4f72b

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

src/assets/scad.collection.njk

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
<title>OpenSCAD Files</title>
55
{% w3_theme_css theme %}
66
<script>
7-
console.log({{ page | dump | safe }});
7+
const searchParams = new URLSearchParams(window.location.search);
8+
9+
if (searchParams.has("dump", "true")) {
10+
console.log({{ page | dump | safe }});
11+
}
812
</script>
913
</head>
1014
<body>

src/assets/scad.viewer.njk

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
<meta charset="UTF-8" />
55
<title>{{ stlFile }}</title>
66
{% threejs_importmap %}
7-
<script>
8-
window.STL_URL = new URL("{{ stlFile }}", window.location.href)
9-
console.log({{ url }})
10-
</script>
7+
<script>window.STL_URL = new URL("{{ stlFile }}", window.location.href);</script>
118
{% w3_theme_css theme %}
129
<style>
1310
body {

src/core/shortcodes.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,22 @@ export function addShortcodes(eleventyConfig: EleventyConfig) {
2020
* @link https://www.w3.org/StyleSheets/Core/preview
2121
*/
2222
registerShortcode("w3_theme_css", (userTheme: ModelViewerTheme) => {
23-
const $theme = useNonEmptyOrDefault(userTheme, DEFAULT_PLUGIN_THEME);
24-
const url = `https://www.w3.org/StyleSheets/Core/${$theme}`;
25-
return `<link rel="stylesheet" href="${url}">`;
23+
const theme = useNonEmptyOrDefault(userTheme, DEFAULT_PLUGIN_THEME);
24+
const url = `https://www.w3.org/StyleSheets/Core/${theme}`;
25+
const w3cThemeCssLinkTag = `<link id="__eleventy_scad_theme" rel="stylesheet" href="${url}">`;
26+
const themeOverrideScriptTag = `<script>
27+
document.addEventListener('DOMContentLoaded', function (evt) {
28+
console.log("content loaded");
29+
const searchParams = new URLSearchParams(window.location.search);
30+
if (searchParams.has("theme")) {
31+
const themeOverride = searchParams.get("theme");
32+
console.log("has theme override", { themeOverride });
33+
const link = document.getElementById("__eleventy_scad_theme");
34+
link.href = link.href.replace("${theme}", themeOverride);
35+
}
36+
}, false);
37+
</script>`;
38+
return [w3cThemeCssLinkTag, themeOverrideScriptTag].join("\n");
2639
});
2740

2841
/**

src/core/templates.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,28 @@ export function addScadCollectionVirtualTemplate(
2727
);
2828

2929
const DEFAULT_COLLECTION_TEMPLATE = "index.njk";
30-
eleventyConfig.addTemplate(
31-
DEFAULT_COLLECTION_TEMPLATE,
32-
`<ul>
33-
{% for item in collections.scad %}
34-
<li><a href="{{ item.data.page.url | url }}">{{ item.data.title }}</a></li>
35-
{% endfor %}
36-
</ul>`,
37-
{
38-
layout: DEFAULT_COLLECTION_LAYOUT,
39-
theme: pageTheme,
40-
},
41-
);
30+
const tableHTML = `<!-- added by addScadCollectionVirtualTemplate -->
31+
<table>
32+
<thead>
33+
<tr>
34+
<th>Title</th>
35+
<th>Location</th>
36+
</tr>
37+
</thead>
38+
<tbody>
39+
{% for item in collections.scad %}
40+
<tr>
41+
<td>
42+
<a href="{{ item.data.page.url | url }}">{{ item.data.title }}</a>
43+
</td>
44+
<td>{{ item.data.page.url }}</td>
45+
</tr>
46+
{% endfor %}
47+
</tbody>
48+
</table>`;
49+
eleventyConfig.addTemplate(DEFAULT_COLLECTION_TEMPLATE, tableHTML, {
50+
layout: DEFAULT_COLLECTION_LAYOUT,
51+
theme: pageTheme,
52+
});
4253
log(`(virtual) added "%o"`, DEFAULT_COLLECTION_TEMPLATE);
4354
}

src/plugin.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,8 @@ export function EleventyPluginOpenSCAD(
153153
}
154154

155155
if (!data.page.url) {
156-
// _log(
157-
// `${red("issue:")} ${reset(data.stlFile)} ${gray(`from ${inputPath}`)}`,
158-
// );
159156
throw new Error(
160-
`${inputPath} must have "parmalink:true" in the frontmatter`,
157+
`${inputPath} must have "parmalink:true" set in the frontmatter`,
161158
);
162159
// return inputContent;
163160
}

0 commit comments

Comments
 (0)