Skip to content

Commit

Permalink
1.0.4 - handle nested nested overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
robintindale committed Oct 14, 2017
1 parent 7bddd15 commit 2fd1a0a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "Library Symbol Replacer",
"identifier" : "com.zeroheight.library-symbol-replacer",
"version" : "1.0.3",
"version" : "1.0.4",
"description" : "replace local symbols with library symbols",
"authorEmail" : "support@zeroheight.com",
"author" : "zeroheight team",
Expand Down
28 changes: 27 additions & 1 deletion library-symbol-replacer.sketchplugin/Contents/Sketch/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var replaceSymbols = function(context) {
if(instances.length > 0){
totalInstances += instances.length;
imports.push({
localSymbol:localSymbol,
localSymbol: localSymbol,
librarySymbol: librarySymbol,
localInstances: instances
});
Expand All @@ -63,19 +63,31 @@ var replaceSymbols = function(context) {
return;
}

var idmap = {};
for(var i = 0 ; i < imports.length ; ++i){
var obj = imports[i];

// import the symbol into the document
var importedSymbol = librariesController().importForeignSymbol_fromLibrary_intoDocument_(
obj.librarySymbol, library, documentData);

var localID = String(obj.localSymbol.symbolID());
var foreignID = String(importedSymbol.symbolMaster().symbolID());
idmap[localID] = foreignID;

// replace all local instances with the newly imported symbol
for(var j = 0 ; j < obj.localInstances.length ; ++j){
obj.localInstances[j].changeInstanceToSymbol(importedSymbol.symbolMaster());
}
}

// ensure there are no dangling overrides still pointing to local symbols
var allInstances = getAllInstances(context.document);
for(var i = 0 ; i < allInstances.length ; ++i){
log('updating instance ' + allInstances[i]);
MSLayerPaster.updateOverridesOnInstance_withIDMap_(allInstances[i], idmap);
}

var decision = yesNoDialog('Cool! All done.\n\nDo you want to delete the ' +
imports.length + ' symbol' + (imports.length === 1 ? '' : 's')
+ ' which have now been replaced? '+
Expand All @@ -89,6 +101,20 @@ var replaceSymbols = function(context) {
}
}

var getAllInstances = function(document){
var predicate = NSPredicate.predicateWithFormat("className == %@", "MSSymbolInstance");
var filteredArray = NSArray.array()
var loopPages = document.pages().objectEnumerator()
var page = null;
var scope = null;
while (page = loopPages.nextObject()) {
scope = page.children();
filteredArray = filteredArray.arrayByAddingObjectsFromArray(
scope.filteredArrayUsingPredicate(predicate))
}
return filteredArray;
}

var showAlert = function(message){
var app = NSApplication.sharedApplication();
app.displayDialog_withTitle(message, 'Library Symbol Replacer');
Expand Down

0 comments on commit 2fd1a0a

Please sign in to comment.