Skip to content

Commit a2b3a72

Browse files
author
Stan
committed
Merge pull request #3 from nolastan/sheets-api
Support color palettes
2 parents cfcca6f + f89f6f7 commit a2b3a72

File tree

5 files changed

+74
-17
lines changed

5 files changed

+74
-17
lines changed

Contents/Sketch/colors.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var applyColors = function (newColors, docData) {
2+
var app = NSApplication.sharedApplication();
3+
var appController = app.delegate();
4+
var colors = [];
5+
for(var i=0; i<newColors.length; i++) {
6+
var color = MSColor.colorWithSVGString("#" + newColors[i].Rgb);
7+
color.alpha = newColors[i] .Opacity;
8+
colors.push(color);
9+
}
10+
colors = MSArray.dataArrayWithArray(colors);
11+
appController.globalAssets().setPrimitiveColors(colors);
12+
appController.globalAssets().objectDidChange();
13+
}

Contents/Sketch/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
},
1818
"identifier" : "com.github.nolastan.sync",
19-
"version" : "1.0",
19+
"version" : "1.1",
2020
"description" : "Sync styles and symbols",
2121
"authorEmail" : "nolastan@gmail.com",
2222
"name" : "Sync"

Contents/Sketch/script.cocoascript

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
@import 'typography.js';
2+
@import 'colors.js';
23

34
var onRun = function(context) {
45
var doc = context.document;
56
var dict = NSThread.mainThread().threadDictionary();
67

78
showOptions();
8-
9-
var styles = getSheetsu(dict['syncSource']);
10-
11-
loadTypography(styles, doc.documentData().layerTextStyles());
9+
loadFromURL(dict['syncSource']);
1210

1311
doc.showMessage('Sync complete!');
1412

1513
updatePanelHack();
1614

17-
function getSheetsu(queryURL) {
15+
function loadFromURL(queryURL) {
16+
var data;
17+
if(queryURL.indexOf("google.com") > -1) {
18+
var sheetID=queryURL.substring(queryURL.lastIndexOf("/d/")+3,queryURL.lastIndexOf("/"));
19+
for(var i = 1; true; i++) {
20+
queryURL = "https://spreadsheets.google.com/feeds/list/" + sheetID + "/" + i + "/public/values?alt=json";
21+
data = request(queryURL);
22+
if(data == "Invalid query parameter value for grid_id.") {
23+
break;
24+
}
25+
data = JSON.parse(data);
26+
if(data.feed.title.$t == 'Typography') {
27+
applyTypography(parseSheetsData(data), doc.documentData().layerTextStyles());
28+
} else if (data.feed.title.$t == 'Colors') {
29+
applyColors(parseSheetsData(data), doc.documentData());
30+
}
31+
}
32+
} else {
33+
applyTypography(request(queryURL).result, doc.documentData().layerTextStyles());
34+
}
35+
}
36+
37+
function request(queryURL) {
1838
var request = NSMutableURLRequest.new();
1939
[request setHTTPMethod:@"GET"];
2040
[request setURL:[NSURL URLWithString:queryURL]];
@@ -26,10 +46,24 @@ var onRun = function(context) {
2646

2747
var dataString = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
2848

49+
return dataString;
50+
}
2951

30-
var data = JSON.parse(dataString);
31-
32-
return data.result;
52+
function parseSheetsData(data) {
53+
var result = [];
54+
var items = data.feed.entry;
55+
for(var i=0; i<items.length; i++) {
56+
var item = items[i];
57+
var resultItem = {}
58+
for(var key in item) {
59+
var attr = key.split('gsx$')[1];
60+
if(!attr) { continue; }
61+
attr = capitalize(attr);
62+
resultItem[attr] = item[key]['$t'];
63+
}
64+
result.push(resultItem);
65+
}
66+
return result;
3367
}
3468

3569
function showOptions() {
@@ -39,8 +73,8 @@ var onRun = function(context) {
3973
otherButton:nil
4074
informativeTextWithFormat:"Enter the URL where your styles live."];
4175

42-
var input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
43-
input.setStringValue(dict['syncSource'] || "https://sheetsu.com/apis/a49fa7c8");
76+
var input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 54)];
77+
input.setStringValue(dict['syncSource'] || "https://docs.google.com/spreadsheets/d/1LunoLOZbmcyGORV5mCe6qKPdN0NVuosm7lNeSo98-tQ/pubhtml");
4478
alert.setAccessoryView(input);
4579
var button = alert.runModal();
4680
input.validateEditing();
@@ -54,4 +88,9 @@ var onRun = function(context) {
5488
selection[i].setIsSelected(true);
5589
}
5690
}
91+
92+
function capitalize(string) {
93+
return string.charAt(0).toUpperCase() + string.slice(1);
94+
}
95+
5796
}

Contents/Sketch/typography.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var loadTypography = function (newStyles, sharedStyles) {
1+
var applyTypography = function (newStyles, sharedStyles) {
22

33
var alignmentHash = {
44
'left': 0,

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,27 @@ Install from [Sketch Toolbox](http://sketchtoolbox.com/) (recommended) or [downl
1010

1111
1. Create a Google Sheet from [this template](https://drive.google.com/previewtemplate?id=17q6GOMM1X6kkvgeL3LeGkRr4C2vOhpM_JiQUWxbBtew&mode=public) (click the "Use this template" button).
1212

13-
2. Visit [Sheetsu](http://sheetsu.com/) and generate an API for your new sheet.
13+
2. Select *File > Publish to web…* and then click the *Publish* button.
1414

15-
3. Run the Sync command from the plugin menu and paste your Sheetsu URL into the prompt.
15+
3. Copy the link to your sheet. (See below if you are using Google Apps at work)
1616

17-
All your text styles should now be synced with your spreadsheet. Run the plugin again any time to update. Share your Sheetsu URL with your team to stay in sync.
17+
4. Run the Sync command from the plugin menu and paste your URL into the prompt.
18+
19+
Your text styles and color palette should now be synced with your spreadsheet. Run the plugin again any time to update. Share your published sheet URL with your team to stay in sync.
20+
21+
## Using Google Apps at work?
22+
Some companies prevent employees from publishing sheets. If the *Published content & settings* drill-down in the *Publish to the web* modal says that people at your company must log in to view, then Sync will not be able to access your sheet. Don't worry – you can still use Sync for typography. Just visit [Sheetsu](http://sheetsu.com/) to generate an API for your new sheet. Use your new Sheetsu URL and continue to step 4.
1823

1924
**Need help?** [View the screencast](https://dl.dropboxusercontent.com/s/f4ubqenqz8n5wne/68D4E91B-173A-4AA0-964C-AA7F9EA77AC8-5233-000032842DD067F4.gif?dl=0), [create an issue](https://github.com/nolastan/sync.sketchplugin/issues/new) or [tweet @stan](https://twitter.com/stan).
2025

2126
**Find it useful?** Please [like Sync on Dribbble](https://dribbble.com/shots/2367116-Sync-Sketch-Plugin).
2227

2328
# Pattern Libraries
2429
[Share your pattern library](https://github.com/nolastan/sync.sketchplugin/issues/new?title=Add%20library&body=I%27d%20like%20to%20add%20this%20library%20I%20created:%20) with the Sync community.
25-
* [Material Design](https://sheetsu.com/apis/592bd16f) ([Download Roboto](https://www.google.com/fonts/specimen/Roboto))
30+
* [Material Design](https://docs.google.com/spreadsheets/d/1UkS9KRWmjvDu_DpCnB3KZ0mcYdmgyZmuWu-lkpfgjMw/pubhtml) ([Download Roboto](https://www.google.com/fonts/specimen/Roboto))
2631

2732
# Custom API
28-
As an alternative to Google Sheets, you can create a custom JSON api with the following structure:
33+
As an alternative to Google Sheets, you can create a custom JSON api with the following structure. Currently this method only supports typography.
2934
```
3035
{
3136

0 commit comments

Comments
 (0)