Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions
80
Lottie-Export.sketchplugin/Contents/Sketch/MochaJSDelegate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// MochaJSDelegate.js | ||
// MochaJSDelegate | ||
// | ||
// Created by Matt Curtis | ||
// Copyright (c) 2015. All rights reserved. | ||
// | ||
|
||
var MochaJSDelegate = function(selectorHandlerDict){ | ||
var uniqueClassName = "MochaJSDelegate_DynamicClass_" + NSUUID.UUID().UUIDString(); | ||
|
||
var delegateClassDesc = MOClassDescription.allocateDescriptionForClassWithName_superclass_(uniqueClassName, NSObject); | ||
|
||
delegateClassDesc.registerClass(); | ||
|
||
// Handler storage | ||
|
||
var handlers = {}; | ||
|
||
// Define interface | ||
|
||
this.setHandlerForSelector = function(selectorString, func){ | ||
var handlerHasBeenSet = (selectorString in handlers); | ||
var selector = NSSelectorFromString(selectorString); | ||
|
||
handlers[selectorString] = func; | ||
|
||
if(!handlerHasBeenSet){ | ||
/* | ||
For some reason, Mocha acts weird about arguments: | ||
https://github.com/logancollins/Mocha/issues/28 | ||
We have to basically create a dynamic handler with a likewise dynamic number of predefined arguments. | ||
*/ | ||
|
||
var dynamicHandler = function(){ | ||
var functionToCall = handlers[selectorString]; | ||
|
||
if(!functionToCall) return; | ||
|
||
return functionToCall.apply(delegateClassDesc, arguments); | ||
}; | ||
|
||
var args = [], regex = /:/g; | ||
while(match = regex.exec(selectorString)) args.push("arg"+args.length); | ||
|
||
dynamicFunction = eval("(function("+args.join(",")+"){ return dynamicHandler.apply(this, arguments); })"); | ||
|
||
delegateClassDesc.addInstanceMethodWithSelector_function_(selector, dynamicFunction); | ||
} | ||
}; | ||
|
||
this.removeHandlerForSelector = function(selectorString){ | ||
delete handlers[selectorString]; | ||
}; | ||
|
||
this.getHandlerForSelector = function(selectorString){ | ||
return handlers[selectorString]; | ||
}; | ||
|
||
this.getAllHandlers = function(){ | ||
return handlers; | ||
}; | ||
|
||
this.getClass = function(){ | ||
return NSClassFromString(uniqueClassName); | ||
}; | ||
|
||
this.getClassInstance = function(){ | ||
return NSClassFromString(uniqueClassName).new(); | ||
}; | ||
|
||
// Conveience | ||
|
||
if(typeof selectorHandlerDict == "object"){ | ||
for(var selectorString in selectorHandlerDict){ | ||
this.setHandlerForSelector(selectorString, selectorHandlerDict[selectorString]); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
@import 'sketchLottieExport.js' | ||
|
||
// TODO: turn only layers with prefix into images | ||
// TODO: nested artboards (overlapping artboards) – ignore inner ones | ||
// TODO: Add outline option for nested/overlapping artboards | ||
|
||
// Global initalised variables from 'context' | ||
var selection, doc, scriptPath, scriptFolder, app | ||
var manifestJSON, iconImage | ||
|
||
function onSetUp(context) { | ||
selection = context.selection | ||
doc = context.document | ||
scriptPath = context.scriptPath | ||
scriptFolder = scriptPath.stringByDeletingLastPathComponent() | ||
app = NSApplication.sharedApplication() | ||
} | ||
|
||
|
||
// **************************** | ||
// Handlers | ||
// **************************** | ||
|
||
function exportSelection(context) { | ||
|
||
var selection = context.selection; | ||
var artboards = [] | ||
selection.forEach(function(selectedArtboard) { | ||
if (selectedArtboard.isMemberOfClass(MSArtboardGroup) || selectedArtboard.isMemberOfClass(MSSymbolMaster)) { | ||
artboards.push(selectedArtboard) | ||
} | ||
}) | ||
if (artboards.length > 0) { | ||
exportArtboards(artboards) | ||
} else { | ||
alertNoArtboards(noArtboardsAlertMessage()) | ||
} | ||
} | ||
|
||
|
||
|
||
// Return the appropriate alert message for when there is no artboards | ||
function noArtboardsAlertMessage() { | ||
return "Please select artboard(s) or symbol(s) and try again." | ||
} | ||
|
||
// **************************** | ||
// Helper Methods | ||
// **************************** | ||
|
||
// Show an alert when there are no Artboards | ||
function alertNoArtboards(message) { | ||
var alert = NSAlert.alloc().init() | ||
alert.setIcon(iconImage) | ||
alert.setMessageText("Lottie Export - No Artboards/Symbols") | ||
alert.setInformativeText(message) | ||
alert.addButtonWithTitle("Got it") | ||
return alert.runModal() | ||
} | ||
|
||
|
||
// Return the version number for sketch — turned into a single integer | ||
// e.g. '3.8.5' => 385, '40.2' => 402 | ||
function sketchVersionNumber() { | ||
var version = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") | ||
var versionNumber = version.stringByReplacingOccurrencesOfString_withString(".", "") + "" | ||
while(versionNumber.length != 3) { | ||
versionNumber += "0" | ||
} | ||
return parseInt(versionNumber) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "Lottie export", | ||
"description": "Export artboards to Lottie — from the current page, all pages or current selection", | ||
"author": "Brandon Withrow", | ||
"homepage": "https://github.com/buba447/Lottie-Sketch-Export", | ||
"version": "0.1", | ||
"identifier": "com.withrow.sketch.lottie-export", | ||
"appcast": "TODO", | ||
"compatibleVersion": "39", | ||
"bundleVersion": "1.0", | ||
"commands": [ | ||
{ | ||
"name": "Export selected artboards/symbols", | ||
"identifier": "selectedArtboards", | ||
"shortcut": "", | ||
"script": "main.js", | ||
"handler": "exportSelection", | ||
"icon": "icons\/selection.png", | ||
"description": "Export all selected Artboards and Symbols into Lottie JSON" | ||
} | ||
], | ||
"menu": { | ||
"title" : "Lottie export", | ||
"items": [ | ||
"selectedArtboards" | ||
] | ||
} | ||
} |
Oops, something went wrong.