-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy and Paste for Framer.sketchplugin
133 lines (101 loc) · 3.37 KB
/
Copy and Paste for Framer.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
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
// (shift cmd c)
// This plugin copies FramerJS boiler plate code for the currently selected layer.
// Example:
/*
# Import Table Cell from Sketch
tableCell = layers.Table_Cell
# Add states for Table Cell
tableCell.states.add
hidden: {opacity: 0}
# Animation options for Table Cell
tableCell.states.animationOptions =
curve: "ease-in"
time: 0.25
# Interaction for Table Cell
tableCell.on Events.Click, ->
tableCell.states.next()
*/
if (selection.count() == 1) {
// ask user for the Framer object that stores all Sketch layers
var framer;
// remember the Framer/Sketch object for the current Sketch runtime session
var persistent = [[NSThread mainThread] threadDictionary];
if (persistent["com.getflourish.framer"] == null) {
var value = String([doc askForUserInput:"Name of Framer Import:" initialValue:"layers"]);
persistent["com.getflourish.framer"] = value;
}
framer = persistent["com.getflourish.framer"];
// get selected layer
var layer = selection[0];
// layer name
var name = layer.name();
// replace spaces by underscores
var cleanName = name.replace(" ", "_");
// camel case for use in Framer
var layerName = toCamelCase(cleanName);
// make the final definition
var definition = "# Import " + name + " from Sketch";
definition += "\n";
definition += layerName + " = " + framer + "." + cleanName;
definition += "\n";
definition += "\n";
// Handle artboards, groups and layers independently
if (layer.className() == "MSArtboardGroup") {
definition += "# Make artboard visible";
definition += "\n";
definition += layerName + ".visible = true";
// copy definition to the clipboard
setClipboard(definition);
// show message
doc.showMessage("Copied " + name + " for FramerJS.");
} else if (layer.className() == "MSLayerGroup") {
// States
definition += "# Add states for " + name;
definition += "\n";
definition += layerName + ".states.add";
definition += "\n\t";
definition += "hidden: {opacity: 0}";
definition += "\n";
definition += "\n";
// Animation Options
definition += "# Animation options for " + name;
definition += "\n";
definition += layerName + ".states.animationOptions =";
definition += "\n\t";
definition += 'curve: "ease-in"';
definition += "\n\t";
definition += 'time: 0.25';
// Interaction
definition += "\n";
definition += "\n";
definition += "# Interaction for " + name;
definition += "\n";
definition += layerName + ".on Events.Click, ->"
definition += "\n\t";
definition += layerName + ".states.next()";
// copy definition to the clipboard
setClipboard(definition);
// show message
doc.showMessage("Copied " + name + " for FramerJS.");
} else {
doc.showMessage("Framer can only import Groups");
}
}
// will convert any string to camel case
function toCamelCase(str) {
return str
.replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
.replace(/\s/g, '')
.replace(/^(.)/, function($1) { return $1.toLowerCase(); })
.replace("_", "")
.replace("/", "");
}
// set the clipboard to the given text
function setClipboard(text) {
pasteBoard = null;
if(typeof text === 'undefined') return null;
if(!this.pasteBoard) this.pasteBoard = NSPasteboard.generalPasteboard();
this.pasteBoard.declareTypes_owner([NSPasteboardTypeString], null);
this.pasteBoard.setString_forType(text, NSPasteboardTypeString);
return true;
}