-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.js
462 lines (361 loc) · 13.4 KB
/
main.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
@import 'utilities.js'
var selection, doc, scriptPath, scriptFolder, app
var iconImage, sheetValues
// Saving and receiving document URL
var docData, command, pluginIdentifier, url
var appVersion, plugin
// Setup variables based on the context
function setup(context) {
selection = context.selection
doc = context.document
scriptPath = context.scriptPath
scriptFolder = scriptPath.stringByDeletingLastPathComponent()
app = NSApplication.sharedApplication()
plugin = context.plugin
iconImage = NSImage.alloc().initByReferencingFile(context.plugin.urlForResourceNamed("icon.png").path())
appVersion = BCSketchInfo.shared().metadata().appVersion
docData = context.document.documentData()
command = context.command;
pluginIdentifier = context.plugin.identifier()
url = command.valueForKey_onLayer_forPluginIdentifier('docURL', docData, pluginIdentifier)
if (!url) {
url = ''
}
}
// ****************************
// Plugin command handler
// ****************************
function run(context) {
setup(context)
// Ask the user to update their URL, then sync the content
if (updateSheetURL())
syncContent()
}
function importContent(context) {
setup(context)
// If there's currently no valid URL — ask the user to update it, then import it
if (!validateURL()) {
if (updateSheetURL())
syncContent()
} else {
syncContent()
}
}
function updateSheetURL() {
// After presenting the options - return whether the user clicked 'import' or not
return showOptions() == '1000'
}
// The actual work!!
// Fetch the content from the Sheets URL
// Update the values accordingly
function syncContent() {
print('Importing content from: ' + url)
// If the user didn't enter a valid URL, tell them, then exit
var sheetId = validateURL()
if (!sheetId) {
var alert = NSAlert.alloc().init()
alert.setIcon(iconImage)
alert.setMessageText("Invalid Google Sheet URL")
alert.setInformativeText("The URL you entered wasn't valid")
alert.addButtonWithTitle("Ok")
return alert.runModal()
} else if (sheetId == "e") {
var alert = NSAlert.alloc().init()
alert.setIcon(iconImage)
alert.setMessageText("Invalid Google Sheet URL")
alert.setInformativeText("It looks like you've used the URL generated on the 'Publish to the web' screen. \n\nWhilst it is important that you do 'publish' the spreadsheet to the web, you need to use the URL for the spreadsheet that is in the browser address bar instead.")
alert.addButtonWithTitle("Ok")
return alert.runModal()
}
// Fetch all the values for the given URL
fetchSheetValues(sheetId)
if (sheetValues.length < 1)
return
// Update the values for each page
doc.pages().forEach(function (page) {
var sheetTitle = valueFromName(page.name())
var pageValues = valuesForSheet(sheetTitle)
// If no sheet title provided - just use the first sheet
if (sheetTitle == '' || pageValues == null) {
var firstSheet = sheetValues[0]
pageValues = firstSheet.values
}
page.children().forEach(function (child) {
if (child.isMemberOfClass(MSSymbolInstance)) {
// Store new overrides that need to be made
var overrides = {}
child.symbolMaster().children().forEach(function (symbolLayer) {
// Ignore layers that are not text layers or shapes
// Only include layers that have a '#' in the name
if (!(symbolLayer.isMemberOfClass(MSTextLayer) || canLayerHaveFill(symbolLayer)) || symbolLayer.name().indexOf('#') < 0) {
return
}
var objectID = symbolLayer.objectID().toString()
var existingOverrides = child.overrides()
if (existingOverrides == null) {
// no overrides exist, add one
child.overrides = { objectID: "overrideText" }
existingOverrides = child.overrides()
}
// get the existing overrides and create a mutable copy
var mutableOverrides = NSMutableDictionary.dictionaryWithDictionary(existingOverrides)
mutableOverrides.setObject_forKey(NSMutableDictionary.dictionaryWithDictionary(existingOverrides.objectForKey(0)), 0)
var nameFormat = formatName(symbolLayer.name())
var childName = nameFormat.lookupName
var values = pageValues[childName]
// Get the index from the name of the Symbol instance. e.g. '.3'
var instanceIndex = indexFromName(child.name())
// Set the overrides to match the index of the instance name
if (instanceIndex && instanceIndex != nameFormat.index) {
nameFormat.index = instanceIndex
}
var index = nameFormat.index.toString()
if (index.toLowerCase() === 'n' || index.toLowerCase() === 'x') {
index = Math.floor(Math.random() * values.length) + 1
} else {
index = parseInt(index)
}
// If there's no value associated, skip it
if (!values || values.length < 1) {
return
}
// Set the value of the text layer accordingly
// Based on if it was given an index, otherwise return the first one
var value = (values.length >= index) ? values[index - 1] : ''
var textValue = value || ''
if (symbolLayer.isMemberOfClass(MSTextLayer)) {
mutableOverrides.setObject_forKey(textValue, objectID)
} else if (canLayerHaveFill(symbolLayer)) {
var imageURL = imageURLFromValue(value)
if (imageURL) {
var imageData = getImageDataFromURL(imageURL)
mutableOverrides.setObject_forKey(imageData, objectID)
}
}
child.overrides = mutableOverrides
})
}
// Only check text layers or shape layers
// Only include layers that have a '#' in the name
if (!(child.isMemberOfClass(MSTextLayer) || canLayerHaveFill(child)) || child.name().indexOf('#') < 0)
return
var nameFormat = formatName(child.name())
var childName = nameFormat.lookupName
var values = pageValues[childName]
// If there's no value associated, skip it
if (!values || values.length < 1) {
return
}
var index = nameFormat.index.toString()
if (index.toLowerCase() === 'n' || index.toLowerCase() === 'x') {
index = Math.floor(Math.random() * values.length) + 1
} else {
index = parseInt(index)
}
// Set the value of the text layer accordingly
// Based on if it was given an index, otherwise return the first one
var value = (values.length >= index) ? values[index - 1] : ''
var textValue = value || ''
if (child.isMemberOfClass(MSTextLayer)) {
child.setStringValue(textValue)
} else if (canLayerHaveFill(child)) {
// Get an image URL for the shape
var imageURL = imageURLFromValue(value)
if (imageURL) {
var imageData = getImageDataFromURL(imageURL)
fillLayerWithImageData(child, imageData)
}
}
})
})
command.setValue_forKey_onLayer_forPluginIdentifier(url, 'docURL', docData, pluginIdentifier)
doc.showMessage("Content successfully imported! ⚡️")
doc.reloadInspector()
}
// **********************
// Helper methods
// **********************
// Validate whether the URL contains a valid sheet ID
// Return the SheetID, or null
function validateURL() {
if (url === '') {
return null
}
var regex = /\/spreadsheets\/d\/([a-zA-Z0-9-_]+)/g
var matches = regex.exec(url)
return (matches && matches.length > 1) ? matches[1] : null
}
// Validate if there's a URL for the image as the value in the sheet
function imageURLFromValue(value) {
if (value === '') {
return null
}
var regex = /(https?:\/\/.*\.(?:png|jpg|jpeg))/i
var matches = regex.exec(value)
return (matches && matches.length > 1) ? matches[1] : null
}
// Downloads an image from a URL and returns it as a NSImageData
function getImageDataFromURL(url) {
var request = NSURLRequest.requestWithURL(NSURL.URLWithString(url))
var data = NSURLConnection.sendSynchronousRequest_returningResponse_error(request, null, null)
var image
if (!data) {
print('Error fetching image')
doc.showMessage("Error in fetching image URL.")
image = NSImage.alloc().initByReferencingFile(plugin.urlForResourceNamed("Placeholder.png").path())
} else {
image = NSImage.alloc().initWithData(data)
}
if (isCurrentVersionBefore('47')) {
return MSImageData.alloc().initWithImage_convertColorSpace(image, false)
}
return MSImageData.alloc().initWithImage(image)
}
// Changes the fill of a layer to be the NSImageData passed in
function fillLayerWithImageData(layer, imageData) {
var fill = layer.style().fills().firstObject()
fill.setFillType(4)
fill.setImage(imageData)
fill.setPatternFillType(1)
}
// Show the options to the user - to enter their url
function showOptions() {
var alert = NSAlert.alloc().init()
alert.setIcon(iconImage)
alert.setMessageText("Import data from Google Sheet")
alert.setInformativeText("In your spreadsheet;\nGo 'File > Publish to the web...' \nClick 'Publish' and then copy the URL from the address bar.\n\nNOTE: Do not use the URL on the 'Publish to the web' page — use the URL from the browser address bar.")
alert.addButtonWithTitle("Import")
alert.addButtonWithTitle("Cancel")
var input = NSTextField.alloc().initWithFrame(NSMakeRect(0, 0, 300, 54))
input.setStringValue(url)
input.setPlaceholderString('Google Sheets publish URL')
alert.setAccessoryView(input)
var response = alert.runModal()
input.validateEditing()
url = input.stringValue()
return response
}
// Split the name up into a format
// Based on the content after '#'
// - lookupName is the value directly after '#', before a '.'
// - index is any value after a '.' after '#'
function formatName(name) {
// TODO: portion — for carry over text
var format = {
lookupName: undefined,
index: 1
}
// Remove spaces
name = name.replace(/\s/g, '').toLowerCase()
var split = name.split('#')
if (split.length <= 1)
return format
split = split[1].split('.')
if (split.length > 1) {
format.index = split[1]
}
format.lookupName = split[0]
return format
}
// Get the text after '#'
// Also remove spaces, and make it all lowercase
function valueFromName(name) {
var split = name.replace(/\s/g, '').toLowerCase().split('#')
return (split.length > 1) ? split[1] : null
}
// Get the text after '.'
// Also remove spaces, and make it all lowercase
function indexFromName(name) {
var split = name.replace(/\s/g, '').toLowerCase().split('.')
return (split.length > 1) ? split[1] : null
}
// Returns all the spreadsheet values for the sheet, based on the sheetName
function valuesForSheet(sheetName) {
if (!sheetName) {
return null
}
var sheet = sheetValues.find(function (sheet) {
return sheet.title.replace(/\s/g, '').toLowerCase() == sheetName.replace(/\s/g, '').toLowerCase()
})
return sheet ? sheet.values : null
}
// For the given Google Sheet ID, fetch all the values for all the sheets within it
function fetchSheetValues(sheetID) {
sheetValues = []
// Keep incrementing fo rth enext sheet, until no sheet is found
for (var i = 1; true; i++) {
var values = fetchValuesForPage(sheetID, i)
if (!values) {
break
} else {
// Add the data to the sheetValues
sheetValues.push(values)
}
}
}
// Fetch the values for a given page within a Google Sheet
// Return the parse data
function fetchValuesForPage(sheetID, pageNumber) {
var queryURL = 'https://spreadsheets.google.com/feeds/list/' + sheetID + '/' + pageNumber + '/public/values?alt=json'
var request = NSMutableURLRequest.new()
request.setHTTPMethod('GET')
request.setURL(NSURL.URLWithString(queryURL))
var error = NSError.new()
var responseCode = null
var response = NSURLConnection.sendSynchronousRequest_returningResponse_error(request, responseCode, error)
var dataString = NSString.alloc().initWithData_encoding(response, NSUTF8StringEncoding)
try {
var data = JSON.parse(dataString)
return parseData(data)
} catch (e) {
doc.showMessage("Failed to process the document data correctly")
return null
}
}
// Parse the data to how we want it
function parseData(data) {
var values = {}
data.feed.entry.forEach(function (entry) {
Object.keys(entry).filter(function (key) {
return key.indexOf('gsx$') == 0
}).forEach(function (key) {
var newKey = key.substring(4)
if (!(values.hasOwnProperty(newKey))) {
values[newKey] = []
}
var newValue = entry[key]['$t']
if (newValue) {
var currentArray = values[newKey]
currentArray.push(newValue)
values[newKey] = currentArray
}
})
})
return {
title: data.feed.title.$t,
values: values
}
}
function compareVersion(a, b) {
var i, cmp, len, re = /(\.0)+[^\.]*$/;
a = (a + '').replace(re, '').split('.');
b = (b + '').replace(re, '').split('.');
len = Math.min(a.length, b.length);
for (i = 0; i < len; i++) {
cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
if (cmp !== 0) {
return cmp;
}
}
return a.length - b.length;
}
function isCurrentVersionBefore(version) {
return compareVersion(appVersion, version) < 0
}
function canLayerHaveFill(layer) {
if (isCurrentVersionBefore("52")) {
return layer.isMemberOfClass(MSShapeGroup)
} else {
return layer.isKindOfClass(MSShapePathLayer)
}
}