-
Notifications
You must be signed in to change notification settings - Fork 10
/
io.js
112 lines (81 loc) · 2.61 KB
/
io.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
//
// Contains method for IO operations
//
// Loads the map dictionary, that can tell for which Sketch cass which UIkit belongs
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
}
//Loads a snippet-type json file
function loadSnippet(snippetName)
{
var filePath = pluginPath();
filePath = [filePath stringByAppendingString:snippetName]
filePath = [filePath stringByAppendingString:@".snippet"]
custom_log("Loading snippet: " + filePath)
id snippet = loadJSON(filePath)
custom_log("Snippet loaded[Done]")
return snippet
}
////
// COMMON (Should be in a separate file)
////
//General function to load a JSON from a filePath
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())
}
return dataToJSONObject(data)
}
//Converts NSData to JSON serialzed object
function dataToJSONObject(data)
{
var errorPointer = MOPointer.alloc().initWithValue(nil)
var json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:errorPointer]
if(errorPointer.value())
{
custom_log("Error:" + errorPointer.value())
}
return json
}
//Path to the plugins folder (to reach snippets and view mapping)
function pluginPath()
{
var path = MSPluginManager.mainPluginsFolderURL();
path = [path absoluteString]
path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
path = [path stringByReplacingOccurrencesOfString:@"file://" withString:@""]
return path
}
function writeTextToFile(path, text)
{
if (typeof path !== 'string')
return false;
var string = NSString.stringWithUTF8String(text);
var errorPointer = MOPointer.alloc().initWithValue(nil)
var result = [string writeToFile:path atomically:1 encoding:NSUTF8StringEncoding error:errorPointer];
if(errorPointer.value())
{
custom_log("Error:" + errorPointer.value())
}
return result;
}
function exportImageToFile(path, layer, format)
{
var finalPath = path + "." + format
var rect = [layer absoluteDirtyRect]
var slice = [MSExportRequest requestWithName:"ExportRequest" rect:rect]
[slice setIncludeArtboardBackground:false];
custom_log("Saving slice: "+slice+" to path: "+finalPath)
[doc saveArtboardOrSlice:slice toFile:finalPath]
}