-
Notifications
You must be signed in to change notification settings - Fork 10
/
Editor.sketchplugin
102 lines (74 loc) · 2.02 KB
/
Editor.sketchplugin
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
// (ctrl shift e)
if([selection count] == 0)
{
custom_log("No selection found")
return
}
var target = selection[0]
var userInfo = [target userInfo]
if(userInfo == nil)
{
userInfo = [NSMutableDictionary new]
}
else
{
userInfo = [userInfo mutableCopy]
}
custom_log("Current userInfo: "+userInfo)
var viewClassString = userInfo["Sketch2Code.viewClass"]
if(viewClassString == nil)
{
var viewMap = loadObjectMap()
var targetClassName = NSStringFromClass([target class])
var viewMapDescription = viewMap[targetClassName]
if(viewMapDescription)
{
viewClassString = viewMap[targetClassName]["viewClass"] + ""
}
else
{
viewClassString = targetClassName
}
}
var updatedViewClassString = doc.askForUserInput_initialValue("Override default UIKit class for " + [target name], viewClassString)
custom_log("New class: " + updatedViewClassString)
userInfo["Sketch2Code.viewClass"] = updatedViewClassString
target.userInfo = userInfo
//Should be common
function loadObjectMap()
{
var path = pluginPath()
path = [path stringByAppendingString:@"view.map"]
custom_log("Loading map at:" + path)
id map = loadJSON(path)
custom_log("" + map)
custom_log("Map loading [Done]")
return map
}
function loadJSON(filePath)
{
var errorPointer = MOPointer.alloc().initWithValue(nil)
var data = [NSData dataWithContentsOfFile:filePath options:0 error:errorPointer]
if(errorPointer.value())
{
custom_log("Error:" + errorPointer.value())
}
var json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:errorPointer]
if(errorPointer.value())
{
custom_log("Error:" + errorPointer.value())
}
return json
}
function pluginPath()
{
var path = MSPluginManager.pluginsURL();
path = [path absoluteString]
path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
path = [path stringByReplacingOccurrencesOfString:@"file://" withString:@""]
return path
}
function custom_log(string)
{
log("SKETCH2CODE | " + string)
}