-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config-panel.js
102 lines (89 loc) · 2.47 KB
/
config-panel.js
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
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import React, {useState} from 'react';
import {Button, Icons, TextArea, withState} from '@kepler.gl/components';
import {KeplerGlSchema} from '@kepler.gl/schemas';
import {visStateLens, mapStateLens, mapStyleLens} from '@kepler.gl/reducers';
import {CopyToClipboard} from 'react-copy-to-clipboard';
import styled from 'styled-components';
const Params = {
true: 'True',
false: 'False',
null: 'None'
};
export const StyleCopyConfig = styled.div.attrs({
className: 'copy-config'
})`
width: 100%;
display: flex;
flex-direction: column;
height: 100%;
.copy-button {
position: absolute;
margin-top: -45px;
right: 28px;
}
textarea {
overflow-y: scroll;
white-space: pre-wrap;
width: 100%;
height: 100%;
resize: none;
}
`;
// convert config from string to pything dictionary
function configStringify(config) {
// replace true => True; false => False; null => None
const configStr = JSON.stringify(config, null, 2);
return configStr.replace(/: ([a-z]+)/g, function(_, key) {
return ': ' + Params[key] || ': ' + key;
});
}
export const CopyConfig = ({config}) => {
const [copied, setCopy] = useState(false);
const value = configStringify(config);
return (
<StyleCopyConfig>
<CopyToClipboard text={value} onCopy={() => setCopy(true)} className="copy-button">
<Button width="100px">
<Icons.Clipboard height="16px" />
{copied ? 'Copied!' : 'Copy'}
</Button>
</CopyToClipboard>
<TextArea value={value} readOnly selected />
</StyleCopyConfig>
);
};
function CustomSidePanelsFactory() {
const CustomPanels = ({activeSidePanel, visState, mapState, mapStyle}) => {
const config = KeplerGlSchema.getConfigToSave({visState, mapState, mapStyle});
if (activeSidePanel === 'config') {
return <CopyConfig config={config} />;
}
return null;
};
CustomPanels.defaultProps = {
panels: [
{
id: 'config',
label: 'Config',
iconComponent: Icons.CodeAlt
}
]
};
const ConnectedCustomPanels = withState(
[visStateLens, mapStateLens, mapStyleLens],
state => state
)(CustomPanels);
ConnectedCustomPanels.defaultProps = {
panels: [
{
id: 'config',
label: 'modal.exportMap.json.configTitle',
iconComponent: Icons.CodeAlt
}
]
};
return ConnectedCustomPanels;
}
export default CustomSidePanelsFactory;