Skip to content

Commit

Permalink
Merge pull request #5 from DWilliames/develop
Browse files Browse the repository at this point in the history
Remove arrow functions and custom updater
  • Loading branch information
DWilliames authored Jul 29, 2017
2 parents b982c90 + 1071b91 commit 2613c08
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,19 @@
var defaultsKey = "com.davidwilliames.sketch-plugins.google-sheets-content-sync"

var defaultsURL = ''
var defaultsLastUpdateCheckDay = 0

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

if (allDefaults) {
if (allDefaults.lastUpdateCheckDay) {
defaultsLastUpdateCheckDay = allDefaults.lastUpdateCheckDay
}
if (allDefaults[documentID]) {
defaultsURL = allDefaults[documentID]
}
if (allDefaults && allDefaults[documentID]) {
defaultsURL = allDefaults[documentID]
}
}

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

var newDefaults = {
lastUpdateCheckDay: defaultsLastUpdateCheckDay,
[documentID]: defaultsURL
}

Expand Down
32 changes: 10 additions & 22 deletions Google sheets content sync.sketchplugin/Contents/Sketch/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@import 'utilities.js'

var selection, doc, scriptPath, scriptFolder, app
var manifestJSON, iconImage, sheetValues
var iconImage, sheetValues

// Setup variables based on the context
function setup(context) {
Expand All @@ -12,36 +12,25 @@ function setup(context) {
scriptFolder = scriptPath.stringByDeletingLastPathComponent()
app = NSApplication.sharedApplication()

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

fetchDefaults(doc.hash())

// Return the opposite of if it was updated
return !updateIfNeeded()
}

// ****************************
// Plugin command handler
// ****************************

function run(context) {
// If the user opted to update the plugin, then return
if (!setup(context)) {
return
}
setup(context)

// Ask the user to update their URL, then sync the content
if (updateSheetURL())
syncContent()

}

function importContent(context) {
// If the user opted to update the plugin, then return
if (!setup(context)) {
return
}
setup(context)

// If there's currently no valid URL — ask the user to update it, then import it
if (!validateURL()) {
Expand All @@ -50,7 +39,6 @@ function importContent(context) {
} else {
syncContent()
}

}

function updateSheetURL() {
Expand Down Expand Up @@ -82,7 +70,7 @@ function syncContent() {
return

// Update the values for each page
doc.pages().forEach(page => {
doc.pages().forEach(function(page) {

var sheetTitle = valueFromName(page.name())
var pageValues = valuesForSheet(sheetTitle)
Expand All @@ -93,13 +81,13 @@ function syncContent() {
pageValues = firstSheet.values
}

page.children().forEach(child => {
page.children().forEach(function(child) {
if (child.isMemberOfClass(MSSymbolInstance)) {

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

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

var sheet = sheetValues.find(sheet => {
var sheet = sheetValues.find(function(sheet) {
return sheet.title.replace(/\s/g, '').toLowerCase() == sheetName.replace(/\s/g, '').toLowerCase()
})

Expand Down Expand Up @@ -292,10 +280,10 @@ function fetchValuesForPage(sheetID, pageNumber) {
function parseData(data) {
var values = {}

data.feed.entry.forEach(entry => {
Object.keys(entry).filter(key => {
data.feed.entry.forEach(function(entry) {
Object.keys(entry).filter(function(key) {
return key.indexOf('gsx$') == 0
}).forEach(key => {
}).forEach(function(key) {
var newKey = key.substring(4)
if (!(values.hasOwnProperty(newKey))) {
values[newKey] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Sync content within a Google Sheets document, to text layers — based on their names",
"author": "David Williames",
"homepage": "https://github.com/DWilliames/Google-sheets-content-sync-sketch-plugin",
"version": 1.1,
"version": "1.1.1",
"identifier": "com.davidwilliames.sketch-plugins.google-sheets-content-sync",
"compatibleVersion": 39,
"bundleVersion": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,5 @@
// ****************************
// Checking for updates
// ****************************

// If an update is available - alert the user
// return 'if the user opted to download an update'
function updateIfNeeded() {

if (isTodayNewDay() && checkPluginUpdate()) {
var alert = NSAlert.alloc().init()
alert.setIcon(iconImage)
alert.setMessageText("New Update available 🔥")
alert.setInformativeText("There's a new update available for '" + manifestJSON.name + "'. Please download and install the new version.")
alert.addButtonWithTitle("Download")
alert.addButtonWithTitle("Cancel")
if (alert.runModal() == '1000') {
NSWorkspace.sharedWorkspace().openURL(NSURL.URLWithString(manifestJSON.homepage))
return true
}
}

return false
}

// Return if an update has not been checked for today yet
function isTodayNewDay() {

var lastUpdateCheckDay = defaultsLastUpdateCheckDay
var formatter = NSDateFormatter.alloc().init()
formatter.setDateStyle(NSDateFormatterShortStyle)
var today = formatter.stringFromDate(NSDate.date())

defaultsLastUpdateCheckDay = today

saveDefaults(doc.hash())

if (lastUpdateCheckDay) {
return lastUpdateCheckDay != today
} else {
return true
}
}

// Check the remote repository for the manifest verion number
// Return whether there is a new update available
function checkPluginUpdate() {
var newUpdateAvailable = false
try {
var response = getJSONFromURL('https://raw.githubusercontent.com/DWilliames/Google-sheets-content-sync-sketch-plugin/master/Google%20sheets%20content%20sync.sketchplugin/Contents/Sketch/manifest.json')
if (response && response.version) {
if (response.version.toString() != manifestJSON.version.toString()) {
newUpdateAvailable = true
}
}
} catch (e) {
return false
}
return newUpdateAvailable
}

// Return a JSON object from a file path
function getJSONFromFile(filePath) {
var data = NSData.dataWithContentsOfFile(filePath)
return NSJSONSerialization.JSONObjectWithData_options_error(data, 0, nil)
}

// Return a json object from a URL
function getJSONFromURL(url) {
var request = NSURLRequest.requestWithURL(NSURL.URLWithString(url))
var response = NSURLConnection.sendSynchronousRequest_returningResponse_error(request, nil, nil)
return NSJSONSerialization.JSONObjectWithData_options_error(response, 0, nil)
}

0 comments on commit 2613c08

Please sign in to comment.