@refinedev/devtools 1.1.37 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/dist/components/apply-styles.d.cts +6 -0
  2. package/dist/components/apply-styles.d.cts.map +1 -0
  3. package/dist/components/apply-styles.d.mts +6 -0
  4. package/dist/components/apply-styles.d.mts.map +1 -0
  5. package/dist/components/apply-styles.d.ts +6 -0
  6. package/dist/components/apply-styles.d.ts.map +1 -0
  7. package/dist/components/devtools-pin.d.cts +4 -2
  8. package/dist/components/devtools-pin.d.cts.map +1 -1
  9. package/dist/components/devtools-pin.d.mts +4 -2
  10. package/dist/components/devtools-pin.d.mts.map +1 -1
  11. package/dist/components/devtools-pin.d.ts +4 -2
  12. package/dist/components/devtools-pin.d.ts.map +1 -1
  13. package/dist/components/devtools-selector.d.cts +3 -2
  14. package/dist/components/devtools-selector.d.cts.map +1 -1
  15. package/dist/components/devtools-selector.d.mts +3 -2
  16. package/dist/components/devtools-selector.d.mts.map +1 -1
  17. package/dist/components/devtools-selector.d.ts +3 -2
  18. package/dist/components/devtools-selector.d.ts.map +1 -1
  19. package/dist/components/icons/selector-button.d.cts +1 -0
  20. package/dist/components/icons/selector-button.d.cts.map +1 -1
  21. package/dist/components/icons/selector-button.d.mts +1 -0
  22. package/dist/components/icons/selector-button.d.mts.map +1 -1
  23. package/dist/components/icons/selector-button.d.ts +1 -0
  24. package/dist/components/icons/selector-button.d.ts.map +1 -1
  25. package/dist/components/selectable-elements.d.cts +8 -0
  26. package/dist/components/selectable-elements.d.cts.map +1 -0
  27. package/dist/components/selectable-elements.d.mts +8 -0
  28. package/dist/components/selectable-elements.d.mts.map +1 -0
  29. package/dist/components/selectable-elements.d.ts +8 -0
  30. package/dist/components/selectable-elements.d.ts.map +1 -0
  31. package/dist/index.cjs +94 -2
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.d.cts +2 -2
  34. package/dist/index.d.cts.map +1 -1
  35. package/dist/index.d.mts +2 -2
  36. package/dist/index.d.mts.map +1 -1
  37. package/dist/index.d.ts +2 -2
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.mjs +94 -2
  40. package/dist/index.mjs.map +1 -1
  41. package/dist/panel.d.cts.map +1 -1
  42. package/dist/panel.d.mts.map +1 -1
  43. package/dist/panel.d.ts.map +1 -1
  44. package/dist/utilities/use-selector.d.cts +6 -7
  45. package/dist/utilities/use-selector.d.cts.map +1 -1
  46. package/dist/utilities/use-selector.d.mts +6 -7
  47. package/dist/utilities/use-selector.d.mts.map +1 -1
  48. package/dist/utilities/use-selector.d.ts +6 -7
  49. package/dist/utilities/use-selector.d.ts.map +1 -1
  50. package/package.json +5 -4
  51. package/src/components/apply-styles.tsx +19 -0
  52. package/src/components/devtools-pin.tsx +37 -26
  53. package/src/components/devtools-selector.tsx +34 -76
  54. package/src/components/icons/selector-button.tsx +18 -0
  55. package/src/components/selectable-elements.tsx +255 -0
  56. package/src/index.ts +2 -2
  57. package/src/panel.tsx +13 -2
  58. package/src/utilities/use-selector.tsx +74 -183
  59. package/dist/components/selector-box.d.cts +0 -8
  60. package/dist/components/selector-box.d.cts.map +0 -1
  61. package/dist/components/selector-box.d.mts +0 -8
  62. package/dist/components/selector-box.d.mts.map +0 -1
  63. package/dist/components/selector-box.d.ts +0 -8
  64. package/dist/components/selector-box.d.ts.map +0 -1
  65. package/dist/components/selector-hint.d.cts +0 -4
  66. package/dist/components/selector-hint.d.cts.map +0 -1
  67. package/dist/components/selector-hint.d.mts +0 -4
  68. package/dist/components/selector-hint.d.mts.map +0 -1
  69. package/dist/components/selector-hint.d.ts +0 -4
  70. package/dist/components/selector-hint.d.ts.map +0 -1
  71. package/src/components/selector-box.tsx +0 -107
  72. package/src/components/selector-hint.tsx +0 -57
@@ -1,3 +1,4 @@
1
+ import React from "react";
1
2
  import {
2
3
  getElementFromFiber,
3
4
  getFiberFromElement,
@@ -7,62 +8,31 @@ import {
7
8
  getParentOfFiber,
8
9
  } from "@aliemir/dom-to-fiber-utils";
9
10
  import { DevToolsContext } from "@refinedev/devtools-shared";
10
- import debounce from "lodash/debounce";
11
- import React from "react";
12
11
 
13
12
  type Fiber = Exclude<ReturnType<typeof getFiberFromElement>, null>;
14
13
 
14
+ type SelectableElement = {
15
+ element: HTMLElement;
16
+ name: string;
17
+ };
18
+
15
19
  export const useSelector = (active: boolean) => {
16
20
  const { devtoolsUrl } = React.useContext(DevToolsContext);
17
- const [traceItems, setTraceItems] = React.useState<string[]>([]);
18
-
19
- React.useEffect(() => {
20
- if (active) {
21
- fetch(
22
- `${devtoolsUrl ?? "http://localhost:5001"}/api/unique-trace-items`,
23
- ).then((res) =>
24
- res.json().then((data: { data: string[] }) => setTraceItems(data.data)),
25
- );
26
- }
27
- }, [active, devtoolsUrl]);
28
-
29
- const [selectedFiber, setSelectedFiber] = React.useState<{
30
- stateNode: Fiber | null;
31
- nameFiber: Fiber | null;
32
- }>({
33
- stateNode: null,
34
- nameFiber: null,
35
- });
36
- const [activeFiber, setActiveFiber] = React.useState<{
37
- stateNode: Fiber | null;
38
- nameFiber: Fiber | null;
39
- derived?: boolean;
40
- }>({
41
- stateNode: null,
42
- nameFiber: null,
43
- derived: false,
44
- });
21
+ const [selectableElements, setSelectableElements] = React.useState<
22
+ SelectableElement[]
23
+ >([]);
45
24
 
46
- React.useEffect(() => {
47
- if (active) {
48
- return () => {
49
- setSelectedFiber({
50
- stateNode: null,
51
- nameFiber: null,
52
- });
53
- setActiveFiber({
54
- stateNode: null,
55
- nameFiber: null,
56
- derived: false,
57
- });
58
- };
59
- }
25
+ const fetchTraceItems = React.useCallback(async () => {
26
+ const response = await fetch(
27
+ `${devtoolsUrl ?? "http://localhost:5001"}/api/unique-trace-items`,
28
+ );
29
+ const data = await response.json();
60
30
 
61
- return () => 0;
62
- }, [active]);
31
+ return data.data as string[];
32
+ }, [devtoolsUrl]);
63
33
 
64
34
  const selectAppropriateFiber = React.useCallback(
65
- (start: Fiber | null) => {
35
+ (start: Fiber | null, activeTraceItems: string[]) => {
66
36
  let fiber = start;
67
37
  let firstParentOfNodeWithName: Fiber | null;
68
38
  let fiberWithStateNode: Fiber | null;
@@ -74,7 +44,7 @@ export const useSelector = (active: boolean) => {
74
44
  firstParentOfNodeWithName = getFirstFiberHasName(fiber);
75
45
  // Get the first fiber node that has a state node (look up the tree)
76
46
  fiberWithStateNode = getFirstStateNodeFiber(firstParentOfNodeWithName);
77
- acceptedName = traceItems.includes(
47
+ acceptedName = activeTraceItems.includes(
78
48
  getNameFromFiber(firstParentOfNodeWithName) ?? "",
79
49
  );
80
50
  if (!acceptedName) {
@@ -93,157 +63,78 @@ export const useSelector = (active: boolean) => {
93
63
  nameFiber: null,
94
64
  };
95
65
  },
96
- [traceItems],
97
- );
98
-
99
- const pickFiber = React.useCallback(
100
- (target: HTMLElement) => {
101
- const fiber = getFiberFromElement(target);
102
-
103
- setSelectedFiber(selectAppropriateFiber(fiber));
104
- return;
105
- },
106
- [traceItems],
66
+ [],
107
67
  );
108
68
 
109
- React.useEffect(() => {
110
- if (
111
- activeFiber.stateNode !== selectedFiber.stateNode ||
112
- activeFiber.nameFiber !== selectedFiber.nameFiber
113
- ) {
114
- setActiveFiber({
115
- stateNode: selectedFiber.stateNode,
116
- nameFiber: selectedFiber.nameFiber,
117
- derived: false,
118
- });
119
- }
120
- }, [selectedFiber]);
121
-
122
- const previousValues = React.useRef<{
123
- rect: {
124
- width: number;
125
- height: number;
126
- x: number;
127
- y: number;
128
- };
129
- name: string;
130
- }>({
131
- rect: {
132
- width: 0,
133
- height: 0,
134
- x: 0,
135
- y: 0,
136
- },
137
- name: "",
138
- });
69
+ const getUniqueNodes = React.useCallback((nodes: SelectableElement[]) => {
70
+ const uniques: SelectableElement[] = [];
139
71
 
140
- const { rect, name } = React.useMemo(() => {
141
- if (!active) {
142
- return {
143
- rect: {
144
- width: 0,
145
- height: 0,
146
- x: 0,
147
- y: 0,
148
- },
149
- name: "",
150
- };
151
- }
152
- if (activeFiber.stateNode || activeFiber.nameFiber) {
153
- // Get the element from the fiber with a state node
154
- const element = activeFiber.stateNode
155
- ? getElementFromFiber(activeFiber.stateNode)
156
- : null;
157
- // Get the name of the fiber node with a name
158
- const fiberName = activeFiber.nameFiber
159
- ? getNameFromFiber(activeFiber.nameFiber)
160
- : null;
161
-
162
- if (!element) {
163
- return {
164
- rect: previousValues.current.rect,
165
- name: fiberName ?? previousValues.current.name,
166
- };
72
+ nodes.forEach((node) => {
73
+ const isElementExist = uniques.find(
74
+ (item) => item.element === node.element,
75
+ );
76
+ if (!isElementExist) {
77
+ uniques.push(node);
78
+ }
79
+ });
80
+
81
+ return uniques;
82
+ }, []);
83
+
84
+ const traverseBodyNodes = React.useCallback(
85
+ (
86
+ node: HTMLElement | null,
87
+ activeTraceItems: string[],
88
+ ): SelectableElement[] => {
89
+ if (!node) {
90
+ return [];
167
91
  }
168
92
 
169
- const bounding = element.getBoundingClientRect?.();
93
+ const items: SelectableElement[] = [];
170
94
 
171
- if (!bounding) {
172
- return {
173
- rect: previousValues.current.rect,
174
- name: fiberName ?? previousValues.current.name,
175
- };
176
- }
95
+ const fiber = getFiberFromElement(node);
96
+ const targetFiber = selectAppropriateFiber(fiber, activeTraceItems);
177
97
 
178
- return {
179
- rect: {
180
- width: bounding.width,
181
- height: bounding.height,
182
- x: bounding.left,
183
- y: bounding.top,
184
- },
185
- name: fiberName ?? previousValues.current.name,
186
- };
187
- }
98
+ if (targetFiber.nameFiber && targetFiber.stateNode) {
99
+ const element = getElementFromFiber(targetFiber.stateNode);
100
+ const name = getNameFromFiber(targetFiber.nameFiber);
101
+ if (element && name) {
102
+ items.push({
103
+ element,
104
+ name,
105
+ });
106
+ }
107
+ }
188
108
 
189
- return previousValues.current;
190
- }, [activeFiber, active]);
109
+ for (let i = 0; i < node?.children?.length ?? 0; i++) {
110
+ items.push(
111
+ ...traverseBodyNodes(
112
+ node.children[i] as HTMLElement,
113
+ activeTraceItems,
114
+ ),
115
+ );
116
+ }
191
117
 
192
- previousValues.current = {
193
- rect,
194
- name,
195
- };
118
+ return items;
119
+ },
120
+ [selectAppropriateFiber],
121
+ );
196
122
 
197
- React.useEffect(() => {
198
- if (active) {
199
- const listener = (e: KeyboardEvent) => {
200
- // if user presses shift, toggle the derived state and set the active fiber to the parent
201
- if (e.key === "Shift" && activeFiber.stateNode) {
202
- e.stopPropagation();
203
- e.preventDefault();
204
-
205
- const parent = getParentOfFiber(activeFiber.nameFiber);
206
-
207
- const fibers = selectAppropriateFiber(parent);
208
-
209
- if (fibers.nameFiber && fibers.stateNode) {
210
- setActiveFiber({
211
- ...fibers,
212
- derived: true,
213
- });
214
- return;
215
- }
216
- }
217
- };
123
+ const prepareSelector = React.useCallback(async () => {
124
+ const fetchedTraceItems = await fetchTraceItems();
125
+ const traversedNodes = traverseBodyNodes(document.body, fetchedTraceItems);
126
+ const uniqueNodes = getUniqueNodes(traversedNodes);
218
127
 
219
- document.addEventListener("keydown", listener);
220
- return () => document.removeEventListener("keydown", listener);
221
- }
222
- return () => 0;
223
- }, [activeFiber, active]);
128
+ setSelectableElements(uniqueNodes);
129
+ }, [fetchTraceItems, traverseBodyNodes, getUniqueNodes]);
224
130
 
225
131
  React.useEffect(() => {
226
132
  if (active) {
227
- let previousTarget: HTMLElement | null = null;
228
- const listener = debounce((e: MouseEvent) => {
229
- if (e?.target) {
230
- if (previousTarget === e.target) {
231
- return;
232
- }
233
- pickFiber(e.target as HTMLElement);
234
- previousTarget = e.target as HTMLElement;
235
- }
236
- }, 50);
237
-
238
- document.addEventListener("mousemove", listener);
239
-
240
- return () => document.removeEventListener("mousemove", listener);
133
+ prepareSelector();
241
134
  }
242
- return () => 0;
243
- }, [active, pickFiber]);
135
+ }, [active, prepareSelector]);
244
136
 
245
137
  return {
246
- rect,
247
- name,
138
+ selectableElements,
248
139
  };
249
140
  };
@@ -1,8 +0,0 @@
1
- export declare const SelectorBox: ({ width, height, x, y, name, }: {
2
- width: number;
3
- height: number;
4
- x: number;
5
- y: number;
6
- name: string;
7
- }) => JSX.Element;
8
- //# sourceMappingURL=selector-box.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"selector-box.d.ts","sourceRoot":"","sources":["../../src/components/selector-box.tsx"],"names":[],"mappings":"AAMA,eAAO,MAAM,WAAW;WAOf,MAAM;YACL,MAAM;OACX,MAAM;OACN,MAAM;UACH,MAAM;iBAyFb,CAAC"}
@@ -1,8 +0,0 @@
1
- export declare const SelectorBox: ({ width, height, x, y, name, }: {
2
- width: number;
3
- height: number;
4
- x: number;
5
- y: number;
6
- name: string;
7
- }) => JSX.Element;
8
- //# sourceMappingURL=selector-box.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"selector-box.d.ts","sourceRoot":"","sources":["../../src/components/selector-box.tsx"],"names":[],"mappings":"AAMA,eAAO,MAAM,WAAW;WAOf,MAAM;YACL,MAAM;OACX,MAAM;OACN,MAAM;UACH,MAAM;iBAyFb,CAAC"}
@@ -1,8 +0,0 @@
1
- export declare const SelectorBox: ({ width, height, x, y, name, }: {
2
- width: number;
3
- height: number;
4
- x: number;
5
- y: number;
6
- name: string;
7
- }) => JSX.Element;
8
- //# sourceMappingURL=selector-box.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"selector-box.d.ts","sourceRoot":"","sources":["../../src/components/selector-box.tsx"],"names":[],"mappings":"AAMA,eAAO,MAAM,WAAW;WAOf,MAAM;YACL,MAAM;OACX,MAAM;OACN,MAAM;UACH,MAAM;iBAyFb,CAAC"}
@@ -1,4 +0,0 @@
1
- export declare const SelectorHint: ({ active }: {
2
- active: boolean;
3
- }) => JSX.Element;
4
- //# sourceMappingURL=selector-hint.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"selector-hint.d.ts","sourceRoot":"","sources":["../../src/components/selector-hint.tsx"],"names":[],"mappings":"AAEA,eAAO,MAAM,YAAY;YAA0B,OAAO;iBAsDzD,CAAC"}
@@ -1,4 +0,0 @@
1
- export declare const SelectorHint: ({ active }: {
2
- active: boolean;
3
- }) => JSX.Element;
4
- //# sourceMappingURL=selector-hint.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"selector-hint.d.ts","sourceRoot":"","sources":["../../src/components/selector-hint.tsx"],"names":[],"mappings":"AAEA,eAAO,MAAM,YAAY;YAA0B,OAAO;iBAsDzD,CAAC"}
@@ -1,4 +0,0 @@
1
- export declare const SelectorHint: ({ active }: {
2
- active: boolean;
3
- }) => JSX.Element;
4
- //# sourceMappingURL=selector-hint.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"selector-hint.d.ts","sourceRoot":"","sources":["../../src/components/selector-hint.tsx"],"names":[],"mappings":"AAEA,eAAO,MAAM,YAAY;YAA0B,OAAO;iBAsDzD,CAAC"}
@@ -1,107 +0,0 @@
1
- import React from "react";
2
- import { ArrowUnionIcon } from "./icons/arrow-union-icon";
3
-
4
- const Y_OFFSET = 38;
5
- const X_OFFSET = 7;
6
-
7
- export const SelectorBox = ({
8
- width,
9
- height,
10
- x,
11
- y,
12
- name,
13
- }: {
14
- width: number;
15
- height: number;
16
- x: number;
17
- y: number;
18
- name: string;
19
- }) => {
20
- const namePosition =
21
- y > Y_OFFSET ? "top" : height > 3 * Y_OFFSET ? "inset" : "bottom";
22
-
23
- const outlinePosition = x > X_OFFSET && y > X_OFFSET ? "outside" : "inside";
24
-
25
- return (
26
- <div
27
- id="selector-box"
28
- style={{
29
- pointerEvents: "none",
30
- position: "fixed",
31
- opacity: name ? 1 : 0,
32
- transitionProperty: "width, height, transform, opacity",
33
- transitionDuration: "0.15s",
34
- transitionTimingFunction: "ease-out",
35
- border: "1.5px solid #47EBEB",
36
- borderRadius: "4px",
37
- background: "rgba(37,160,160, 0.25)",
38
- backdropFilter: "sepia(30%) hue-rotate(180deg)",
39
- zIndex: 99998,
40
- ...(outlinePosition === "outside"
41
- ? {
42
- left: -6,
43
- top: -6,
44
- width: width + 10,
45
- height: height + 10,
46
- transform: `translate(${x}px, ${y}px)`,
47
- }
48
- : {
49
- left: 0,
50
- top: 0,
51
- width: width - 10,
52
- height: height - 20,
53
- transform: `translate(${x + 4}px, ${y + 4}px)`,
54
- }),
55
- }}
56
- >
57
- <div
58
- style={{
59
- position: "absolute",
60
- left: "-1.5px",
61
- background: "#303450",
62
- border: "1px solid #474E6B",
63
- borderRadius: "4px",
64
- padding: "8px 12px",
65
- fontSize: "12px",
66
- lineHeight: "16px",
67
- fontWeight: 400,
68
- color: "#ffffff",
69
- display: name ? "block" : "none",
70
- ...(namePosition === "top" && {
71
- top: "-38px",
72
- }),
73
- ...(namePosition === "bottom" && {
74
- bottom: "-38px",
75
- }),
76
- ...(namePosition === "inset" && {
77
- top: "4px",
78
- left: "8px",
79
- }),
80
- }}
81
- >
82
- {name}
83
-
84
- <ArrowUnionIcon
85
- style={{
86
- position: "absolute",
87
- left: "30%",
88
- ...(namePosition === "top" && {
89
- transform: "translateX(-50%) rotate(180deg)",
90
- bottom: "-9px",
91
- }),
92
- ...(namePosition === "bottom" && {
93
- transform: "translateX(-50%) ",
94
- top: "-9px",
95
- }),
96
- ...(namePosition === "inset" && {
97
- transform: "translateX(-50%) rotate(-90deg)",
98
- top: "8px",
99
- left: "-1px",
100
- }),
101
- display: name ? "block" : "none",
102
- }}
103
- />
104
- </div>
105
- </div>
106
- );
107
- };
@@ -1,57 +0,0 @@
1
- import React from "react";
2
-
3
- export const SelectorHint = ({ active }: { active: boolean }) => {
4
- return (
5
- <div
6
- style={{
7
- color: "white",
8
- pointerEvents: "none",
9
- position: "absolute",
10
- left: "calc(-50% - 144px - 14px)",
11
- top: "-36px",
12
- width: "max-content",
13
- borderRadius: "8px",
14
- backgroundColor: "#40414A",
15
- opacity: active ? 1 : 0,
16
- transitionDuration: "0.2s",
17
- transitionProperty: "transform,opacity",
18
- transitionTimingFunction: "ease-in-out",
19
- padding: "8px",
20
- fontSize: "12px",
21
- lineHeight: "12px",
22
- fontWeight: 400,
23
- textShadow:
24
- "0 0 2px #1D1E30, 1px 0 2px #1D1E30, -1px 0 2px #1D1E30, 0 1px 2px #1D1E30, 0 -1px 2px #1D1E30",
25
- }}
26
- >
27
- <kbd
28
- style={{
29
- marginLeft: "4px",
30
- padding: "2px 4px",
31
- borderRadius: "2px",
32
- background: "#1D1E30",
33
- color: "#CFD7E2",
34
- border: "none",
35
- textShadow: "none",
36
- }}
37
- >
38
- shift
39
- </kbd>{" "}
40
- to move to parent.
41
- <kbd
42
- style={{
43
- marginLeft: "4px",
44
- padding: "2px 4px",
45
- borderRadius: "2px",
46
- background: "#1D1E30",
47
- color: "#CFD7E2",
48
- border: "none",
49
- textShadow: "none",
50
- }}
51
- >
52
- space
53
- </kbd>{" "}
54
- to highlight in monitor.
55
- </div>
56
- );
57
- };