From f46fddab74be77006e3b0d8e2f007c5888ba8ef1 Mon Sep 17 00:00:00 2001 From: Mai Trung Duc Date: Wed, 14 Jun 2023 14:52:31 +0800 Subject: [PATCH] feat: add more attributes that can be edited --- package.json | 4 +- src/pages/panel/components/Attributes.tsx | 128 +++++++++++ .../panel/components/InspectedElement.tsx | 111 ++------- src/pages/panel/components/Panel.scss | 24 +- src/pages/panel/components/constants.ts | 213 +++++++++++++++++- .../panel/devtools/konvaDevtoolsOutline.ts | 1 + .../panel/devtools/konvaDevtoolsOverlay.ts | 23 +- yarn.lock | 17 ++ 8 files changed, 413 insertions(+), 108 deletions(-) create mode 100644 src/pages/panel/components/Attributes.tsx diff --git a/package.json b/package.json index 193e4cd..2eb2bf8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "konva-inspector", - "version": "0.0.9", + "version": "0.0.10", "description": "Devtools for your Konva App", "license": "MIT", "repository": { @@ -18,6 +18,7 @@ "type": "module", "dependencies": { "konva": "^9.0.2", + "lodash-es": "^4.17.21", "react": "18.2.0", "react-dom": "18.2.0" }, @@ -26,6 +27,7 @@ "@testing-library/react": "^13.4.0", "@types/chrome": "^0.0.224", "@types/jest": "29.0.3", + "@types/lodash-es": "^4.17.7", "@types/node": "18.15.11", "@types/react": "18.0.21", "@types/react-dom": "18.0.11", diff --git a/src/pages/panel/components/Attributes.tsx b/src/pages/panel/components/Attributes.tsx new file mode 100644 index 0000000..b339d65 --- /dev/null +++ b/src/pages/panel/components/Attributes.tsx @@ -0,0 +1,128 @@ +import React from "react"; +import { bridge } from ".."; +import CopyToClipboard from "./CopyToClipboard"; +import { IAttr } from "./constants"; + +interface IProps { + title: string; + nodeAttrs: Record; + attrs: IAttr[]; + custom?: boolean; + updateAttr: (attrName: string, val: any) => Promise; +} + +export default function Attributes({ + title, + nodeAttrs, + attrs, + custom, + updateAttr, +}: IProps) { + const copyToClipBoard = () => { + bridge( + `window.copy(window.__KONVA_DEVTOOLS_GLOBAL_HOOK__ && window.__KONVA_DEVTOOLS_GLOBAL_HOOK__.selection.selected().attrs)` + ); + }; + + return ( +
+
+
{title}
+ +
+
+ {attrs.map((item) => { + let input; + + switch (item.type) { + case "boolean": { + input = ( + updateAttr(item.name, e.target.checked)} + /> + ); + break; + } + case "number": { + input = ( + + updateAttr( + item.name, + isNaN(e.target.valueAsNumber) + ? null // JSON.stringify will not preserve undefined, so we have to use null here + : e.target.valueAsNumber + ) + } + min={item.min} + /> + ); + break; + } + case "json": { + input = ( +