-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
313 lines (274 loc) · 9.75 KB
/
main.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
const clipboard = require("clipboard");
const { Color, selection } = require("scenegraph")
let panel;
let flutterCodes = "";
let selectedPaths = [];
function create() {
const HTML = `
<style>
.break {
flex-wrap: wrap;
}
label.row>span {
color: #8E8E8E;
width: 5px;
text-align: right;
font-size: 12px;
}
label.row input {
flex: 1 1 auto;
}
.show {
display: block;
}
.hide {
display: none;
}
.codeView {
height: 500px;
border: 1px solid #ccc;
font-size: 140%;
overflow: auto;
padding: 10px;
}
</style>
<form method="dialog" id="main">
<h5>Area</h5>
<div class="row break">
<label class="row">
<span>↔︎</span>
<input type="number" uxp-quiet="true" id="txtW" value="10" size=10 placeholder="Width" />
</label>
<label class="row">
<span>↕︎</span>
<input type="number" uxp-quiet="true" id="txtH" value="10" size=10 placeholder="Height" />
</label>
</div>
<label>
<input type="checkbox" id="cb_artboard" checked>Match artboard size<br>
</label>
<br>
<label>
<input type="checkbox" id="cb_style" checked>Generate style code<br>
</label>
<br><br>
<button id="ok" type="submit" uxp-variant="cta">Copy</button><br>
<h5>Flutter Code</h5><br>
<div id="flutter_code" uxp-quiet="true" class="codeView"></div>
</form>
<h5 id="warning">Need to select path to work with this plugin</h5>
`
panel = document.createElement("div");
panel.innerHTML = HTML;
panel.querySelector("form").addEventListener("submit", function () {
clipboard.copyText(flutterCodes);
console.log("Code generated and copied to clipboard");
// console.log(flutterCodes);
});
panel.querySelector("#cb_artboard").addEventListener("change", updateAreaInfo);
panel.querySelector("#cb_style").addEventListener("change", updateAreaInfo);
return panel;
}
function updateAreaInfo() {
let width = panel.querySelector("#txtW");
let height = panel.querySelector("#txtH");
let isArtboardChecked = panel.querySelector("#cb_artboard").checked;
width.readOnly = isArtboardChecked;
height.readOnly = isArtboardChecked;
let area = isArtboardChecked ? selection.focusedArtboard.localBounds : selection.items[0].localBounds;
console.log(area);
width.value = round(area.width);
height.value = round(area.height);
generatePathData(selectedPaths);
document.querySelector("#flutter_code").innerHTML = flutterCodes.trim().replace(/\n/g, "<br>");
}
function show(event) {
if (!panel) event.node.appendChild(create());
}
function update() {
const { Path } = require("scenegraph");
let form = document.querySelector("form");
let warning = document.querySelector("#warning");
if (!selection || selection.items.length == 0) {
form.className = "hide";
warning.className = "show";
}
// if (selection.items.length > 1) {
// form.className = "hide";
// warning.className = "show";
// warning.innerHTML = "You should not select mulltiple paths!";
// }
selectedPaths = [];
selection.items.forEach(element => {
if (element instanceof Path) selectedPaths.push(element);
});
if (selectedPaths.length == 0) {
form.className = "hide";
warning.className = "show";
} else {
form.className = "show";
warning.className = "hide";
updateAreaInfo();
}
}
function generatePathData(selectedPaths) {
flutterCodes = "";
let inputWidth = panel.querySelector("#txtW").value;
let inputHeight = panel.querySelector("#txtH").value;
let isStyleChecked = panel.querySelector("#cb_style").checked;
let isArtboardChecked = panel.querySelector("#cb_artboard").checked;
selectedPaths.forEach(path => {
let f = new Color(path.fill);
let fillColor = "0x" + f.a.toString(16) + f.toHex().slice(1);
let s = new Color(path.stroke);
let strokeColor = "0x" + s.a.toString(16) + s.toHex().slice(1);
let style = {
"enabled": isStyleChecked,
"fillEnabled": path.fillEnabled,
"fill": fillColor,
"strokeEnabled": path.strokeEnabled,
"stroke": strokeColor,
"strokeWidth": path.strokeWidth,
"strokeCap": path.strokeEndCaps,
"strokeJoin": path.strokeJoins
}
// console.log(style);
let segments = extractedPathData(path.pathData);
let offset = findOffsetPoint(segments);
let normalized = normalizePoints(segments, offset);
let code = generateCode(normalized, {
"reposition": isArtboardChecked,
"x": path.topLeftInParent.x,
"y": path.topLeftInParent.y,
"name": path.name.toLowerCase().replace(/ /g, "_"),
"width": isArtboardChecked ? inputWidth : round(path.localBounds.width),
"height": isArtboardChecked ? inputHeight : round(path.localBounds.height),
"style": style
});
// console.log(code);
flutterCodes += (code + "\n\n");
});
}
const extractedPathData = (rawPathStr) => {
const segments = rawPathStr.split(" ");
const output = [];
let last = -1;
for (let i = 0; i < segments.length; i++) {
if (isNaN(segments[i])) {
output.push([segments[i]]);
last++;
} else {
output[last].push(parseFloat(segments[i]));
}
}
return output;
}
const findOffsetPoint = (pathSegments) => {
let offset = [9999, 9999];
for (var id in pathSegments) {
let segment = pathSegments[id];
switch (segment[0]) {
case "M":
if (segment[1] < offset[0]) offset[0] = segment[1];
if (segment[2] < offset[1]) offset[1] = segment[2];
break;
case "L":
if (segment[1] < offset[0]) offset[0] = segment[1];
if (segment[2] < offset[1]) offset[1] = segment[2];
break;
case "C":
if (segment[5] < offset[0]) offset[0] = segment[5];
if (segment[6] < offset[1]) offset[1] = segment[6];
break;
}
}
return offset;
}
const normalizePoints = (pathData, offsetPoint) => {
let data = [];
for (var id in pathData) {
var segment = pathData[id];
data.push(segment);
if (segment.length < 2) continue;
for (let i = 1; i < segment.length; i++) {
let v = segment[i] - offsetPoint[i % 2 != 0 ? 0 : 1];
data[id][i] = Math.round(v * 100) / 100;
}
}
return data;
}
const round = (number) => { return Math.round(number * 100) / 100; }
function generateCode(path, props) {
let xs = props.name + "_xs";
let ys = props.name + "_ys";
var code = [
[" // " + props.name, 0],
["double " + xs + " = size.width / " + round(props.width) + ";", 1],
["double " + ys + " = size.height / " + round(props.height) + ";", 1],
["Path " + props.name + " = Path()", 2]
];
for (var segmentId in path) {
var segment = path[segmentId];
switch (segment[0]) {
case "M":
code.push([
" ..moveTo("
+ segment[1] + " * " + xs + ", " + segment[2] + " * " + ys + ""
+ ")"
, 1]);
break;
case "L":
code.push([
" ..lineTo("
+ segment[1] + " * " + xs + ", " + segment[2] + " * " + ys + ""
+ ")"
, 1]);
break;
case "C":
code.push([
" ..cubicTo("
+ segment[1] + " * " + xs + ", " + segment[2] + " * " + ys + ","
+ segment[3] + " * " + xs + ", " + segment[4] + " * " + ys + ","
+ segment[5] + " * " + xs + ", " + segment[6] + " * " + ys + ""
+ ")"
, 1]);
break;
case "Z":
code.push([" ..close()", 1]);
break;
}
}
code[code.length - 1][0] += ";";
let ox = round(props.x);
let oy = round(props.y);
if (props.reposition && (ox != 0.0 || oy != 0.0)) code.push([
props.name + " = " + props.name + ".shift(Offset(" + ox + " * " + xs + ", " + oy + " * " + ys + "));"
, 2]);
if (props.style.enabled && props.style.fillEnabled) code.push([
"Paint " + props.name + "_fillPaint = Paint()..style = PaintingStyle.fill..color = Color(" + props.style.fill + ");"
, 2]);
if (props.style.enabled && props.style.strokeEnabled) {
code.push(["Paint " + props.name + "_strokePaint = Paint()", 2]);
code.push([" ..style = PaintingStyle.stroke", 1]);
code.push([" ..color = Color(" + props.style.stroke + ")", 1]);
code.push([" ..strokeWidth = " + props.style.strokeWidth, 1]);
if (props.style.strokeCap != "butt") code.push([" ..strokeCap = StrokeCap." + props.style.strokeCap, 1]);
if (props.style.strokeJoin != "miter") code.push([" ..strokeJoin = StrokeJoin." + props.style.strokeJoin, 1]);
code.push([";", 0]);
}
var returnCode = "";
for (var lineId in code) {
var line = code[lineId];
for (var i = 0; i < line[1]; i++) returnCode += "\n";
returnCode += line[0];
}
return returnCode;
}
module.exports = {
panels: {
generatePathData: {
show,
update
}
}
};