Skip to content

Commit

Permalink
removed internal videos
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Kwak committed Jan 4, 2019
1 parent 080f342 commit 757d67a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 75 deletions.
85 changes: 42 additions & 43 deletions sg-fit-to-object/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* - "Rotate Clock Fit Item": Rotate the item clockwise and fit the artboard.
* - "Rotate Counter Clock Fit Item": Rotate the item counter clockwise and fit the artboard.
*
* Demo: https://adobe.enterprise.slack.com/files/W7FG32S6M/FA8KX3269/apr-17-2018_16-36-00.mp4
*/

var Artboard = require("scenegraph").Artboard;
Expand All @@ -30,64 +29,64 @@ function rotatateClockFitItem(selection) {
fitIt(selection, "right");
}

function rotateCounterClockFitItem(selection){
function rotateCounterClockFitItem(selection) {
fitIt(selection, "left");
}

// Main function

function fitIt(selection, direction) {
if (selection.hasArtwork){
if (selection.hasArtwork) {
var selectedItem = selection.items[0];
var itemsToFit = getItemsToFit(selection);

itemsToFit.forEach(function(itemToFit){
itemsToFit.forEach(function (itemToFit) {
// Duplicate the selected item
commands.duplicate();

// Rotate the selection as requested
rotate(selection.items[0], direction, selection.items[0].localCenterPoint);
// Rotate the selection as requested
rotate(selection.items[0], direction, selection.items[0].localCenterPoint);

// Resize to fit the target object
resize(selection.items[0], itemToFit.globalBounds);
// Resize to fit the target object
resize(selection.items[0], itemToFit.globalBounds);

// Place the item in the center of the target object
if (itemToFit.parent instanceof Group){
selection.items[0].placeInParentCoordinates(selection.items[0].localCenterPoint, getCenterPoint(itemToFit));
} else {
selection.items[0].placeInParentCoordinates(selection.items[0].localCenterPoint, itemToFit.localCenterPoint);
}
// Place the item in the center of the target object
if (itemToFit.parent instanceof Group) {
selection.items[0].placeInParentCoordinates(selection.items[0].localCenterPoint, getCenterPoint(itemToFit));
} else {
selection.items[0].placeInParentCoordinates(selection.items[0].localCenterPoint, itemToFit.localCenterPoint);
}

// Bring the item to the front
commands.bringToFront()
// Bring the item to the front
commands.bringToFront()
})
}
}

// Helper functions

function getItemsToFit(selection){
function getItemsToFit(selection) {
var selectedItem = selection.items[0];
var parent = selectedItem.parent;
var itemsToFit = [];

// First use case where items are grouped
if (parent instanceof Group){
if (parent instanceof Group) {
parent.children.forEach(function (childNode, i) {
if (!childNode.selected){
itemsToFit.push(childNode);
if (!childNode.selected) {
itemsToFit.push(childNode);
}
});
} else {
// Second use case where items are not grouped (Fitting the Artboard)
while (true){
if (parent instanceof Artboard){
itemsToFit.push(parent);
break;
} else {
parent = parent.parent;
}
}
// Second use case where items are not grouped (Fitting the Artboard)
while (true) {
if (parent instanceof Artboard) {
itemsToFit.push(parent);
break;
} else {
parent = parent.parent;
}
}
}
return itemsToFit;
}
Expand All @@ -104,7 +103,7 @@ function rotate(node, direction, centerPoint) {
} else if (node.rotation == -180) {
node.rotateAround(180, centerPoint);
}
} else if (direction == "left"){
} else if (direction == "left") {
if (node.rotation == 0) {
node.rotateAround(-90, centerPoint);
} else if (node.rotation == 90) {
Expand All @@ -114,7 +113,7 @@ function rotate(node, direction, centerPoint) {
} else if (node.rotation == -180) {
node.rotateAround(-270, centerPoint);
}
} else if (direction == "right"){
} else if (direction == "right") {
if (node.rotation == 0) {
node.rotateAround(90, centerPoint);
} else if (node.rotation == -90) {
Expand All @@ -129,25 +128,25 @@ function rotate(node, direction, centerPoint) {

function resize(node, globalBounds) {
// Resize to fit the artboard
if (node.rotation == 0 || node.rotation == -180 || node.rotation == 180){
node.resize(globalBounds.width, globalBounds.height);
} else if (node.rotation == 90 || node.rotation == -90) {
node.resize(globalBounds.height, globalBounds.width);
}
if (node.rotation == 0 || node.rotation == -180 || node.rotation == 180) {
node.resize(globalBounds.width, globalBounds.height);
} else if (node.rotation == 90 || node.rotation == -90) {
node.resize(globalBounds.height, globalBounds.width);
}
}

function getCenterPoint(node) {
console.log(node.parent)
return {
x: node.boundsInParent.x + node.boundsInParent.width/2,
y: node.boundsInParent.y + node.boundsInParent.height/2
x: node.boundsInParent.x + node.boundsInParent.width / 2,
y: node.boundsInParent.y + node.boundsInParent.height / 2
}
}

module.exports = {
commands: {
fitItem: fitItem,
rotatateClockFitItem: rotatateClockFitItem,
rotateCounterClockFitItem: rotateCounterClockFitItem
}
commands: {
fitItem: fitItem,
rotatateClockFitItem: rotatateClockFitItem,
rotateCounterClockFitItem: rotateCounterClockFitItem
}
};
63 changes: 31 additions & 32 deletions sg-margin-guides/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
*
* Run "Remove margin guide" to remove the guide rectangles from selected artboard(s).
*
* Demo: https://adobexdteam.slack.com/archives/C0B913DPV/p1524599264000015
*/


var {Rectangle, Color, Text, Group, Artboard, Ellipse, Line} = require("scenegraph");
var { Rectangle, Color, Text, Group, Artboard, Ellipse, Line } = require("scenegraph");
var commands = require("commands");
var artboards = {};

function createMarginGuide(selection) {
selection.items.forEach(function(node){
if(node instanceof Artboard ){
if ((node.guid in artboards)){
selection.items.forEach(function (node) {
if (node instanceof Artboard) {
if ((node.guid in artboards)) {
removeMarginGuide(selection);
createMarginGuide(selection);
} else {
Expand All @@ -26,19 +25,19 @@ function createMarginGuide(selection) {
var metrics = getMetrics(artboard);

// Create left rectangle
var leftCenterPoint = {x: metrics.left/2, y: metrics.artboardHeight/2};
var leftCenterPoint = { x: metrics.left / 2, y: metrics.artboardHeight / 2 };
createRectangle(artboard, metrics.leftMargin, metrics.leftMargin, metrics.artboardHeight, leftCenterPoint);

// Create right rectangle
var leftCenterPoint = {x: metrics.right + metrics.rightMargin/2, y: metrics.artboardHeight/2};
var leftCenterPoint = { x: metrics.right + metrics.rightMargin / 2, y: metrics.artboardHeight / 2 };
createRectangle(artboard, metrics.rightMargin, metrics.rightMargin, metrics.artboardHeight, leftCenterPoint);

// Create top rectangle
var topCenterPoint = {x: metrics.artboardWidth/2, y: metrics.top - metrics.topMargin/2};
var topCenterPoint = { x: metrics.artboardWidth / 2, y: metrics.top - metrics.topMargin / 2 };
createRectangle(artboard, metrics.topMargin, metrics.artboardWidth, metrics.topMargin, topCenterPoint);

// Create bottom rectangle
var bottomCenterPoint = {x: metrics.artboardWidth/2, y: metrics.bottom + metrics.bottomMargin/2};
var bottomCenterPoint = { x: metrics.artboardWidth / 2, y: metrics.bottom + metrics.bottomMargin / 2 };
createRectangle(artboard, metrics.bottomMargin, metrics.artboardWidth, metrics.bottomMargin, bottomCenterPoint);
}
} else {
Expand All @@ -48,11 +47,11 @@ function createMarginGuide(selection) {
}

function removeMarginGuide(selection) {
selection.items.forEach(function(node){
if(node instanceof Artboard && (node.guid in artboards)){
selection.items.forEach(function (node) {
if (node instanceof Artboard && (node.guid in artboards)) {
var artboard = node;
artboard.children.forEach(function(child){
if (artboards[artboard.guid].indexOf(child.guid) > -1){
artboard.children.forEach(function (child) {
if (artboards[artboard.guid].indexOf(child.guid) > -1) {
child.removeFromParent();
}
})
Expand All @@ -62,7 +61,7 @@ function removeMarginGuide(selection) {
}


function createRectangle(artboard, marginLength, marginWidth, marginHeight, placementCenterPoint){
function createRectangle(artboard, marginLength, marginWidth, marginHeight, placementCenterPoint) {
// Create and place the rectangle
var rectangle = new Rectangle();
rectangle.width = marginWidth;
Expand All @@ -76,26 +75,26 @@ function createRectangle(artboard, marginLength, marginWidth, marginHeight, plac
var text = new Text();
text.text = marginLength.toString();
text.styleRanges = [{
length: text.text.length,
fontFamily: 'Impact',
fontStyle: 'Regular',
fontSize: 40,
charSpacing: 0,
underline: false,
fill: new Color("#000")
}];
text.stroke = new Color("#fff");
text.strokeEnabled = true;
text.strokeWidth = 1;
text.visible = true;
length: text.text.length,
fontFamily: 'Impact',
fontStyle: 'Regular',
fontSize: 40,
charSpacing: 0,
underline: false,
fill: new Color("#000")
}];
text.stroke = new Color("#fff");
text.strokeEnabled = true;
text.strokeWidth = 1;
text.visible = true;
artboard.addChild(text);
text.placeInParentCoordinates(text.localCenterPoint, placementCenterPoint);

artboards[artboard.guid].push(rectangle.guid);
artboards[artboard.guid].push(text.guid);
}

function getMetrics(artboard){
function getMetrics(artboard) {

var metrics = {};
metrics.artboardHeight = artboard.height;
Expand All @@ -109,7 +108,7 @@ function getMetrics(artboard){
metrics.bottom = 0;
metrics.bottomMargin = 0;

artboard.children.forEach(function(artboardChild){
artboard.children.forEach(function (artboardChild) {
if (artboardChild.boundsInParent.x < metrics.left) {
metrics.left = artboardChild.boundsInParent.x;
metrics.leftMargin = artboardChild.boundsInParent.x;
Expand All @@ -132,8 +131,8 @@ function getMetrics(artboard){


module.exports = {
commands: {
createMarginGuide: createMarginGuide,
removeMarginGuide: removeMarginGuide
}
commands: {
createMarginGuide: createMarginGuide,
removeMarginGuide: removeMarginGuide
}
};

0 comments on commit 757d67a

Please sign in to comment.