-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathimport.js
executable file
·222 lines (176 loc) · 7.76 KB
/
import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
@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. Select SVG Font
font = Library.fetch.selectPanel("Select SVG file of icon-font","file")
svg = Library.fetch.file(font.path()).toString()
fontname = svg.match(/id=".+?"/)[0].split('"')[1]
if (fontname == "fontawesomeregular")
fontname = "FontAwesome"
if (fontname == "mfg_labs_iconsetregular")
fontname = "MFG Labs Iconset"
if (fontname == "batch_iconsregular")
fontname = "Batch"
if (fontname == "weather_iconsregular")
fontname = "Weather Icons"
if (fontname == "black_tielight")
fontname = "Black Tie Light"
if (fontname == "black_tieregular")
fontname = "Black Tie Regular"
if (fontname == "black_tiebold")
fontname = "Black Tie Bold"
if (fontname == "black_tiesolid")
fontname = "Black Tie Solid"
// 2. Create Window
var wrapper = Library.Widgets.window("Import Font",fontname)
// 3. Create a text field to write icon container
var container = [[NSTextField alloc] initWithFrame:NSMakeRect(200,275,200,24)]
container.setBackgroundColor(NSColor.clearColor())
container.setPlaceholderString(@"Write icon-container here")
container.setTarget(self)
container.setCOSJSTargetFunction(function(sender){
wrapper.window.orderOut(nil)
NSApp.stopModalWithCode(NSOKButton)
})
wrapper.main.addSubview(container)
// 3.1. Create a shortcut
var shortcut = [[NSTextField alloc] initWithFrame:NSMakeRect(200,145,200,24)]
shortcut.setBackgroundColor(NSColor.clearColor())
shortcut.setPlaceholderString(@"Write command shortcut here")
shortcut.setTarget(self)
shortcut.setCOSJSTargetFunction(function(sender){
wrapper.window.orderOut(nil)
NSApp.stopModalWithCode(NSOKButton)
})
wrapper.main.addSubview(shortcut)
hint = Library.Widgets.subtitle("You need to write like this: <i class='fa fa-*****'></i>",14,NSColor.grayColor(),NSMakeRect(130, 220, 500, 30))
wrapper.main.addSubview(hint)
hint2 = Library.Widgets.subtitle("It will be like this: <i class='fa fa-iconname'></i>",14,NSColor.grayColor(),NSMakeRect(145, 190, 500, 30))
wrapper.main.addSubview(hint2)
hint3 = Library.Widgets.subtitle("You need to write like this: cmd ctrl f",14,NSColor.grayColor(),NSMakeRect(170, 100, 500, 30))
wrapper.main.addSubview(hint3)
// 3.2. Create a button to start import
var submit = [[NSButton alloc] initWithFrame:NSMakeRect(230, 30, 200, 50)]
submit.setTitle("")
submit.setAction("callAction:")
submit.setWantsLayer(true)
submit.setCOSJSTargetFunction(function(sender) {
wrapper.window.orderOut(nil)
NSApp.stopModalWithCode(NSOKButton)
})
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 = "+ Import this font"
submit.setLayer(submit_text)
wrapper.main.addSubview(submit)
// 3.3 Parse Glyphs of Font
glyphs = svg.match(/<glyph[^>]*?>/g)
var list = {"icons": []}
for (i = 0; i < glyphs.length; i++) {
glyph = glyphs[i]
if (glyph.match(/glyph-name=".+?"/)) {
name = glyph.match(/glyph-name=".+?"/)[0].split('"')[1]
} else {
name = "icon"+i
}
if (glyph.match(/unicode=".+?"/)) {
unicode = glyph.match(/unicode=".+?"/)[0].split('"')[1]
sliced = unicode.slice(3,7)
icon = {"name":name,"id":name,"unicode":sliced,"created":1}
if (glyph.match(/d=".+?"/)) {
list.icons.push(icon)
}
}
}
json = JSON.stringify(list)
// 4. Build a directory for custom icon-fonts
var bundle_folder = resources + "/bundle"
if (![fileManager fileExistsAtPath:bundle_folder]) {
[fileManager createDirectoryAtPath:bundle_folder withIntermediateDirectories:true attributes:nil error:nil]
}
// 5. Build a json file for new font into bundle_folder folder
if (![fileManager fileExistsAtPath:bundle_folder +"/"+ fontname + ".json"]) {
[fileManager createFileAtPath:bundle_folder +"/"+ fontname + ".json" contents:"" attributes:nil]
}
// 8. Find path of manifest.json
var manifestPath = directoryPlugin + "/manifest.json"
// 9. Fetch data of manifest.json
var data = [NSData dataWithContentsOfFile:manifestPath]
manifest = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]
// 10. Add a command to manifest.json
count = manifest.commands.count()
if (manifest.menu.items.count() < 4) {
var structure = [NSData dataWithContentsOfFile:directoryPlugin + "/structure.json"]
form = [NSJSONSerialization JSONObjectWithData:structure options:NSJSONReadingMutableContainers error:nil]
[manifest setValue:[form objectForKey:@"menu"] forKeyPath:@"menu"];
[manifest setValue:[form objectForKey:@"commands"] forKeyPath:@"commands"];
count = 7;
}
var response = NSApp.runModalForWindow(wrapper.window)
// if is the response is ok, import font
if (response == NSOKButton) {
// Create shortcut for new font
writed_shortcut = shortcut.stringValue()
if (!(writed_shortcut && writed_shortcut.length() > 0)) {
writed_shortcut = nil
}
// add command to manifest
menu = [manifest objectForKey:@"menu"]
items = [menu objectForKey:@"items"]
first = [items objectAtIndex:0]
firstlist = [first objectForKey:@"items"]
// put nextid
nextid = count +1
// search nextid is true?
for (i = 0; i < firstlist.count(); i++) {
item = firstlist[i]
if (item == nextid + "_add_grid") {
nextid = count+30
}
}
command = {
"script": "add_grid.js",
"handler": "handleFont",
"name": fontname,
"identifier": nextid + "_add_grid",
"shortcut": writed_shortcut
}
[[manifest objectForKey:@"commands"] addObject:command]
[[first objectForKey:@"items"] addObject:@""+nextid + "_add_grid"]
// 10/2. Write font data to json file
var t = [NSString stringWithFormat:@"%@", json]
f = [NSString stringWithFormat:@"%@", bundle_folder +"/"+ fontname + ".json"]
[t writeToFile:f atomically:true encoding:NSUTF8StringEncoding error:nil]
// 11. Write new data to manifest.file
Library.create.file(manifest,manifestPath)
// 12. Fetch font.json file
properties = Library.fetch.json("fonts.json",plugin)
// 13. Create properties for new font
writed_cont = container.stringValue()
// if user didnt write a container create example
if (!(writed_cont && writed_cont.length() > 0)) {
writed_cont = "<i class='"+fontname+"-*****'></i>"
}
property = {
"path": fontname + ".json",
"container": writed_cont
}
// 14. Add new font properties to font.json
if ([properties isKindOfClass:[NSDictionary class]]) {
dict = [[NSMutableDictionary alloc] initWithDictionary:properties]
list = [[NSMutableDictionary alloc] initWithDictionary:[dict objectForKey:@"fonts"]]
list[@""+fontname] = property
dict[@"fonts"] = list
}
// 15. Write new data to fonts.json
Library.create.file(dict,resources + "/fonts.json")
}
};