@lark-apaas/miaoda-inspector 0.1.0-alpha.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 (74) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +1 -0
  3. package/dist/es/Inspector/Inspector.js +179 -0
  4. package/dist/es/Inspector/Overlay.js +400 -0
  5. package/dist/es/Inspector/hooks/index.js +3 -0
  6. package/dist/es/Inspector/hooks/use-effect-event.js +15 -0
  7. package/dist/es/Inspector/hooks/use-layout-effect.js +3 -0
  8. package/dist/es/Inspector/hooks/use-mouse.js +22 -0
  9. package/dist/es/Inspector/index.js +1 -0
  10. package/dist/es/Inspector/utils/fiber.js +61 -0
  11. package/dist/es/Inspector/utils/highlight.js +74 -0
  12. package/dist/es/Inspector/utils/index.js +3 -0
  13. package/dist/es/Inspector/utils/inspect.js +119 -0
  14. package/dist/es/Inspector/utils/overlay.js +20 -0
  15. package/dist/es/MiaodaInspector/MiaodaInspector.css +46 -0
  16. package/dist/es/MiaodaInspector/MiaodaInspector.js +397 -0
  17. package/dist/es/MiaodaInspector/index.js +1 -0
  18. package/dist/es/config/ui-config.js +787 -0
  19. package/dist/es/index.js +2 -0
  20. package/dist/es/types/iframe-events.js +1 -0
  21. package/dist/es/types/index.js +1 -0
  22. package/dist/es/utils/config-mapper.js +163 -0
  23. package/dist/es/utils/css.js +5 -0
  24. package/dist/es/utils/index.js +3 -0
  25. package/dist/es/utils/origin.js +19 -0
  26. package/dist/es/utils/style-calculator.js +158 -0
  27. package/dist/lib/Inspector/Inspector.js +203 -0
  28. package/dist/lib/Inspector/Overlay.js +419 -0
  29. package/dist/lib/Inspector/hooks/index.js +20 -0
  30. package/dist/lib/Inspector/hooks/use-effect-event.js +25 -0
  31. package/dist/lib/Inspector/hooks/use-layout-effect.js +13 -0
  32. package/dist/lib/Inspector/hooks/use-mouse.js +32 -0
  33. package/dist/lib/Inspector/index.js +11 -0
  34. package/dist/lib/Inspector/utils/fiber.js +95 -0
  35. package/dist/lib/Inspector/utils/highlight.js +84 -0
  36. package/dist/lib/Inspector/utils/index.js +20 -0
  37. package/dist/lib/Inspector/utils/inspect.js +153 -0
  38. package/dist/lib/Inspector/utils/overlay.js +39 -0
  39. package/dist/lib/MiaodaInspector/MiaodaInspector.css +46 -0
  40. package/dist/lib/MiaodaInspector/MiaodaInspector.js +447 -0
  41. package/dist/lib/MiaodaInspector/index.js +11 -0
  42. package/dist/lib/config/ui-config.js +797 -0
  43. package/dist/lib/index.js +21 -0
  44. package/dist/lib/types/iframe-events.js +4 -0
  45. package/dist/lib/types/index.js +47 -0
  46. package/dist/lib/utils/config-mapper.js +182 -0
  47. package/dist/lib/utils/css.js +20 -0
  48. package/dist/lib/utils/index.js +61 -0
  49. package/dist/lib/utils/origin.js +44 -0
  50. package/dist/lib/utils/style-calculator.js +207 -0
  51. package/dist/types/Inspector/Inspector.d.ts +102 -0
  52. package/dist/types/Inspector/Overlay.d.ts +71 -0
  53. package/dist/types/Inspector/hooks/index.d.ts +3 -0
  54. package/dist/types/Inspector/hooks/use-effect-event.d.ts +9 -0
  55. package/dist/types/Inspector/hooks/use-layout-effect.d.ts +2 -0
  56. package/dist/types/Inspector/hooks/use-mouse.d.ts +9 -0
  57. package/dist/types/Inspector/index.d.ts +1 -0
  58. package/dist/types/Inspector/utils/fiber.d.ts +34 -0
  59. package/dist/types/Inspector/utils/highlight.d.ts +8 -0
  60. package/dist/types/Inspector/utils/index.d.ts +3 -0
  61. package/dist/types/Inspector/utils/inspect.d.ts +110 -0
  62. package/dist/types/Inspector/utils/overlay.d.ts +30 -0
  63. package/dist/types/MiaodaInspector/MiaodaInspector.d.ts +47 -0
  64. package/dist/types/MiaodaInspector/index.d.ts +1 -0
  65. package/dist/types/config/ui-config.d.ts +49 -0
  66. package/dist/types/index.d.ts +4 -0
  67. package/dist/types/types/iframe-events.d.ts +155 -0
  68. package/dist/types/types/index.d.ts +1 -0
  69. package/dist/types/utils/config-mapper.d.ts +12 -0
  70. package/dist/types/utils/css.d.ts +2 -0
  71. package/dist/types/utils/index.d.ts +3 -0
  72. package/dist/types/utils/origin.d.ts +9 -0
  73. package/dist/types/utils/style-calculator.d.ts +19 -0
  74. package/package.json +48 -0
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Lark Technologies Pte. Ltd. and/or its affiliates
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,provided that the above copyright notice and this permission notice appear in all copies.
6
+
7
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
8
+ IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
9
+ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
10
+ EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
11
+ CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
12
+ DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13
+ ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @lark-apaas/miaoda-inspector
@@ -0,0 +1,179 @@
1
+ "use client";
2
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
3
+ import { useState, useEffect, useRef } from "react";
4
+ import hotkeys from "hotkeys-js";
5
+ import { useLayoutEffect, useEffectEvent, useMousePosition } from "./hooks";
6
+ import { Overlay } from "./Overlay";
7
+ import { setupHighlighter, getElementCodeInfo, getElementInspect, getReferenceFiber } from "./utils";
8
+ import { getElementFiberUpward } from "./utils/fiber";
9
+ const getParentElement = (element) => {
10
+ let parent = element;
11
+ while (parent && parent.tagName !== "HTML") {
12
+ if (parent.dataset.miaodaComponentName) {
13
+ return parent;
14
+ }
15
+ const fiber = getElementFiberUpward(parent);
16
+ const parentFiber = parent.parentElement ? getElementFiberUpward(parent.parentElement) : void 0;
17
+ const referenceFiber = getReferenceFiber(fiber);
18
+ const parentReferenceFiber = parentFiber ? getReferenceFiber(parentFiber) : null;
19
+ const currentName = referenceFiber === null || referenceFiber === void 0 ? void 0 : referenceFiber.pendingProps["data-miaoda-component-name"];
20
+ const parentName = parentReferenceFiber ? parentReferenceFiber.pendingProps["data-miaoda-component-name"] : null;
21
+ if (currentName && currentName !== parentName) {
22
+ return parent;
23
+ }
24
+ parent = parent.parentElement;
25
+ }
26
+ return;
27
+ };
28
+ export const defaultHotkeys = () => {
29
+ var _navigator_platform;
30
+ return ((_navigator_platform = navigator.platform) === null || _navigator_platform === void 0 ? void 0 : _navigator_platform.startsWith("Mac")) ? [
31
+ "Ctrl",
32
+ "Shift",
33
+ "Command",
34
+ "C"
35
+ ] : [
36
+ "Ctrl",
37
+ "Shift",
38
+ "Alt",
39
+ "C"
40
+ ];
41
+ };
42
+ export const Inspector = (props) => {
43
+ const { keys, onHoverElement, onClickElement, onInspectElement, active: controlledActive, onActiveChange, handleCodeInfo, disable = true, children } = props;
44
+ const [isActive, setActive] = useState(controlledActive !== null && controlledActive !== void 0 ? controlledActive : false);
45
+ const hotkey = keys === null ? null : (keys !== null && keys !== void 0 ? keys : []).join("+");
46
+ const overlayRef = useRef();
47
+ const mouseRef = useMousePosition({
48
+ disable
49
+ });
50
+ useLayoutEffect(() => {
51
+ if (controlledActive !== void 0) {
52
+ setActive(controlledActive);
53
+ }
54
+ }, [
55
+ controlledActive
56
+ ]);
57
+ const activate = useEffectEvent(() => {
58
+ onActiveChange === null || onActiveChange === void 0 ? void 0 : onActiveChange(true);
59
+ if (controlledActive === void 0) {
60
+ setActive(true);
61
+ }
62
+ });
63
+ const deactivate = useEffectEvent(() => {
64
+ onActiveChange === null || onActiveChange === void 0 ? void 0 : onActiveChange(false);
65
+ if (controlledActive === void 0) {
66
+ setActive(false);
67
+ }
68
+ });
69
+ const handleHoverElement = useEffectEvent((element) => {
70
+ var _overlay_inspect;
71
+ const overlay = overlayRef.current;
72
+ const targetElement = getParentElement(element);
73
+ if (!targetElement) {
74
+ return;
75
+ }
76
+ const codeInfo = getElementCodeInfo(targetElement);
77
+ const relativePath = codeInfo === null || codeInfo === void 0 ? void 0 : codeInfo.relativePath;
78
+ const absolutePath = codeInfo === null || codeInfo === void 0 ? void 0 : codeInfo.absolutePath;
79
+ const { fiber, name, title } = getElementInspect(targetElement);
80
+ overlay === null || overlay === void 0 ? void 0 : (_overlay_inspect = overlay.inspect) === null || _overlay_inspect === void 0 ? void 0 : _overlay_inspect.call(overlay, [
81
+ targetElement
82
+ ], title, relativePath !== null && relativePath !== void 0 ? relativePath : absolutePath);
83
+ onHoverElement === null || onHoverElement === void 0 ? void 0 : onHoverElement({
84
+ element: targetElement,
85
+ fiber,
86
+ codeInfo,
87
+ name
88
+ });
89
+ });
90
+ const handleClickElement = useEffectEvent((element) => {
91
+ const targetElement = getParentElement(element);
92
+ if (!targetElement) {
93
+ return;
94
+ }
95
+ const codeInfo = getElementCodeInfo(targetElement);
96
+ const { fiber, name, title } = getElementInspect(targetElement);
97
+ onClickElement === null || onClickElement === void 0 ? void 0 : onClickElement({
98
+ element: targetElement,
99
+ fiber,
100
+ codeInfo,
101
+ name,
102
+ title
103
+ });
104
+ if (fiber && codeInfo) {
105
+ onInspectElement === null || onInspectElement === void 0 ? void 0 : onInspectElement({
106
+ element: targetElement,
107
+ fiber,
108
+ codeInfo,
109
+ name,
110
+ title
111
+ });
112
+ }
113
+ });
114
+ const startInspect = useEffectEvent(() => {
115
+ if (overlayRef.current || disable)
116
+ return;
117
+ const overlay = new Overlay(handleCodeInfo);
118
+ overlayRef.current = overlay;
119
+ hotkeys(`esc`, deactivate);
120
+ const stopCallback = setupHighlighter({
121
+ onPointerOver: handleHoverElement,
122
+ onClick: handleClickElement
123
+ });
124
+ overlay.setRemoveCallback(stopCallback);
125
+ const initPoint = mouseRef.current;
126
+ const initElement = document.elementFromPoint(initPoint.x, initPoint.y);
127
+ if (initElement)
128
+ handleHoverElement(initElement);
129
+ });
130
+ const stopInspect = useEffectEvent(() => {
131
+ var _overlayRef_current;
132
+ (_overlayRef_current = overlayRef.current) === null || _overlayRef_current === void 0 ? void 0 : _overlayRef_current.remove();
133
+ overlayRef.current = void 0;
134
+ hotkeys.unbind(`esc`, deactivate);
135
+ });
136
+ useEffect(() => {
137
+ if (isActive) {
138
+ startInspect();
139
+ } else {
140
+ stopInspect();
141
+ }
142
+ return stopInspect;
143
+ }, [
144
+ isActive
145
+ ]);
146
+ useEffect(() => {
147
+ const handleScroll = () => {
148
+ var _overlayRef_current;
149
+ (_overlayRef_current = overlayRef.current) === null || _overlayRef_current === void 0 ? void 0 : _overlayRef_current.clearRect();
150
+ };
151
+ window.addEventListener("scroll", handleScroll, true);
152
+ return () => {
153
+ window.removeEventListener("scroll", handleScroll, true);
154
+ };
155
+ }, []);
156
+ useEffect(() => {
157
+ const handleHotKeys = () => {
158
+ if (overlayRef.current) {
159
+ deactivate();
160
+ } else {
161
+ activate();
162
+ }
163
+ };
164
+ const bindKey = hotkey === null || disable ? null : hotkey || defaultHotkeys().join("+");
165
+ if (bindKey) {
166
+ hotkeys(bindKey, handleHotKeys);
167
+ return () => {
168
+ hotkeys.unbind(bindKey, handleHotKeys);
169
+ };
170
+ }
171
+ return;
172
+ }, [
173
+ hotkey,
174
+ disable
175
+ ]);
176
+ return /* @__PURE__ */ _jsx(_Fragment, {
177
+ children: children !== null && children !== void 0 ? children : null
178
+ });
179
+ };
@@ -0,0 +1,400 @@
1
+ import { getElementDimensions, getNestedBoundingClientRect } from "./utils";
2
+ const overlayStyles = {
3
+ padding: "transparent",
4
+ margin: "transparent",
5
+ border: "transparent"
6
+ };
7
+ function boxWrap(dims, what, node) {
8
+ Object.assign(node.style, {
9
+ borderTopWidth: `${dims[`${what}Top`]}px`,
10
+ borderLeftWidth: `${dims[`${what}Left`]}px`,
11
+ borderRightWidth: `${dims[`${what}Right`]}px`,
12
+ borderBottomWidth: `${dims[`${what}Bottom`]}px`,
13
+ borderStyle: "solid"
14
+ });
15
+ }
16
+ function findTipPos(dims, bounds, tipSize) {
17
+ const tipHeight = Math.max(tipSize.height, 20);
18
+ const tipWidth = Math.max(tipSize.width, 60);
19
+ const margin = 4;
20
+ let top;
21
+ if (dims.top - tipHeight - margin >= bounds.top) {
22
+ top = dims.top - tipHeight - margin;
23
+ } else if (dims.top + dims.height + tipHeight <= bounds.top + bounds.height) {
24
+ if (dims.top + dims.height < bounds.top + 0) {
25
+ top = bounds.top + margin;
26
+ } else {
27
+ top = dims.top + dims.height + margin;
28
+ }
29
+ } else if (dims.top - tipHeight <= bounds.top + bounds.height) {
30
+ if (dims.top - tipHeight - margin < bounds.top + margin) {
31
+ top = bounds.top + margin;
32
+ } else {
33
+ top = dims.top - tipHeight - margin;
34
+ }
35
+ } else {
36
+ top = bounds.top + bounds.height - tipHeight - margin;
37
+ }
38
+ let { left } = dims;
39
+ if (dims.left < bounds.left) {
40
+ left = bounds.left;
41
+ }
42
+ if (dims.left + tipWidth > bounds.left + bounds.width) {
43
+ left = bounds.left + bounds.width - tipWidth;
44
+ }
45
+ return {
46
+ style: {
47
+ top: `${top}px`,
48
+ // 目前 box 的样式是设置的 outline,为了对齐要向左偏移 1px
49
+ left: `${left - 1}px`
50
+ }
51
+ };
52
+ }
53
+ class OverlayRect {
54
+ remove() {
55
+ if (this.node.parentNode) {
56
+ this.node.parentNode.removeChild(this.node);
57
+ }
58
+ }
59
+ update(box, dims) {
60
+ boxWrap(dims, "margin", this.node);
61
+ boxWrap(dims, "border", this.border);
62
+ boxWrap(dims, "padding", this.padding);
63
+ Object.assign(this.content.style, {
64
+ height: `${box.height - dims.borderTop - dims.borderBottom - dims.paddingTop - dims.paddingBottom}px`,
65
+ width: `${box.width - dims.borderLeft - dims.borderRight - dims.paddingLeft - dims.paddingRight}px`
66
+ });
67
+ Object.assign(this.node.style, {
68
+ top: `${box.top - dims.marginTop}px`,
69
+ left: `${box.left - dims.marginLeft}px`
70
+ });
71
+ }
72
+ constructor(doc, container) {
73
+ this.node = doc.createElement("div");
74
+ this.border = doc.createElement("div");
75
+ this.padding = doc.createElement("div");
76
+ this.content = doc.createElement("div");
77
+ this.border.style.borderColor = overlayStyles.border;
78
+ this.padding.style.borderColor = overlayStyles.padding;
79
+ Object.assign(this.node.style, {
80
+ borderColor: overlayStyles.margin,
81
+ pointerEvents: "none",
82
+ position: "fixed",
83
+ outline: "1px dashed #1456f0"
84
+ });
85
+ this.node.style.zIndex = "10000000";
86
+ this.node.appendChild(this.border);
87
+ this.border.appendChild(this.padding);
88
+ this.padding.appendChild(this.content);
89
+ container.prepend(this.node);
90
+ }
91
+ }
92
+ class ClickOverlayRect {
93
+ remove() {
94
+ if (this.node.parentNode) {
95
+ this.node.parentNode.removeChild(this.node);
96
+ }
97
+ }
98
+ update(box, dims) {
99
+ Object.assign(this.node.style, {
100
+ top: `${box.top - 1 - dims.marginTop}px`,
101
+ left: `${box.left - 1 - dims.marginLeft}px`,
102
+ width: `${box.width + dims.marginLeft + dims.marginRight + 2}px`,
103
+ height: `${box.height + dims.marginTop + dims.marginBottom + 2}px`
104
+ });
105
+ }
106
+ constructor(doc, container) {
107
+ this.node = doc.createElement("div");
108
+ this.border = doc.createElement("div");
109
+ Object.assign(this.border.style, {
110
+ pointerEvents: "none",
111
+ position: "absolute",
112
+ top: "0",
113
+ left: "0",
114
+ width: "100%",
115
+ height: "100%",
116
+ boxSizing: "border-box",
117
+ border: "1px solid #1456f0"
118
+ });
119
+ Object.assign(this.node.style, {
120
+ pointerEvents: "none",
121
+ position: "fixed",
122
+ zIndex: "9999999"
123
+ });
124
+ this.node.appendChild(this.border);
125
+ container.appendChild(this.node);
126
+ }
127
+ }
128
+ class OverlayTip {
129
+ remove() {
130
+ if (this.tip.parentNode) {
131
+ this.tip.parentNode.removeChild(this.tip);
132
+ }
133
+ }
134
+ updateText(name) {
135
+ this.titleDiv.textContent = name;
136
+ }
137
+ updatePosition(dims, bounds) {
138
+ const tipRect = this.tip.getBoundingClientRect();
139
+ const tipPos = findTipPos(dims, bounds, {
140
+ width: tipRect.width,
141
+ height: tipRect.height
142
+ });
143
+ Object.assign(this.tip.style, tipPos.style);
144
+ if (dims.width === 0 || dims.height === 0) {
145
+ this.invisible();
146
+ } else {
147
+ Object.assign(this.tip.style, {
148
+ display: "flex"
149
+ });
150
+ }
151
+ }
152
+ invisible() {
153
+ Object.assign(this.tip.style, {
154
+ display: "none"
155
+ });
156
+ }
157
+ constructor(doc, container) {
158
+ this.tip = doc.createElement("div");
159
+ Object.assign(this.tip.style, {
160
+ display: "flex",
161
+ flexFlow: "row nowrap",
162
+ alignItems: "center",
163
+ backgroundColor: "#1456f0",
164
+ borderRadius: "6px",
165
+ fontFamily: '"SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace',
166
+ fontWeight: "bold",
167
+ padding: "1px 12px",
168
+ pointerEvents: "none",
169
+ position: "fixed",
170
+ fontSize: "14px",
171
+ lineHeight: "22px",
172
+ whiteSpace: "nowrap"
173
+ });
174
+ this.nameSpan = doc.createElement("span");
175
+ this.tip.appendChild(this.nameSpan);
176
+ Object.assign(this.nameSpan.style, {
177
+ display: "flex",
178
+ flexDirection: "column"
179
+ });
180
+ this.titleDiv = doc.createElement("div");
181
+ this.nameSpan.appendChild(this.titleDiv);
182
+ Object.assign(this.titleDiv.style, {
183
+ color: "#fff",
184
+ fontSize: "14px",
185
+ borderRadius: "6px"
186
+ });
187
+ this.tip.style.zIndex = "10000000";
188
+ container.appendChild(this.tip);
189
+ }
190
+ }
191
+ export class Overlay {
192
+ remove() {
193
+ this.tip.remove();
194
+ this.rects.forEach((rect) => {
195
+ rect.remove();
196
+ });
197
+ this.rects.length = 0;
198
+ if (this.container.parentNode) {
199
+ this.container.parentNode.removeChild(this.container);
200
+ }
201
+ this.removeCallback();
202
+ }
203
+ setRemoveCallback(callback) {
204
+ this.removeCallback = callback.bind(this);
205
+ }
206
+ inspect(nodes, name, info) {
207
+ this.resizeObserver.disconnect();
208
+ const elements = nodes.filter((node) => node.nodeType === Node.ELEMENT_NODE);
209
+ this.currentElements = nodes;
210
+ while (this.rects.length > elements.length) {
211
+ const rect = this.rects.pop();
212
+ rect === null || rect === void 0 ? void 0 : rect.remove();
213
+ }
214
+ if (elements.length === 0) {
215
+ return;
216
+ }
217
+ elements.forEach((element) => {
218
+ this.resizeObserver.observe(element);
219
+ });
220
+ this.updatePositions(elements, name, info);
221
+ }
222
+ updatePositions(nodes, name, info) {
223
+ const elements = nodes.filter((node) => node.nodeType === Node.ELEMENT_NODE);
224
+ while (this.rects.length > elements.length) {
225
+ const rect = this.rects.pop();
226
+ rect === null || rect === void 0 ? void 0 : rect.remove();
227
+ }
228
+ if (elements.length === 0) {
229
+ return;
230
+ }
231
+ while (this.rects.length < elements.length) {
232
+ this.rects.push(new OverlayRect(this.window.document, this.container));
233
+ }
234
+ const outerBox = {
235
+ top: Number.POSITIVE_INFINITY,
236
+ right: Number.NEGATIVE_INFINITY,
237
+ bottom: Number.NEGATIVE_INFINITY,
238
+ left: Number.POSITIVE_INFINITY
239
+ };
240
+ elements.forEach((element, index) => {
241
+ const box = getNestedBoundingClientRect(element, this.window);
242
+ const dims = getElementDimensions(element);
243
+ outerBox.top = Math.min(outerBox.top, box.top - dims.marginTop);
244
+ outerBox.right = Math.max(outerBox.right, box.left + box.width + dims.marginRight);
245
+ outerBox.bottom = Math.max(outerBox.bottom, box.top + box.height + dims.marginBottom);
246
+ outerBox.left = Math.min(outerBox.left, box.left - dims.marginLeft);
247
+ const rect = this.rects[index];
248
+ rect.update(box, dims);
249
+ });
250
+ if (!name) {
251
+ name = elements[0].nodeName.toLowerCase();
252
+ }
253
+ this.currentName = name;
254
+ if (this.handleCodeInfo) {
255
+ const [displayName] = this.handleCodeInfo(name || "", info);
256
+ this.tip.updateText(displayName);
257
+ } else {
258
+ this.tip.updateText(name);
259
+ }
260
+ const tipBounds = getNestedBoundingClientRect(this.tipBoundsWindow.document.documentElement, this.window);
261
+ this.tip.updatePosition({
262
+ top: outerBox.top,
263
+ left: outerBox.left,
264
+ height: outerBox.bottom - outerBox.top,
265
+ width: outerBox.right - outerBox.left
266
+ }, {
267
+ top: tipBounds.top + this.tipBoundsWindow.scrollY,
268
+ left: tipBounds.left + this.tipBoundsWindow.scrollX,
269
+ height: this.tipBoundsWindow.innerHeight,
270
+ width: this.tipBoundsWindow.innerWidth
271
+ });
272
+ }
273
+ clearRect() {
274
+ var _this_rects;
275
+ (_this_rects = this.rects) === null || _this_rects === void 0 ? void 0 : _this_rects.forEach((rect) => {
276
+ rect.remove();
277
+ });
278
+ this.rects.length = 0;
279
+ this.tip.invisible();
280
+ }
281
+ constructor(handleCodeInfo) {
282
+ this.handleCodeInfo = handleCodeInfo;
283
+ const currentWindow = window.__REACT_DEVTOOLS_TARGET_WINDOW__ || window;
284
+ this.window = currentWindow;
285
+ const tipBoundsWindow = window.__REACT_DEVTOOLS_TARGET_WINDOW__ || window;
286
+ this.tipBoundsWindow = tipBoundsWindow;
287
+ const doc = currentWindow.document;
288
+ this.container = doc.createElement("div");
289
+ this.container.style.zIndex = "10000000";
290
+ this.tip = new OverlayTip(doc, this.container);
291
+ this.rects = [];
292
+ this.removeCallback = () => {
293
+ };
294
+ let rafId = null;
295
+ this.resizeObserver = new ResizeObserver(() => {
296
+ if (rafId) {
297
+ cancelAnimationFrame(rafId);
298
+ }
299
+ rafId = requestAnimationFrame(() => {
300
+ if (this.currentElements && this.currentName) {
301
+ this.updatePositions(this.currentElements, this.currentName);
302
+ }
303
+ });
304
+ });
305
+ doc.body.appendChild(this.container);
306
+ }
307
+ }
308
+ export class ClickOverlay {
309
+ remove() {
310
+ this.tip.remove();
311
+ if (this.rect) {
312
+ this.rect.remove();
313
+ this.rect = null;
314
+ }
315
+ this.currentElement = null;
316
+ this.resizeObserver.disconnect();
317
+ this.currentName = void 0;
318
+ if (this.container.parentNode) {
319
+ this.container.parentNode.removeChild(this.container);
320
+ }
321
+ }
322
+ highlight(element, name) {
323
+ if (!this.rect) {
324
+ this.rect = new ClickOverlayRect(this.window.document, this.container);
325
+ }
326
+ this.resizeObserver.disconnect();
327
+ this.currentElement = element;
328
+ if (!name) {
329
+ name = element.nodeName.toLowerCase();
330
+ }
331
+ this.currentName = name;
332
+ this.tip.updateText(name);
333
+ this.updatePosition(element);
334
+ this.resizeObserver.observe(element);
335
+ }
336
+ updatePosition(element) {
337
+ var _this_rect;
338
+ const outerBox = {
339
+ top: Number.POSITIVE_INFINITY,
340
+ right: Number.NEGATIVE_INFINITY,
341
+ bottom: Number.NEGATIVE_INFINITY,
342
+ left: Number.POSITIVE_INFINITY
343
+ };
344
+ const box = getNestedBoundingClientRect(element, this.window);
345
+ const dims = getElementDimensions(element);
346
+ outerBox.top = Math.min(outerBox.top, box.top - dims.marginTop);
347
+ outerBox.right = Math.max(outerBox.right, box.left + box.width + dims.marginRight);
348
+ outerBox.bottom = Math.max(outerBox.bottom, box.top + box.height + dims.marginBottom);
349
+ outerBox.left = Math.min(outerBox.left, box.left - dims.marginLeft);
350
+ (_this_rect = this.rect) === null || _this_rect === void 0 ? void 0 : _this_rect.update(box, dims);
351
+ const tipBounds = getNestedBoundingClientRect(this.tipBoundsWindow.document.documentElement, this.window);
352
+ if (box.width === 0 || box.height === 0) {
353
+ this.tip.invisible();
354
+ }
355
+ this.tip.updatePosition({
356
+ top: outerBox.top,
357
+ left: outerBox.left,
358
+ height: outerBox.bottom - outerBox.top,
359
+ width: outerBox.right - outerBox.left
360
+ }, {
361
+ top: tipBounds.top + this.tipBoundsWindow.scrollY,
362
+ left: tipBounds.left + this.tipBoundsWindow.scrollX,
363
+ height: this.tipBoundsWindow.innerHeight,
364
+ width: this.tipBoundsWindow.innerWidth
365
+ });
366
+ }
367
+ clearRect() {
368
+ var _this_rect;
369
+ (_this_rect = this.rect) === null || _this_rect === void 0 ? void 0 : _this_rect.remove();
370
+ this.tip.invisible();
371
+ }
372
+ constructor(positionUpdate) {
373
+ this.currentElement = null;
374
+ this.currentName = void 0;
375
+ this.positionUpdate = positionUpdate;
376
+ const currentWindow = window.__REACT_DEVTOOLS_TARGET_WINDOW__ || window;
377
+ this.window = currentWindow;
378
+ const tipBoundsWindow = window.__REACT_DEVTOOLS_TARGET_WINDOW__ || window;
379
+ this.tipBoundsWindow = tipBoundsWindow;
380
+ const doc = currentWindow.document;
381
+ this.container = doc.createElement("div");
382
+ this.container.style.zIndex = "9999999";
383
+ this.tip = new OverlayTip(doc, this.container);
384
+ this.rect = null;
385
+ let rafId = null;
386
+ this.resizeObserver = new ResizeObserver(() => {
387
+ var _this_positionUpdate, _this;
388
+ if (rafId) {
389
+ cancelAnimationFrame(rafId);
390
+ }
391
+ rafId = requestAnimationFrame(() => {
392
+ if (this.currentElement) {
393
+ this.updatePosition(this.currentElement);
394
+ }
395
+ });
396
+ (_this_positionUpdate = (_this = this).positionUpdate) === null || _this_positionUpdate === void 0 ? void 0 : _this_positionUpdate.call(_this);
397
+ });
398
+ doc.body.appendChild(this.container);
399
+ }
400
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./use-layout-effect";
2
+ export * from "./use-effect-event";
3
+ export * from "./use-mouse";
@@ -0,0 +1,15 @@
1
+ import { useMemo, useRef } from "react";
2
+ export const useEffectEvent = (callback) => {
3
+ const callbackRef = useRef(callback);
4
+ callbackRef.current = useMemo(() => callback, [
5
+ callback
6
+ ]);
7
+ const stableRef = useRef();
8
+ if (!stableRef.current) {
9
+ stableRef.current = function(...args) {
10
+ var _callbackRef_current;
11
+ return (_callbackRef_current = callbackRef.current) === null || _callbackRef_current === void 0 ? void 0 : _callbackRef_current.apply(this, args);
12
+ };
13
+ }
14
+ return stableRef.current;
15
+ };
@@ -0,0 +1,3 @@
1
+ var _window_document, _window;
2
+ import { useEffect, useLayoutEffect as _useLayoutEffect } from "react";
3
+ export const useLayoutEffect = typeof window !== "undefined" && ((_window = window) === null || _window === void 0 ? void 0 : (_window_document = _window.document) === null || _window_document === void 0 ? void 0 : _window_document.createElement) ? _useLayoutEffect : useEffect;
@@ -0,0 +1,22 @@
1
+ import { useEffect, useRef } from "react";
2
+ export const useMousePosition = ({ disable }) => {
3
+ const mouseRef = useRef({
4
+ x: 0,
5
+ y: 0
6
+ });
7
+ const recordMousePoint = (ev) => {
8
+ mouseRef.current.x = ev.clientX;
9
+ mouseRef.current.y = ev.clientY;
10
+ };
11
+ useEffect(() => {
12
+ if (!disable) {
13
+ document.addEventListener("mousemove", recordMousePoint, true);
14
+ }
15
+ return () => {
16
+ document.removeEventListener("mousemove", recordMousePoint, true);
17
+ };
18
+ }, [
19
+ disable
20
+ ]);
21
+ return mouseRef;
22
+ };
@@ -0,0 +1 @@
1
+ export { Inspector } from "./Inspector";