-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathcode.ts
More file actions
110 lines (98 loc) · 3.42 KB
/
code.ts
File metadata and controls
110 lines (98 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import {
retrieveGenericLinearGradients,
retrieveGenericSolidUIColors,
} from "./common/retrieveUI/retrieveColors";
import {
addWarning,
clearWarnings,
warnings,
} from "./common/commonConversionWarnings";
import { postConversionComplete, postEmptyMessage } from "./messaging";
import { PluginSettings } from "types";
import { convertToCode } from "./common/retrieveUI/convertToCode";
import { generateHTMLPreview } from "./html/htmlMain";
import { oldConvertNodesToAltNodes } from "./altNodes/oldAltConversion";
import {
getNodeByIdAsyncCalls,
getNodeByIdAsyncTime,
getStyledTextSegmentsCalls,
getStyledTextSegmentsTime,
nodesToJSON,
processColorVariablesCalls,
processColorVariablesTime,
resetPerformanceCounters,
} from "./altNodes/jsonNodeConversion";
import { bench } from "./log";
export const run = async (settings: PluginSettings) => {
resetPerformanceCounters();
clearWarnings();
const { framework, useOldPluginVersion2025 } = settings;
const selection = figma.currentPage.selection;
if (selection.length === 0) {
postEmptyMessage();
return;
}
// Timing with Date.now() instead of console.time
const nodeToJSONStart = Date.now();
let convertedSelection: any;
if (useOldPluginVersion2025) {
convertedSelection = oldConvertNodesToAltNodes(selection, null);
console.log("convertedSelection", convertedSelection);
} else {
convertedSelection = await nodesToJSON(selection, settings);
bench(`[benchmark] nodesToJSON: ${Date.now() - nodeToJSONStart}ms`);
console.log("nodeJson", convertedSelection);
}
console.log("[debug] convertedSelection", { ...convertedSelection[0] });
// ignore when nothing was selected
// If the selection was empty, the converted selection will also be empty.
if (convertedSelection.length === 0) {
postEmptyMessage();
return;
}
const convertToCodeStart = Date.now();
const code = await convertToCode(convertedSelection, settings);
bench(
`[benchmark] convertToCode: ${Date.now() - convertToCodeStart}ms`,
);
const generatePreviewStart = Date.now();
const htmlPreview = await generateHTMLPreview(convertedSelection, settings);
bench(
`[benchmark] generateHTMLPreview: ${Date.now() - generatePreviewStart}ms`,
);
const colorPanelStart = Date.now();
const colors = retrieveGenericSolidUIColors(framework);
const gradients = retrieveGenericLinearGradients(framework);
bench(
`[benchmark] color and gradient panel: ${Date.now() - colorPanelStart}ms`,
);
bench(
`[benchmark] total generation time: ${Date.now() - nodeToJSONStart}ms`,
);
// Log performance statistics
bench(
`[benchmark] getNodeByIdAsync: ${getNodeByIdAsyncTime}ms (${getNodeByIdAsyncCalls} calls, avg: ${(getNodeByIdAsyncTime / getNodeByIdAsyncCalls || 1).toFixed(2)}ms)`,
);
bench(
`[benchmark] getStyledTextSegments: ${getStyledTextSegmentsTime}ms (${getStyledTextSegmentsCalls} calls, avg: ${
getStyledTextSegmentsCalls > 0
? (getStyledTextSegmentsTime / getStyledTextSegmentsCalls).toFixed(2)
: 0
}ms)`,
);
bench(
`[benchmark] processColorVariables: ${processColorVariablesTime}ms (${processColorVariablesCalls} calls, avg: ${
processColorVariablesCalls > 0
? (processColorVariablesTime / processColorVariablesCalls).toFixed(2)
: 0
}ms)`,
);
postConversionComplete({
code,
htmlPreview,
colors,
gradients,
settings,
warnings: [...warnings],
});
};