Skip to content

Commit

Permalink
Initial Commit. Plugin Alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
buba447 committed Mar 14, 2018
1 parent eb44231 commit b333423
Show file tree
Hide file tree
Showing 5 changed files with 565 additions and 0 deletions.
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 Lottie-Export.sketchplugin/Contents/Sketch/MochaJSDelegate.js
@@ -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]);
}
}
};
72 changes: 72 additions & 0 deletions Lottie-Export.sketchplugin/Contents/Sketch/main.js
@@ -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)
}

28 changes: 28 additions & 0 deletions Lottie-Export.sketchplugin/Contents/Sketch/manifest.json
@@ -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"
]
}
}

0 comments on commit b333423

Please sign in to comment.