@lark-apaas/miaoda-inspector 0.1.0-alpha.1 → 0.1.0-alpha.2

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