Skip to content

Commit

Permalink
4.0.1 release, all icons insert with one command, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
keremciu committed Jun 11, 2016
1 parent 71bc424 commit 55330b6
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 15 deletions.
2 changes: 1 addition & 1 deletion iconfont.sketchplugin/Contents/Resources/fonts.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"fonts" : {

}
}
}
2 changes: 1 addition & 1 deletion iconfont.sketchplugin/Contents/Sketch/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var onRun = function(context) {
writed_icon = icon_name.stringValue()
selected_font = selectbox.objectValueOfSelectedItem()
font = fonts[selected_font]
icons = Library.fetch.json(font.path,plugin)
icons = Library.fetch.json("/bundle/" + font.path,plugin)

// 6. Find matched icon
var matched = Library.fetch.icon("alias",writed_icon.lowercaseString(),icons)
Expand Down
186 changes: 186 additions & 0 deletions iconfont.sketchplugin/Contents/Sketch/add_all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
@import "const/library.js";

var handleFonts = function(context) {
var handler = context.command.name()
var font = Library.fetch.font(handler,context.plugin)

// onRun function with context, json file path, title and font name.
onRun(context,"/bundle/" + font.path,handler);
var json = Library.fetch.json("fonts.json",plugin)
}

var onRun = function(context) {

var plugin = context.plugin
var doc = context.document
var selection = context.selection.firstObject()
var filtered = false
var json = Library.fetch.json("fonts.json",plugin)
var fonts = [json objectForKey:@"fonts"]

// 1. create a wrapper windows
var wrapper = Library.Widgets.window("Add an icon - ", "Select an icon")

// 2. create list properties
var allIcons = [NSMutableArray array];
var fontcount = 0
var previousfontcount = 0

for (var font in fonts) {
var i = Object.keys(fonts).indexOf(font);
var fontsCount = fonts.count()
var path = "/bundle/" + fonts[font].path;

json = Library.fetch.json(path,plugin)
icons = [json objectForKey:@"icons"]
ic = icons.count()
// font0 = fontname
eval("font" + i + " = font");

// font0count = fontcount + previousfontcount
if (i == 0) {
eval("font" + i + "count = 0");
previousfontcount = ic
} else if ((i+1) == fontsCount) {
eval("font" + i + "count = previousfontcount");
previousfontcount = previousfontcount + ic
eval("font" + (i+1) + "count = previousfontcount");
} else {
eval("font" + i + "count = previousfontcount");
previousfontcount = previousfontcount + ic
}

allIcons = [allIcons arrayByAddingObjectsFromArray:icons]
fontcount++
}

unfilter = allIcons
count = allIcons.count()
width = 545
col_size = Math.ceil(width / 50)
row_size = Math.ceil(count / col_size)
height = Math.ceil(row_size * 50)
list = [[NSScrollView alloc] initWithFrame:NSMakeRect(25,25,554,320)]

// 3. create a button prototype for matrix
prototype = NSButtonCell.alloc().init()
prototype.setButtonType(NSToggleButton)
prototype.setTitle("-")
prototype.setBezeled(true)
prototype.setBezelStyle(NSThickSquareBezelStyle)

// 4. create a matrix
matrix = [[NSMatrix alloc] initWithFrame:NSMakeRect(0, 45, width, height)
mode:NSRadioModeMatrix prototype:prototype numberOfRows:row_size numberOfColumns:col_size];
matrix.setCellSize(NSMakeSize(47, 47))
matrix.setIntercellSpacing(NSMakeSize(2, 2))
cellArray = matrix.cells()

// 5. loop all icons
for (var c=0; c < count; c++) {
var fCountName;
for (var fc=0; fc < fontcount; fc++) {
fc_count = eval("font" + fc + "count")
if (c == fc_count) {
fCountName = eval("font" + fc)
}
}
// escape icon
icon = Library.parse.escape('\\u' + allIcons[c].unicode)
// get cell
cell = cellArray.objectAtIndex(c)
// set tooltip
[matrix setToolTip:@""+allIcons[c].name + " - " + fCountName forCell:cell];
// set title
cell.setTitle(icon)
// set font
cell.setFont([NSFont fontWithName:@""+fCountName size:20.0])
// set loop index into tag variable
cell.setTag(c)
// // cell needs to able to click itself
cell.setTarget(self)
cell.setAction("callAction:")
// // click function
cell.setCOSJSTargetFunction(function(sender) {
wrapper.window.orderOut(nil)
NSApp.stopModalWithCode(NSOKButton)
})
}

// 6. create a searchbox to filter icons
var searchbox = [[NSTextField alloc] initWithFrame:NSMakeRect(200,357,150,24)]
searchbox.setBackgroundColor(NSColor.clearColor())
searchbox.setPlaceholderString(@"Search an icon...")
searchbox.setTarget(self)
searchbox.setAction("callAction:")
searchbox.setCOSJSTargetFunction(function(sender) {
if (filtered == true)
allIcons = unfilter
// get filter
var q = searchbox.stringValue()
// search icons with filter
allIcons = Library.parse.research(q,allIcons)
// find icons with the "key"
newCount = allIcons.unicodes.length
newRows = Math.ceil(newCount / col_size)
newHeight = Math.ceil(newRows * 50)
if (newCount > col_size) {
newCol = col_size
} else {
newCol = newCount
}

// 7. new frame and data
newFrame = NSMakeRect(0, 45, width, newHeight);
[matrix setFrame:newFrame];
[matrix renewRows:newRows columns:newCol];

for (var i=0; i < newCount; i++)
{
var fCountName;
for (var fc=0; fc < fontcount; fc++) {
filteredIconNumber = allIcons.number[i]
fc_count = eval("font" + fc + "count")
next = eval("font" + (fc+1) + "count")

if (filteredIconNumber >= fc_count && filteredIconNumber <= next) {
fCountName = eval("font" + fc)
}
}

newCell = cellArray.objectAtIndex(i)
[matrix setToolTip:@""+ allIcons.names[i] + " - " + fCountName forCell:newCell];
newCell.setTitle(allIcons.unicodes[i])
// set font
newCell.setFont([NSFont fontWithName:@""+fCountName size:20.0])
newCell.setTag(i)
}

filtered = true
}];

wrapper.main.addSubview(searchbox)

list.setDocumentView(matrix)
list.setHasVerticalScroller(true)
wrapper.main.addSubview(list)

// 5. build window
var response = NSApp.runModalForWindow(wrapper.window)

selected = matrix.selectedCell().tag()
icon = matrix.selectedCell().title()
fontname = matrix.selectedCell().font().fontName()

if (filtered) {
name = allIcons.names[selected] + ' - ' + fontname
} else {
name = allIcons[selected].name + ' - ' + fontname
}

// if is the response is ok, add icon
if (response == NSOKButton) {
Library.create.icon(doc,selection,fontname,name,icon)
}

};
2 changes: 2 additions & 0 deletions iconfont.sketchplugin/Contents/Sketch/add_grid.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,6 @@ var onRun = function(context,path,fontname) {
Library.create.icon(doc,selection,fontname,name,icon)
}

tools.checkPluginUpdate()

};
Empty file modified iconfont.sketchplugin/Contents/Sketch/const/export.js
100644 → 100755
Empty file.
Empty file modified iconfont.sketchplugin/Contents/Sketch/const/import.js
100644 → 100755
Empty file.
Empty file modified iconfont.sketchplugin/Contents/Sketch/const/install.js
100644 → 100755
Empty file.
67 changes: 65 additions & 2 deletions iconfont.sketchplugin/Contents/Sketch/const/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,28 @@ var Library = {
// Valid types for parameters is Array
//
"layer": function (container, type, parameters) {
var layer = container.addLayerOfType(type);
var layer
switch(type) {
case "rectangle":
var rectangleShape = MSRectangleShape.alloc().init()
if (typeof(parameters.rect) !== 'undefined')
rectangleShape.frame = MSRect.rectWithRect(parameters.rect)
else
rectangleShape.frame = MSRect.rectWithRect(NSMakeRect(0, 0, 50, 50))
layer = MSShapeGroup.shapeWithPath(rectangleShape)
container.addLayers([layer])
break
case "group":
layer = [[MSLayerGroup alloc] init]
[container addLayers:[layer]]
break
case "text":
layer = [[MSTextLayer alloc] init]
[container addLayers:[layer]]
default:
break
}
if (typeof(parameters.name) !== 'undefined') layer.name = parameters.name;
if (typeof(parameters.rect) !== 'undefined') layer.rect = parameters.rect;
if (typeof(parameters.color) !== 'undefined') {
this.util.setFillColor(layer, parameters.color);
}
Expand Down Expand Up @@ -244,13 +263,15 @@ var Library = {
result = NSObject.alloc().init()
result.unicodes = []
result.names = []
result.number = []

for (var i=0 ; i < [icons count]; i++) {
icon = icons[i]["name"]

if (icon.lowercaseString().indexOf(q) > -1) {
result.names.push(icons[i]["id"])
result.unicodes.push(Library.parse.escape('\\u' + icons[i]["unicode"]))
result.number.push(i)
}
}

Expand Down Expand Up @@ -446,3 +467,45 @@ var Library = {
},

};

var tools = {
appVersion: "4.0.1",
versionComponents : function() {
var info = [[NSBundle mainBundle] infoDictionary];
var items = [[(info["CFBundleShortVersionString"]) componentsSeparatedByString:"."] mutableCopy];

while([items count] < 3)
[items addObject:"0"];

return items;
},
majorVersion : function() {
var items = tools.versionComponents();

return items[0];
},
minorVersion : function() {
var items = tools.versionComponents();

return items[1];
},
getJSONFromURL: function(url) {
var request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]],
response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil],
responseObj = [NSJSONSerialization JSONObjectWithData:response options:nil error:nil]
return responseObj
},
checkPluginUpdate: function(){
try{
var response = this.getJSONFromURL('https://raw.githubusercontent.com/keremciu/sketch-iconfont/master/iconfont.sketchplugin/Contents/Sketch/manifest.json')
if(response && response.version) {
var rgx = new RegExp("\\d","g");
var removeVersion = parseFloat(response.version.match(rgx).join(""))
var installedVersion = parseFloat(this.appVersion.match(rgx).join(""))
if (removeVersion > installedVersion) [doc showMessage:"New plugin update is available! Visit github.com/keremciu/sketch-iconfont"]
}
}catch(e){
log(e);
}
}
};
2 changes: 1 addition & 1 deletion iconfont.sketchplugin/Contents/Sketch/const/remove.js
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified iconfont.sketchplugin/Contents/Sketch/convert.js
100644 → 100755
Empty file.
Loading

0 comments on commit 55330b6

Please sign in to comment.