@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,397 @@
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
+ import { jsx as _jsx } from "react/jsx-runtime";
54
+ import { useState, useRef, useCallback, useEffect } from "react";
55
+ import { Inspector } from "../Inspector";
56
+ import { ClickOverlay } from "../Inspector/Overlay";
57
+ import { cx, postMessage, isIncomingMessage, calculateFontSizeInfo, calculateFontWeightInfo, calculateBorderRadiusInfo, calculateBorderWidthInfo, calculateTextAlignInfo, calculatePaddingInfo, calculateMarginInfo, calculateColorInfo, calculateBackgroundColorInfo, calculateBorderColorInfo } from "../utils";
58
+ import "./MiaodaInspector.css";
59
+ import { getAllStyleConfigs } from "../utils/config-mapper";
60
+ function safeClassName(el) {
61
+ const cn = el === null || el === void 0 ? void 0 : el.className;
62
+ if (typeof cn === "string") {
63
+ return cn;
64
+ }
65
+ if (typeof (cn === null || cn === void 0 ? void 0 : cn.baseVal) === "string") {
66
+ return cn.baseVal;
67
+ }
68
+ return "";
69
+ }
70
+ function analyzeTextOnlyNode(element, componentName, canUseNewInspector) {
71
+ if (!element || componentName && componentName[0] === componentName[0].toUpperCase() || !canUseNewInspector || element instanceof SVGElement) {
72
+ return {
73
+ hasOnlyTextNodes: false,
74
+ textContent: null
75
+ };
76
+ }
77
+ try {
78
+ const childNodes = Array.from(element.childNodes);
79
+ const textNodes = childNodes.filter((node) => node.nodeType === Node.TEXT_NODE);
80
+ const hasOnlyText = childNodes.length > 0 && textNodes.length === childNodes.length;
81
+ return {
82
+ hasOnlyTextNodes: hasOnlyText,
83
+ textContent: hasOnlyText ? element.textContent : null
84
+ };
85
+ } catch (e) {
86
+ console.error("Error analyzing text-only node:", e);
87
+ }
88
+ return {
89
+ hasOnlyTextNodes: false,
90
+ textContent: null
91
+ };
92
+ }
93
+ const getElementCoordinates = (element) => {
94
+ const boundingClientRect = element === null || element === void 0 ? void 0 : element.getBoundingClientRect();
95
+ const computedStyle = element ? window.getComputedStyle(element) : null;
96
+ const marginLeft = computedStyle ? parseFloat(computedStyle.marginLeft) || 0 : 0;
97
+ const marginRight = computedStyle ? parseFloat(computedStyle.marginRight) || 0 : 0;
98
+ const marginTop = computedStyle ? parseFloat(computedStyle.marginTop) || 0 : 0;
99
+ const marginBottom = computedStyle ? parseFloat(computedStyle.marginBottom) || 0 : 0;
100
+ const totalWidth = boundingClientRect ? boundingClientRect.width + marginLeft + marginRight : 0;
101
+ const totalHeight = boundingClientRect ? boundingClientRect.height + marginTop + marginBottom : 0;
102
+ const adjustedTop = boundingClientRect ? boundingClientRect.top - marginTop : 0;
103
+ const adjustedLeft = boundingClientRect ? boundingClientRect.left - marginLeft : 0;
104
+ const adjustedRight = boundingClientRect ? boundingClientRect.right + marginRight : 0;
105
+ const adjustedBottom = boundingClientRect ? boundingClientRect.bottom + marginBottom : 0;
106
+ return {
107
+ width: totalWidth,
108
+ height: totalHeight,
109
+ top: adjustedTop,
110
+ left: adjustedLeft,
111
+ right: adjustedRight,
112
+ bottom: adjustedBottom
113
+ };
114
+ };
115
+ const selectedElementMap = /* @__PURE__ */ new Map();
116
+ function elementInfoToKey(relativePath, lineNumber, columnNumber) {
117
+ return `${relativePath}_${lineNumber}_${columnNumber}`;
118
+ }
119
+ const enableInner = (() => {
120
+ var _localStorage;
121
+ return ((_localStorage = localStorage) === null || _localStorage === void 0 ? void 0 : _localStorage.getItem("miaoda-inspector-inner")) === "true";
122
+ })();
123
+ export function MiaodaInspector(props) {
124
+ const [_active, setActive] = useState(false);
125
+ const [inBody, setInBody] = useState(true);
126
+ const active = (_active || enableInner) && inBody;
127
+ const canUseNewInspector = useRef(false);
128
+ const clickOverlayRef = useRef(null);
129
+ const defaultUIConfig = props.theme.themeToken;
130
+ const styleConfigs = getAllStyleConfigs(defaultUIConfig);
131
+ const mouseleave = useCallback(() => {
132
+ setInBody(false);
133
+ }, []);
134
+ const mouseenter = useCallback(() => {
135
+ setInBody(true);
136
+ }, []);
137
+ const handleMessage = useCallback((event) => {
138
+ const { data } = event;
139
+ if (isIncomingMessage(data, "InitConfig")) {
140
+ const { inspectorActive, userToken, appId, inspectorVersion } = data.data;
141
+ if (inspectorActive !== void 0) {
142
+ setActive(inspectorActive);
143
+ if (inspectorActive === false) {
144
+ var _clickOverlayRef_current;
145
+ (_clickOverlayRef_current = clickOverlayRef.current) === null || _clickOverlayRef_current === void 0 ? void 0 : _clickOverlayRef_current.remove();
146
+ clickOverlayRef.current = null;
147
+ }
148
+ }
149
+ if (userToken !== void 0) {
150
+ window.MIAODA_BUILTIN_TTT = userToken;
151
+ }
152
+ if (appId !== void 0) {
153
+ window.MIAODA_APP_ID = appId;
154
+ }
155
+ postMessage({
156
+ type: "InspectorUIConfig",
157
+ data: defaultUIConfig
158
+ });
159
+ if (inspectorVersion === "2") {
160
+ canUseNewInspector.current = true;
161
+ }
162
+ } else if (isIncomingMessage(data, "ClearSelected")) {
163
+ var _clickOverlayRef_current1;
164
+ const { keepModifications = false } = data.data;
165
+ (_clickOverlayRef_current1 = clickOverlayRef.current) === null || _clickOverlayRef_current1 === void 0 ? void 0 : _clickOverlayRef_current1.remove();
166
+ clickOverlayRef.current = null;
167
+ if (canUseNewInspector.current && !keepModifications) {
168
+ for (const [_, selectedElement] of selectedElementMap.entries()) {
169
+ if (selectedElement.element) {
170
+ if (selectedElement.originalState.textContent) {
171
+ selectedElement.element.textContent = selectedElement.originalState.textContent;
172
+ }
173
+ if (selectedElement.originalState.className && !(selectedElement.element instanceof SVGElement)) {
174
+ selectedElement.element.className = selectedElement.originalState.className;
175
+ }
176
+ }
177
+ if (selectedElement.dispose) {
178
+ selectedElement.dispose();
179
+ }
180
+ }
181
+ selectedElementMap.clear();
182
+ }
183
+ } else if (isIncomingMessage(data, "EditText")) {
184
+ const { elementId, textContent } = data.data;
185
+ const selectedElement = selectedElementMap.get(elementId);
186
+ if (selectedElement && selectedElement.element) {
187
+ selectedElement.element.textContent = textContent;
188
+ }
189
+ } else if (isIncomingMessage(data, "EditStyle")) {
190
+ const { elementId, styles } = data.data;
191
+ const selectedElement = selectedElementMap.get(elementId);
192
+ if (selectedElement && selectedElement.element) {
193
+ const { element } = selectedElement;
194
+ for (const [property] of Object.entries(styles)) {
195
+ if (!(property in selectedElement.originalState.styles)) {
196
+ selectedElement.originalState.styles[property] = element.style.getPropertyValue(property);
197
+ }
198
+ }
199
+ for (const [property, value] of Object.entries(styles)) {
200
+ element.style.setProperty(property, value);
201
+ }
202
+ const existingDispose = selectedElement.dispose;
203
+ const styleDispose = () => {
204
+ for (const [property, value] of Object.entries(selectedElement.originalState.styles)) {
205
+ if (value) {
206
+ element.style.setProperty(property, value);
207
+ } else {
208
+ element.style.removeProperty(property);
209
+ }
210
+ }
211
+ };
212
+ selectedElement.dispose = () => {
213
+ styleDispose();
214
+ if (existingDispose) {
215
+ existingDispose();
216
+ }
217
+ };
218
+ }
219
+ } else if (isIncomingMessage(data, "EditClassName")) {
220
+ const { elementId, className } = data.data;
221
+ const selectedElement = selectedElementMap.get(elementId);
222
+ if (selectedElement && selectedElement.element) {
223
+ const { element } = selectedElement;
224
+ if (selectedElement.originalState.className === void 0) {
225
+ selectedElement.originalState.className = safeClassName(element);
226
+ }
227
+ element.className = cx(element.className, className);
228
+ const existingDispose = selectedElement.dispose;
229
+ const classDispose = () => {
230
+ if (selectedElement.originalState.className) {
231
+ element.className = selectedElement.originalState.className;
232
+ }
233
+ };
234
+ selectedElement.dispose = () => {
235
+ classDispose();
236
+ if (existingDispose) {
237
+ existingDispose();
238
+ }
239
+ };
240
+ }
241
+ }
242
+ }, [
243
+ defaultUIConfig
244
+ ]);
245
+ useEffect(() => {
246
+ document.body.addEventListener("mouseleave", mouseleave, false);
247
+ document.body.addEventListener("mouseenter", mouseenter, false);
248
+ const handleScroll = () => {
249
+ var _clickOverlayRef_current;
250
+ (_clickOverlayRef_current = clickOverlayRef.current) === null || _clickOverlayRef_current === void 0 ? void 0 : _clickOverlayRef_current.remove();
251
+ clickOverlayRef.current = null;
252
+ postMessage({
253
+ type: "PageScroll",
254
+ data: {}
255
+ });
256
+ };
257
+ window.addEventListener("scroll", handleScroll, true);
258
+ return () => {
259
+ document.body.removeEventListener("mouseleave", mouseleave, false);
260
+ document.body.removeEventListener("mouseenter", mouseenter, false);
261
+ window.removeEventListener("scroll", handleScroll, true);
262
+ };
263
+ }, []);
264
+ useEffect(() => {
265
+ window.addEventListener("message", handleMessage);
266
+ return () => {
267
+ window.removeEventListener("message", handleMessage);
268
+ };
269
+ }, []);
270
+ useEffect(() => {
271
+ postMessage({
272
+ type: "PageMounted",
273
+ data: {
274
+ version: "0.1.0",
275
+ theme: props.theme
276
+ }
277
+ });
278
+ }, []);
279
+ const selectInspectorElement = useCallback((params) => {
280
+ var _clickOverlayRef_current;
281
+ const { codeInfo, element, name, title } = params;
282
+ const coords = getElementCoordinates(element);
283
+ const textInfo = analyzeTextOnlyNode(element, name, canUseNewInspector.current);
284
+ const fontSizeInfo = calculateFontSizeInfo(element, name, canUseNewInspector.current, styleConfigs);
285
+ const fontWeightInfo = calculateFontWeightInfo(element, name, canUseNewInspector.current, styleConfigs);
286
+ const borderRadiusInfo = calculateBorderRadiusInfo(element, name, canUseNewInspector.current, styleConfigs);
287
+ const borderWidthInfo = calculateBorderWidthInfo(element, name, canUseNewInspector.current, styleConfigs);
288
+ const textAlignInfo = calculateTextAlignInfo(element, name, canUseNewInspector.current, styleConfigs);
289
+ const paddingInfo = calculatePaddingInfo(element, name, canUseNewInspector.current, styleConfigs);
290
+ const marginInfo = calculateMarginInfo(element, name, canUseNewInspector.current, styleConfigs);
291
+ const colorInfo = calculateColorInfo(element, name, canUseNewInspector.current, styleConfigs);
292
+ const backgroundColorInfo = calculateBackgroundColorInfo(element, name, canUseNewInspector.current, styleConfigs);
293
+ const borderColorInfo = calculateBorderColorInfo(element, name, canUseNewInspector.current, styleConfigs);
294
+ const id = elementInfoToKey(codeInfo.relativePath, codeInfo.lineNumber, codeInfo.columnNumber);
295
+ const selectElement = selectedElementMap.has(id);
296
+ if (!selectElement) {
297
+ selectedElementMap.set(id, {
298
+ elementId: id,
299
+ element,
300
+ originalState: {
301
+ textContent: textInfo.textContent,
302
+ className: safeClassName(element),
303
+ styles: {}
304
+ }
305
+ });
306
+ }
307
+ if (textInfo.hasOnlyTextNodes && canUseNewInspector.current) {
308
+ element.setAttribute("contenteditable", "true");
309
+ element.focus();
310
+ const TextUpdate = () => {
311
+ postMessage({
312
+ type: "TextUpdate",
313
+ data: {
314
+ elementId: id,
315
+ textContent: element.textContent
316
+ }
317
+ });
318
+ };
319
+ const dispose = () => {
320
+ element.removeAttribute("contenteditable");
321
+ element.removeEventListener("input", TextUpdate);
322
+ };
323
+ element.addEventListener("input", TextUpdate);
324
+ const _selectElement = selectedElementMap.get(id);
325
+ if (_selectElement) {
326
+ selectedElementMap.set(id, _object_spread_props(_object_spread({}, _selectElement), {
327
+ dispose
328
+ }));
329
+ }
330
+ }
331
+ var _element_id;
332
+ postMessage({
333
+ type: "SelectInspectorElement",
334
+ data: _object_spread_props(_object_spread({}, codeInfo), {
335
+ // 移除打头的 /
336
+ relativePath: codeInfo.relativePath,
337
+ tagName: element === null || element === void 0 ? void 0 : element.tagName,
338
+ componentName: name,
339
+ className: safeClassName(element),
340
+ id: (_element_id = element === null || element === void 0 ? void 0 : element.id) !== null && _element_id !== void 0 ? _element_id : "",
341
+ x: coords.left,
342
+ y: coords.top,
343
+ width: coords.width,
344
+ height: coords.height,
345
+ top: coords.top,
346
+ right: coords.right,
347
+ bottom: coords.bottom,
348
+ left: coords.left,
349
+ elementId: id,
350
+ textContent: textInfo.hasOnlyTextNodes ? textInfo.textContent : null,
351
+ fontSize: fontSizeInfo,
352
+ fontWeight: fontWeightInfo,
353
+ borderRadius: borderRadiusInfo,
354
+ borderWidth: borderWidthInfo,
355
+ textAlign: textAlignInfo,
356
+ padding: paddingInfo,
357
+ margin: marginInfo,
358
+ color: colorInfo,
359
+ backgroundColor: backgroundColorInfo,
360
+ borderColor: borderColorInfo,
361
+ metadata: codeInfo.metadata
362
+ })
363
+ });
364
+ (_clickOverlayRef_current = clickOverlayRef.current) === null || _clickOverlayRef_current === void 0 ? void 0 : _clickOverlayRef_current.remove();
365
+ clickOverlayRef.current = null;
366
+ const clickOverlay = new ClickOverlay(() => {
367
+ const _coords = getElementCoordinates(element);
368
+ postMessage({
369
+ type: "ElementResize",
370
+ data: _object_spread({
371
+ elementId: id
372
+ }, _coords)
373
+ });
374
+ });
375
+ clickOverlayRef.current = clickOverlay;
376
+ clickOverlay.highlight(element, title);
377
+ }, []);
378
+ const handleCodeInfo = useCallback((name, info) => {
379
+ const relativePath = (info === null || info === void 0 ? void 0 : info.startsWith(props.cwd)) ? info.replace(props.cwd, "") : info;
380
+ var _relativePath_replace;
381
+ return [
382
+ name,
383
+ (_relativePath_replace = relativePath === null || relativePath === void 0 ? void 0 : relativePath.replace(/^\//, "")) !== null && _relativePath_replace !== void 0 ? _relativePath_replace : ""
384
+ ];
385
+ }, [
386
+ props.cwd
387
+ ]);
388
+ return /* @__PURE__ */ _jsx(Inspector, {
389
+ disable: false,
390
+ active,
391
+ onClickElement: selectInspectorElement,
392
+ handleCodeInfo,
393
+ onActiveChange: (newActive) => {
394
+ setActive(newActive);
395
+ }
396
+ });
397
+ }
@@ -0,0 +1 @@
1
+ export { MiaodaInspector } from "./MiaodaInspector";