Skip to content

Commit 2613c08

Browse files
authored
Merge pull request #5 from DWilliames/develop
Remove arrow functions and custom updater
2 parents b982c90 + 1071b91 commit 2613c08

File tree

4 files changed

+13
-99
lines changed

4 files changed

+13
-99
lines changed

Google sheets content sync.sketchplugin/Contents/Sketch/defaults.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,19 @@
55
var defaultsKey = "com.davidwilliames.sketch-plugins.google-sheets-content-sync"
66

77
var defaultsURL = ''
8-
var defaultsLastUpdateCheckDay = 0
98

109
function fetchDefaults(documentID) {
1110
var allDefaults = NSUserDefaults.standardUserDefaults().dictionaryForKey(defaultsKey)
1211

13-
if (allDefaults) {
14-
if (allDefaults.lastUpdateCheckDay) {
15-
defaultsLastUpdateCheckDay = allDefaults.lastUpdateCheckDay
16-
}
17-
if (allDefaults[documentID]) {
18-
defaultsURL = allDefaults[documentID]
19-
}
12+
if (allDefaults && allDefaults[documentID]) {
13+
defaultsURL = allDefaults[documentID]
2014
}
2115
}
2216

2317
function saveDefaults(documentID) {
2418
var allDefaults = NSUserDefaults.standardUserDefaults().dictionaryForKey(defaultsKey)
2519

2620
var newDefaults = {
27-
lastUpdateCheckDay: defaultsLastUpdateCheckDay,
2821
[documentID]: defaultsURL
2922
}
3023

Google sheets content sync.sketchplugin/Contents/Sketch/main.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@import 'utilities.js'
33

44
var selection, doc, scriptPath, scriptFolder, app
5-
var manifestJSON, iconImage, sheetValues
5+
var iconImage, sheetValues
66

77
// Setup variables based on the context
88
function setup(context) {
@@ -12,36 +12,25 @@ function setup(context) {
1212
scriptFolder = scriptPath.stringByDeletingLastPathComponent()
1313
app = NSApplication.sharedApplication()
1414

15-
manifestJSON = getJSONFromFile(scriptFolder + "/manifest.json")
1615
iconImage = NSImage.alloc().initByReferencingFile(context.plugin.urlForResourceNamed("icon.png").path())
1716

1817
fetchDefaults(doc.hash())
19-
20-
// Return the opposite of if it was updated
21-
return !updateIfNeeded()
2218
}
2319

2420
// ****************************
2521
// Plugin command handler
2622
// ****************************
2723

2824
function run(context) {
29-
// If the user opted to update the plugin, then return
30-
if (!setup(context)) {
31-
return
32-
}
25+
setup(context)
3326

3427
// Ask the user to update their URL, then sync the content
3528
if (updateSheetURL())
3629
syncContent()
37-
3830
}
3931

4032
function importContent(context) {
41-
// If the user opted to update the plugin, then return
42-
if (!setup(context)) {
43-
return
44-
}
33+
setup(context)
4534

4635
// If there's currently no valid URL — ask the user to update it, then import it
4736
if (!validateURL()) {
@@ -50,7 +39,6 @@ function importContent(context) {
5039
} else {
5140
syncContent()
5241
}
53-
5442
}
5543

5644
function updateSheetURL() {
@@ -82,7 +70,7 @@ function syncContent() {
8270
return
8371

8472
// Update the values for each page
85-
doc.pages().forEach(page => {
73+
doc.pages().forEach(function(page) {
8674

8775
var sheetTitle = valueFromName(page.name())
8876
var pageValues = valuesForSheet(sheetTitle)
@@ -93,13 +81,13 @@ function syncContent() {
9381
pageValues = firstSheet.values
9482
}
9583

96-
page.children().forEach(child => {
84+
page.children().forEach(function(child) {
9785
if (child.isMemberOfClass(MSSymbolInstance)) {
9886

9987
// Store new overrides that need to be made
10088
var overrides = {}
10189

102-
child.symbolMaster().children().forEach(symbolLayer => {
90+
child.symbolMaster().children().forEach(function(symbolLayer) {
10391
// Ignore layers that are not text layers
10492
// Only include layers that have a '#' in the name
10593
if (!symbolLayer.isMemberOfClass(MSTextLayer) || symbolLayer.name().indexOf('#') < 0)
@@ -241,7 +229,7 @@ function valuesForSheet(sheetName) {
241229
return null
242230
}
243231

244-
var sheet = sheetValues.find(sheet => {
232+
var sheet = sheetValues.find(function(sheet) {
245233
return sheet.title.replace(/\s/g, '').toLowerCase() == sheetName.replace(/\s/g, '').toLowerCase()
246234
})
247235

@@ -292,10 +280,10 @@ function fetchValuesForPage(sheetID, pageNumber) {
292280
function parseData(data) {
293281
var values = {}
294282

295-
data.feed.entry.forEach(entry => {
296-
Object.keys(entry).filter(key => {
283+
data.feed.entry.forEach(function(entry) {
284+
Object.keys(entry).filter(function(key) {
297285
return key.indexOf('gsx$') == 0
298-
}).forEach(key => {
286+
}).forEach(function(key) {
299287
var newKey = key.substring(4)
300288
if (!(values.hasOwnProperty(newKey))) {
301289
values[newKey] = []

Google sheets content sync.sketchplugin/Contents/Sketch/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Sync content within a Google Sheets document, to text layers — based on their names",
44
"author": "David Williames",
55
"homepage": "https://github.com/DWilliames/Google-sheets-content-sync-sketch-plugin",
6-
"version": 1.1,
6+
"version": "1.1.1",
77
"identifier": "com.davidwilliames.sketch-plugins.google-sheets-content-sync",
88
"compatibleVersion": 39,
99
"bundleVersion": 1,
Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,5 @@
1-
// ****************************
2-
// Checking for updates
3-
// ****************************
4-
5-
// If an update is available - alert the user
6-
// return 'if the user opted to download an update'
7-
function updateIfNeeded() {
8-
9-
if (isTodayNewDay() && checkPluginUpdate()) {
10-
var alert = NSAlert.alloc().init()
11-
alert.setIcon(iconImage)
12-
alert.setMessageText("New Update available 🔥")
13-
alert.setInformativeText("There's a new update available for '" + manifestJSON.name + "'. Please download and install the new version.")
14-
alert.addButtonWithTitle("Download")
15-
alert.addButtonWithTitle("Cancel")
16-
if (alert.runModal() == '1000') {
17-
NSWorkspace.sharedWorkspace().openURL(NSURL.URLWithString(manifestJSON.homepage))
18-
return true
19-
}
20-
}
21-
22-
return false
23-
}
24-
25-
// Return if an update has not been checked for today yet
26-
function isTodayNewDay() {
27-
28-
var lastUpdateCheckDay = defaultsLastUpdateCheckDay
29-
var formatter = NSDateFormatter.alloc().init()
30-
formatter.setDateStyle(NSDateFormatterShortStyle)
31-
var today = formatter.stringFromDate(NSDate.date())
32-
33-
defaultsLastUpdateCheckDay = today
34-
35-
saveDefaults(doc.hash())
36-
37-
if (lastUpdateCheckDay) {
38-
return lastUpdateCheckDay != today
39-
} else {
40-
return true
41-
}
42-
}
43-
44-
// Check the remote repository for the manifest verion number
45-
// Return whether there is a new update available
46-
function checkPluginUpdate() {
47-
var newUpdateAvailable = false
48-
try {
49-
var response = getJSONFromURL('https://raw.githubusercontent.com/DWilliames/Google-sheets-content-sync-sketch-plugin/master/Google%20sheets%20content%20sync.sketchplugin/Contents/Sketch/manifest.json')
50-
if (response && response.version) {
51-
if (response.version.toString() != manifestJSON.version.toString()) {
52-
newUpdateAvailable = true
53-
}
54-
}
55-
} catch (e) {
56-
return false
57-
}
58-
return newUpdateAvailable
59-
}
60-
611
// Return a JSON object from a file path
622
function getJSONFromFile(filePath) {
633
var data = NSData.dataWithContentsOfFile(filePath)
644
return NSJSONSerialization.JSONObjectWithData_options_error(data, 0, nil)
655
}
66-
67-
// Return a json object from a URL
68-
function getJSONFromURL(url) {
69-
var request = NSURLRequest.requestWithURL(NSURL.URLWithString(url))
70-
var response = NSURLConnection.sendSynchronousRequest_returningResponse_error(request, nil, nil)
71-
return NSJSONSerialization.JSONObjectWithData_options_error(response, 0, nil)
72-
}

0 commit comments

Comments
 (0)