-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.html
More file actions
343 lines (316 loc) · 12.4 KB
/
browser.html
File metadata and controls
343 lines (316 loc) · 12.4 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tweakpane Tree Plugin Test</title>
<style>
body {
font-family: sans-serif;
margin: 20px;
}
#info {
margin-top: 20px;
padding: 10px;
background: #f0f0f0;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>Tweakpane Tree Plugin Test</h1>
<div id="tabs">
<button id="tab-static" aria-pressed="true">Static Tree</button>
<button id="tab-dynamic" aria-pressed="false">Dynamic Tree</button>
</div>
<div id="tab-contents">
<div id="pane-static" class="tab-content" style="display: block; --tp-blade-value-width: auto;"></div>
<div id="pane-dynamic" class="tab-content" style="display: none; --tp-blade-value-width: auto;"></div>
</div>
<script type="module">
import {Pane} from '../node_modules/@arijs/tweakpane/dist/tweakpane.js';
import * as TweakpaneTreePlugin from '../dist/arijs-tweakpane-plugin-tree.js';
const params = {
selectedItem: {
selectedPathIndices: [],
selectedPathValues: [],
selectedLeafValue: undefined
}
};
// Static pane (existing content)
const paneStatic = new Pane({
container: document.getElementById('pane-static')
});
// Register plugin
paneStatic.registerPlugin(TweakpaneTreePlugin);
// Add tree binding with sample data
const staticChildren = [
{
label: 'Colors',
children: [
{ label: 'Red', value: '#ff0000' },
{ label: 'Green', value: '#00ff00' },
{ label: 'Blue', value: '#0000ff' },
{ label: 'Yellow', value: '#ffff00' },
{ label: 'Cyan', value: '#00ffff' },
{ label: 'Magenta', value: '#ff00ff' },
{
label: 'Light Colors',
children: [
{ label: 'Light Red', value: '#ffcccc' },
{ label: 'Light Green', value: '#ccffcc' },
{ label: 'Light Blue', value: '#ccccff' },
{ label: 'Light Yellow', value: '#ffffcc' },
{ label: 'Light Cyan', value: '#ccffff' },
{ label: 'Light Magenta', value: '#ffccff' }
]
},
{
label: 'Dark Colors',
children: [
{ label: 'Dark Red', value: '#880000' },
{ label: 'Dark Green', value: '#008800' },
{ label: 'Dark Blue', value: '#000088' }
]
}
]
},
{
label: 'Sizes',
children: [
{ label: 'Extra Small', value: 5 },
{ label: 'Small', value: 10 },
{ label: 'Medium', value: 20 },
{ label: 'Large', value: 30 },
{ label: 'Extra Large', value: 40 },
{
label: 'Custom Sizes',
children: [
{ label: 'Tiny', value: 2 },
{ label: 'Mini', value: 8 },
{ label: 'Huge', value: 50 },
{ label: 'Gigantic', value: 100 }
]
}
]
},
{
label: 'Fonts',
children: [
{ label: 'Arial', value: 'Arial' },
{ label: 'Times New Roman', value: 'Times New Roman' },
{ label: 'Courier New', value: 'Courier New' },
{ label: 'Georgia', value: 'Georgia' },
{ label: 'Verdana', value: 'Verdana' }
]
},
{
label: 'Standalone Option Long Long Long Long Long Looooooooooooooooooooooooooooooooooong label',
value: 'standalone'
}
]
// Build a small control panel below the static pane to mirror the dynamic pane
const staticControls = document.createElement('div')
staticControls.style.marginTop = '8px'
staticControls.innerHTML = `
<pre id="static-value-display" style="margin-top:8px"></pre>
`
document.getElementById('pane-static').appendChild(staticControls)
// Keep static display in sync
document.getElementById('static-value-display').textContent = JSON.stringify(params.selectedItem, null, 2)
const treeApiStatic = paneStatic.addBinding(params, 'selectedItem', {
view: 'tree',
maxHeight: `min(calc(100vh - 6em), 100em)`, // Limit height to 100em with scrolling
children: staticChildren
}).on('change', (ev) => {
console.log('Tree value changed:', ev.value);
document.getElementById('static-value-display').textContent = JSON.stringify(ev.value, null, 2);
Object.assign(params.selectedItem, ev.value)
});
// Initialize display
document.getElementById('static-value-display').textContent = JSON.stringify(params.selectedItem, null, 2);
window.paneStatic = paneStatic;
window.treeApiStatic = treeApiStatic;
// --- Dynamic tree pane -------------------------------------------------
// Initial dynamic tree data (starts with a small set)
const dynamicParams = {
selectedItem: {
selectedPathIndices: [],
selectedPathValues: [],
selectedLeafValue: undefined,
// The special `tree` property is read by the plugin's view
tree: [
{ label: 'Animals', children: [ { label: 'Dog', value: 'dog' }, { label: 'Cat', value: 'cat' } ] },
{ label: 'Plants', children: [ { label: 'Tree', value: 'tree' } ] },
],
}
}
const paneDynamic = new Pane({
container: document.getElementById('pane-dynamic')
})
paneDynamic.registerPlugin(TweakpaneTreePlugin)
const treeApiDynamic = paneDynamic.addBinding(dynamicParams, 'selectedItem', {
view: 'tree',
maxHeight: `min(calc(100vh - 6em), 100em)`,
}).on('change', (ev) => {
// Update display and merge value back into our params holder. Use
// Object.assign so we don't accidentally drop the `tree` property.
document.getElementById('dynamic-value-display').textContent = JSON.stringify(ev.value, null, 2)
Object.assign(dynamicParams.selectedItem, ev.value)
})
// Attach to window for debugging
window.paneDynamic = paneDynamic
window.treeApiDynamic = treeApiDynamic
// Build a small control panel below the dynamic pane
const dynamicControls = document.createElement('div')
dynamicControls.style.marginTop = '8px'
dynamicControls.innerHTML = `
<div style="display:flex;gap:8px;align-items:center">
<label>Label: <input id="dyn-label" type="text" style="width:220px"></label>
<label>Value: <input id="dyn-value" type="text" style="width:220px"></label>
</div>
<div style="margin-top:8px;display:flex;gap:8px;flex-wrap:wrap">
<button id="insert-before">Insert before selected</button>
<button id="append-end">Append to end of tree</button>
<button id="insert-child">Insert as child</button>
<button id="remove-selected">Remove selected</button>
<button id="update-selected">Update selected</button>
</div>
<pre id="dynamic-value-display" style="margin-top:8px"></pre>
`
document.getElementById('pane-dynamic').appendChild(dynamicControls)
// Utility helpers for manipulating the dynamic tree data structure
function getTreeRoot() {
// Always use our params object as the single source of truth for the
// demo. We'll call `paneDynamic.refresh()` after mutations so the binding
// reads the updated `tree` array.
return dynamicParams.selectedItem.tree
}
/**
* Traverse a tree (array of nodes) following an array of indices.
* Returns { node, parentArr, index } where `node` is the found node
* (or undefined), `parentArr` is the array that contains the node,
* and `index` is the final index in that array (or -1 when none).
*/
function traverseTree(root, pathIndices) {
if (!Array.isArray(root)) return { node: undefined, parentArr: undefined, index: -1 }
if (!pathIndices || pathIndices.length === 0) {
return { node: undefined, parentArr: root, index: -1 }
}
let parentArr = root
let node = parentArr[pathIndices[0]]
for (let i = 1; i < pathIndices.length; i++) {
if (!node) return { node: undefined, parentArr, index: -1 }
parentArr = node.children = node.children || []
node = parentArr[pathIndices[i]]
}
const lastIdx = pathIndices[pathIndices.length - 1]
return { node, parentArr, index: lastIdx }
}
function emitTreeChange() {
// Use the documented `refresh()` API to force the pane to re-read
// bound parameters. This is simpler and more robust than emitting
// events on internal binding objects.
try {
if (paneDynamic && typeof paneDynamic.refresh === 'function') {
paneDynamic.refresh()
}
} catch (e) {
// ignore
}
// Update the visible JSON snapshot
document.getElementById('dynamic-value-display').textContent = JSON.stringify(dynamicParams.selectedItem, null, 2)
}
// NOTE: use traverseTree(getTreeRoot(), pathIndices) instead of the
// previous findParentArrayAndIndex helper. traverseTree returns the
// parent array as `parentArr` and the final `index`.
// Button handlers
document.getElementById('insert-before').addEventListener('click', () => {
const label = /** @type {HTMLInputElement} */ (document.getElementById('dyn-label')).value || 'New'
const value = /** @type {HTMLInputElement} */ (document.getElementById('dyn-value')).value || undefined
const sel = dynamicParams.selectedItem.selectedPathIndices
const { parentArr, index } = traverseTree(getTreeRoot(), sel)
const node = value === undefined || value === '' ? { label, children: [] } : { label, value }
if (index >= 0) {
parentArr.splice(index, 0, node)
} else {
parentArr.unshift(node)
}
emitTreeChange()
})
document.getElementById('append-end').addEventListener('click', () => {
const label = /** @type {HTMLInputElement} */ (document.getElementById('dyn-label')).value || 'New'
const value = /** @type {HTMLInputElement} */ (document.getElementById('dyn-value')).value || undefined
const node = value === undefined || value === '' ? { label, children: [] } : { label, value }
const tree = getTreeRoot()
tree.push(node)
emitTreeChange()
})
document.getElementById('insert-child').addEventListener('click', () => {
const label = /** @type {HTMLInputElement} */ (document.getElementById('dyn-label')).value || 'New'
const value = /** @type {HTMLInputElement} */ (document.getElementById('dyn-value')).value || undefined
const sel = dynamicParams.selectedItem.selectedPathIndices
const tree = getTreeRoot()
if (!sel || sel.length === 0) {
// append to root
tree.push(value === undefined || value === '' ? { label, children: [] } : { label, value })
} else {
// find selected node using helper
const { node } = traverseTree(tree, sel)
if (!node) return
if (!Array.isArray(node.children)) {
node.children = []
}
node.children.push(value === undefined || value === '' ? { label, children: [] } : { label, value })
}
emitTreeChange()
})
document.getElementById('remove-selected').addEventListener('click', () => {
const sel = dynamicParams.selectedItem.selectedPathIndices
if (!sel || sel.length === 0) return
const { parentArr, index } = traverseTree(getTreeRoot(), sel)
if (index >= 0) {
parentArr.splice(index, 1)
// clear selection
dynamicParams.selectedItem.selectedPathIndices = []
dynamicParams.selectedItem.selectedPathValues = []
dynamicParams.selectedItem.selectedLeafValue = undefined
emitTreeChange()
}
})
document.getElementById('update-selected').addEventListener('click', () => {
const label = /** @type {HTMLInputElement} */ (document.getElementById('dyn-label')).value || 'Updated'
const value = /** @type {HTMLInputElement} */ (document.getElementById('dyn-value')).value || undefined
const sel = dynamicParams.selectedItem.selectedPathIndices
if (!sel) return
const { parentArr, index } = traverseTree(getTreeRoot(), sel)
if (index >= 0 && parentArr[index]) {
const item = parentArr[index]
item.label = label
if (value === undefined || value === '') {
// treat as node
if ('value' in item) delete item.value
if (!Array.isArray(item.children)) item.children = []
} else {
item.value = value
}
emitTreeChange()
}
})
// initialize dynamic display
document.getElementById('dynamic-value-display').textContent = JSON.stringify(dynamicParams.selectedItem, null, 2)
// --- Tabs behavior -----------------------------------------------------
document.getElementById('tab-static').addEventListener('click', () => {
document.getElementById('pane-static').style.display = 'block'
document.getElementById('pane-dynamic').style.display = 'none'
document.getElementById('tab-static').setAttribute('aria-pressed', 'true')
document.getElementById('tab-dynamic').setAttribute('aria-pressed', 'false')
})
document.getElementById('tab-dynamic').addEventListener('click', () => {
document.getElementById('pane-static').style.display = 'none'
document.getElementById('pane-dynamic').style.display = 'block'
document.getElementById('tab-static').setAttribute('aria-pressed', 'false')
document.getElementById('tab-dynamic').setAttribute('aria-pressed', 'true')
})
</script>
</body>
</html>