Skip to content

Commit f055c3b

Browse files
feat: _includes/SelectPalette ( Fixes #125 )
1 parent e84d64d commit f055c3b

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
<#
3+
.SYNOPSIS
4+
Includes a palette selector
5+
.DESCRIPTION
6+
Includes a palette selector in a page. This allows the page to use multiple color palettes.
7+
#>
8+
param(
9+
[uri]
10+
$PaletteListSource = 'https://4bitcss.com/Palette-List.json',
11+
12+
# The Palette CDN. This is the root URL of all palettes.
13+
[uri]
14+
$PaletteCDN = 'https://cdn.jsdelivr.net/gh/2bitdesigns/4bitcss@latest/css/',
15+
16+
# The identifier for the palette `<select>`.
17+
[string]
18+
$SelectPaletteId = 'SelectPalette',
19+
20+
# The identifier for the stylesheet. By default, palette.
21+
[string]
22+
$PaletteId = 'palette'
23+
)
24+
25+
26+
$JavaScript = @"
27+
function GetRandomPalette() {
28+
var SelectPalette = document.getElementById('$SelectPaletteId')
29+
if (SelectPalette) {
30+
var randomNumber = Math.floor(Math.random() * SelectPalette.length);
31+
SelectPalette.selectedIndex = randomNumber
32+
SetPalette()
33+
}
34+
}
35+
"@
36+
37+
$HTML = @"
38+
<script>
39+
$JavaScript
40+
</script>
41+
<button onclick='GetRandomPalette()'>Random Palette</button>
42+
"@
43+
44+
$HTML
45+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<#
2+
.SYNOPSIS
3+
Includes a palette selector
4+
.DESCRIPTION
5+
Includes a palette selector in a page. This allows the page to use multiple color palettes.
6+
#>
7+
param(
8+
[uri]
9+
$PaletteListSource = 'https://4bitcss.com/Palette-List.json',
10+
11+
# The Palette CDN. This is the root URL of all palettes.
12+
[uri]
13+
$PaletteCDN = 'https://cdn.jsdelivr.net/gh/2bitdesigns/4bitcss@latest/css/',
14+
15+
# The identifier for the palette `<select>`.
16+
[string]
17+
$SelectPaletteId = 'SelectPalette',
18+
19+
# The identifier for the stylesheet. By default, palette.
20+
[string]
21+
$PaletteId = 'palette'
22+
)
23+
24+
25+
$SetPaletteFunction = @"
26+
function SetPalette() {
27+
var palette = document.getElementById('$PaletteId')
28+
if (! palette) {
29+
palette = document.createElement('link')
30+
palette.rel = 'stylesheet'
31+
palette.id = 'palette'
32+
document.head.appendChild(palette)
33+
}
34+
var selectedPalette = document.getElementById('$SelectPaletteId').value
35+
palette.href = '$PaletteCDN' + selectedPalette + '.css'
36+
}
37+
"@
38+
39+
if ($palleteList) {
40+
41+
}
42+
43+
$paletteSelector = @"
44+
<select id='$SelectPaletteId' onchange='SetPalette()'>
45+
$(
46+
if (-not $script:PaletteList) {
47+
if ($site.Palettes.Count) {
48+
$script:PaletteList = $site.Palettes.Keys | Sort-Object
49+
} else {
50+
$script:PaletteList = Invoke-RestMethod $PaletteListSource
51+
}
52+
}
53+
foreach ($paletteName in $script:PaletteList) {
54+
"<option value='$([Web.HttpUtility]::HtmlAttributeEncode($paletteName))'>$([Web.HttpUtility]::HtmlEncode($paletteName))</option>"
55+
}
56+
)
57+
</select>
58+
"@
59+
60+
61+
$HTML = @"
62+
<script>
63+
$SetPaletteFunction
64+
</script>
65+
$PaletteSelector
66+
<script>
67+
var currentPaletteName = getComputedStyle(document.querySelector("body")).getPropertyValue('--PaletteName');
68+
var paletteSelector = document.getElementById('$SelectPaletteId')
69+
if (paletteSelector && currentPaletteName) {
70+
for (var paletteIndex = 0; paletteIndex < paletteSelector.options.length; paletteIndex++) {
71+
if (paletteSelector.options[paletteIndex].value == currentPaletteName) {
72+
paletteSelector.selectedIndex = paletteIndex
73+
break
74+
}
75+
}
76+
}
77+
</script>
78+
"@
79+
80+
$HTML
81+

0 commit comments

Comments
 (0)