diff --git a/iconfont.sketchplugin/Contents/Resources/fonts.json b/iconfont.sketchplugin/Contents/Resources/fonts.json old mode 100644 new mode 100755 index 276109e..701e6fb --- a/iconfont.sketchplugin/Contents/Resources/fonts.json +++ b/iconfont.sketchplugin/Contents/Resources/fonts.json @@ -2,4 +2,4 @@ "fonts" : { } -} +} \ No newline at end of file diff --git a/iconfont.sketchplugin/Contents/Sketch/add.js b/iconfont.sketchplugin/Contents/Sketch/add.js index 9437191..ba534cc 100755 --- a/iconfont.sketchplugin/Contents/Sketch/add.js +++ b/iconfont.sketchplugin/Contents/Sketch/add.js @@ -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) diff --git a/iconfont.sketchplugin/Contents/Sketch/add_all.js b/iconfont.sketchplugin/Contents/Sketch/add_all.js new file mode 100644 index 0000000..0a6ef0f --- /dev/null +++ b/iconfont.sketchplugin/Contents/Sketch/add_all.js @@ -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) + } + +}; diff --git a/iconfont.sketchplugin/Contents/Sketch/add_grid.js b/iconfont.sketchplugin/Contents/Sketch/add_grid.js old mode 100644 new mode 100755 index 1e31472..c7aeb6f --- a/iconfont.sketchplugin/Contents/Sketch/add_grid.js +++ b/iconfont.sketchplugin/Contents/Sketch/add_grid.js @@ -127,4 +127,6 @@ var onRun = function(context,path,fontname) { Library.create.icon(doc,selection,fontname,name,icon) } + tools.checkPluginUpdate() + }; diff --git a/iconfont.sketchplugin/Contents/Sketch/const/export.js b/iconfont.sketchplugin/Contents/Sketch/const/export.js old mode 100644 new mode 100755 diff --git a/iconfont.sketchplugin/Contents/Sketch/const/import.js b/iconfont.sketchplugin/Contents/Sketch/const/import.js old mode 100644 new mode 100755 diff --git a/iconfont.sketchplugin/Contents/Sketch/const/install.js b/iconfont.sketchplugin/Contents/Sketch/const/install.js old mode 100644 new mode 100755 diff --git a/iconfont.sketchplugin/Contents/Sketch/const/library.js b/iconfont.sketchplugin/Contents/Sketch/const/library.js index 18c415f..fd9b281 100755 --- a/iconfont.sketchplugin/Contents/Sketch/const/library.js +++ b/iconfont.sketchplugin/Contents/Sketch/const/library.js @@ -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); } @@ -244,6 +263,7 @@ var Library = { result = NSObject.alloc().init() result.unicodes = [] result.names = [] + result.number = [] for (var i=0 ; i < [icons count]; i++) { icon = icons[i]["name"] @@ -251,6 +271,7 @@ var Library = { 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) } } @@ -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); + } + } +}; diff --git a/iconfont.sketchplugin/Contents/Sketch/const/remove.js b/iconfont.sketchplugin/Contents/Sketch/const/remove.js old mode 100644 new mode 100755 index b6efd78..1d38e40 --- a/iconfont.sketchplugin/Contents/Sketch/const/remove.js +++ b/iconfont.sketchplugin/Contents/Sketch/const/remove.js @@ -1 +1 @@ -@import "library.js"; var onRun = function(context) { var plugin = context.plugin var doc = context.document var fileManager = NSFileManager.defaultManager() var scriptFullPath = context.scriptPath var directoryPlugin = [[scriptFullPath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] var resources = [directoryPlugin stringByDeletingLastPathComponent] + "/Resources" // 1. Create Window var wrapper = Library.Widgets.window("Remove Font", "Select font to remove") // 2. Fetch fonts.json file var json = Library.fetch.json("fonts.json",plugin) var fonts = [json objectForKey:@"fonts"] // 3. create a combobox to select font which wants to remove var selectbox = [[NSComboBox alloc] initWithFrame:NSMakeRect(90,175,220,24)] selectbox.addItemsWithObjectValues(fonts) selectbox.setItemHeight(20.0) selectbox.selectItemAtIndex(0) selectbox.setEditable(false) wrapper.main.addSubview(selectbox) // 4. create a remove button var submit = [[NSButton alloc] initWithFrame:NSMakeRect(350, 150, 200, 50)] submit.setTitle("") submit.setAction("callAction:") submit.setWantsLayer(true) submit.setCOSJSTargetFunction(function(sender) { wrapper.window.orderOut(nil) NSApp.stopModalWithCode(NSOKButton) }) // 4.1. create a layer for remove button var submit_text = CATextLayer.layer() submit_text.setBackgroundColor(NSColor.blackColor()) submit_text.setForegroundColor(CGColorCreateGenericRGB(215/255, 159/255, 0/255, 1.0)) submit_text.setFontSize(18) submit_text.contentsScale = NSScreen.mainScreen().backingScaleFactor() submit_text.string = "- Remove this font" submit.setLayer(submit_text) wrapper.main.addSubview(submit) var response = NSApp.runModalForWindow(wrapper.window) // if is the response is ok, remove the font if (response == NSOKButton) { selected_font = selectbox.objectValueOfSelectedItem() font = fonts[selected_font] // // 4. Remove the json file of the font // full_path = resources + "/bundle/" + font.path [fileManager removeItemAtPath:full_path error:nil] // // 5. Remove the font from fonts.json file // properties = Library.fetch.json("fonts.json",plugin) if ([properties isKindOfClass:[NSDictionary class]]) { dict = [[NSMutableDictionary alloc] initWithDictionary:properties] list = [[NSMutableDictionary alloc] initWithDictionary:[dict objectForKey:@"fonts"]] [list removeObjectForKey:@""+selected_font] dict[@"fonts"] = list } // 6. Write new data to fonts.json Library.create.file(dict,resources + "/fonts.json") // // 7. Remove the font commands from manifest.json // var manifestPath = directoryPlugin + "/manifest.json" var data = [NSData dataWithContentsOfFile:manifestPath] manifest = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] // 9. Remove the command from manifest.json commands = [manifest objectForKey:@"commands"] for (i = 0; i < commands.length(); i++) { var item = commands[i] // if (selected_font == item.name) { if ([selected_font isEqualToString:@""+item.name]) { if (![commands removeObject:item]) { log('first remove command does not work') } menuid = item.identifier menu = [manifest objectForKey:@"menu"] items = [menu objectForKey:@"items"] first = [items objectAtIndex:0] if (![[first objectForKey:@"items"] removeObject:@""+menuid]) { log('second remove command does not work') } } } log([manifest objectForKey:@"commands"].length()) // manifest commands if ([manifest objectForKey:@"commands"].length() <= 6) { items = ["install","import"] menu = [manifest objectForKey:@"menu"] [menu setValue:items forKeyPath:@"items"]; } // 11. Write new data to manifest file Library.create.file(manifest,manifestPath) doc.showMessage(selected_font + " is deleted from the plugin.") } }; \ No newline at end of file +@import "library.js"; var onRun = function(context) { var plugin = context.plugin var doc = context.document var fileManager = NSFileManager.defaultManager() var scriptFullPath = context.scriptPath var directoryPlugin = [[scriptFullPath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] var resources = [directoryPlugin stringByDeletingLastPathComponent] + "/Resources" // 1. Create Window var wrapper = Library.Widgets.window("Remove Font", "Select font to remove") // 2. Fetch fonts.json file var json = Library.fetch.json("fonts.json",plugin) var fonts = [json objectForKey:@"fonts"] // 3. create a combobox to select font which wants to remove var selectbox = [[NSComboBox alloc] initWithFrame:NSMakeRect(90,175,220,24)] selectbox.addItemsWithObjectValues(fonts) selectbox.setItemHeight(20.0) selectbox.selectItemAtIndex(0) selectbox.setEditable(false) wrapper.main.addSubview(selectbox) // 4. create a remove button var submit = [[NSButton alloc] initWithFrame:NSMakeRect(350, 150, 200, 50)] submit.setTitle("") submit.setAction("callAction:") submit.setWantsLayer(true) submit.setCOSJSTargetFunction(function(sender) { wrapper.window.orderOut(nil) NSApp.stopModalWithCode(NSOKButton) }) // 4.1. create a layer for remove button var submit_text = CATextLayer.layer() submit_text.setBackgroundColor(NSColor.blackColor()) submit_text.setForegroundColor(CGColorCreateGenericRGB(215/255, 159/255, 0/255, 1.0)) submit_text.setFontSize(18) submit_text.contentsScale = NSScreen.mainScreen().backingScaleFactor() submit_text.string = "- Remove this font" submit.setLayer(submit_text) wrapper.main.addSubview(submit) var response = NSApp.runModalForWindow(wrapper.window) // if is the response is ok, remove the font if (response == NSOKButton) { selected_font = selectbox.objectValueOfSelectedItem() font = fonts[selected_font] // // 4. Remove the json file of the font // full_path = resources + "/bundle/" + font.path [fileManager removeItemAtPath:full_path error:nil] // // 5. Remove the font from fonts.json file // properties = Library.fetch.json("fonts.json",plugin) if ([properties isKindOfClass:[NSDictionary class]]) { dict = [[NSMutableDictionary alloc] initWithDictionary:properties] list = [[NSMutableDictionary alloc] initWithDictionary:[dict objectForKey:@"fonts"]] [list removeObjectForKey:@""+selected_font] dict[@"fonts"] = list } // 6. Write new data to fonts.json Library.create.file(dict,resources + "/fonts.json") // // 7. Remove the font commands from manifest.json // var manifestPath = directoryPlugin + "/manifest.json" var data = [NSData dataWithContentsOfFile:manifestPath] manifest = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] // 9. Remove the command from manifest.json commands = [manifest objectForKey:@"commands"] for (i = 0; i < commands.count(); i++) { var item = commands[i] // if (selected_font == item.name) { if ([selected_font isEqualToString:@""+item.name]) { if (![commands removeObject:item]) { log('first remove command does not work') } menuid = item.identifier menu = [manifest objectForKey:@"menu"] items = [menu objectForKey:@"items"] first = [items objectAtIndex:0] if (![[first objectForKey:@"items"] removeObject:@""+menuid]) { log('second remove command does not work') } } } log([manifest objectForKey:@"commands"].count()) // manifest commands if ([manifest objectForKey:@"commands"].count() <= 8) { items = ["install","import"] menu = [manifest objectForKey:@"menu"] [menu setValue:items forKeyPath:@"items"]; } // 11. Write new data to manifest file Library.create.file(manifest,manifestPath) doc.showMessage(selected_font + " is deleted from the plugin.") } }; \ No newline at end of file diff --git a/iconfont.sketchplugin/Contents/Sketch/convert.js b/iconfont.sketchplugin/Contents/Sketch/convert.js old mode 100644 new mode 100755 diff --git a/iconfont.sketchplugin/Contents/Sketch/manifest.json b/iconfont.sketchplugin/Contents/Sketch/manifest.json index d887cf4..51a61a8 100755 --- a/iconfont.sketchplugin/Contents/Sketch/manifest.json +++ b/iconfont.sketchplugin/Contents/Sketch/manifest.json @@ -1,13 +1,6 @@ { "author" : "Kerem Sevencan", "commands" : [ - { - "script" : "convert.js", - "handler" : "onRun", - "shortcut" : "cmd ctrl l", - "name" : "Convert Icons [Outlines]", - "identifier" : "convert" - }, { "script" : "learn.js", "handler" : "onRun", @@ -15,6 +8,13 @@ "name" : "HTML of Selected Icon", "identifier" : "learn" }, + { + "script" : "convert.js", + "handler" : "onRun", + "shortcut" : "cmd ctrl u", + "name" : "Convert Icons [Outlines]", + "identifier" : "convert" + }, { "script" : "add.js", "handler" : "onRun", @@ -45,6 +45,12 @@ "handler" : "onRun", "name" : "Remove a Font", "identifier" : "remove" + }, + { + "script" : "add_all.js", + "handler" : "onRun", + "name" : "All Insert", + "identifier" : "add_all" } ], "menu" : { @@ -55,8 +61,8 @@ ] }, "identifier" : "com.keremciu.sketch.iconfont", - "version" : "3.3.2", - "description" : "Use icon-fonts like a boss.", - "name" : "Icon Font", + "version" : "4.0.1", + "description" : "Use icons like a boss.", + "name" : "Icons", "authorEmail" : "info@kerem.ws" } diff --git a/iconfont.sketchplugin/Contents/Sketch/structure.json b/iconfont.sketchplugin/Contents/Sketch/structure.json old mode 100644 new mode 100755 index 02f47af..90e2bab --- a/iconfont.sketchplugin/Contents/Sketch/structure.json +++ b/iconfont.sketchplugin/Contents/Sketch/structure.json @@ -45,6 +45,13 @@ "name" : "Remove a Font", "identifier" : "remove" }, + { + "script" : "add_all.js", + "handler" : "onRun", + "shortcut" : "cmd ctrl a", + "name" : "All Insert", + "identifier" : "add_all" + } ], "menu" : { "title" : "Icon Font", @@ -54,6 +61,7 @@ "items" : [ ] }, + "add_all", "add", "learn", "convert",