@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
@@ -1,397 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export { MiaodaInspector } from "./MiaodaInspector";
package/dist/es/index.js DELETED
@@ -1,2 +0,0 @@
1
- export { MiaodaInspector } from "./MiaodaInspector";
2
- export { isOutgoingMessage } from "./utils";
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export { InitConfigMessage, ClearSelectedMessage, EditTextMessage, EditStyleMessage, EditClassNameMessage, OutgoingMessage, PageMountedMessage, TextUpdateMessage, SelectInspectorElementMessage, IncomingMessage, InspectorInfo } from "./iframe-events";
@@ -1,3 +0,0 @@
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";
@@ -1,19 +0,0 @@
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
- }
@@ -1,158 +0,0 @@
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);
File without changes
File without changes