Skip to content

Commit a76136b

Browse files
author
Lukas Ondrej
committed
SVG icon placeholders
To add an icon placeholder: - Create a shape group (rectangle, oval, a flattened shape, etc) and set any of its properties as desired (all properties will apply to the final icon once populated) - Name the shape group layer such as {{sample_icon}} (double curly brackets must be used to indicate an icon placeholder) - Specify the path to the SVG icon in your data, for example, "sample_icon": "assets/icons/sample_icon.svg" NOTES: - Icons must be in SVG format, preferably flattened into a single layer. If the icon is not flattened, union boolean operation will be applied to it upon import to merge it into a single layer. - Importing many SVGs into Sketch can take a while. If you need to populate many icon placeholders, make sure you wait until the icons appear (it can take more than 5 seconds with hundreds of icons)
1 parent 64c5f45 commit a76136b

6 files changed

Lines changed: 362 additions & 68 deletions

File tree

Sketch Data Populator.sketchplugin/Contents/Sketch/library.cocoascript

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@ var Library = {};
1818
//find layers
1919
self.findLayersInLayer = findLayersInLayer;
2020
self.findLayerInLayer = findLayerInLayer;
21+
self.findPageWithName = findPageWithName;
2122
self.resizeTextLayer = resizeTextLayer;
2223
self.refreshTextLayer = refreshTextLayer;
2324
self.getSelectedLayers = getSelectedLayers;
2425

2526
//check layer type
2627
self.isLayerGroup = isLayerGroup;
28+
self.isLayerShapeGroup = isLayerShapeGroup;
2729
self.isLayerText = isLayerText;
30+
self.addPage = addPage;
2831

2932
//other
3033
self.mergeStringWithValues = mergeStringWithValues;
3134
self.randomInteger = randomInteger;
35+
self.delay = delay;
36+
self.scaleToBoundingBox = scaleToBoundingBox;
37+
// self.importSVG = importSVG;
3238

3339
//ui elements
3440
self.createAlert = createAlert;
@@ -194,13 +200,37 @@ var Library = {};
194200
}
195201

196202

197-
function resizeTextLayer(layer) {
198-
[layer adjustFrameToFit];
199-
[layer select: true byExpandingSelection: false];
200-
[layer setIsEditingText: true];
201-
[layer setIsEditingText: false];
202-
[layer select: false byExpandingSelection: false];
203-
}
203+
function findPageWithName(name, fullMatch) {
204+
205+
var doc = context.document;
206+
var pages = jsArray([doc pages]);
207+
for(var i = 0; i < pages.length; i++) {
208+
var currentPage = pages[i];
209+
210+
//if page matches name
211+
if(fullMatch) {
212+
if([currentPage name] == name) {
213+
return currentPage;
214+
}
215+
}
216+
else {
217+
if([currentPage name].indexOf(name) > -1) {
218+
return currentPage;
219+
}
220+
}
221+
}
222+
223+
return;
224+
}
225+
226+
227+
function resizeTextLayer(layer) {
228+
[layer adjustFrameToFit];
229+
[layer select: true byExpandingSelection: false];
230+
[layer setIsEditingText: true];
231+
[layer setIsEditingText: false];
232+
[layer select: false byExpandingSelection: false];
233+
}
204234

205235

206236
function refreshTextLayer(layer) {
@@ -227,11 +257,35 @@ var Library = {};
227257
}
228258

229259

260+
function isLayerShapeGroup(layer) {
261+
return ([layer isKindOfClass: [MSShapeGroup class]]);
262+
}
263+
264+
230265
function isLayerText(layer) {
231266
return ([layer isKindOfClass: [MSTextLayer class]]);
232267
}
233268

234269

270+
function addPage(name) {
271+
272+
//get doc
273+
var doc = context.document;
274+
275+
//get current page
276+
var currentPage = doc.currentPage();
277+
278+
//create new page
279+
var page = doc.addBlankPage();
280+
page.setName(name);
281+
282+
//make current page active again
283+
doc.setCurrentPage(currentPage);
284+
285+
return page;
286+
}
287+
288+
235289
function mergeStringWithValues(string, values) {
236290

237291
//get properties in values
@@ -255,6 +309,41 @@ var Library = {};
255309
}
256310

257311

312+
function delay(callback, delay, opt) {
313+
[coscript setShouldKeepAround:true];
314+
var cosi = [coscript scheduleWithInterval:delay jsFunction:function(cinterval) {
315+
callback(opt);
316+
}];
317+
}
318+
319+
320+
function scaleToBoundingBox(boundingFrame, contentFrame) {
321+
322+
//prepare final frame
323+
var finalFrame = {};
324+
325+
//get ratio
326+
var boundingShortestSide = (boundingFrame.width < boundingFrame.height) ? boundingFrame.width : boundingFrame.height;
327+
var contentLongestSide = (contentFrame.width > contentFrame.height) ? contentFrame.width : contentFrame.height;
328+
var ratio = boundingShortestSide / contentLongestSide;
329+
330+
//set final frame size
331+
finalFrame.width = contentFrame.width * ratio;
332+
finalFrame.height = contentFrame.height * ratio;
333+
334+
//set final frame position
335+
finalFrame.x = boundingFrame.x + ((boundingFrame.width - finalFrame.width) / 2);
336+
finalFrame.y = boundingFrame.y + ((boundingFrame.height - finalFrame.height) / 2);
337+
338+
return finalFrame;
339+
}
340+
341+
342+
function importSVG(path, callback) {
343+
344+
}
345+
346+
258347
function createLabel(text, fontSize, bold, frame) {
259348

260349
var label = [[NSTextField alloc] initWithFrame:frame];

0 commit comments

Comments
 (0)