forked from fkling/astexplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.js
More file actions
102 lines (90 loc) · 2.69 KB
/
Tree.js
File metadata and controls
102 lines (90 loc) · 2.69 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
import Element from './tree/Element';
import PropTypes from 'prop-types';
import React from 'react';
import PubSub from 'pubsub-js';
import {logEvent} from '../../utils/logger';
import {treeAdapterFromParseResult} from '../../core/TreeAdapter.js';
import {SelectedNodeProvider} from './SelectedNodeContext.js';
import focusNodes from './focusNodes.js'
import './css/tree.css'
const {useReducer, useMemo, useRef, useLayoutEffect} = React;
const STORAGE_KEY = 'tree_settings';
function initSettings() {
const storedSettings = global.localStorage.getItem(STORAGE_KEY);
return storedSettings ?
JSON.parse(storedSettings) :
{
autofocus: true,
hideFunctions: true,
hideEmptyKeys: false,
hideLocationData: false,
hideTypeKeys: false,
};
}
function reducer(state, element) {
const newState = {...state, [element.name]: element.checked};
global.localStorage.setItem(STORAGE_KEY, JSON.stringify(newState));
logEvent(
'tree_view_settings',
element.checked ? 'enabled' : 'disabled',
element.name
);
return newState;
}
function makeCheckbox(name, settings, updateSettings) {
return (
<input
type="checkbox"
name={name}
checked={settings[name]}
onChange={event => updateSettings(event.target)}
/>
);
}
export default function Tree({parseResult, position}) {
const [settings, updateSettings] = useReducer(reducer, null, initSettings);
const treeAdapter = useMemo(
() => treeAdapterFromParseResult(parseResult, settings),
[parseResult.treeAdapter, settings],
);
const rootElement = useRef();
focusNodes('init');
useLayoutEffect(() => {
focusNodes('focus', rootElement);
});
return (
<div className="tree-visualization container">
<div className="toolbar">
<label title="Auto open the node at the cursor in the source code">
{makeCheckbox('autofocus', settings, updateSettings)}
Autofocus
</label>
​
{treeAdapter.getConfigurableFilters().map(filter => (
<span key={filter.key}>
<label>
{makeCheckbox(filter.key, settings, updateSettings)}
{filter.label}
</label>
​
</span>
))}
</div>
<ul ref={rootElement} onMouseLeave={() => {PubSub.publish('CLEAR_HIGHLIGHT');}}>
<SelectedNodeProvider>
<Element
value={parseResult.ast}
level={0}
treeAdapter={treeAdapter}
autofocus={settings.autofocus}
position={position}
/>
</SelectedNodeProvider>
</ul>
</div>
);
}
Tree.propTypes = {
parseResult: PropTypes.object,
position: PropTypes.number,
};