@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
@@ -0,0 +1,2 @@
1
+ export { MiaodaInspector } from "./MiaodaInspector";
2
+ export { isOutgoingMessage } from "./utils";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { InitConfigMessage, ClearSelectedMessage, EditTextMessage, EditStyleMessage, EditClassNameMessage, OutgoingMessage, PageMountedMessage, TextUpdateMessage, SelectInspectorElementMessage, IncomingMessage, InspectorInfo } from "./iframe-events";
@@ -0,0 +1,163 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
14
+ function _object_spread(target) {
15
+ for (var i = 1; i < arguments.length; i++) {
16
+ var source = arguments[i] != null ? arguments[i] : {};
17
+ var ownKeys2 = Object.keys(source);
18
+ if (typeof Object.getOwnPropertySymbols === "function") {
19
+ ownKeys2 = ownKeys2.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
+ }));
22
+ }
23
+ ownKeys2.forEach(function(key) {
24
+ _define_property(target, key, source[key]);
25
+ });
26
+ }
27
+ return target;
28
+ }
29
+ function ownKeys(object, enumerableOnly) {
30
+ var keys = Object.keys(object);
31
+ if (Object.getOwnPropertySymbols) {
32
+ var symbols = Object.getOwnPropertySymbols(object);
33
+ if (enumerableOnly) {
34
+ symbols = symbols.filter(function(sym) {
35
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
+ });
37
+ }
38
+ keys.push.apply(keys, symbols);
39
+ }
40
+ return keys;
41
+ }
42
+ function _object_spread_props(target, source) {
43
+ source = source != null ? source : {};
44
+ if (Object.getOwnPropertyDescriptors) {
45
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
+ } else {
47
+ ownKeys(Object(source)).forEach(function(key) {
48
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
+ });
50
+ }
51
+ return target;
52
+ }
53
+ function getColorStyleCalculationConfig(config, propertyName) {
54
+ const prefixMap = {
55
+ color: "text",
56
+ backgroundColor: "bg",
57
+ borderColor: "border"
58
+ };
59
+ const prefix = prefixMap[propertyName];
60
+ if (!prefix) {
61
+ throw new Error(`Unsupported color property: ${propertyName}`);
62
+ }
63
+ const map = {};
64
+ for (const [tokenName, colorValue] of Object.entries(config.tokens)) {
65
+ if (!tokenName.includes("-") || !colorValue.startsWith("#")) {
66
+ continue;
67
+ }
68
+ const tailwindClass = `${prefix}-${tokenName}`;
69
+ map[colorValue.toLowerCase()] = tailwindClass;
70
+ }
71
+ const colorConfig = config.common[propertyName];
72
+ if (colorConfig.type === "color-picker") {
73
+ for (const colorGroup of colorConfig.recommendColors) {
74
+ for (const colorOption of colorGroup.options) {
75
+ if (colorOption.type === "tailwind" && colorOption.rawValue) {
76
+ map[colorOption.rawValue.toLowerCase()] = colorOption.value;
77
+ }
78
+ }
79
+ }
80
+ }
81
+ let cssProperty;
82
+ if (propertyName === "color") {
83
+ cssProperty = "color";
84
+ } else if (propertyName === "backgroundColor") {
85
+ cssProperty = "backgroundColor";
86
+ } else {
87
+ cssProperty = "borderColor";
88
+ }
89
+ return {
90
+ cssProperty,
91
+ tailwindPrefix: prefix,
92
+ map,
93
+ transformValue: (value) => value.toLowerCase()
94
+ };
95
+ }
96
+ export function getStyleCalculationConfig(config, propertyName) {
97
+ const uiComponent = config.common[propertyName];
98
+ if (uiComponent.type === "color-picker") {
99
+ return getColorStyleCalculationConfig(config, propertyName);
100
+ }
101
+ if (uiComponent.type !== "select" && uiComponent.type !== "spacing") {
102
+ throw new Error(`Unsupported UI component type: ${uiComponent.type} for property: ${propertyName}`);
103
+ }
104
+ const map = {};
105
+ for (const option of uiComponent.options) {
106
+ const numericValue = parseInt(option.rawValue, 10);
107
+ if (!Number.isNaN(numericValue)) {
108
+ map[numericValue] = option.value;
109
+ } else {
110
+ map[option.rawValue] = option.value;
111
+ }
112
+ }
113
+ const configMap = {
114
+ fontSize: {
115
+ cssProperty: "fontSize",
116
+ tailwindPrefix: "text",
117
+ tolerance: 2
118
+ },
119
+ fontWeight: {
120
+ cssProperty: "fontWeight",
121
+ tailwindPrefix: "font",
122
+ transformValue: (value) => parseInt(value, 10)
123
+ },
124
+ borderRadius: {
125
+ cssProperty: "borderRadius",
126
+ tailwindPrefix: "rounded"
127
+ },
128
+ borderWidth: {
129
+ cssProperty: "borderWidth",
130
+ tailwindPrefix: "border"
131
+ },
132
+ textAlign: {
133
+ cssProperty: "textAlign",
134
+ tailwindPrefix: "text",
135
+ transformValue: (value) => value
136
+ },
137
+ padding: {
138
+ cssProperty: "padding",
139
+ tailwindPrefix: "p",
140
+ skipTailwind: true
141
+ },
142
+ margin: {
143
+ cssProperty: "margin",
144
+ tailwindPrefix: "m",
145
+ skipTailwind: true
146
+ }
147
+ };
148
+ const baseConfig = configMap[propertyName];
149
+ if (!baseConfig) {
150
+ throw new Error(`Unknown property: ${propertyName}`);
151
+ }
152
+ return _object_spread_props(_object_spread({}, baseConfig), {
153
+ map
154
+ });
155
+ }
156
+ export function getAllStyleConfigs(config) {
157
+ const result = {};
158
+ const keys = Object.keys(config.common);
159
+ for (const key of keys) {
160
+ result[key] = getStyleCalculationConfig(config, key);
161
+ }
162
+ return result;
163
+ }
@@ -0,0 +1,5 @@
1
+ import clsx from "clsx";
2
+ import { twMerge } from "tailwind-merge";
3
+ export function cx(...args) {
4
+ return twMerge(clsx(...args));
5
+ }
@@ -0,0 +1,3 @@
1
+ export { getPreviewParentOrigin, postMessage, isIncomingMessage, isOutgoingMessage } from "./origin";
2
+ export { cx } from "./css";
3
+ export { calculateFontSizeInfo, calculateFontWeightInfo, calculateBorderRadiusInfo, calculateBorderWidthInfo, calculateTextAlignInfo, calculatePaddingInfo, calculateMarginInfo, calculateColorInfo, calculateBackgroundColorInfo, calculateBorderColorInfo } from "./style-calculator";
@@ -0,0 +1,19 @@
1
+ export function getPreviewParentOrigin() {
2
+ const { origin } = window.location;
3
+ if (origin.includes("feishuapp.cn")) {
4
+ return "https://miaoda.feishu.cn";
5
+ }
6
+ if (origin.includes("fsapp.kundou.cn")) {
7
+ return "https://miaoda.feishu-pre.cn";
8
+ }
9
+ return "https://miaoda.feishu-boe.cn";
10
+ }
11
+ export function postMessage(message, targetOrigin) {
12
+ window.parent.postMessage(message, targetOrigin !== null && targetOrigin !== void 0 ? targetOrigin : getPreviewParentOrigin());
13
+ }
14
+ export function isOutgoingMessage(msg, type) {
15
+ return msg.type === type;
16
+ }
17
+ export function isIncomingMessage(msg, type) {
18
+ return msg.type === type;
19
+ }
@@ -0,0 +1,158 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
14
+ function _object_spread(target) {
15
+ for (var i = 1; i < arguments.length; i++) {
16
+ var source = arguments[i] != null ? arguments[i] : {};
17
+ var ownKeys2 = Object.keys(source);
18
+ if (typeof Object.getOwnPropertySymbols === "function") {
19
+ ownKeys2 = ownKeys2.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
+ }));
22
+ }
23
+ ownKeys2.forEach(function(key) {
24
+ _define_property(target, key, source[key]);
25
+ });
26
+ }
27
+ return target;
28
+ }
29
+ function ownKeys(object, enumerableOnly) {
30
+ var keys = Object.keys(object);
31
+ if (Object.getOwnPropertySymbols) {
32
+ var symbols = Object.getOwnPropertySymbols(object);
33
+ if (enumerableOnly) {
34
+ symbols = symbols.filter(function(sym) {
35
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
+ });
37
+ }
38
+ keys.push.apply(keys, symbols);
39
+ }
40
+ return keys;
41
+ }
42
+ function _object_spread_props(target, source) {
43
+ source = source != null ? source : {};
44
+ if (Object.getOwnPropertyDescriptors) {
45
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
+ } else {
47
+ ownKeys(Object(source)).forEach(function(key) {
48
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
+ });
50
+ }
51
+ return target;
52
+ }
53
+ export function rgbaToHex(rgba) {
54
+ const match = rgba.match(/rgba?\(([^)]+)\)/);
55
+ if (!match)
56
+ return rgba;
57
+ const values = match[1].split(",").map((v) => v.trim());
58
+ const r = parseInt(values[0], 10);
59
+ const g = parseInt(values[1], 10);
60
+ const b = parseInt(values[2], 10);
61
+ const toHex = (n) => {
62
+ const hex = n.toString(16);
63
+ return hex.length === 1 ? `0${hex}` : hex;
64
+ };
65
+ if (values.length === 4) {
66
+ const a = parseFloat(values[3]);
67
+ if (!Number.isNaN(a) && a < 1) {
68
+ const alphaHex = Math.round(a * 255).toString(16);
69
+ const alphaHexStr = alphaHex.length === 1 ? `0${alphaHex}` : alphaHex;
70
+ return `#${toHex(r)}${toHex(g)}${toHex(b)}${alphaHexStr}`;
71
+ }
72
+ }
73
+ return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
74
+ }
75
+ function isValidElement(element, componentName, canUseNewInspector) {
76
+ return !!(element && componentName && componentName[0] !== componentName[0].toUpperCase() && canUseNewInspector && !(element instanceof SVGElement));
77
+ }
78
+ function isValidElementWithComponent(element, componentName, canUseNewInspector) {
79
+ return !!(element && componentName && canUseNewInspector && !(element instanceof SVGElement));
80
+ }
81
+ function shouldBeInherited(element, cssProperty, tailwindPattern) {
82
+ const hasDirectStyle = element.style[cssProperty] !== "";
83
+ const hasTailwindClass = element.className.match(tailwindPattern);
84
+ return !hasDirectStyle && !hasTailwindClass;
85
+ }
86
+ function findClosestMatch(value, map, tolerance = 2) {
87
+ let closestClass = Object.values(map)[0];
88
+ let minDiff = Infinity;
89
+ for (const [px, className] of Object.entries(map)) {
90
+ const diff = Math.abs(value - Number(px));
91
+ if (diff < minDiff) {
92
+ minDiff = diff;
93
+ closestClass = className;
94
+ }
95
+ }
96
+ return {
97
+ className: minDiff <= tolerance ? closestClass : `raw:${value}`,
98
+ isExact: minDiff <= tolerance
99
+ };
100
+ }
101
+ function calculateStyleProperty(element, config) {
102
+ const computedStyle = window.getComputedStyle(element);
103
+ const currentValue = computedStyle[config.cssProperty];
104
+ if (!currentValue) {
105
+ return null;
106
+ }
107
+ let tailwindClassName;
108
+ if (config.cssProperty === "textAlign" || config.skipTailwind) {
109
+ tailwindClassName = config.map[currentValue] || `raw:${currentValue}`;
110
+ } else if (config.cssProperty === "color" || config.cssProperty === "backgroundColor" || config.cssProperty === "borderColor") {
111
+ const normalizedColor = rgbaToHex(currentValue);
112
+ const colorValue = config.transformValue ? config.transformValue(normalizedColor) : normalizedColor;
113
+ tailwindClassName = config.map[colorValue] || `raw:${normalizedColor}`;
114
+ } else {
115
+ const numericValue = config.transformValue ? config.transformValue(currentValue) : parseFloat(currentValue);
116
+ const { className } = findClosestMatch(numericValue, config.map, config.tolerance);
117
+ tailwindClassName = className;
118
+ }
119
+ const inherited = config.tailwindPattern ? shouldBeInherited(element, config.cssProperty, config.tailwindPattern) : false;
120
+ return {
121
+ value: currentValue,
122
+ inherited,
123
+ tailwindClassName
124
+ };
125
+ }
126
+ export function calculateStyle(styleName, element, componentName, canUseNewInspector, styleConfigs) {
127
+ if ((styleName === "fontSize" || styleName === "fontWeight" || styleName === "color" || styleName === "textAlign") && !isValidElement(element, componentName, canUseNewInspector)) {
128
+ return null;
129
+ }
130
+ if (!isValidElementWithComponent(element, componentName, canUseNewInspector)) {
131
+ return null;
132
+ }
133
+ const config = styleConfigs[styleName];
134
+ const tailwindPattern = new RegExp(`\\b${config.tailwindPrefix}-(${Object.keys(config.map).join("|")})\\b`);
135
+ return calculateStyleProperty(element, _object_spread_props(_object_spread({}, config), {
136
+ tailwindPattern
137
+ }));
138
+ }
139
+ export const calculateFontSizeInfo = (element, componentName, canUseNewInspector, styleConfigs) => calculateStyle("fontSize", element, componentName, canUseNewInspector, styleConfigs);
140
+ export const calculateFontWeightInfo = (element, componentName, canUseNewInspector, styleConfigs) => calculateStyle("fontWeight", element, componentName, canUseNewInspector, styleConfigs);
141
+ export const calculateBorderRadiusInfo = (element, componentName, canUseNewInspector, styleConfigs) => calculateStyle("borderRadius", element, componentName, canUseNewInspector, styleConfigs);
142
+ export const calculateBorderWidthInfo = (element, componentName, canUseNewInspector, styleConfigs) => calculateStyle("borderWidth", element, componentName, canUseNewInspector, styleConfigs);
143
+ export const calculateTextAlignInfo = (element, componentName, canUseNewInspector, styleConfigs) => calculateStyle("textAlign", element, componentName, canUseNewInspector, styleConfigs);
144
+ export const calculatePaddingInfo = (element, componentName, canUseNewInspector, styleConfigs) => {
145
+ if (!isValidElementWithComponent(element, componentName, canUseNewInspector)) {
146
+ return null;
147
+ }
148
+ return calculateStyleProperty(element, styleConfigs.padding);
149
+ };
150
+ export const calculateMarginInfo = (element, componentName, canUseNewInspector, styleConfigs) => {
151
+ if (!isValidElementWithComponent(element, componentName, canUseNewInspector)) {
152
+ return null;
153
+ }
154
+ return calculateStyleProperty(element, styleConfigs.margin);
155
+ };
156
+ export const calculateColorInfo = (element, componentName, canUseNewInspector, styleConfigs) => calculateStyle("color", element, componentName, canUseNewInspector, styleConfigs);
157
+ export const calculateBackgroundColorInfo = (element, componentName, canUseNewInspector, styleConfigs) => calculateStyle("backgroundColor", element, componentName, canUseNewInspector, styleConfigs);
158
+ export const calculateBorderColorInfo = (element, componentName, canUseNewInspector, styleConfigs) => calculateStyle("borderColor", element, componentName, canUseNewInspector, styleConfigs);
@@ -0,0 +1,203 @@
1
+ "use client";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ function _export(target, all) {
7
+ for (var name in all)
8
+ Object.defineProperty(target, name, {
9
+ enumerable: true,
10
+ get: all[name]
11
+ });
12
+ }
13
+ _export(exports, {
14
+ defaultHotkeys: function() {
15
+ return defaultHotkeys;
16
+ },
17
+ Inspector: function() {
18
+ return Inspector;
19
+ }
20
+ });
21
+ const _jsxruntime = require("react/jsx-runtime");
22
+ const _react = require("react");
23
+ const _hotkeysjs = /* @__PURE__ */ _interop_require_default(require("hotkeys-js"));
24
+ const _hooks = require("./hooks");
25
+ const _Overlay = require("./Overlay");
26
+ const _utils = require("./utils");
27
+ const _fiber = require("./utils/fiber");
28
+ function _interop_require_default(obj) {
29
+ return obj && obj.__esModule ? obj : {
30
+ default: obj
31
+ };
32
+ }
33
+ const getParentElement = (element) => {
34
+ let parent = element;
35
+ while (parent && parent.tagName !== "HTML") {
36
+ if (parent.dataset.miaodaComponentName) {
37
+ return parent;
38
+ }
39
+ const fiber = (0, _fiber.getElementFiberUpward)(parent);
40
+ const parentFiber = parent.parentElement ? (0, _fiber.getElementFiberUpward)(parent.parentElement) : void 0;
41
+ const referenceFiber = (0, _utils.getReferenceFiber)(fiber);
42
+ const parentReferenceFiber = parentFiber ? (0, _utils.getReferenceFiber)(parentFiber) : null;
43
+ const currentName = referenceFiber === null || referenceFiber === void 0 ? void 0 : referenceFiber.pendingProps["data-miaoda-component-name"];
44
+ const parentName = parentReferenceFiber ? parentReferenceFiber.pendingProps["data-miaoda-component-name"] : null;
45
+ if (currentName && currentName !== parentName) {
46
+ return parent;
47
+ }
48
+ parent = parent.parentElement;
49
+ }
50
+ return;
51
+ };
52
+ const defaultHotkeys = () => {
53
+ var _navigator_platform;
54
+ return ((_navigator_platform = navigator.platform) === null || _navigator_platform === void 0 ? void 0 : _navigator_platform.startsWith("Mac")) ? [
55
+ "Ctrl",
56
+ "Shift",
57
+ "Command",
58
+ "C"
59
+ ] : [
60
+ "Ctrl",
61
+ "Shift",
62
+ "Alt",
63
+ "C"
64
+ ];
65
+ };
66
+ const Inspector = (props) => {
67
+ const { keys, onHoverElement, onClickElement, onInspectElement, active: controlledActive, onActiveChange, handleCodeInfo, disable = true, children } = props;
68
+ const [isActive, setActive] = (0, _react.useState)(controlledActive !== null && controlledActive !== void 0 ? controlledActive : false);
69
+ const hotkey = keys === null ? null : (keys !== null && keys !== void 0 ? keys : []).join("+");
70
+ const overlayRef = (0, _react.useRef)();
71
+ const mouseRef = (0, _hooks.useMousePosition)({
72
+ disable
73
+ });
74
+ (0, _hooks.useLayoutEffect)(() => {
75
+ if (controlledActive !== void 0) {
76
+ setActive(controlledActive);
77
+ }
78
+ }, [
79
+ controlledActive
80
+ ]);
81
+ const activate = (0, _hooks.useEffectEvent)(() => {
82
+ onActiveChange === null || onActiveChange === void 0 ? void 0 : onActiveChange(true);
83
+ if (controlledActive === void 0) {
84
+ setActive(true);
85
+ }
86
+ });
87
+ const deactivate = (0, _hooks.useEffectEvent)(() => {
88
+ onActiveChange === null || onActiveChange === void 0 ? void 0 : onActiveChange(false);
89
+ if (controlledActive === void 0) {
90
+ setActive(false);
91
+ }
92
+ });
93
+ const handleHoverElement = (0, _hooks.useEffectEvent)((element) => {
94
+ var _overlay_inspect;
95
+ const overlay = overlayRef.current;
96
+ const targetElement = getParentElement(element);
97
+ if (!targetElement) {
98
+ return;
99
+ }
100
+ const codeInfo = (0, _utils.getElementCodeInfo)(targetElement);
101
+ const relativePath = codeInfo === null || codeInfo === void 0 ? void 0 : codeInfo.relativePath;
102
+ const absolutePath = codeInfo === null || codeInfo === void 0 ? void 0 : codeInfo.absolutePath;
103
+ const { fiber, name, title } = (0, _utils.getElementInspect)(targetElement);
104
+ overlay === null || overlay === void 0 ? void 0 : (_overlay_inspect = overlay.inspect) === null || _overlay_inspect === void 0 ? void 0 : _overlay_inspect.call(overlay, [
105
+ targetElement
106
+ ], title, relativePath !== null && relativePath !== void 0 ? relativePath : absolutePath);
107
+ onHoverElement === null || onHoverElement === void 0 ? void 0 : onHoverElement({
108
+ element: targetElement,
109
+ fiber,
110
+ codeInfo,
111
+ name
112
+ });
113
+ });
114
+ const handleClickElement = (0, _hooks.useEffectEvent)((element) => {
115
+ const targetElement = getParentElement(element);
116
+ if (!targetElement) {
117
+ return;
118
+ }
119
+ const codeInfo = (0, _utils.getElementCodeInfo)(targetElement);
120
+ const { fiber, name, title } = (0, _utils.getElementInspect)(targetElement);
121
+ onClickElement === null || onClickElement === void 0 ? void 0 : onClickElement({
122
+ element: targetElement,
123
+ fiber,
124
+ codeInfo,
125
+ name,
126
+ title
127
+ });
128
+ if (fiber && codeInfo) {
129
+ onInspectElement === null || onInspectElement === void 0 ? void 0 : onInspectElement({
130
+ element: targetElement,
131
+ fiber,
132
+ codeInfo,
133
+ name,
134
+ title
135
+ });
136
+ }
137
+ });
138
+ const startInspect = (0, _hooks.useEffectEvent)(() => {
139
+ if (overlayRef.current || disable)
140
+ return;
141
+ const overlay = new _Overlay.Overlay(handleCodeInfo);
142
+ overlayRef.current = overlay;
143
+ (0, _hotkeysjs.default)(`esc`, deactivate);
144
+ const stopCallback = (0, _utils.setupHighlighter)({
145
+ onPointerOver: handleHoverElement,
146
+ onClick: handleClickElement
147
+ });
148
+ overlay.setRemoveCallback(stopCallback);
149
+ const initPoint = mouseRef.current;
150
+ const initElement = document.elementFromPoint(initPoint.x, initPoint.y);
151
+ if (initElement)
152
+ handleHoverElement(initElement);
153
+ });
154
+ const stopInspect = (0, _hooks.useEffectEvent)(() => {
155
+ var _overlayRef_current;
156
+ (_overlayRef_current = overlayRef.current) === null || _overlayRef_current === void 0 ? void 0 : _overlayRef_current.remove();
157
+ overlayRef.current = void 0;
158
+ _hotkeysjs.default.unbind(`esc`, deactivate);
159
+ });
160
+ (0, _react.useEffect)(() => {
161
+ if (isActive) {
162
+ startInspect();
163
+ } else {
164
+ stopInspect();
165
+ }
166
+ return stopInspect;
167
+ }, [
168
+ isActive
169
+ ]);
170
+ (0, _react.useEffect)(() => {
171
+ const handleScroll = () => {
172
+ var _overlayRef_current;
173
+ (_overlayRef_current = overlayRef.current) === null || _overlayRef_current === void 0 ? void 0 : _overlayRef_current.clearRect();
174
+ };
175
+ window.addEventListener("scroll", handleScroll, true);
176
+ return () => {
177
+ window.removeEventListener("scroll", handleScroll, true);
178
+ };
179
+ }, []);
180
+ (0, _react.useEffect)(() => {
181
+ const handleHotKeys = () => {
182
+ if (overlayRef.current) {
183
+ deactivate();
184
+ } else {
185
+ activate();
186
+ }
187
+ };
188
+ const bindKey = hotkey === null || disable ? null : hotkey || defaultHotkeys().join("+");
189
+ if (bindKey) {
190
+ (0, _hotkeysjs.default)(bindKey, handleHotKeys);
191
+ return () => {
192
+ _hotkeysjs.default.unbind(bindKey, handleHotKeys);
193
+ };
194
+ }
195
+ return;
196
+ }, [
197
+ hotkey,
198
+ disable
199
+ ]);
200
+ return /* @__PURE__ */ (0, _jsxruntime.jsx)(_jsxruntime.Fragment, {
201
+ children: children !== null && children !== void 0 ? children : null
202
+ });
203
+ };