Skip to content

Commit c7e1ad2

Browse files
committed
updated-copy-behavior
1 parent 98f71ed commit c7e1ad2

File tree

5 files changed

+91
-52
lines changed

5 files changed

+91
-52
lines changed

dev-server/src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ ReactDom.render(
7676
}}
7777
name={false}
7878
iconStyle="triangle"
79-
shouldCollapse={({ src, namespace, type }) =>
80-
namespace.indexOf("moment") > -1}
79+
shouldCollapse={({ src, type }) =>
80+
type === "object" &&
81+
src.constructor &&
82+
src.constructor.name === "Moment"}
8183
/>
8284

8385
<br />
@@ -197,7 +199,8 @@ function getExampleJson1() {
197199
},
198200
string_number: "1234",
199201
date: new Date(),
200-
moment: Moment()
202+
moment: Moment(),
203+
regexp: /[0-9]/gi
201204
}
202205
}
203206

src/js/components/CopyToClipboard.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import React from 'react'
1+
import React from "react"
2+
3+
import { toType } from "./../helpers/util"
4+
import stringifyVariable from "./../helpers/stringifyVariable"
25

36
//clibboard icon
47
import { Clippy } from "./icons"
@@ -8,24 +11,28 @@ import Theme from "./../themes/getStyle"
811

912
export default class extends React.Component {
1013
constructor(props) {
11-
super(props);
12-
this.copiedTimer = null;
14+
super(props)
15+
this.copiedTimer = null
1316
}
1417

15-
state = {copied: false}
18+
state = { copied: false }
1619

1720
componentWillUnmount() {
1821
if (this.copiedTimer) {
19-
clearTimeout(this.copiedTimer);
20-
this.copiedTimer = null;
22+
clearTimeout(this.copiedTimer)
23+
this.copiedTimer = null
2124
}
2225
}
2326

2427
handleCopy = () => {
2528
const container = document.createElement("textarea")
2629
const { clickCallback, src, namespace } = this.props
2730

28-
container.innerHTML = JSON.stringify(src, null, " ")
31+
container.innerHTML = JSON.stringify(
32+
this.clipboardValue(src),
33+
null,
34+
" "
35+
)
2936

3037
document.body.appendChild(container)
3138
container.select()
@@ -67,6 +74,18 @@ export default class extends React.Component {
6774
return <Clippy class="copy-icon" {...Theme(theme, "copy-icon")} />
6875
}
6976

77+
clipboardValue = value => {
78+
const type = toType(value)
79+
switch (type) {
80+
case "function":
81+
return value.toString()
82+
case "regexp":
83+
return value.toString()
84+
default:
85+
return value
86+
}
87+
}
88+
7089
render() {
7190
const { src, theme, hidden } = this.props
7291
let style = Theme(theme, "copy-to-clipboard").style
@@ -90,4 +109,4 @@ export default class extends React.Component {
90109
</span>
91110
)
92111
}
93-
}
112+
}
Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,82 @@
1-
import React from 'react';
2-
import DataTypeLabel from './DataTypeLabel';
1+
import React from "react"
2+
import DataTypeLabel from "./DataTypeLabel"
33

44
//theme
5-
import Theme from './../../themes/getStyle';
5+
import Theme from "./../../themes/getStyle"
66

77
//attribute store for storing collapsed state
8-
import AttributeStore from './../../stores/ObjectAttributes';
8+
import AttributeStore from "./../../stores/ObjectAttributes"
99

1010
export default class extends React.Component {
11-
1211
constructor(props) {
13-
super(props);
12+
super(props)
1413

1514
this.state.collapsed = AttributeStore.get(
1615
props.rjvId,
1716
props.namespace,
18-
'collapsed',
17+
"collapsed",
1918
true
20-
);
19+
)
2120
}
2221

2322
state = {
2423
collapsed: true
2524
}
2625

2726
toggleCollapsed = () => {
28-
this.state.collapsed = !this.state.collapsed;
27+
this.state.collapsed = !this.state.collapsed
2928
AttributeStore.set(
3029
this.props.rjvId,
3130
this.props.namespace,
32-
'collapsed',
31+
"collapsed",
3332
this.state.collapsed
34-
);
35-
this.setState(this.state);
33+
)
34+
this.setState(this.state)
3635
}
3736

3837
render() {
39-
const type_name = 'function';
40-
const {props} = this;
41-
const {collapsed} = this.state;
38+
const type_name = "function"
39+
const { props } = this
40+
const { collapsed } = this.state
4241

4342
return (
44-
<div {...Theme(props.theme, 'function')} >
45-
<DataTypeLabel type_name={type_name} {...props} />
46-
<span class="rjv-function-container"
47-
onClick={()=>{this.toggleCollapsed()}} >
48-
{this.getFunctionDisplay(collapsed)}
49-
</span>
50-
</div>
51-
);
43+
<div {...Theme(props.theme, "function")}>
44+
<DataTypeLabel type_name={type_name} {...props} />
45+
<span
46+
{...Theme(props.theme, "function-value")}
47+
class="rjv-function-container"
48+
onClick={() => {
49+
this.toggleCollapsed()
50+
}}
51+
>
52+
{this.getFunctionDisplay(collapsed)}
53+
</span>
54+
</div>
55+
)
5256
}
5357

54-
getFunctionDisplay = (collapsed) => {
55-
const {props} = this;
58+
getFunctionDisplay = collapsed => {
59+
const { props } = this
5660

5761
if (collapsed) {
58-
return (<span >
59-
{this.props.value.toString().slice(9, -1).replace(/\{[\s\S]+/, '')}
60-
<span class='function-collapsed' style={{fontWeight: 'bold'}}>
61-
<span>{"{"}</span>
62-
<span {...Theme(props.theme, 'ellipsis')}>...</span>
63-
<span>{"}"}</span>
62+
return (
63+
<span>
64+
{this.props.value
65+
.toString()
66+
.slice(9, -1)
67+
.replace(/\{[\s\S]+/, "")}
68+
<span
69+
class="function-collapsed"
70+
style={{ fontWeight: "bold" }}
71+
>
72+
<span>{"{"}</span>
73+
<span {...Theme(props.theme, "ellipsis")}>...</span>
74+
<span>{"}"}</span>
75+
</span>
6476
</span>
65-
</span>);
77+
)
6678
} else {
67-
return this.props.value.toString().slice(9, -1);
79+
return this.props.value.toString().slice(9, -1)
6880
}
6981
}
70-
71-
}
82+
}

src/js/themes/getStyle.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ const getDefaultThemeStyling = theme => {
179179
cursor: "pointer",
180180
whiteSpace: "pre-line"
181181
},
182+
"function-value": {
183+
fontStyle: "italic"
184+
},
182185
integer: {
183186
display: "inline-block",
184187
color: colors.dataTypes.integer

test/tests/js/Index-test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ describe("<Index />", function() {
3030
two: "two"
3131
}
3232
},
33-
arr: [[1, "two"], { one: "one", two: 2 }]
33+
arr: [[1, "two"], { one: "one", two: 2 }],
34+
regexp: /[0-9]/gi
3435
}}
3536
/>
3637
)
37-
expect(wrapper.find(".data-type-label")).to.have.length(13)
38-
expect(wrapper.find(".data-type-label")).to.have.length(13)
38+
expect(wrapper.find(".data-type-label")).to.have.length(14)
39+
expect(wrapper.find(".data-type-label")).to.have.length(14)
3940
})
4041

4142
it("check object-size labels from index", function() {
@@ -55,7 +56,8 @@ describe("<Index />", function() {
5556
two: "two"
5657
}
5758
},
58-
arr: [[1, "two"], { one: "one", two: 2 }]
59+
arr: [[1, "two"], { one: "one", two: 2 }],
60+
regexp: /[0-9]/gi
5961
}}
6062
displayObjectSize={true}
6163
displayDataTypes={true}
@@ -82,11 +84,12 @@ describe("<Index />", function() {
8284
test: true,
8385
passing: "hopefully",
8486
arr: [5],
85-
obj: {}
87+
obj: {},
88+
regexp: /[0-9]/gi
8689
}}
8790
/>
8891
)
89-
expect(wrapper.find(".copy-to-clipboard-container")).to.have.length(6)
92+
expect(wrapper.find(".copy-to-clipboard-container")).to.have.length(7)
9093
})
9194

9295
it("index test componentWillReceiveProps", function() {

0 commit comments

Comments
 (0)