Skip to content

Commit 0a1b580

Browse files
author
Timothy Johnson
committed
Search node tree
1 parent fe4652a commit 0a1b580

File tree

6 files changed

+132
-51
lines changed

6 files changed

+132
-51
lines changed

dest/devtools/search.svg

Lines changed: 4 additions & 0 deletions
Loading

src/App.svelte

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,55 @@
11
<script>
2-
import { hoveredNodeId, rootNodes } from './store.js'
2+
import {
3+
searchResult,
4+
searchValue,
5+
hoveredNodeId,
6+
rootNodes
7+
} from './store.js'
38
import ComponentView from './ComponentView.svelte'
9+
import Toolbar from './Toolbar.svelte'
10+
import VisibilityButton from './VisibilityButton.svelte'
411
import Node from './nodes/Node.svelte'
512
</script>
613

714
<style>
15+
.root {
16+
display: flex;
17+
flex-direction: column;
18+
flex-grow: 1;
19+
}
20+
21+
input {
22+
display: inline-block;
23+
margin-right: 5px;
24+
padding: 0 10px 0 22px;
25+
height: 22px;
26+
border: 1px solid rgb(224, 224, 226);
27+
border-radius: 2px;
28+
background: 6px center / 12px no-repeat url('/devtools/search.svg') #ffffff;
29+
font-size: inherit;
30+
}
31+
832
ul {
933
overflow: auto;
1034
flex-grow: 1;
1135
margin-top: 8px;
1236
}
1337
38+
.results :global(> li) {
39+
margin-bottom: 10px;
40+
padding-bottom: 10px;
41+
border-bottom: 1px solid rgb(242, 242, 243);
42+
}
43+
44+
.results :global(> li:last-child) {
45+
border-bottom: none;
46+
}
47+
48+
.root div {
49+
margin-top: 40%;
50+
text-align: center;
51+
}
52+
1453
p {
1554
position: absolute;
1655
top: 40%;
@@ -20,11 +59,29 @@
2059
</style>
2160

2261
{#if $rootNodes.length}
23-
<ul on:mouseleave={e => ($hoveredNodeId = null)}>
24-
{#each $rootNodes as node (node.id)}
25-
<Node {node} />
26-
{/each}
27-
</ul>
62+
<div class="root">
63+
<Toolbar>
64+
<input placeholder="Search Tree" bind:value={$searchValue} />
65+
<VisibilityButton />
66+
</Toolbar>
67+
{#if $searchResult}
68+
{#if $searchResult.length}
69+
<ul class="results" on:mouseleave={e => ($hoveredNodeId = null)}>
70+
{#each $searchResult as node (node.id)}
71+
<Node {node} forceCollapsed={true} />
72+
{/each}
73+
</ul>
74+
{:else}
75+
<div>No results</div>
76+
{/if}
77+
{:else}
78+
<ul on:mouseleave={e => ($hoveredNodeId = null)}>
79+
{#each $rootNodes as node (node.id)}
80+
<Node {node} />
81+
{/each}
82+
</ul>
83+
{/if}
84+
</div>
2885
<ComponentView />
2986
{:else}
3087
<p>

src/ComponentView.svelte

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { devtools } from 'chrome'
33
import { selectedNode } from './store.js'
44
import PropertyList from './PropertyList.svelte'
5-
import VisibilityButton from './VisibilityButton.svelte'
5+
import Toolbar from './Toolbar.svelte'
66
77
let isResizing = false
88
let width = 300
@@ -28,45 +28,6 @@
2828
.resize:hover {
2929
border-color: rgb(177, 177, 179);
3030
}
31-
32-
.toolbar {
33-
padding: 0 5px;
34-
border-bottom: 1px solid rgb(224, 224, 226);
35-
background-color: rgb(249, 249, 250);
36-
text-align: right;
37-
}
38-
39-
.toolbar :global(button) {
40-
margin: 1px;
41-
padding: 5px;
42-
outline: none;
43-
border: none;
44-
border-radius: 2px;
45-
background-color: transparent;
46-
line-height: 0;
47-
cursor: pointer;
48-
}
49-
50-
.toolbar :global(button:hover) {
51-
background-color: rgb(237, 237, 240);
52-
}
53-
54-
.toolbar :global(button:active) {
55-
opacity: 0.8;
56-
}
57-
58-
.toolbar :global(button:disabled) {
59-
background-color: transparent;
60-
opacity: 0.2;
61-
cursor: default;
62-
}
63-
64-
.toolbar :global(img) {
65-
width: 16px;
66-
height: 16px;
67-
vertical-align: middle;
68-
opacity: 0.8;
69-
}
7031
</style>
7132

7233
<svelte:window
@@ -75,14 +36,13 @@
7536

7637
<div class="root" style={`width: ${width}px`}>
7738
<div class="resize" on:mousedown={e => (isResizing = true)} />
78-
<div class="toolbar">
79-
<VisibilityButton />
39+
<Toolbar>
8040
<button
8141
disabled={$selectedNode.id === undefined}
8242
on:click={e => devtools.inspectedWindow.eval('inspect(window.$s)')}>
8343
<img src="/devtools/tool-inspector.svg" alt="Inspect" title="Inspect" />
8444
</button>
85-
</div>
45+
</Toolbar>
8646
{#if $selectedNode.type == 'component'}
8747
<PropertyList
8848
id={$selectedNode.id}

src/Toolbar.svelte

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<style>
2+
div {
3+
padding: 0 5px;
4+
border-bottom: 1px solid rgb(224, 224, 226);
5+
background-color: rgb(249, 249, 250);
6+
text-align: right;
7+
}
8+
9+
div :global(button) {
10+
margin: 1px;
11+
padding: 5px;
12+
outline: none;
13+
border: none;
14+
border-radius: 2px;
15+
background-color: transparent;
16+
line-height: 0;
17+
cursor: pointer;
18+
}
19+
20+
div :global(button:hover) {
21+
background-color: rgb(237, 237, 240);
22+
}
23+
24+
div :global(button:active) {
25+
opacity: 0.8;
26+
}
27+
28+
div :global(button:disabled) {
29+
background-color: transparent;
30+
opacity: 0.2;
31+
cursor: default;
32+
}
33+
34+
div :global(img) {
35+
width: 16px;
36+
height: 16px;
37+
vertical-align: middle;
38+
opacity: 0.8;
39+
}
40+
</style>
41+
42+
<div>
43+
<slot />
44+
</div>

src/nodes/Node.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
88
export let node
99
export let depth = 1
10+
export let forceCollapsed
11+
12+
let _collapsed = forceCollapsed || node.collapsed
1013
1114
let _timeout = null
1215
node.invalidate = () => {
@@ -32,6 +35,8 @@
3235
flash = flash || node.children.length != lastLength
3336
lastLength = node.children.length
3437
}
38+
39+
$: if (!forceCollapsed) node.collapsed = _collapsed
3540
</script>
3641

3742
<style>
@@ -86,7 +91,7 @@
8691
<svelte:component
8792
this={nodeType}
8893
tagName={node.tagName}
89-
bind:collapsed={node.collapsed}
94+
bind:collapsed={_collapsed}
9095
{...node.detail}
9196
hasChildren={node.children.length != 0}
9297
hover={$hoveredNodeId == node.id}

src/store.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { writable, get } from 'svelte/store'
1+
import { writable, get, derived } from 'svelte/store'
22

33
export const visibility = writable({
44
component: true,
@@ -10,6 +10,17 @@ export const visibility = writable({
1010
export const selectedNode = writable({})
1111
export const hoveredNodeId = writable(null)
1212
export const rootNodes = writable([])
13+
export const searchValue = writable('')
14+
export const searchResult = derived(searchValue, value =>
15+
value.length < 2
16+
? null
17+
: Array.from(nodeMap.values()).filter(
18+
node =>
19+
node.tagName.includes(value) ||
20+
(node.detail && JSON.stringify(node.detail).includes(value))
21+
)
22+
)
23+
1324
const nodeMap = new Map()
1425

1526
const port = chrome.runtime.connect()

0 commit comments

Comments
 (0)