-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlist.js
178 lines (154 loc) · 5.48 KB
/
list.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { render } from 'preact'
import { useState, useEffect } from 'preact/hooks'
import { html } from 'htm/preact'
/**
* @typedef {Object} Shader
* @property {string} name - Display name of the shader
* @property {string} fileUrl - URL to the shader source file
* @property {string} visualizerUrl - URL to view the shader in the visualizer
*/
/**
* Fetches shader code and extracts preset URLs
* @param {Object} props
* @param {string} props.name - Display name of the shader
* @param {string} props.fileUrl - URL to the shader source file
* @param {string} props.visualizerUrl - URL to view the shader in the visualizer
*/
const MusicVisual = ({ name, fileUrl, visualizerUrl }) => {
const [presets, setPresets] = useState([])
const [shaderCode, setShaderCode] = useState('')
// Fetch shader source code
useEffect(() => {
if (!fileUrl) return
const fetchShaderCode = async () => {
const res = await fetch(fileUrl)
const text = await res.text()
setShaderCode(text)
}
fetchShaderCode()
}, [fileUrl])
// Extract presets when shader code is loaded
useEffect(() => {
if (!shaderCode) return
setPresets(extractPresets(visualizerUrl, shaderCode))
}, [shaderCode, visualizerUrl])
const linkIcon = html`<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M13.19 8.688a4.5 4.5 0 011.242 7.244l-4.5 4.5a4.5 4.5 0 01-6.364-6.364l1.757-1.757m13.35-.622l1.757-1.757a4.5 4.5 0 00-6.364-6.364l-4.5 4.5a4.5 4.5 0 001.242 7.244" />
</svg>`
const copyUrl = (url) => {
navigator.clipboard.writeText(url)
const button = event.currentTarget
button.innerHTML = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M4.5 12.75l6 6 9-13.5" />
</svg>`
setTimeout(() => {
button.innerHTML = button.innerHTML = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M13.19 8.688a4.5 4.5 0 011.242 7.244l-4.5 4.5a4.5 4.5 0 01-6.364-6.364l1.757-1.757m13.35-.622l1.757-1.757a4.5 4.5 0 00-6.364-6.364l-4.5 4.5a4.5 4.5 0 001.242 7.244" />
</svg>`
}, 1000)
}
return html`
<li>
<div class="link-group">
<button
class="copy-link"
onClick=${() => copyUrl(`${window.location.host}${visualizerUrl}`)}
title="Copy link"
>${linkIcon}</button>
<a class="main-link" href="${visualizerUrl}">${name}</a>
<a class="edit-link" href="${getEditUrl(visualizerUrl)}">edit</a>
</div>
<ul>
${presets.map((preset, index) => html`
<li>
<div class="link-group">
<button
class="copy-link"
onClick=${() => copyUrl(preset)}
title="Copy link"
>${linkIcon}</button>
<a class="main-link" href="${preset}">Preset ${index + 1}</a>
<a class="edit-link" href="${getEditUrl(preset)}">edit</a>
</div>
<${PresetParams} preset=${preset} />
</li>
`)}
</ul>
</li>
`
}
const getEditUrl = (visualizationUrl) => {
try {
const url = new URL(visualizationUrl)
url.pathname = '/edit.html'
return url.toString()
} catch (e) {
return `edit.html${visualizationUrl}`
}
}
const PresetParams = ({ preset }) => {
const params = new URL(preset).searchParams
const presetProps = Array.from(params.entries()).filter(filterPresetProps)
return html`
<div class="chip-list">
${presetProps.map(([key, value], index) => html`
<div
class="chip pastel-color"
style="--n: ${index / presetProps.length}"
>
${key}: ${value}
</div>
`)}
</div>
`
}
const filterPresetProps = ([key]) => {
if (key === 'shader') return false
if (key.endsWith('.min')) return false
if (key.endsWith('.max')) return false
return true
}
/**
* Extracts preset URLs from shader code
* @param {string} visualizerUrl - Base visualizer URL
* @param {string} shaderCode - Raw shader source code
* @returns {string[]} Array of preset URLs
*/
const extractPresets = (visualizerUrl, shaderCode) => {
if (!shaderCode) return []
return shaderCode
.split('\n')
.filter(line => line.includes('http://') || line.includes('https://'))
.map(line => getPresetUrl(visualizerUrl, line))
}
/**
* Creates a preset URL by combining the visualizer base URL with preset parameters
* @param {string} visualizerUrl - Base visualizer URL
* @param {string} line - Line containing a preset URL
* @returns {string} Combined URL with merged parameters
*/
const getPresetUrl = (visualizerUrl, line) => {
const presetUrlMatch = line.match(/https?:\/\/[^\s]+/)
if (!presetUrlMatch) return visualizerUrl
const presetUrl = new URL(presetUrlMatch[0])
const baseUrl = new URL(visualizerUrl, window.location.href)
const resultUrl = new URL(baseUrl.pathname, window.location.origin)
// Add preset parameters first
for (const [key, value] of presetUrl.searchParams) {
resultUrl.searchParams.set(key, value)
}
// Override with visualizer parameters
for (const [key, value] of baseUrl.searchParams) {
resultUrl.searchParams.set(key, value)
}
resultUrl.pathname = ''
return resultUrl.toString()
}
// Load shaders and render the list
const shaders = await fetch('/shaders.json').then(res => res.json())
const List = () => html`
<ul>
${shaders.map(shader => html`<${MusicVisual} ...${shader} />`)}
</ul>
`
render(html`<${List} />`, document.getElementsByTagName('main')[0])