-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcopyPasteProps.ts
177 lines (167 loc) · 3.42 KB
/
copyPasteProps.ts
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
/**
* Copy properties from one node to another while avoiding conflicts. When no target node is provided it returns a new object.
*
* For example:
* ```js
* const rectangle = figma.createRectangle()
* const frame = figma.createFrame()
*
* copyPaste({ rectangle, frame, exclude: ['fills'] })
* ```
*
* This will copy and paste all properties except for `fills` and readonly properties.
*
* @param source - Node being copied from
* @param target - Node being copied to
* @param include - Props that should be copied
* @param exclude - Props that shouldn't be copied
*/
interface Arguments {
source: {}
target?: {}
include?: string[]
exclude?: string[]
}
const nodeProps: string[] = [
'id',
'parent',
'name',
'removed',
'visible',
'locked',
'children',
'constraints',
'absoluteTransform',
'relativeTransform',
'x',
'y',
'rotation',
'width',
'height',
'constrainProportions',
'layoutAlign',
'layoutGrow',
'opacity',
'blendMode',
'isMask',
'effects',
'effectStyleId',
'expanded',
'backgrounds',
'backgroundStyleId',
'fills',
'strokes',
'strokeWeight',
'strokeMiterLimit',
'strokeAlign',
'strokeCap',
'strokeJoin',
'dashPattern',
'fillStyleId',
'strokeStyleId',
'cornerRadius',
'cornerSmoothing',
'topLeftRadius',
'topRightRadius',
'bottomLeftRadius',
'bottomRightRadius',
'exportSettings',
'overflowDirection',
'numberOfFixedChildren',
'overlayPositionType',
'overlayBackground',
'overlayBackgroundInteraction',
'reactions',
'description',
'remote',
'key',
'layoutMode',
'primaryAxisSizingMode',
'counterAxisSizingMode',
'primaryAxisAlignItems',
'counterAxisAlignItems',
'paddingLeft',
'paddingRight',
'paddingTop',
'paddingBottom',
'itemSpacing',
'horizontalPadding',
'verticalPadding',
'layoutGrids',
'gridStyleId',
'clipsContent',
'guides'
]
const readonly: string[] = [
'id',
'parent',
'removed',
'children',
'absoluteTransform',
'width',
'height',
'overlayPositionType',
'overlayBackground',
'overlayBackgroundInteraction',
'reactions',
'remote',
'key',
'type'
]
export default function copyPasteProps({ source, target, include, exclude }: Arguments) {
let allowlist: string[] = nodeProps
if (include) {
allowlist = include
} else if (exclude) {
allowlist = allowlist.filter(function (el) {
return !exclude.concat(readonly).includes(el)
})
}
const val = source
const type = typeof source
if (
type === 'undefined' ||
type === 'number' ||
type === 'string' ||
type === 'boolean' ||
type === 'symbol' ||
source === null
) {
return val
} else if (type === 'object') {
if (val instanceof Array) {
return val.map(copyPasteProps)
} else if (val instanceof Uint8Array) {
return new Uint8Array(val)
} else {
const o: any = {}
for (const key1 in val) {
if (target) {
for (const key2 in target) {
if (allowlist.includes(key2)) {
if (key1 === key2) {
o[key1] = copyPasteProps({
source: val[key1],
target,
include,
exclude
})
}
}
}
} else {
if (allowlist.includes(key1)) {
o[key1] = copyPasteProps({ source: val[key1], include, exclude })
}
}
}
if (target) {
!o.fillStyleId && o.fills ? null : delete o.fills
!o.strokeStyleId && o.strokes ? null : delete o.strokes
!o.backgroundStyleId && o.backgrounds ? null : delete o.backgrounds
}
return target ? Object.assign(target, o) : o
}
}
throw 'unknown'
}