Skip to content

Commit 1b03a30

Browse files
refactor treemap into proxy for improved perf (#24)
* rename tree, tree-utils with svelte prefix * refactor site state to treeOpts * change value to getValue, add onUpdate without breaking tests * reuse old node in createNode * update * wip * change children to list of ids, fix recurOpts in site * fix parseValueFormatter * update snapshots * fix valueFormat number as value * working $proxy version of treemap, wee * recreate snapshots * remove unused code
1 parent 754219b commit 1b03a30

26 files changed

+480
-435
lines changed

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package.json
22
pnpm-lock.yaml
33
node_modules
4-
package
4+
pkg
55
.svelte-kit
6+
**/__snapshots__/*.json
67
build
78
dist
89
tmp

packages/site/src/components/DataSelector.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<script lang="ts">
2-
import { DATA, state, setExampleData } from '$lib/store'
2+
import { DATA, treeOpts, setExampleData } from '$lib/store'
33
</script>
44

55
<div class="mt-2 flex items-center gap-2">
66
<label for="data-selector" class="control-label">Example Data:</label>
77
<div>
88
<select
99
id="data-selector"
10-
value={$state.selectedData}
10+
value={$treeOpts.selectedData}
1111
oninput={e => setExampleData(e.currentTarget.value)}
1212
class="control-select"
1313
>

packages/site/src/components/DiffValue.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
} = getTreeContext()
1515
let hasChildren = $derived(node.children.length > 0)
1616
let descend = $derived(!node.collapsed && hasChildren)
17-
let value = $derived(node.value)
17+
let value = $derived(node.getValue())
1818
1919
function replaceSpacesWithNonBreakingSpace(value: string) {
2020
return value.replace(/\s/gm, ' ')
@@ -110,8 +110,8 @@
110110
{#if descend}
111111
<li class="row">
112112
<ul>
113-
{#each node.children as child}
114-
<TreeViewNode id={child.id} />
113+
{#each node.children as id}
114+
<TreeViewNode {id} />
115115
{/each}
116116
</ul>
117117
</li>

packages/site/src/components/PropsForm.svelte

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
<script lang="ts">
2-
import { state, update } from '$lib/store'
2+
import { treeOpts, update } from '$lib/store'
33
</script>
44

55
<fieldset class="container-sm flex flex-col border-2 p-2 text-sm">
66
<legend class="text-0A px-2 text-base">Available props</legend>
77
<div class="m-2 mt-0">
88
<div class="field">
99
<label for="leftIndent">--tree-view-left-indent</label>
10-
<input id="leftIndent" class="bg-01 text-0B w-20 pl-1" bind:value={$state.leftIndent} />
10+
<input id="leftIndent" class="bg-01 text-0B w-20 pl-1" bind:value={$treeOpts.leftIndent} />
1111
</div>
1212
<div class="field">
1313
<label for="lineHeight">--tree-view-li-line-height</label>
14-
<input id="lineHeight" class="bg-01 text-0B w-20 pl-1" bind:value={$state.lineHeight} />
14+
<input id="lineHeight" class="bg-01 text-0B w-20 pl-1" bind:value={$treeOpts.lineHeight} />
1515
</div>
1616
<div class="field">
1717
<label for="fontFamily">--tree-view-font-family</label>
18-
<input id="fontFamily" class="bg-01 text-0B w-48 pl-1" bind:value={$state.fontFamily} />
18+
<input id="fontFamily" class="bg-01 text-0B w-48 pl-1" bind:value={$treeOpts.fontFamily} />
1919
</div>
2020
<div class="field">
2121
<label for="fontSize">--tree-view-font-size</label>
22-
<input id="fontSize" class="bg-01 text-0B w-20 pl-1" bind:value={$state.fontSize} />
22+
<input id="fontSize" class="bg-01 text-0B w-20 pl-1" bind:value={$treeOpts.fontSize} />
2323
</div>
2424
<div class="field">
2525
<label for="keyMarginRight">--tree-view-key-margin-right</label>
2626
<input
2727
id="keyMarginRight"
2828
class="bg-01 text-0B w-20 pl-1"
29-
bind:value={$state.keyMarginRight}
29+
bind:value={$treeOpts.keyMarginRight}
3030
/>
3131
</div>
3232
<div class="field">
3333
<label for="showLogButton">showLogButton</label>
34-
<input id="showLogButton" type="checkbox" bind:checked={$state.showLogButton} />
34+
<input id="showLogButton" type="checkbox" bind:checked={$treeOpts.showLogButton} />
3535
</div>
3636
<div class="field">
3737
<label for="showCopyButton">showCopyButton</label>
38-
<input id="showCopyButton" type="checkbox" bind:checked={$state.showCopyButton} />
38+
<input id="showCopyButton" type="checkbox" bind:checked={$treeOpts.showCopyButton} />
3939
</div>
4040
<div class="field">
4141
<label>treeNode</label>
@@ -52,7 +52,7 @@
5252
<textarea
5353
id="recursionOpts"
5454
class="bg-01 text-0B h-44"
55-
value={$state.recursionOpts}
55+
value={$treeOpts.recursionOpts}
5656
oninput={e => update('recursionOpts', e.currentTarget.value)}
5757
></textarea>
5858
</div>
@@ -61,7 +61,7 @@
6161
<textarea
6262
id="valueFormatter"
6363
class="bg-01 text-0B h-44"
64-
value={$state.valueFormatter}
64+
value={$treeOpts.valueFormatter}
6565
oninput={e => update('valueFormatter', e.currentTarget.value)}
6666
></textarea>
6767
</div>
@@ -78,7 +78,7 @@
7878
<textarea
7979
id="theme"
8080
class="bg-01 text-0B h-44 w-full"
81-
value={$state.theme}
81+
value={$treeOpts.theme}
8282
oninput={e => update('theme', e.currentTarget.value)}
8383
></textarea>
8484
</div>

packages/site/src/components/TailwindNode.svelte

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
propsStore: { props: propsObj }
1515
} = getTreeContext()
1616
17-
let hasChildren = $derived(node && node.children.length > 0)
17+
let hasChildren = $derived(node.children.length > 0)
1818
let descend = $derived(!node.collapsed && hasChildren)
1919
2020
// Function to create truncated preview of objects and arrays
@@ -65,12 +65,13 @@
6565
6666
// Get the appropriate value to display
6767
function getDisplayValue(): string {
68+
const val = node.getValue()
6869
if (hasChildren && node.collapsed) {
6970
// Show truncated preview when collapsed
70-
return createTruncatedPreview(node.value, node.type)
71+
return createTruncatedPreview(val, node.type)
7172
} else {
7273
// Show full value when expanded or for leaf nodes
73-
return $propsObj.valueFormatter?.(node.value, node) ?? String(node.value)
74+
return $propsObj.valueFormatter?.(val, node) ?? String(val)
7475
}
7576
}
7677
@@ -129,23 +130,15 @@
129130
{/if}
130131

131132
<!-- Node content -->
132-
<button
133-
class="node-content"
134-
class:clickable={hasChildren}
135-
onclick={hasChildren ? handleToggleCollapse : undefined}
136-
>
133+
<button class="node-content" class:cursor-pointer={hasChildren} onclick={handleToggleCollapse}>
137134
<!-- Key section -->
138135
<div class="node-key-section">
139136
<span class="node-key">{node.key}:</span>
140137
</div>
141138

142139
<!-- Value section -->
143140
<div class="node-value-section">
144-
<div
145-
class={`node-value truncate ${getTypeClasses()}`}
146-
class:clickable={hasChildren}
147-
data-type={node.type}
148-
>
141+
<div class={`node-value truncate ${getTypeClasses()}`} data-type={node.type}>
149142
{getDisplayValue()}
150143
</div>
151144
</div>
@@ -190,8 +183,8 @@
190183

191184
{#if descend}
192185
<div class="children-container">
193-
{#each node.children as child}
194-
<TreeViewNode id={child.id} />
186+
{#each node.children as id}
187+
<TreeViewNode {id} />
195188
{/each}
196189
</div>
197190
{/if}
@@ -205,7 +198,7 @@
205198
}
206199
207200
.tree-node-card {
208-
@apply flex items-center gap-2 rounded-lg border border-gray-200 bg-white p-2 transition-all duration-200 ease-in-out hover:bg-gray-100 dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700;
201+
@apply flex items-center gap-2 rounded-lg border border-gray-200 bg-white transition-all duration-200 ease-in-out hover:bg-gray-100 dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700;
209202
}
210203
211204
.tree-node-card.has-children {
@@ -269,7 +262,7 @@
269262
}
270263
271264
.children-container {
272-
@apply ml-6 mt-2 space-y-1;
265+
@apply ml-6 mt-0.5 space-y-0.5;
273266
}
274267
275268
/* Responsive design */

packages/site/src/lib/parser.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ export const parseRecursionOpts = (
1313
): Record<string, unknown> | undefined => {
1414
try {
1515
const parsed = new Function(`return ${str}`)()
16+
console.log('parsed', parsed)
1617
if (parsed && typeof parsed === 'object') {
1718
parsed.isCircularNode(testNode, new Map())
1819
parsed.shouldExpandNode(testNode)
1920
parsed.mapChildren([], 'array', testNode)
2021
return parsed
2122
}
22-
} catch (e) {}
23+
} catch (e) {
24+
console.log('e', e)
25+
}
2326
return undefined
2427
}
2528

@@ -30,10 +33,12 @@ export const parseValueFormatter = (
3033
try {
3134
const parsed = new Function(`return ${str}`)()
3235
if (typeof parsed === 'function') {
33-
parsed(testNode.value, testNode)
36+
parsed(testNode.getValue(), testNode)
3437
return parsed
3538
}
36-
} catch (e) {}
39+
} catch (e) {
40+
console.log('e', e)
41+
}
3742
return undefined
3843
}
3944

0 commit comments

Comments
 (0)