Skip to content

Commit c46e3b7

Browse files
committed
Reduce code complexity
1 parent cf1f05e commit c46e3b7

1 file changed

Lines changed: 99 additions & 98 deletions

File tree

src/explode_shape_layer.jsx

Lines changed: 99 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,39 @@ function explodeLayer(layer) {
2525
var pb = new ProgressBar(1, contents.numProperties, 1);
2626
pb.start();
2727

28-
// Browse through contents array
29-
for(var i = contents.numProperties; i > 0; i--) {
28+
try {
29+
// Browse through contents array
30+
for(var i = contents.numProperties; i > 0; i--) {
3031

31-
// Get the original property
32-
var _prop = contents.property(i);
33-
pb.update(contents.numProperties - i)
32+
// Get the original property
33+
var _prop = contents.property(i);
34+
pb.update(contents.numProperties - i)
3435

35-
// Skip the property if not enabled
36-
if (!_prop.enabled) continue;
36+
// Skip the property if not enabled
37+
if (!_prop.enabled) continue;
3738

38-
// Duplicate the original layer and rename with property name
39-
var new_layer = emptyDuplicateLayer(layer)
39+
// Duplicate the original layer and rename with property name
40+
var new_layer = emptyDuplicateLayer(layer)
4041

41-
new_layer.name = layer.name + ' - ' + _prop.name;
42-
new_layer.enabled = false;
43-
new_layer.shy = true;
42+
new_layer.name = layer.name + ' - ' + _prop.name;
43+
new_layer.enabled = false;
44+
new_layer.shy = true;
4445

45-
layers.push(new_layer);
46+
layers.push(new_layer);
4647

47-
if (!new_layer.property("Contents").canAddProperty(_prop.matchName)) continue;
48+
if (!new_layer.property("Contents").canAddProperty(_prop.matchName)) continue;
4849

49-
var prop = new_layer.property("Contents").addProperty(_prop.matchName)
50+
var prop = new_layer.property("Contents").addProperty(_prop.matchName)
5051

51-
copyProperties(_prop, prop, '')
52+
copyProperties(_prop, prop, 0)
5253

54+
}
55+
} catch(e) {
56+
cLog("An error occured: " + e.message);
57+
pb.end();
5358
}
5459

60+
5561
pb.end();
5662

5763
for(var i = 0; i < layers.length; i++) {
@@ -120,14 +126,31 @@ function emptyDuplicateLayer(layer) {
120126

121127
}
122128

123-
function copyProperties(origin, target, prefix) {
129+
/**
130+
* Copy properties of a layer to another (recursive).
131+
*
132+
* @param {Object} origin
133+
* @param {Object} target
134+
* @param {Number} level
135+
*/
136+
function copyProperties(origin, target, level) {
137+
var indent = repeatStr(" ", level === undefined ? 0 : level);
138+
var prefix = indent + "⌞-";
139+
140+
cDebug(indent + origin.name);
124141

125142
for(var i=1; i <= origin.numProperties; i++) {
126143

127144
var _prop = origin.property(i);
128145

129146
if(!_prop.enabled || !target.canAddProperty(_prop.matchName)) return;
130147

148+
// Skipped properties
149+
if(arrayIncludes(['ADBE Vector Materials Group'], _prop.matchName)) {
150+
cDebug(prefix + _prop.matchName + " (skipped)");
151+
continue;
152+
}
153+
131154
cDebug(prefix + _prop.matchName);
132155

133156
var prop = target.addProperty(_prop.matchName);
@@ -138,48 +161,82 @@ function copyProperties(origin, target, prefix) {
138161
copyProperty('mode', _prop, prop)
139162
break;
140163

141-
case 'ADBE Vector Materials Group':
142-
cDebug(prefix + '-- skipped');
143-
break;
144-
145164
case 'ADBE Vector Graphic - Stroke':
146-
copyPropertyStroke(_prop, prop);
165+
copyProperty('composite', _prop, prop);
166+
copyProperty('color', _prop, prop);
167+
copyProperty('strokeWidth', _prop, prop);
168+
copyProperty('lineCap', _prop, prop);
169+
copyProperty('lineJoin', _prop, prop);
170+
copyProperty('miterLimit', _prop, prop);
147171
break;
148172

149173
case 'ADBE Vector Graphic - Fill':
150-
copyPropertyFill(_prop, prop);
174+
copyProperty('composite', _prop, prop);
175+
copyProperty('fillRule', _prop, prop);
176+
copyProperty('color', _prop, prop);
151177
break;
152178

153179
case 'ADBE Vector Transform Group':
154-
copyPropertyTransform(_prop, prop);
180+
copyProperty('anchorPoint', _prop, prop);
181+
copyProperty('position', _prop, prop);
182+
copyProperty('scale', _prop, prop);
183+
copyProperty('skew', _prop, prop);
184+
copyProperty('skewAxis', _prop, prop);
185+
copyProperty('rotation', _prop, prop);
186+
copyProperty('opacity', _prop, prop);
155187
break;
156188

157189
case 'ADBE Vector Shape - Rect':
158-
copyPropertyRect(_prop, prop);
190+
copyProperty('shapeDirection', _prop, prop)
191+
copyProperty('size', _prop, prop)
192+
copyProperty('position', _prop, prop)
193+
copyProperty('roundness', _prop, prop)
159194
break;
160195

161196
case 'ADBE Vector Shape - Ellipse':
162-
copyPropertyEllipse(_prop, prop);
197+
copyProperty('shapeDirection', _prop, prop)
198+
copyProperty('size', _prop, prop)
199+
copyProperty('position', _prop, prop)
163200
break;
164201

165202
case 'ADBE Vector Shape - Star':
166-
copyPropertyStar(_prop, prop);
203+
copyProperty('shapeDirection', _prop, prop)
204+
copyProperty('type', _prop, prop)
205+
copyProperty('points', _prop, prop)
206+
copyProperty('position', _prop, prop)
207+
copyProperty('rotation', _prop, prop)
208+
copyProperty('innerRadius', _prop, prop)
209+
copyProperty('outerRadius', _prop, prop)
210+
copyProperty('innerRoundness', _prop, prop)
211+
copyProperty('outerRoundness', _prop, prop)
167212
break;
168213

169214
case 'ADBE Root Vectors Group':
170215
case 'ADBE Vectors Group':
171216
case 'ADBE Vector Group':
172-
copyProperties(_prop, prop, prefix += ' ')
217+
copyProperties(_prop, prop, level + 1);
173218
break;
174219

175220
case 'ADBE Vector Shape - Group':
176-
copyPropertyShape(_prop, prop);
221+
prop.property('ADBE Vector Shape').setValue( _prop.property('ADBE Vector Shape').value );
177222
break;
178223

224+
// case 'Vector Stroke Taper':
225+
// copyProperty('startLength', _prop, prop);
226+
// copyProperty('endLength', _prop, prop);
227+
// copyProperty('startWidth', _prop, prop);
228+
// copyProperty('endWidth', _prop, prop);
229+
// copyProperty('startEase', _prop, prop);
230+
// copyProperty('endEase', _prop, prop);
231+
// break;
232+
179233
case 'ADBE Vector Blend Mode':
180234
prop.setValue( _prop.value );
181235
break;
182236

237+
default:
238+
cDebug(prefix + '>> not supported!');
239+
break;
183240
}
184241

185242
}
@@ -190,78 +247,22 @@ function copyProperty(name, origin, target) {
190247
target[name].setValue( origin[name].value );
191248
}
192249

193-
function copyPropertyShape(origin, target) {
194-
target.property('ADBE Vector Shape').setValue( origin.property('ADBE Vector Shape').value );
195-
}
196-
197-
function copyPropertyStroke(origin, target) {
198-
199-
copyProperty('composite', origin, target);
200-
copyProperty('color', origin, target);
201-
copyProperty('strokeWidth', origin, target);
202-
copyProperty('lineCap', origin, target);
203-
copyProperty('lineJoin', origin, target);
204-
copyProperty('miterLimit', origin, target);
205-
206-
// TOFIX : dash are present, no mater if deleted or not ! (disabled for now)
207-
// if(false && origin.dash.enabled) {
208-
//
209-
// for(var i=1; i <= origin.dash.numProperties; i++) {
210-
//
211-
// var dashProp = origin.dash.property(i);
212-
//
213-
// if(dashProp.enabled)
214-
// target.dash.addProperty(dashProp.matchName).setValue(dashProp.value);
215-
//
216-
// }
217-
//
218-
// }
219-
220-
}
221-
222-
function copyPropertyFill(origin, target) {
223-
224-
copyProperty('composite', origin, target);
225-
copyProperty('fillRule', origin, target);
226-
copyProperty('color', origin, target);
227-
228-
}
229-
230-
function copyPropertyTransform(origin, target) {
231-
232-
copyProperty('anchorPoint', origin, target);
233-
copyProperty('position', origin, target);
234-
copyProperty('scale', origin, target);
235-
copyProperty('skew', origin, target);
236-
copyProperty('skewAxis', origin, target);
237-
copyProperty('rotation', origin, target);
238-
copyProperty('opacity', origin, target);
239-
240-
}
241-
242-
function copyPropertyRect(origin, target) {
243-
copyProperty('shapeDirection', origin, target)
244-
copyProperty('size', origin, target)
245-
copyProperty('position', origin, target)
246-
copyProperty('roundness', origin, target)
247-
}
250+
function arrayIncludes(arr, needle) {
251+
for(var i=arr.length - 1; i>=0; i--){
252+
if(arr[i] === needle) {
253+
return true;
254+
}
255+
}
248256

249-
function copyPropertyEllipse(origin, target) {
250-
copyProperty('shapeDirection', origin, target)
251-
copyProperty('size', origin, target)
252-
copyProperty('position', origin, target)
257+
return false;
253258
}
254259

255-
function copyPropertyStar(origin, target) {
256-
copyProperty('shapeDirection', origin, target)
257-
copyProperty('type', origin, target)
258-
copyProperty('points', origin, target)
259-
copyProperty('position', origin, target)
260-
copyProperty('rotation', origin, target)
261-
copyProperty('innerRadius', origin, target)
262-
copyProperty('outerRadius', origin, target)
263-
copyProperty('innerRoundness', origin, target)
264-
copyProperty('outerRoundness', origin, target)
260+
function repeatStr(str, amount) {
261+
var rtn = "";
262+
for(var i=0; i<amount; i++) {
263+
rtn += str;
264+
}
265+
return rtn;
265266
}
266267

267268
function createUI(that) {

0 commit comments

Comments
 (0)