@hprint/plugins 0.0.1-alpha.2 → 0.0.1-alpha.3
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.
- package/dist/index.js +17 -17
- package/dist/index.mjs +1071 -1057
- package/dist/src/plugins/AlignGuidLinePlugin.d.ts +7 -2
- package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -1
- package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -1
- package/dist/src/plugins/LockPlugin.d.ts.map +1 -1
- package/dist/src/plugins/QrCodePlugin.d.ts +5 -0
- package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/assets/style/resizePlugin.css +27 -27
- package/src/objects/Arrow.js +47 -47
- package/src/objects/ThinTailArrow.js +50 -50
- package/src/plugins/AlignGuidLinePlugin.ts +1152 -1141
- package/src/plugins/BarCodePlugin.ts +2 -2
- package/src/plugins/ControlsPlugin.ts +251 -251
- package/src/plugins/ControlsRotatePlugin.ts +111 -111
- package/src/plugins/CopyPlugin.ts +255 -255
- package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
- package/src/plugins/DrawLinePlugin.ts +162 -162
- package/src/plugins/DrawPolygonPlugin.ts +205 -205
- package/src/plugins/DringPlugin.ts +125 -125
- package/src/plugins/FlipPlugin.ts +59 -59
- package/src/plugins/FontPlugin.ts +165 -165
- package/src/plugins/FreeDrawPlugin.ts +49 -49
- package/src/plugins/GroupAlignPlugin.ts +365 -365
- package/src/plugins/GroupPlugin.ts +82 -82
- package/src/plugins/GroupTextEditorPlugin.ts +198 -198
- package/src/plugins/HistoryPlugin.ts +181 -181
- package/src/plugins/ImageStroke.ts +121 -121
- package/src/plugins/LayerPlugin.ts +108 -108
- package/src/plugins/LockPlugin.ts +242 -240
- package/src/plugins/MaskPlugin.ts +155 -155
- package/src/plugins/MaterialPlugin.ts +224 -224
- package/src/plugins/MiddleMousePlugin.ts +45 -45
- package/src/plugins/MoveHotKeyPlugin.ts +46 -46
- package/src/plugins/PathTextPlugin.ts +89 -89
- package/src/plugins/PolygonModifyPlugin.ts +224 -224
- package/src/plugins/PrintPlugin.ts +81 -81
- package/src/plugins/PsdPlugin.ts +52 -52
- package/src/plugins/QrCodePlugin.ts +322 -329
- package/src/plugins/ResizePlugin.ts +278 -278
- package/src/plugins/RulerPlugin.ts +78 -78
- package/src/plugins/SimpleClipImagePlugin.ts +244 -244
- package/src/plugins/UnitPlugin.ts +326 -326
- package/src/plugins/WaterMarkPlugin.ts +257 -257
- package/src/types/eventType.ts +11 -11
- package/src/utils/psd.js +432 -432
- package/src/utils/ruler/guideline.ts +145 -145
- package/src/utils/ruler/index.ts +91 -91
- package/src/utils/ruler/ruler.ts +924 -924
- package/src/utils/ruler/utils.ts +162 -162
- package/tsconfig.json +10 -10
- package/vite.config.ts +29 -29
package/src/utils/psd.js
CHANGED
|
@@ -1,432 +1,432 @@
|
|
|
1
|
-
import _ from 'lodash-es';
|
|
2
|
-
import { v4 as uuid } from 'uuid';
|
|
3
|
-
|
|
4
|
-
// 入口文件
|
|
5
|
-
|
|
6
|
-
async function psdToJson(psdFile) {
|
|
7
|
-
// 初始化
|
|
8
|
-
const json = getTransform();
|
|
9
|
-
|
|
10
|
-
// 设置背景
|
|
11
|
-
const { clipPath, workspase } = getClipPath(psdFile);
|
|
12
|
-
json.clipPath = clipPath;
|
|
13
|
-
json.objects.push(workspase);
|
|
14
|
-
|
|
15
|
-
// 拍平
|
|
16
|
-
const list = flattenTree(psdFile.children);
|
|
17
|
-
|
|
18
|
-
// 转换
|
|
19
|
-
const fabricJhildren = await psdChildrenTransform(list);
|
|
20
|
-
json.objects.push(...fabricJhildren.reverse());
|
|
21
|
-
|
|
22
|
-
// 输出文件
|
|
23
|
-
const jsonStr = JSON.stringify(json, null, 2);
|
|
24
|
-
return jsonStr;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async function psdChildrenTransform(children) {
|
|
28
|
-
function attrTransform(childrens) {
|
|
29
|
-
const childrensFilter = childrens.filter((item) => {
|
|
30
|
-
if (item.text) {
|
|
31
|
-
item.type = 'text';
|
|
32
|
-
}
|
|
33
|
-
return !item.isHidden;
|
|
34
|
-
});
|
|
35
|
-
return childrensFilter.map(async (item) => {
|
|
36
|
-
// 公共属性映射
|
|
37
|
-
const commonAttr = baseUtils([
|
|
38
|
-
'left',
|
|
39
|
-
'top',
|
|
40
|
-
['width', (info) => info.width + 50],
|
|
41
|
-
'height',
|
|
42
|
-
'name',
|
|
43
|
-
['visible', () => true],
|
|
44
|
-
['opacity', (info) => 255 / info.opacity],
|
|
45
|
-
['id', uuid],
|
|
46
|
-
])(item);
|
|
47
|
-
|
|
48
|
-
// 类型映射
|
|
49
|
-
const typeList = [
|
|
50
|
-
{
|
|
51
|
-
sourceType: 'Layer',
|
|
52
|
-
targetType: 'image',
|
|
53
|
-
attr: [],
|
|
54
|
-
async customTransform(item) {
|
|
55
|
-
// 图片遮罩
|
|
56
|
-
// const { maskData } = item.layerFrame.layerProperties;
|
|
57
|
-
// let clipPath;
|
|
58
|
-
// if (maskData) {
|
|
59
|
-
// clipPath = getAttrByType('rect');
|
|
60
|
-
// clipPath.left = maskData.left;
|
|
61
|
-
// clipPath.top = maskData.top;
|
|
62
|
-
// }
|
|
63
|
-
const base64 = await getLayerBse64(item);
|
|
64
|
-
return {
|
|
65
|
-
src: base64,
|
|
66
|
-
// clipPath,
|
|
67
|
-
};
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
sourceType: 'text',
|
|
72
|
-
targetType: 'textbox',
|
|
73
|
-
attr: ['text'],
|
|
74
|
-
async customTransform(item) {
|
|
75
|
-
const { FontSize, FillColor, Tracking } =
|
|
76
|
-
item.textProperties.EngineDict.StyleRun.RunArray[0]
|
|
77
|
-
.StyleSheet.StyleSheetData;
|
|
78
|
-
return {
|
|
79
|
-
charSpacing: Tracking,
|
|
80
|
-
fontFamily: '站酷快乐体',
|
|
81
|
-
fill: getColor(FillColor.Values),
|
|
82
|
-
fontSize: FontSize || 12,
|
|
83
|
-
};
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
];
|
|
87
|
-
|
|
88
|
-
const sourctTypeKey = 'type';
|
|
89
|
-
const transformTypeInfo = typeList.find(
|
|
90
|
-
(info) => info.sourceType === item[sourctTypeKey]
|
|
91
|
-
);
|
|
92
|
-
if (transformTypeInfo) {
|
|
93
|
-
const baseAttr = getAttrByType(transformTypeInfo.targetType);
|
|
94
|
-
const typeAttr = baseUtils(transformTypeInfo.attr)(item);
|
|
95
|
-
const customTransformAttr = transformTypeInfo.customTransform
|
|
96
|
-
? await transformTypeInfo.customTransform(item)
|
|
97
|
-
: null;
|
|
98
|
-
return _.assign(
|
|
99
|
-
baseAttr,
|
|
100
|
-
commonAttr,
|
|
101
|
-
baseAttr,
|
|
102
|
-
typeAttr,
|
|
103
|
-
customTransformAttr
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
return {
|
|
107
|
-
...getAttrByType('textbox'),
|
|
108
|
-
};
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
const resault = await Promise.all(attrTransform(children));
|
|
112
|
-
return resault;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function getTransform() {
|
|
116
|
-
// 初始化
|
|
117
|
-
const json = {
|
|
118
|
-
version: '5.3.0',
|
|
119
|
-
objects: [],
|
|
120
|
-
clipPath: null,
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
return json;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function baseUtils(baseAttr) {
|
|
127
|
-
return function (item) {
|
|
128
|
-
const attrs = {
|
|
129
|
-
desc: {},
|
|
130
|
-
};
|
|
131
|
-
baseAttr.forEach((attrType) => {
|
|
132
|
-
// 直接转换
|
|
133
|
-
if (typeof attrType === 'string') {
|
|
134
|
-
const value = _.get(item, attrType);
|
|
135
|
-
_.set(attrs, attrType, value);
|
|
136
|
-
} else {
|
|
137
|
-
const [key, transform] = attrType;
|
|
138
|
-
if (typeof transform === 'function') {
|
|
139
|
-
const value = transform(item);
|
|
140
|
-
if (value) {
|
|
141
|
-
_.set(attrs, key, value);
|
|
142
|
-
}
|
|
143
|
-
} else {
|
|
144
|
-
const value = _.get(item, transform);
|
|
145
|
-
_.set(attrs, key, value);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
return attrs;
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function getAttrByType(type) {
|
|
154
|
-
const typeMap = {
|
|
155
|
-
group: {
|
|
156
|
-
type: 'group',
|
|
157
|
-
version: '5.3.0',
|
|
158
|
-
originX: 'left',
|
|
159
|
-
originY: 'top',
|
|
160
|
-
fill: 'rgb(0,0,0)',
|
|
161
|
-
stroke: null,
|
|
162
|
-
strokeWidth: 0,
|
|
163
|
-
strokeDashArray: null,
|
|
164
|
-
strokeLineCap: 'butt',
|
|
165
|
-
strokeDashOffset: 0,
|
|
166
|
-
strokeLineJoin: 'miter',
|
|
167
|
-
strokeUniform: false,
|
|
168
|
-
strokeMiterLimit: 4,
|
|
169
|
-
scaleX: 1,
|
|
170
|
-
scaleY: 1,
|
|
171
|
-
flipX: false,
|
|
172
|
-
flipY: false,
|
|
173
|
-
shadow: null,
|
|
174
|
-
visible: true,
|
|
175
|
-
backgroundColor: '',
|
|
176
|
-
fillRule: 'nonzero',
|
|
177
|
-
paintFirst: 'fill',
|
|
178
|
-
globalCompositeOperation: 'source-over',
|
|
179
|
-
skewX: 0,
|
|
180
|
-
skewY: 0,
|
|
181
|
-
selectable: true,
|
|
182
|
-
hasControls: true,
|
|
183
|
-
},
|
|
184
|
-
textbox: {
|
|
185
|
-
type: 'textbox',
|
|
186
|
-
version: '5.3.0',
|
|
187
|
-
originX: 'left',
|
|
188
|
-
originY: 'top',
|
|
189
|
-
fill: 'rgb(0,0,0)',
|
|
190
|
-
stroke: null,
|
|
191
|
-
strokeWidth: 1,
|
|
192
|
-
strokeDashArray: null,
|
|
193
|
-
strokeLineCap: 'butt',
|
|
194
|
-
strokeDashOffset: 0,
|
|
195
|
-
strokeLineJoin: 'miter',
|
|
196
|
-
strokeUniform: false,
|
|
197
|
-
strokeMiterLimit: 4,
|
|
198
|
-
scaleX: 1,
|
|
199
|
-
scaleY: 1,
|
|
200
|
-
angle: 0,
|
|
201
|
-
flipX: false,
|
|
202
|
-
flipY: false,
|
|
203
|
-
shadow: '',
|
|
204
|
-
backgroundColor: '',
|
|
205
|
-
fillRule: 'nonzero',
|
|
206
|
-
paintFirst: 'fill',
|
|
207
|
-
globalCompositeOperation: 'source-over',
|
|
208
|
-
skewX: 0,
|
|
209
|
-
skewY: 0,
|
|
210
|
-
fontFamily: 'arial',
|
|
211
|
-
fontWeight: 'normal',
|
|
212
|
-
fontSize: 80,
|
|
213
|
-
text: '诸事顺遂',
|
|
214
|
-
underline: false,
|
|
215
|
-
overline: false,
|
|
216
|
-
linethrough: false,
|
|
217
|
-
textAlign: 'left',
|
|
218
|
-
fontStyle: 'normal',
|
|
219
|
-
lineHeight: 1.16,
|
|
220
|
-
textBackgroundColor: '',
|
|
221
|
-
charSpacing: 0,
|
|
222
|
-
styles: [],
|
|
223
|
-
direction: 'ltr',
|
|
224
|
-
path: null,
|
|
225
|
-
pathStartOffset: 0,
|
|
226
|
-
pathSide: 'left',
|
|
227
|
-
pathAlign: 'baseline',
|
|
228
|
-
minWidth: 20,
|
|
229
|
-
splitByGrapheme: true,
|
|
230
|
-
selectable: true,
|
|
231
|
-
hasControls: true,
|
|
232
|
-
},
|
|
233
|
-
image: {
|
|
234
|
-
type: 'image',
|
|
235
|
-
version: '5.3.0',
|
|
236
|
-
originX: 'left',
|
|
237
|
-
originY: 'top',
|
|
238
|
-
fill: 'rgb(0,0,0)',
|
|
239
|
-
stroke: null,
|
|
240
|
-
strokeWidth: 0,
|
|
241
|
-
strokeDashArray: null,
|
|
242
|
-
strokeLineCap: 'butt',
|
|
243
|
-
strokeDashOffset: 0,
|
|
244
|
-
strokeLineJoin: 'miter',
|
|
245
|
-
strokeUniform: false,
|
|
246
|
-
strokeMiterLimit: 4,
|
|
247
|
-
scaleX: 1,
|
|
248
|
-
scaleY: 1,
|
|
249
|
-
angle: 0,
|
|
250
|
-
flipX: false,
|
|
251
|
-
flipY: false,
|
|
252
|
-
shadow: null,
|
|
253
|
-
backgroundColor: '',
|
|
254
|
-
fillRule: 'nonzero',
|
|
255
|
-
paintFirst: 'fill',
|
|
256
|
-
globalCompositeOperation: 'source-over',
|
|
257
|
-
skewX: 0,
|
|
258
|
-
skewY: 0,
|
|
259
|
-
cropX: 0,
|
|
260
|
-
cropY: 0,
|
|
261
|
-
selectable: true,
|
|
262
|
-
hasControls: true,
|
|
263
|
-
src: '',
|
|
264
|
-
crossOrigin: 'anonymous',
|
|
265
|
-
filters: [],
|
|
266
|
-
},
|
|
267
|
-
rect: {
|
|
268
|
-
type: 'rect',
|
|
269
|
-
version: '5.3.0',
|
|
270
|
-
originX: 'center',
|
|
271
|
-
originY: 'center',
|
|
272
|
-
left: 0,
|
|
273
|
-
top: 0,
|
|
274
|
-
width: 226,
|
|
275
|
-
height: 302,
|
|
276
|
-
fill: 'rgb(0,0,0)',
|
|
277
|
-
stroke: null,
|
|
278
|
-
strokeWidth: 1,
|
|
279
|
-
strokeDashArray: null,
|
|
280
|
-
strokeLineCap: 'butt',
|
|
281
|
-
strokeDashOffset: 0,
|
|
282
|
-
strokeLineJoin: 'miter',
|
|
283
|
-
strokeUniform: false,
|
|
284
|
-
strokeMiterLimit: 4,
|
|
285
|
-
scaleX: 1,
|
|
286
|
-
scaleY: 1,
|
|
287
|
-
angle: 0,
|
|
288
|
-
flipX: false,
|
|
289
|
-
flipY: false,
|
|
290
|
-
opacity: 1,
|
|
291
|
-
shadow: null,
|
|
292
|
-
visible: true,
|
|
293
|
-
backgroundColor: '',
|
|
294
|
-
fillRule: 'nonzero',
|
|
295
|
-
paintFirst: 'fill',
|
|
296
|
-
globalCompositeOperation: 'source-over',
|
|
297
|
-
skewX: 0,
|
|
298
|
-
skewY: 0,
|
|
299
|
-
rx: 0,
|
|
300
|
-
ry: 0,
|
|
301
|
-
selectable: true,
|
|
302
|
-
hasControls: true,
|
|
303
|
-
inverted: false,
|
|
304
|
-
absolutePositioned: false,
|
|
305
|
-
},
|
|
306
|
-
};
|
|
307
|
-
return typeMap[type];
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
function getClipPath(psdFile) {
|
|
311
|
-
const { width, height } = psdFile;
|
|
312
|
-
const clipPath = {
|
|
313
|
-
type: 'rect',
|
|
314
|
-
version: '5.3.0',
|
|
315
|
-
originX: 'left',
|
|
316
|
-
originY: 'top',
|
|
317
|
-
left: 0,
|
|
318
|
-
top: 0,
|
|
319
|
-
width: width,
|
|
320
|
-
height: height,
|
|
321
|
-
fill: 'rgba(255,255,255,1)',
|
|
322
|
-
stroke: null,
|
|
323
|
-
strokeWidth: 0,
|
|
324
|
-
strokeDashArray: null,
|
|
325
|
-
strokeLineCap: 'butt',
|
|
326
|
-
strokeDashOffset: 0,
|
|
327
|
-
strokeLineJoin: 'miter',
|
|
328
|
-
strokeUniform: false,
|
|
329
|
-
strokeMiterLimit: 4,
|
|
330
|
-
scaleX: 1,
|
|
331
|
-
scaleY: 1,
|
|
332
|
-
angle: 0,
|
|
333
|
-
flipX: false,
|
|
334
|
-
flipY: false,
|
|
335
|
-
opacity: 1,
|
|
336
|
-
shadow: null,
|
|
337
|
-
visible: true,
|
|
338
|
-
backgroundColor: '',
|
|
339
|
-
fillRule: 'nonzero',
|
|
340
|
-
paintFirst: 'fill',
|
|
341
|
-
globalCompositeOperation: 'source-over',
|
|
342
|
-
skewX: 0,
|
|
343
|
-
skewY: 0,
|
|
344
|
-
rx: 0,
|
|
345
|
-
ry: 0,
|
|
346
|
-
selectable: true,
|
|
347
|
-
hasControls: true,
|
|
348
|
-
};
|
|
349
|
-
const workspase = {
|
|
350
|
-
type: 'rect',
|
|
351
|
-
version: '5.3.0',
|
|
352
|
-
originX: 'left',
|
|
353
|
-
originY: 'top',
|
|
354
|
-
left: 0,
|
|
355
|
-
top: 0,
|
|
356
|
-
width: width,
|
|
357
|
-
height: height,
|
|
358
|
-
fill: 'rgba(255,255,255,1)',
|
|
359
|
-
stroke: null,
|
|
360
|
-
strokeWidth: 0,
|
|
361
|
-
strokeDashArray: null,
|
|
362
|
-
strokeLineCap: 'butt',
|
|
363
|
-
strokeDashOffset: 0,
|
|
364
|
-
strokeLineJoin: 'miter',
|
|
365
|
-
strokeUniform: false,
|
|
366
|
-
strokeMiterLimit: 4,
|
|
367
|
-
scaleX: 1,
|
|
368
|
-
scaleY: 1,
|
|
369
|
-
angle: 0,
|
|
370
|
-
flipX: false,
|
|
371
|
-
flipY: false,
|
|
372
|
-
// "opacity": 1,
|
|
373
|
-
shadow: null,
|
|
374
|
-
visible: true,
|
|
375
|
-
backgroundColor: '',
|
|
376
|
-
fillRule: 'nonzero',
|
|
377
|
-
paintFirst: 'fill',
|
|
378
|
-
globalCompositeOperation: 'source-over',
|
|
379
|
-
skewX: 0,
|
|
380
|
-
skewY: 0,
|
|
381
|
-
rx: 0,
|
|
382
|
-
ry: 0,
|
|
383
|
-
id: 'workspace',
|
|
384
|
-
selectable: false,
|
|
385
|
-
hasControls: true,
|
|
386
|
-
};
|
|
387
|
-
return { clipPath, workspase };
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
async function getLayerBse64(layer) {
|
|
391
|
-
try {
|
|
392
|
-
const compositeBuffer = await layer.composite();
|
|
393
|
-
const canvasElement = document.createElement('canvas');
|
|
394
|
-
const context = canvasElement.getContext('2d');
|
|
395
|
-
const imageData = new ImageData(
|
|
396
|
-
compositeBuffer,
|
|
397
|
-
layer.width,
|
|
398
|
-
layer.height
|
|
399
|
-
);
|
|
400
|
-
canvasElement.width = layer.width;
|
|
401
|
-
canvasElement.height = layer.height;
|
|
402
|
-
context.putImageData(imageData, 0, 0);
|
|
403
|
-
const base64 = canvasElement.toDataURL('image/png');
|
|
404
|
-
return base64;
|
|
405
|
-
} catch (error) {
|
|
406
|
-
return '';
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
function getColor(arr) {
|
|
411
|
-
const [, r, g, b] = arr;
|
|
412
|
-
return `rgb(${r * 255}, ${g * 255}, ${b * 255})`;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
function flattenTree(tree) {
|
|
416
|
-
const result = [];
|
|
417
|
-
|
|
418
|
-
function traverse(nodes) {
|
|
419
|
-
nodes.forEach((node) => {
|
|
420
|
-
if (node.children) {
|
|
421
|
-
traverse(node.children); // 递归遍历子节点
|
|
422
|
-
} else {
|
|
423
|
-
result.push(node); // 将当前节点添加到结果数组中
|
|
424
|
-
}
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
traverse(tree);
|
|
429
|
-
return result;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
export default psdToJson;
|
|
1
|
+
import _ from 'lodash-es';
|
|
2
|
+
import { v4 as uuid } from 'uuid';
|
|
3
|
+
|
|
4
|
+
// 入口文件
|
|
5
|
+
|
|
6
|
+
async function psdToJson(psdFile) {
|
|
7
|
+
// 初始化
|
|
8
|
+
const json = getTransform();
|
|
9
|
+
|
|
10
|
+
// 设置背景
|
|
11
|
+
const { clipPath, workspase } = getClipPath(psdFile);
|
|
12
|
+
json.clipPath = clipPath;
|
|
13
|
+
json.objects.push(workspase);
|
|
14
|
+
|
|
15
|
+
// 拍平
|
|
16
|
+
const list = flattenTree(psdFile.children);
|
|
17
|
+
|
|
18
|
+
// 转换
|
|
19
|
+
const fabricJhildren = await psdChildrenTransform(list);
|
|
20
|
+
json.objects.push(...fabricJhildren.reverse());
|
|
21
|
+
|
|
22
|
+
// 输出文件
|
|
23
|
+
const jsonStr = JSON.stringify(json, null, 2);
|
|
24
|
+
return jsonStr;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function psdChildrenTransform(children) {
|
|
28
|
+
function attrTransform(childrens) {
|
|
29
|
+
const childrensFilter = childrens.filter((item) => {
|
|
30
|
+
if (item.text) {
|
|
31
|
+
item.type = 'text';
|
|
32
|
+
}
|
|
33
|
+
return !item.isHidden;
|
|
34
|
+
});
|
|
35
|
+
return childrensFilter.map(async (item) => {
|
|
36
|
+
// 公共属性映射
|
|
37
|
+
const commonAttr = baseUtils([
|
|
38
|
+
'left',
|
|
39
|
+
'top',
|
|
40
|
+
['width', (info) => info.width + 50],
|
|
41
|
+
'height',
|
|
42
|
+
'name',
|
|
43
|
+
['visible', () => true],
|
|
44
|
+
['opacity', (info) => 255 / info.opacity],
|
|
45
|
+
['id', uuid],
|
|
46
|
+
])(item);
|
|
47
|
+
|
|
48
|
+
// 类型映射
|
|
49
|
+
const typeList = [
|
|
50
|
+
{
|
|
51
|
+
sourceType: 'Layer',
|
|
52
|
+
targetType: 'image',
|
|
53
|
+
attr: [],
|
|
54
|
+
async customTransform(item) {
|
|
55
|
+
// 图片遮罩
|
|
56
|
+
// const { maskData } = item.layerFrame.layerProperties;
|
|
57
|
+
// let clipPath;
|
|
58
|
+
// if (maskData) {
|
|
59
|
+
// clipPath = getAttrByType('rect');
|
|
60
|
+
// clipPath.left = maskData.left;
|
|
61
|
+
// clipPath.top = maskData.top;
|
|
62
|
+
// }
|
|
63
|
+
const base64 = await getLayerBse64(item);
|
|
64
|
+
return {
|
|
65
|
+
src: base64,
|
|
66
|
+
// clipPath,
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
sourceType: 'text',
|
|
72
|
+
targetType: 'textbox',
|
|
73
|
+
attr: ['text'],
|
|
74
|
+
async customTransform(item) {
|
|
75
|
+
const { FontSize, FillColor, Tracking } =
|
|
76
|
+
item.textProperties.EngineDict.StyleRun.RunArray[0]
|
|
77
|
+
.StyleSheet.StyleSheetData;
|
|
78
|
+
return {
|
|
79
|
+
charSpacing: Tracking,
|
|
80
|
+
fontFamily: '站酷快乐体',
|
|
81
|
+
fill: getColor(FillColor.Values),
|
|
82
|
+
fontSize: FontSize || 12,
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
const sourctTypeKey = 'type';
|
|
89
|
+
const transformTypeInfo = typeList.find(
|
|
90
|
+
(info) => info.sourceType === item[sourctTypeKey]
|
|
91
|
+
);
|
|
92
|
+
if (transformTypeInfo) {
|
|
93
|
+
const baseAttr = getAttrByType(transformTypeInfo.targetType);
|
|
94
|
+
const typeAttr = baseUtils(transformTypeInfo.attr)(item);
|
|
95
|
+
const customTransformAttr = transformTypeInfo.customTransform
|
|
96
|
+
? await transformTypeInfo.customTransform(item)
|
|
97
|
+
: null;
|
|
98
|
+
return _.assign(
|
|
99
|
+
baseAttr,
|
|
100
|
+
commonAttr,
|
|
101
|
+
baseAttr,
|
|
102
|
+
typeAttr,
|
|
103
|
+
customTransformAttr
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
...getAttrByType('textbox'),
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const resault = await Promise.all(attrTransform(children));
|
|
112
|
+
return resault;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function getTransform() {
|
|
116
|
+
// 初始化
|
|
117
|
+
const json = {
|
|
118
|
+
version: '5.3.0',
|
|
119
|
+
objects: [],
|
|
120
|
+
clipPath: null,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
return json;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function baseUtils(baseAttr) {
|
|
127
|
+
return function (item) {
|
|
128
|
+
const attrs = {
|
|
129
|
+
desc: {},
|
|
130
|
+
};
|
|
131
|
+
baseAttr.forEach((attrType) => {
|
|
132
|
+
// 直接转换
|
|
133
|
+
if (typeof attrType === 'string') {
|
|
134
|
+
const value = _.get(item, attrType);
|
|
135
|
+
_.set(attrs, attrType, value);
|
|
136
|
+
} else {
|
|
137
|
+
const [key, transform] = attrType;
|
|
138
|
+
if (typeof transform === 'function') {
|
|
139
|
+
const value = transform(item);
|
|
140
|
+
if (value) {
|
|
141
|
+
_.set(attrs, key, value);
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
const value = _.get(item, transform);
|
|
145
|
+
_.set(attrs, key, value);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return attrs;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function getAttrByType(type) {
|
|
154
|
+
const typeMap = {
|
|
155
|
+
group: {
|
|
156
|
+
type: 'group',
|
|
157
|
+
version: '5.3.0',
|
|
158
|
+
originX: 'left',
|
|
159
|
+
originY: 'top',
|
|
160
|
+
fill: 'rgb(0,0,0)',
|
|
161
|
+
stroke: null,
|
|
162
|
+
strokeWidth: 0,
|
|
163
|
+
strokeDashArray: null,
|
|
164
|
+
strokeLineCap: 'butt',
|
|
165
|
+
strokeDashOffset: 0,
|
|
166
|
+
strokeLineJoin: 'miter',
|
|
167
|
+
strokeUniform: false,
|
|
168
|
+
strokeMiterLimit: 4,
|
|
169
|
+
scaleX: 1,
|
|
170
|
+
scaleY: 1,
|
|
171
|
+
flipX: false,
|
|
172
|
+
flipY: false,
|
|
173
|
+
shadow: null,
|
|
174
|
+
visible: true,
|
|
175
|
+
backgroundColor: '',
|
|
176
|
+
fillRule: 'nonzero',
|
|
177
|
+
paintFirst: 'fill',
|
|
178
|
+
globalCompositeOperation: 'source-over',
|
|
179
|
+
skewX: 0,
|
|
180
|
+
skewY: 0,
|
|
181
|
+
selectable: true,
|
|
182
|
+
hasControls: true,
|
|
183
|
+
},
|
|
184
|
+
textbox: {
|
|
185
|
+
type: 'textbox',
|
|
186
|
+
version: '5.3.0',
|
|
187
|
+
originX: 'left',
|
|
188
|
+
originY: 'top',
|
|
189
|
+
fill: 'rgb(0,0,0)',
|
|
190
|
+
stroke: null,
|
|
191
|
+
strokeWidth: 1,
|
|
192
|
+
strokeDashArray: null,
|
|
193
|
+
strokeLineCap: 'butt',
|
|
194
|
+
strokeDashOffset: 0,
|
|
195
|
+
strokeLineJoin: 'miter',
|
|
196
|
+
strokeUniform: false,
|
|
197
|
+
strokeMiterLimit: 4,
|
|
198
|
+
scaleX: 1,
|
|
199
|
+
scaleY: 1,
|
|
200
|
+
angle: 0,
|
|
201
|
+
flipX: false,
|
|
202
|
+
flipY: false,
|
|
203
|
+
shadow: '',
|
|
204
|
+
backgroundColor: '',
|
|
205
|
+
fillRule: 'nonzero',
|
|
206
|
+
paintFirst: 'fill',
|
|
207
|
+
globalCompositeOperation: 'source-over',
|
|
208
|
+
skewX: 0,
|
|
209
|
+
skewY: 0,
|
|
210
|
+
fontFamily: 'arial',
|
|
211
|
+
fontWeight: 'normal',
|
|
212
|
+
fontSize: 80,
|
|
213
|
+
text: '诸事顺遂',
|
|
214
|
+
underline: false,
|
|
215
|
+
overline: false,
|
|
216
|
+
linethrough: false,
|
|
217
|
+
textAlign: 'left',
|
|
218
|
+
fontStyle: 'normal',
|
|
219
|
+
lineHeight: 1.16,
|
|
220
|
+
textBackgroundColor: '',
|
|
221
|
+
charSpacing: 0,
|
|
222
|
+
styles: [],
|
|
223
|
+
direction: 'ltr',
|
|
224
|
+
path: null,
|
|
225
|
+
pathStartOffset: 0,
|
|
226
|
+
pathSide: 'left',
|
|
227
|
+
pathAlign: 'baseline',
|
|
228
|
+
minWidth: 20,
|
|
229
|
+
splitByGrapheme: true,
|
|
230
|
+
selectable: true,
|
|
231
|
+
hasControls: true,
|
|
232
|
+
},
|
|
233
|
+
image: {
|
|
234
|
+
type: 'image',
|
|
235
|
+
version: '5.3.0',
|
|
236
|
+
originX: 'left',
|
|
237
|
+
originY: 'top',
|
|
238
|
+
fill: 'rgb(0,0,0)',
|
|
239
|
+
stroke: null,
|
|
240
|
+
strokeWidth: 0,
|
|
241
|
+
strokeDashArray: null,
|
|
242
|
+
strokeLineCap: 'butt',
|
|
243
|
+
strokeDashOffset: 0,
|
|
244
|
+
strokeLineJoin: 'miter',
|
|
245
|
+
strokeUniform: false,
|
|
246
|
+
strokeMiterLimit: 4,
|
|
247
|
+
scaleX: 1,
|
|
248
|
+
scaleY: 1,
|
|
249
|
+
angle: 0,
|
|
250
|
+
flipX: false,
|
|
251
|
+
flipY: false,
|
|
252
|
+
shadow: null,
|
|
253
|
+
backgroundColor: '',
|
|
254
|
+
fillRule: 'nonzero',
|
|
255
|
+
paintFirst: 'fill',
|
|
256
|
+
globalCompositeOperation: 'source-over',
|
|
257
|
+
skewX: 0,
|
|
258
|
+
skewY: 0,
|
|
259
|
+
cropX: 0,
|
|
260
|
+
cropY: 0,
|
|
261
|
+
selectable: true,
|
|
262
|
+
hasControls: true,
|
|
263
|
+
src: '',
|
|
264
|
+
crossOrigin: 'anonymous',
|
|
265
|
+
filters: [],
|
|
266
|
+
},
|
|
267
|
+
rect: {
|
|
268
|
+
type: 'rect',
|
|
269
|
+
version: '5.3.0',
|
|
270
|
+
originX: 'center',
|
|
271
|
+
originY: 'center',
|
|
272
|
+
left: 0,
|
|
273
|
+
top: 0,
|
|
274
|
+
width: 226,
|
|
275
|
+
height: 302,
|
|
276
|
+
fill: 'rgb(0,0,0)',
|
|
277
|
+
stroke: null,
|
|
278
|
+
strokeWidth: 1,
|
|
279
|
+
strokeDashArray: null,
|
|
280
|
+
strokeLineCap: 'butt',
|
|
281
|
+
strokeDashOffset: 0,
|
|
282
|
+
strokeLineJoin: 'miter',
|
|
283
|
+
strokeUniform: false,
|
|
284
|
+
strokeMiterLimit: 4,
|
|
285
|
+
scaleX: 1,
|
|
286
|
+
scaleY: 1,
|
|
287
|
+
angle: 0,
|
|
288
|
+
flipX: false,
|
|
289
|
+
flipY: false,
|
|
290
|
+
opacity: 1,
|
|
291
|
+
shadow: null,
|
|
292
|
+
visible: true,
|
|
293
|
+
backgroundColor: '',
|
|
294
|
+
fillRule: 'nonzero',
|
|
295
|
+
paintFirst: 'fill',
|
|
296
|
+
globalCompositeOperation: 'source-over',
|
|
297
|
+
skewX: 0,
|
|
298
|
+
skewY: 0,
|
|
299
|
+
rx: 0,
|
|
300
|
+
ry: 0,
|
|
301
|
+
selectable: true,
|
|
302
|
+
hasControls: true,
|
|
303
|
+
inverted: false,
|
|
304
|
+
absolutePositioned: false,
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
return typeMap[type];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function getClipPath(psdFile) {
|
|
311
|
+
const { width, height } = psdFile;
|
|
312
|
+
const clipPath = {
|
|
313
|
+
type: 'rect',
|
|
314
|
+
version: '5.3.0',
|
|
315
|
+
originX: 'left',
|
|
316
|
+
originY: 'top',
|
|
317
|
+
left: 0,
|
|
318
|
+
top: 0,
|
|
319
|
+
width: width,
|
|
320
|
+
height: height,
|
|
321
|
+
fill: 'rgba(255,255,255,1)',
|
|
322
|
+
stroke: null,
|
|
323
|
+
strokeWidth: 0,
|
|
324
|
+
strokeDashArray: null,
|
|
325
|
+
strokeLineCap: 'butt',
|
|
326
|
+
strokeDashOffset: 0,
|
|
327
|
+
strokeLineJoin: 'miter',
|
|
328
|
+
strokeUniform: false,
|
|
329
|
+
strokeMiterLimit: 4,
|
|
330
|
+
scaleX: 1,
|
|
331
|
+
scaleY: 1,
|
|
332
|
+
angle: 0,
|
|
333
|
+
flipX: false,
|
|
334
|
+
flipY: false,
|
|
335
|
+
opacity: 1,
|
|
336
|
+
shadow: null,
|
|
337
|
+
visible: true,
|
|
338
|
+
backgroundColor: '',
|
|
339
|
+
fillRule: 'nonzero',
|
|
340
|
+
paintFirst: 'fill',
|
|
341
|
+
globalCompositeOperation: 'source-over',
|
|
342
|
+
skewX: 0,
|
|
343
|
+
skewY: 0,
|
|
344
|
+
rx: 0,
|
|
345
|
+
ry: 0,
|
|
346
|
+
selectable: true,
|
|
347
|
+
hasControls: true,
|
|
348
|
+
};
|
|
349
|
+
const workspase = {
|
|
350
|
+
type: 'rect',
|
|
351
|
+
version: '5.3.0',
|
|
352
|
+
originX: 'left',
|
|
353
|
+
originY: 'top',
|
|
354
|
+
left: 0,
|
|
355
|
+
top: 0,
|
|
356
|
+
width: width,
|
|
357
|
+
height: height,
|
|
358
|
+
fill: 'rgba(255,255,255,1)',
|
|
359
|
+
stroke: null,
|
|
360
|
+
strokeWidth: 0,
|
|
361
|
+
strokeDashArray: null,
|
|
362
|
+
strokeLineCap: 'butt',
|
|
363
|
+
strokeDashOffset: 0,
|
|
364
|
+
strokeLineJoin: 'miter',
|
|
365
|
+
strokeUniform: false,
|
|
366
|
+
strokeMiterLimit: 4,
|
|
367
|
+
scaleX: 1,
|
|
368
|
+
scaleY: 1,
|
|
369
|
+
angle: 0,
|
|
370
|
+
flipX: false,
|
|
371
|
+
flipY: false,
|
|
372
|
+
// "opacity": 1,
|
|
373
|
+
shadow: null,
|
|
374
|
+
visible: true,
|
|
375
|
+
backgroundColor: '',
|
|
376
|
+
fillRule: 'nonzero',
|
|
377
|
+
paintFirst: 'fill',
|
|
378
|
+
globalCompositeOperation: 'source-over',
|
|
379
|
+
skewX: 0,
|
|
380
|
+
skewY: 0,
|
|
381
|
+
rx: 0,
|
|
382
|
+
ry: 0,
|
|
383
|
+
id: 'workspace',
|
|
384
|
+
selectable: false,
|
|
385
|
+
hasControls: true,
|
|
386
|
+
};
|
|
387
|
+
return { clipPath, workspase };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
async function getLayerBse64(layer) {
|
|
391
|
+
try {
|
|
392
|
+
const compositeBuffer = await layer.composite();
|
|
393
|
+
const canvasElement = document.createElement('canvas');
|
|
394
|
+
const context = canvasElement.getContext('2d');
|
|
395
|
+
const imageData = new ImageData(
|
|
396
|
+
compositeBuffer,
|
|
397
|
+
layer.width,
|
|
398
|
+
layer.height
|
|
399
|
+
);
|
|
400
|
+
canvasElement.width = layer.width;
|
|
401
|
+
canvasElement.height = layer.height;
|
|
402
|
+
context.putImageData(imageData, 0, 0);
|
|
403
|
+
const base64 = canvasElement.toDataURL('image/png');
|
|
404
|
+
return base64;
|
|
405
|
+
} catch (error) {
|
|
406
|
+
return '';
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function getColor(arr) {
|
|
411
|
+
const [, r, g, b] = arr;
|
|
412
|
+
return `rgb(${r * 255}, ${g * 255}, ${b * 255})`;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function flattenTree(tree) {
|
|
416
|
+
const result = [];
|
|
417
|
+
|
|
418
|
+
function traverse(nodes) {
|
|
419
|
+
nodes.forEach((node) => {
|
|
420
|
+
if (node.children) {
|
|
421
|
+
traverse(node.children); // 递归遍历子节点
|
|
422
|
+
} else {
|
|
423
|
+
result.push(node); // 将当前节点添加到结果数组中
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
traverse(tree);
|
|
429
|
+
return result;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
export default psdToJson;
|