Skip to content

Instantly share code, notes, and snippets.

@envex
Last active August 29, 2015 14:04
Show Gist options
  • Save envex/81fe2bfffe9cfff9d8c1 to your computer and use it in GitHub Desktop.
Save envex/81fe2bfffe9cfff9d8c1 to your computer and use it in GitHub Desktop.
function getHiddenLayers(layers, isParentVisible, hasParent, parentMaskBounds) {
isParentVisible = _.isUndefined(isParentVisible) ? true : isParentVisible;
hasParent = hasParent || false;
parentMaskBounds = parentMaskBounds || false;
var deferred = Q.defer();
// Get a list of all the hidden layers
_.forEach(layers, function(layer) {
params.pixmap.totalLayers++;
console.log(_helpers.stringify(layer));
// Set our min/max
if ( layer.index > params.pixmap.max ) {
params.pixmap.max = layer.index;
}
if ( layer.index <= params.pixmap.min || params.pixmap.min === 0 ) {
params.pixmap.min = layer.index;
}
// We only need to get the bounds
// of objects that are outside of
// the bounds of the psd so we know
// how to crop
//
// we need to find out if the root
// folder is hidden so we can apply
// it to all it's children
var consideredVisible = layer.visible;
// Parent folder is hidden
// set visibility to hidden
if ( hasParent && ! isParentVisible ) {
consideredVisible = false;
}
// No parent, set visibility to current
// layers visibility
if ( ! hasParent ) {
isParentVisible = layer.visible;
}
// get the bounds of all the visible layers
if ( consideredVisible ) {
// Ignore adjustment layers
// because they throw off the crop
// and don't actually provide accurate bounds
if ( layer.type === "adjustmentLayer" ) {
// console.log("Layer is adjustmentLayer. Ignoring.");
// return;
}
// We also want to ignore the bounds
// if the layer is clipped
if ( layer.clipped === true ) {
// console.log("Layer is clipped. Ignoring.");
// return;
}
// If the layer has a mask, we want
// to use those bounds instead of the
// layers bounds, otherwise it doesn't
// give an accurate crop
if ( ! _.isUndefined(layer.mask) ) {
// We don't want to include mask
// bounds that are negative because
// that would mean it's outside the
// document, which is useless for
// a mask
layer.bounds.top = layer.mask.bounds.top > 0 ? layer.mask.bounds.top : layer.bounds;
layer.bounds.left = layer.mask.bounds.left > 0 ? layer.mask.bounds.left : layer.bounds;
parentMaskBounds = layer.bounds;
} else if ( _.isObject(parentMaskBounds) ) {
// The the parent is a mask, we want
// to set the current bounds to the
// layers bounds
layer.bounds = parentMaskBounds;
} else {
parentMaskBounds = false;
}
//
// If we have bounds with with FX, we
// want to use those instead
//
if ( ! _.isUndefined(layer.boundsWithFX) ) {
layer.bounds = layer.boundsWithFX;
}
//
// Set the offset of the furthest bounds
//
if ( layer.bounds.top < params.pixmap.cropOffset.top ) {
params.pixmap.cropOffset.top = layer.bounds.top;
}
if ( layer.bounds.left < params.pixmap.cropOffset.left ) {
params.pixmap.cropOffset.left = layer.bounds.left;
}
}
// Layer is hidden, we need to push into
// the array
if ( ! consideredVisible ) {
params.pixmap.hiddenLayers.push(layer.index);
}
// We've hit a layer group, so
// we want to loop through all
// it's child layers
if ( layer.layers ) {
getHiddenLayers(layer.layers, consideredVisible, true, parentMaskBounds);
}
deferred.resolve(params);
the_promises.push(deferred.promise);
});
}
getHiddenLayers(_document.layers);
return Q.all(the_promises[0]);
}
[{
"type": "layer",
"name": "Clip",
"visible": true,
"clipped": true
},
{
"type": "layer",
"name": "Clip",
"visible": true,
"clipped": true
},
{
"type": "layer",
"name": "Clip",
"visible": true,
"clipped": true
},
{
"type": "layer",
"name": "Clip Parent",
"visible": false,
"clipped": false
},
{
"type": "layer",
"name": "BG",
"visible": true,
"clipped": false
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment