Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy functionality to tree updated #1626

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion extension/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
"storage",
"file:///*",
"http://*/*",
"https://*/*"
"https://*/*",
"clipboardWrite",
"clipboardRead"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'; style-src * 'unsafe-inline'; img-src 'self' data:;",
"update_url": "https://clients2.google.com/service/update2/crx",
Expand Down
4 changes: 3 additions & 1 deletion extension/edge/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@
"storage",
"file:///*",
"http://*/*",
"https://*/*"
"https://*/*",
"clipboardWrite",
"clipboardRead"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'; style-src * 'unsafe-inline'; img-src 'self' data:;"
}
4 changes: 3 additions & 1 deletion extension/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
"storage",
"file:///*",
"http://*/*",
"https://*/*"
"https://*/*",
"clipboardWrite",
"clipboardRead"
],
"content_security_policy": "script-src 'self'; object-src 'self'; img-src 'self' data:;"
}
2 changes: 2 additions & 0 deletions packages/redux-devtools-inspector-monitor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"immutable": "^4.3.5",
"javascript-stringify": "^2.1.0",
"jsondiffpatch": "^0.6.0",
"lodash.clonedeep": "^4.5.0",
"lodash.debounce": "^4.0.8",
"react-base16-styling": "^0.9.1",
"react-json-tree": "^0.18.0",
Expand All @@ -66,6 +67,7 @@
"@types/dateformat": "^5.0.2",
"@types/hex-rgba": "^1.0.3",
"@types/history": "^4.7.11",
"@types/lodash.clonedeep": "^4.5.9",
"@types/lodash.debounce": "^4.0.9",
"@types/react": "^18.2.58",
"@typescript-eslint/eslint-plugin": "^6.21.0",
Expand Down
39 changes: 37 additions & 2 deletions packages/redux-devtools-inspector-monitor/src/ActionPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import ActionPreviewHeader from './ActionPreviewHeader';
import DiffTab from './tabs/DiffTab';
import StateTab from './tabs/StateTab';
import ActionTab from './tabs/ActionTab';
import { getValueByPath } from './utils/getValueByPath';
import { copyToClipboard } from './utils/copyToClipboard';

export interface TabComponentProps<S, A extends Action<string>> {
labelRenderer: LabelRenderer;
Expand Down Expand Up @@ -184,7 +186,7 @@ class ActionPreview<S, A extends Action<string>> extends Component<

labelRenderer: LabelRenderer = ([key, ...rest], nodeType, expanded) => {
const { onInspectPath, inspectedPath } = this.props;

const reversedPath = [key, ...rest].reverse();
return (
<span>
<span>{key}</span>
Expand All @@ -202,12 +204,45 @@ class ActionPreview<S, A extends Action<string>> extends Component<
onClick={() =>
onInspectPath([
...inspectedPath.slice(0, inspectedPath.length - 1),
...[key, ...rest].reverse(),
...reversedPath,
])
}
>
{'(pin)'}
</span>
<span
css={(theme) => ({
fontSize: '0.7em',
paddingLeft: '5px',
cursor: 'pointer',
'&:hover': {
textDecoration: 'underline',
},
color: theme.PIN_COLOR,
})}
onClick={(event) => {
event.stopPropagation();
let objectForCopying;
if (this.props.tabName === 'Action') {
objectForCopying = getValueByPath(
this.props.action,
reversedPath,
);
} else if (this.props.tabName === 'State') {
objectForCopying = getValueByPath(
this.props.nextState,
reversedPath,
);
}
if (objectForCopying !== undefined) {
copyToClipboard(objectForCopying);
} else {
console.error('Unable to find the object to copy');
}
}}
>
{'(copy)'}
</span>
{!expanded && ': '}
</span>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import cloneDeep from 'lodash.clonedeep';

export function copyToClipboard(object: any) {
try {
const deepCopiedObject = cloneDeep(object);
const jsonString = JSON.stringify(deepCopiedObject, null, 2);
void navigator.clipboard.writeText(jsonString);
} catch (err) {
console.error('Error during copy: ', err);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function getValueByPath(obj: any, path: (string | number)[]) {
let current: any = obj;
for (let i = 0; i < path.length; i++) {
const key = path[i];
const adjustedKey =
typeof key === 'string' && !isNaN(Number(key)) ? parseInt(key, 10) : key;
if (current[adjustedKey] === undefined) {
return undefined;
}
current = current[adjustedKey];
}
return current;
}
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.