-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfx.html
More file actions
324 lines (274 loc) · 7.72 KB
/
sfx.html
File metadata and controls
324 lines (274 loc) · 7.72 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sound Effects</title>
<meta name="description" content="Preview the built in sound effects.">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script src="../dist/b8.js"></script>
<!-- Replace with a separate style.css file if you want -->
<style type="text/css">
body {
background: black;
color: white;
}
button {
padding: 10px 20px;
font-size: 20px;
font-weight: 900;
}
p {
text-align: center;
margin-top: 50px;
}
p a {
color: white;
}
.interface {
padding: 5vw;
}
</style>
<body>
<style>
.interface details {
padding: 0;
background: transparent;
margin: 0;
}
.interface summary {
font-size: 1.2rem;
margin: 0;
}
.interface>div>details>div {
margin-left: 1rem;
}
#sfx-search {
width: 100%;
padding: .5rem 1rem;
margin-bottom: 1rem;
}
details>summary {
padding: .25rem 0;
font-weight: 600;
}
/* add to previous styles */
.sfx-item {
display: flex;
flex-direction: column;
gap: .25rem;
}
.copy-btn {
font-size: 0.6rem;
padding: 0.25rem;
}
.toast {
position: fixed;
inset: auto 1rem 1rem auto;
background: #111;
color: #fff;
padding: .5rem 1rem;
border-radius: .2rem;
opacity: 0;
transform: translateY(6px);
transition: opacity .15s ease, transform .15s ease;
pointer-events: none;
font-size: .9rem;
}
.toast.show {
opacity: 1;
transform: translateY(0);
}
.sfx-list {
display: flex;
flex-wrap: wrap;
gap: .4rem;
padding: .5rem 0;
}
.hidden {
display: none !important;
}
</style>
<input id="sfx-search" type="search" placeholder="Search sounds or paths…" aria-label="Search sounds">
<div class="interface" id="sfx-browser"></div>
<div class="toast" id="sfx-toast" role="status" aria-live="polite"></div>
<p><a href="https://killedbyapixel.github.io/ZzFX/">ZZFX Synth</a></p>
</body>
<script>
/* helper: copy text + toast */
const copyText = async ( text ) => {
try {
if ( navigator.clipboard && navigator.clipboard.writeText ) {
await navigator.clipboard.writeText( text );
} else {
const ta = document.createElement( 'textarea' );
ta.value = text;
ta.setAttribute( 'readonly', '' );
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild( ta );
ta.select();
document.execCommand( 'copy' );
ta.remove();
}
showToast( `Copied: ${text}` );
} catch {
showToast( 'Copy failed' );
}
};
const showToast = ( msg ) => {
const el = document.getElementById( 'sfx-toast' );
el.textContent = msg;
el.classList.add( 'show' );
clearTimeout( showToast._t );
showToast._t = setTimeout( () => el.classList.remove( 'show' ), 1200 );
};
/* Build a tree from paths like:
"ui/click", "ui/confirm", "monsters/dragon/roar"
Tree nodes: { children: Map, items: [] }
*/
const buildTree = ( paths ) => {
const root = { children: new Map(), items: [] };
paths.forEach( ( p ) => {
const parts = p.split( '/' ).filter( Boolean );
let node = root;
parts.forEach( ( part, i ) => {
const isLeaf = i === parts.length - 1;
if ( isLeaf ) {
node.items.push( p ); // store full path as item
} else {
if ( !node.children.has( part ) ) {
node.children.set( part, { children: new Map(), items: [] } );
}
node = node.children.get( part );
}
} );
} );
return root;
};
/* update: render buttons with a neighbour copy button */
const renderTree = ( node, label = '', pathPrefix = '' ) => {
const container = document.createElement( 'div' );
for ( const [ folder, child ] of node.children ) {
const details = document.createElement( 'details' );
const summary = document.createElement( 'summary' );
summary.textContent = folder;
details.appendChild( summary );
const childEl = renderTree( child, folder, pathPrefix ? pathPrefix + '/' + folder : folder );
details.appendChild( childEl );
container.appendChild( details );
}
if ( node.items.length ) {
const list = document.createElement( 'div' );
list.className = 'sfx-list';
node.items
.slice()
.sort( ( a, b ) => a.localeCompare( b ) )
.forEach( ( fullPath ) => {
const wrap = document.createElement( 'div' );
wrap.className = 'sfx-item';
const btn = document.createElement( 'button' );
btn.type = 'button';
btn.className = 'sfx-btn';
btn.textContent = fullPath.split( '/' ).pop();
btn.title = fullPath;
btn.dataset.path = fullPath;
const copy = document.createElement( 'button' );
copy.type = 'button';
copy.className = 'copy-btn';
copy.textContent = 'Copy';
copy.ariaLabel = `Copy path ${fullPath}`;
copy.dataset.copyPath = fullPath;
wrap.appendChild( btn );
wrap.appendChild( copy );
list.appendChild( wrap );
} );
container.appendChild( list );
}
return container;
};
/* Filter UI by a query.
Hides non-matching buttons and collapses groups with no visible matches.
Expands groups that contain matches.
*/
const filterBrowser = ( rootEl, query ) => {
const q = query.trim().toLowerCase();
// 1) Filter items instead of just buttons
const items = rootEl.querySelectorAll( '.sfx-item' );
let anyMatch = false;
items.forEach( ( wrap ) => {
const btn = wrap.querySelector( '.sfx-btn' );
const fullPath = btn.dataset.path.toLowerCase();
const name = btn.textContent.toLowerCase();
const match = !q || fullPath.includes( q ) || name.includes( q );
wrap.classList.toggle( 'hidden', !match );
if ( match ) anyMatch = true;
} );
// 2) For each <details>, show it if it contains any visible .sfx-item
const folders = rootEl.querySelectorAll( 'details' );
folders.forEach( ( d ) => {
const hasVisible = d.querySelector( '.sfx-item:not(.hidden)' ) !== null;
d.classList.toggle( 'hidden', !hasVisible );
// auto-open matches
if ( q && hasVisible ) {
d.open = true;
} else if ( !q ) {
d.open = false;
}
} );
// 3) Empty message
let empty = rootEl.querySelector( '[data-empty]' );
if ( !anyMatch && q ) {
if ( !empty ) {
empty = document.createElement( 'p' );
empty.dataset.empty = '1';
empty.textContent = 'No sounds match your search.';
rootEl.appendChild( empty );
}
} else {
if ( empty ) empty.remove();
}
};
/* update: interaction adds copy handling + keyboard copy */
const setupInteraction = ( rootEl ) => {
rootEl.addEventListener( 'click', ( e ) => {
const copy = e.target.closest( '.copy-btn' );
if ( copy ) {
copyText( copy.dataset.copyPath );
return;
}
const btn = e.target.closest( '.sfx-btn' );
if ( btn ) {
b8.Sfx.play( btn.dataset.path );
}
} );
// Ctrl+C / Cmd+C copies focused sound path
rootEl.addEventListener( 'keydown', ( e ) => {
if ( ( e.ctrlKey || e.metaKey ) && e.key.toLowerCase() === 'c' ) {
const active = document.activeElement;
if ( active && active.classList.contains( 'sfx-btn' ) ) {
e.preventDefault();
copyText( active.dataset.path );
}
}
} );
};
/* Main bootstrap */
( () => {
const container = document.getElementById( 'sfx-browser' );
const search = document.getElementById( 'sfx-search' );
// Get all SFX paths
const paths = b8.Sfx.get(); // assumes array of strings like "ui/click"
// Build and render
const tree = buildTree( paths );
const ui = renderTree( tree );
container.innerHTML = '';
container.appendChild( ui );
setupInteraction( container );
// Live search with tiny debounce
let t = null;
search.addEventListener( 'input', () => {
clearTimeout( t );
t = setTimeout( () => filterBrowser( container, search.value ), 120 );
} );
} )();
</script>