Skip to content

Commit

Permalink
Merge branch 'release/v1.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
borulday committed Mar 29, 2021
2 parents 6241f37 + 4f12773 commit ba51306
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 48 deletions.
35 changes: 21 additions & 14 deletions Zeplin.sketchplugin/Contents/Sketch/main.cocoascript
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
@import "utils.cocoascript";

var onRun = function (context) {
if (!documentExportable(context)) {
var temporaryPath = temporaryPathForDesignFile(context);
if (!temporaryPath) {
return;
}

Expand All @@ -11,27 +12,30 @@ var onRun = function (context) {
return;
}

exportArtboards(context, artboards);
exportArtboards(context, artboards, temporaryPath);
}

var exportColors = function (context) {
if (!documentExportable(context)) {
var temporaryPath = temporaryPathForDesignFile(context);
if (!temporaryPath) {
return;
}

exportObjectsForType(context, @"colors");
exportObjectsForType(context, @"colors", temporaryPath);
}

var exportTextStyles = function (context) {
if (!documentExportable(context)) {
var temporaryPath = temporaryPathForDesignFile(context);
if (!temporaryPath) {
return;
}

exportObjectsForType(context, @"textStyles");
exportObjectsForType(context, @"textStyles", temporaryPath);
}

var exportArtboardsFromCurrentPage = function (context) {
if (!documentExportable(context)) {
var temporaryPath = temporaryPathForDesignFile(context);
if (!temporaryPath) {
return;
}

Expand All @@ -43,11 +47,12 @@ var exportArtboardsFromCurrentPage = function (context) {
return;
}

exportArtboards(context, artboards);
exportArtboards(context, artboards, temporaryPath);
}

var exportArtboardsFromAllPages = function (context) {
if (!documentExportable(context)) {
var temporaryPath = temporaryPathForDesignFile(context);
if (!temporaryPath) {
return;
}

Expand All @@ -59,11 +64,12 @@ var exportArtboardsFromAllPages = function (context) {
return;
}

exportArtboards(context, artboards);
exportArtboards(context, artboards, temporaryPath);
}

var exportSymbolsFromCurrentPage = function (context) {
if (!documentExportable(context)) {
var temporaryPath = temporaryPathForDesignFile(context);
if (!temporaryPath) {
return;
}

Expand All @@ -75,11 +81,12 @@ var exportSymbolsFromCurrentPage = function (context) {
return;
}

exportArtboards(context, artboards);
exportArtboards(context, artboards, temporaryPath);
}

var exportSymbolsFromAllPages = function (context) {
if (!documentExportable(context)) {
var temporaryPath = temporaryPathForDesignFile(context);
if (!temporaryPath) {
return;
}

Expand All @@ -91,7 +98,7 @@ var exportSymbolsFromAllPages = function (context) {
return;
}

exportArtboards(context, artboards);
exportArtboards(context, artboards, temporaryPath);
}

var excludeSublayers = function (context) {
Expand Down
2 changes: 1 addition & 1 deletion Zeplin.sketchplugin/Contents/Sketch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"author": "Zeplin, Inc.",
"authorEmail": "dev@zeplin.io",
"homepage": "https://zeplin.io",
"version": "1.9",
"version": "1.10",
"identifier": "io.zeplin.sketch-plugin",
"icon": "Icons/icZeplin.png",
"commands": [{
Expand Down
84 changes: 51 additions & 33 deletions Zeplin.sketchplugin/Contents/Sketch/utils.cocoascript
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
var documentExportable = function (context) {
var doc = context.document;
var temporaryPath = function() {
var name = [[[NSUUID UUID] UUIDString] stringByAppendingPathExtension:@"sketch"];
var temporaryDirectory = NSTemporaryDirectory();
var temporaryZeplinDirectory = [temporaryDirectory stringByAppendingPathComponent:@"io.zeplin.osx"];

if (![doc fileURL] || [doc isDraft]) {
[NSApp displayDialog:@"Please save the document before exporting to Zeplin." withTitle:@"Document not saved"];
var isDir = MOPointer.alloc().initWithValue_(false);
var fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:temporaryZeplinDirectory isDirectory:isDir] || isDir.value() == false) {
[fileManager removeItemAtPath:temporaryZeplinDirectory error:nil];
[fileManager createDirectoryAtPath:temporaryZeplinDirectory withIntermediateDirectories:true attributes:nil error:nil];
}

var path = [temporaryZeplinDirectory stringByAppendingPathComponent:name];

name = nil;
temporaryDirectory = nil;
temporaryZeplinDirectory = nil;
isDir = nil;
fileManager = nil;

return false;
return path;
}

var temporaryPathForDesignFile = function (context) {
var doc = context.document;
var path = temporaryPath();

[doc showMessage:@"Saving document…"];
[doc saveToURL:[NSURL fileURLWithPath: path] ofType:@"com.bohemiancoding.sketch.drawing.single" forSaveOperation:NSSaveToOperation delegate:nil didSaveSelector:nil contextInfo:nil];

var fileManager = [NSFileManager defaultManager];
var stopTime = [NSDate dateWithTimeIntervalSinceNow:30];
while (![fileManager fileExistsAtPath:path] && [stopTime compare:[NSDate date]] == NSOrderedDescending) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

if ([doc isDocumentEdited]) {
var alert = [NSAlert alertWithMessageText:@"Document not saved" defaultButton:@"Save and Continue" alternateButton:@"Cancel" otherButton:@"Continue" informativeTextWithFormat:@"To capture the latest changes in this Sketch document, Zeplin needs to save it first.\n\n☝️ This might take a bit, depending on the document size."];

var response = [alert runModal];
if (response == NSAlertDefaultReturn) {
[doc showMessage:@"Saving document…"];

[doc saveDocument:nil];
while ([doc isDocumentEdited]) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
} else if (response == NSAlertAlternateReturn) {
return false;
}

response = nil;
alert = nil;

if (![fileManager fileExistsAtPath:path]) {
[doc showMessage:@"Saving document failed…"];

return nil;
}

return true;
fileManager = nil;
stopTime = nil;

return path;
}

var temporaryPath = function() {
var directivesPath = function() {
var name = [[[NSUUID UUID] UUIDString] stringByAppendingPathExtension:@"zpl"];
var temporaryDirectory = NSTemporaryDirectory();
var path = [temporaryDirectory stringByAppendingPathComponent:name];
Expand All @@ -40,7 +57,7 @@ var temporaryPath = function() {
return path;
}

var defaultDirectives = function(context, path) {
var defaultDirectives = function(context, temporaryPath) {
var doc = context.document;

var format = @"json";
Expand Down Expand Up @@ -90,6 +107,7 @@ var defaultDirectives = function(context, path) {
var sketchmigratePath = [[NSBundle mainBundle] pathForResource:@"sketchmigrate" ofType:nil inDirectory:@"sketchtool/bin"];

var directives = [NSMutableDictionary dictionary];
[directives setObject:temporaryPath forKey:@"temporaryPath"];
[directives setObject:[[doc fileURL] path] forKey:@"path"];
[directives setObject:format forKey:@"format"];
[directives setObject:assetLibraries forKey:@"assetLibraries"];
Expand Down Expand Up @@ -141,7 +159,7 @@ var launchZeplin = function (context, path) {
applicationPath = nil;
}

var exportArtboards = function (context, artboards) {
var exportArtboards = function (context, artboards, temporaryPath) {
var doc = context.document;

var foreignSymbolsUpToDate = true;
Expand Down Expand Up @@ -242,9 +260,8 @@ var exportArtboards = function (context, artboards) {

artboard = nil;
allArtboardsLoop = nil;

var path = temporaryPath();
var directives = defaultDirectives(context, path);

var directives = defaultDirectives(context, temporaryPath);
[directives setObject:@"artboards" forKey:@"type"];
[directives setObject:artboardIds forKey:@"artboardIds"];
[directives setObject:pageIds forKey:@"pageIds"];
Expand All @@ -257,6 +274,7 @@ var exportArtboards = function (context, artboards) {
uniqueArtboardSizes = nil;
artboardNamesByIdentifier = nil;

var path = directivesPath();
writeDirectives(directives, path);

directives = nil;
Expand All @@ -266,11 +284,11 @@ var exportArtboards = function (context, artboards) {
path = nil;
}

var exportObjectsForType = function (context, type) {
var path = temporaryPath();
var directives = defaultDirectives(context, path);
var exportObjectsForType = function (context, type, temporaryPath) {
var directives = defaultDirectives(context, temporaryPath);
[directives setObject:type forKey:@"type"];

var path = directivesPath();
writeDirectives(directives, path);

directives = nil;
Expand Down

0 comments on commit ba51306

Please sign in to comment.