Skip to content

Commit

Permalink
fix: toggle always inspect not working if triggered manually
Browse files Browse the repository at this point in the history
  • Loading branch information
maitrungduc1410 committed Jun 24, 2023
1 parent 0055fc3 commit 11d5e17
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "konva-inspector",
"version": "0.0.12",
"version": "0.0.13",
"description": "Devtools for your Konva App",
"license": "MIT",
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions src/pages/panel/components/Panel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ body {
fill: currentColor;
}
.input {
flex: 1 1 100px;
width: 100px;
flex: 1;
font-size: var(--font-size-sans-large);
outline: none;
border: none;
Expand Down Expand Up @@ -249,13 +248,14 @@ body {
.search-input-item {
padding: 0.25rem;
border-bottom: 1px solid var(--color-border);
height: 20px;
}

.inspected-element-data {
overflow-y: auto;
height: calc(
100% - 42px - 3px
); // add 3px as buffer for better scrollbar visibility
100% - 42px - (20px + 0.5rem) - 3px
); // add 3px as buffer for better scrollbar visibility, (20px + 0.5rem) is for the search attr input
.header-row {
display: flex;
align-items: center;
Expand Down
23 changes: 16 additions & 7 deletions src/pages/panel/components/Panel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useRef, useState } from "react";
import "./Panel.scss";
import Element from "./Element";
import { bridge } from "..";
Expand All @@ -18,6 +18,7 @@ const Panel: React.FC = () => {
const [searchText, setSearchText] = useState<string>("");
const [alwaysInspect, setAlwaysInspect] = useState<boolean>(false);
const [isDarkMode, setIsDarkMode] = useState<boolean>(false);
const isMouseOverTreeViewRef = useRef<boolean>(false);

// Handle dark theme
useEffect(() => {
Expand Down Expand Up @@ -70,12 +71,13 @@ const Panel: React.FC = () => {
window.__KONVA_DEVTOOLS_GLOBAL_HOOK__.selection.registerMouseOverEvents()`);

const interval = setInterval(async () => {
getActiveNode();
if (!isMouseOverTreeViewRef.current) {
getActiveNode();
}
}, 500);

return () => {
clearInterval(interval);

bridge(`
window.__KONVA_DEVTOOLS_GLOBAL_HOOK__ &&
window.__KONVA_DEVTOOLS_GLOBAL_HOOK__.selection.unregisterMouseOverEvents()`);
Expand All @@ -88,14 +90,20 @@ const Panel: React.FC = () => {
bridge(
"window.__KONVA_DEVTOOLS_GLOBAL_HOOK__ && window.__KONVA_DEVTOOLS_GLOBAL_HOOK__.selection.deactivate()"
);
isMouseOverTreeViewRef.current = false;
}
function handleMouseOver() {
isMouseOverTreeViewRef.current = true;
}
const inspectedTree = document.getElementById(
"inspected-trees"
) as HTMLDivElement;
inspectedTree.addEventListener("mouseleave", handleMouseLeave);
inspectedTree.addEventListener("mouseover", handleMouseOver);

return () => {
inspectedTree.removeEventListener("mouseleave", handleMouseLeave);
inspectedTree.removeEventListener("mouseover", handleMouseOver);
};
}, []);

Expand Down Expand Up @@ -167,9 +175,6 @@ const Panel: React.FC = () => {
className={alwaysInspect ? "toggle-on" : "toggle-off"}
onClick={() => {
setAlwaysInspect((cur) => !cur);
bridge(
`window.__KONVA_DEVTOOLS_GLOBAL_HOOK__ && window.__KONVA_DEVTOOLS_GLOBAL_HOOK__.selection.setAlwaysInspect(true)`
);
}}
>
<span className="toggle-content" tabIndex={-1}>
Expand Down Expand Up @@ -203,7 +208,11 @@ const Panel: React.FC = () => {
stageIndex={index}
indent={0}
node={item}
onSelectNode={(data) => setSelectedNode(data)}
onSelectNode={(data) => {
setSelectedNode(data);
setAlwaysInspect(false);
setActiveNode(null); // because next interval may not run yet, so we need to set this to make sure active node is null
}}
/>
</div>
))}
Expand Down
5 changes: 3 additions & 2 deletions src/pages/panel/devtools/konvaDevtoolsSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default function konvaDevtoolsSelection(devtools: KonvaDevtools) {
let alwaysInspect = false;

// memoize handler so that we can remove it later
let handlers = {};
// note: do not clear handlers after unregisterMouseOverEvents, otherwise we'll lost reference to remove and the toggle button from React won't work anymore
const handlers = {};

return {
active(serialize = false): Konva.Node | OutlineNode | undefined {
Expand Down Expand Up @@ -44,7 +45,6 @@ export default function konvaDevtoolsSelection(devtools: KonvaDevtools) {
deactivate() {
activeNode = undefined;
activeNodeStageIndex = null;
handlers = {};
devtools.overlay.clear();
},
updateAttrs(attrs: Record<string, string | number | boolean>) {
Expand Down Expand Up @@ -75,6 +75,7 @@ export default function konvaDevtoolsSelection(devtools: KonvaDevtools) {
);
stage.on("click", devtools.selection.unregisterMouseOverEvents);
}
devtools.selection.setAlwaysInspect(true);
1; // add this line so that it'll be returned when evaluation, otherwise it'll throw error because the evaluation returns object class
}
},
Expand Down

0 comments on commit 11d5e17

Please sign in to comment.