-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8991ab9
Showing
14 changed files
with
792 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
@import "common.js"; | ||
|
||
function BaiduMap() {} | ||
|
||
BaiduMap.prototype.prefix = 'baidu'; | ||
BaiduMap.prototype.maxWidth = 1024; | ||
BaiduMap.prototype.maxHeight = 1024; | ||
BaiduMap.prototype.minZoom = 3; | ||
BaiduMap.prototype.maxZoom = 18; | ||
BaiduMap.prototype.zoomLevels = []; | ||
BaiduMap.prototype.ak = '4yWbU5UAZzPq3zG136ioc188jR1p6j0t'; | ||
|
||
BaiduMap.prototype.createMap = function (context) { | ||
calcZoomLevels(this.zoomLevels, this.minZoom, this.maxZoom); | ||
if (!checkLayer(context.selection)) { | ||
return; | ||
} | ||
var viewIndex = []; | ||
var dialog = this.buildOptionDialog(viewIndex); | ||
var options = handleDialog(dialog, viewIndex, this.prefix, dialog.runModal()); | ||
if (!checkOptions(options)) { | ||
return; | ||
} | ||
var layer = context.selection[0]; | ||
var layerSizes = layer.frame(); | ||
var width = Math.min(parseInt([layerSizes width]), this.maxWidth); | ||
var height = Math.min(parseInt([layerSizes height]), this.maxHeight); | ||
var imageUrl = 'https://api.map.baidu.com/staticimage/v2?center=' + encodeURIComponent(options.center) + '&width=' + width + '&height=' + height + '&zoom=' + options.zoom + '&scale=1©right=1&ak=' + this.ak; | ||
fillLayer(context, imageUrl, layer); | ||
} | ||
|
||
BaiduMap.prototype.buildOptionDialog = function (viewIndex) { | ||
var remember = getOption('remember', 0, this.prefix); | ||
|
||
var dialogWindow = COSAlertWindow.new(); | ||
dialogWindow.setMessageText('Map Creator'); | ||
dialogWindow.setInformativeText('Input your options to create a map'); | ||
|
||
dialogWindow.addTextLabelWithValue('Input your map center'); | ||
var center = createTextField(remember == 0 ? '' : getOption('center', '', this.prefix), 'Name or coordinate like \'116.403874,39.914888\''); | ||
dialogWindow.addAccessoryView(center); | ||
|
||
dialogWindow.addTextLabelWithValue('Select zoom level'); | ||
var zoom = createSelect(this.zoomLevels, remember == 0 ? 0 : getOption('zoom', 0, this.prefix)); | ||
dialogWindow.addAccessoryView(zoom); | ||
|
||
var remember = createCheck('Remember my options', remember); | ||
dialogWindow.addAccessoryView(remember); | ||
|
||
dialogWindow.addButtonWithTitle('OK'); | ||
dialogWindow.addButtonWithTitle('Cancel'); | ||
|
||
viewIndex.push({ | ||
key: 'center', | ||
index: 1, | ||
type: 'string' | ||
}); | ||
viewIndex.push({ | ||
key: 'zoom', | ||
index: 3, | ||
type: 'select' | ||
}); | ||
viewIndex.push({ | ||
key: 'remember', | ||
index: 4, | ||
type: 'string' | ||
}); | ||
|
||
return dialogWindow; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
var pluginIdentifier = "io.terence.sketch.mapcreator"; | ||
|
||
function checkLayer(selectedLayers) { | ||
var app = NSApplication.sharedApplication(); | ||
if (!selectedLayers || selectedLayers.count() != 1) { | ||
app.displayDialog_withTitle('Please select exactly 1 shape layer.', 'Invalid shape layer selection'); | ||
return false; | ||
} | ||
var layer = selectedLayers[0]; | ||
if ([layer class] !== MSShapeGroup) { | ||
app.displayDialog_withTitle('Your selection was a ' + [layer name] + ', that is not a shape layer. Please select a shape layer.', 'Invalid layer type'); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function checkOptions(options) { | ||
var app = NSApplication.sharedApplication(); | ||
if (!options) { | ||
return false; | ||
} | ||
if (!options.center) { | ||
app.displayDialog_withTitle('Please enter valid center', 'Invalid center'); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function createTextField(value, placeholder) { | ||
var textField = NSTextField.alloc().initWithFrame(NSMakeRect(0, 0, 300, 24)); | ||
if (value) { | ||
textField.setStringValue(value); | ||
} | ||
if (placeholder) { | ||
textField.setPlaceholderString(placeholder); | ||
} | ||
return textField; | ||
} | ||
|
||
function createSelect(options, selectedIndex, width) { | ||
var selIdx = selectedIndex || 0; | ||
var select = NSPopUpButton.alloc().initWithFrame(NSMakeRect(0, 0, width || 100, 28)); | ||
var i; | ||
if (options) { | ||
select.addItemsWithTitles(options); | ||
select.selectItemAtIndex(selIdx); | ||
} | ||
return select; | ||
} | ||
|
||
function createCheck(title, checked) { | ||
var check = NSButton.alloc().initWithFrame(NSMakeRect(0, 0, 200, 20)) | ||
check.setButtonType(NSSwitchButton); | ||
check.setBezelStyle(NSRoundedBezelStyle); | ||
check.setTitle(title); | ||
check.setState(checked == 0 ? NSOffState : NSOnState); | ||
return check; | ||
} | ||
|
||
function calcZoomLevels(zoomLevels, minZoom, maxZoom) { | ||
if (zoomLevels.length > 0) { | ||
return; | ||
} | ||
var i; | ||
for (i = minZoom; i <= maxZoom; i++) { | ||
zoomLevels.push(i.toString()); | ||
} | ||
} | ||
|
||
function fillLayer(context, imageUrl, layer) { | ||
var data = fetchImage(imageUrl); | ||
if (!data) { | ||
context.document.showMessage("Fetch map error, invalid request params"); | ||
return; | ||
} | ||
var result = NSString.alloc().initWithData_encoding(data, NSUTF8StringEncoding); | ||
if (result) { | ||
context.document.showMessage("Fetch map error, invalid request params, error info: " + result); | ||
return; | ||
} | ||
var imageData = NSImage.alloc().initWithData(data); | ||
var fill = layer.style().fills().firstObject(); | ||
fill.setFillType(4); | ||
fill.setImage(MSImageData.alloc().initWithImage_convertColorSpace(imageData, false)); | ||
fill.setPatternFillType(1); | ||
context.document.showMessage("Map created"); | ||
} | ||
|
||
function fetchImage(url) { | ||
var request = NSURLRequest.requestWithURL(NSURL.URLWithString(url)); | ||
var response = NSURLConnection.sendSynchronousRequest_returningResponse_error(request, null, null); | ||
return response; | ||
} | ||
|
||
function handleDialog(dialog, viewIndex, prefix, responseCode) { | ||
saveOptions(dialog, viewIndex, prefix); | ||
if (responseCode == "1000") { | ||
var result = {}; | ||
var i; | ||
for (i = 0; i < viewIndex.length; i++) { | ||
if (viewIndex[i].type === 'select') { | ||
result[viewIndex[i].key] = dialog.viewAtIndex(viewIndex[i].index).titleOfSelectedItem(); | ||
} else if (viewIndex[i].type === 'string') { | ||
result[viewIndex[i].key] = dialog.viewAtIndex(viewIndex[i].index).stringValue(); | ||
} | ||
}; | ||
return result; | ||
} | ||
return null; | ||
} | ||
|
||
function saveOptions(dialog, viewIndex, prefix) { | ||
var i; | ||
for (i = 0; i < viewIndex.length; i++) { | ||
if (viewIndex[i].type === 'select') { | ||
setPreferences(prefix + '.' + viewIndex[i].key, dialog.viewAtIndex(viewIndex[i].index).indexOfSelectedItem()); | ||
} else if (viewIndex[i].type === 'string') { | ||
setPreferences(prefix + '.' + viewIndex[i].key, dialog.viewAtIndex(viewIndex[i].index).stringValue()); | ||
} | ||
}; | ||
} | ||
|
||
function getOption(key, defaultValue, prefix) { | ||
return getPreferences(prefix + '.' + key, defaultValue); | ||
} | ||
|
||
function getPreferences(key, defaultValue) { | ||
var userDefaults = NSUserDefaults.standardUserDefaults(); | ||
if (!userDefaults.dictionaryForKey(pluginIdentifier)) { | ||
var defaultPreferences = NSMutableDictionary.alloc().init(); | ||
userDefaults.setObject_forKey(defaultPreferences, pluginIdentifier); | ||
userDefaults.synchronize(); | ||
} | ||
var value = userDefaults.dictionaryForKey(pluginIdentifier).objectForKey(key); | ||
return value === undefined ? defaultValue : value; | ||
} | ||
|
||
function setPreferences(key, value) { | ||
var userDefaults = NSUserDefaults.standardUserDefaults(); | ||
var preferences; | ||
if (!userDefaults.dictionaryForKey(pluginIdentifier)) { | ||
preferences = NSMutableDictionary.alloc().init(); | ||
} else { | ||
preferences = NSMutableDictionary.dictionaryWithDictionary(userDefaults.dictionaryForKey(pluginIdentifier)); | ||
} | ||
preferences.setObject_forKey(value, key); | ||
userDefaults.setObject_forKey(preferences, pluginIdentifier); | ||
userDefaults.synchronize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@import "baidu.js"; | ||
@import "mapbox.js"; | ||
|
||
function commandCreateBaiduMap(context) { | ||
var map = new BaiduMap(); | ||
map.createMap(context); | ||
} | ||
|
||
function commandCreateMapboxMap(context) { | ||
var map = new Mapbox(); | ||
map.createMap(context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"author" : "Terence Wu", | ||
"bundleVersion" : 1, | ||
"commands" : [ | ||
{ | ||
"script" : "main.cocoascript", | ||
"name" : "Create Baidu Map", | ||
"handler" : "commandCreateBaiduMap", | ||
"identifier" : "commandCreateBaiduMap" | ||
}, | ||
{ | ||
"script" : "main.cocoascript", | ||
"name" : "Create Mapbox Map", | ||
"handler" : "commandCreateMapboxMap", | ||
"identifier" : "commandCreateMapboxMap" | ||
} | ||
], | ||
"menu" : { | ||
"items" : [ | ||
"commandCreateBaiduMap", | ||
"commandCreateMapboxMap" | ||
], | ||
"title" : "Map Creator" | ||
}, | ||
"homepage" : "https://github.com/terence55/sketch-map-creator", | ||
"identifier" : "io.terence.sketch.mapcreator", | ||
"appcast": "https://raw.githubusercontent.com/terence55/sketch-map-creator/master/appcast.xml", | ||
"version" : "1.0.0", | ||
"description" : "Plugin to create a static map(Baidu, MapBox) with custom options", | ||
"authorEmail" : "trence320@163.com", | ||
"name" : "Map Creator" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
@import "common.js"; | ||
|
||
function Mapbox() { } | ||
|
||
Mapbox.prototype.prefix = 'mapbox'; | ||
Mapbox.prototype.maxWidth = 1280; | ||
Mapbox.prototype.maxHeight = 1280; | ||
Mapbox.prototype.minZoom = 0; | ||
Mapbox.prototype.maxZoom = 22; | ||
Mapbox.prototype.zoomLevels = []; | ||
Mapbox.prototype.types = [ | ||
'streets', | ||
'light', | ||
'dark', | ||
'satellite', | ||
'streets-satellite', | ||
'wheatpaste', | ||
'streets-basic', | ||
'comic', | ||
'outdoors', | ||
'run-bike-hike', | ||
'pencil', | ||
'pirates', | ||
'emerald', | ||
'high-contrast' | ||
]; | ||
Mapbox.prototype.minLon = -180; | ||
Mapbox.prototype.maxLon = 180; | ||
Mapbox.prototype.minLat = -85.0511; | ||
Mapbox.prototype.maxLat = 85.0511; | ||
Mapbox.prototype.ak = 'pk.eyJ1IjoidHJlbmNlMzIwIiwiYSI6ImNqNjRobjF0czFrZGMzMnBvN3VzYzQxenMifQ.BJml_qE3BhBJ2bPodjwfeg'; | ||
|
||
Mapbox.prototype.createMap = function (context) { | ||
calcZoomLevels(this.zoomLevels, this.minZoom, this.maxZoom); | ||
if (!checkLayer(context.selection)) { | ||
return; | ||
} | ||
var app = NSApplication.sharedApplication(); | ||
var viewIndex = []; | ||
var dialog = this.buildOptionDialog(viewIndex); | ||
var options = handleDialog(dialog, viewIndex, this.prefix, dialog.runModal()); | ||
if (!checkOptions(options)) { | ||
return; | ||
} | ||
var coordinate = options.center.split(','); | ||
if (coordinate.length !== 2 | ||
|| (isNaN(coordinate[0] || coordinate[1])) | ||
|| (coordinate[0] < this.minLon || coordinate[0] > this.maxLon) | ||
|| (coordinate[1] < this.minLat || coordinate[1] > this.maxLat)) { | ||
app.displayDialog_withTitle('Please enter valid center, longitude must be between -180 and 180, latitude must be between -85.0511 and 85.0511', 'Invalid center'); | ||
return; | ||
} | ||
var layer = context.selection[0]; | ||
var layerSizes = layer.frame(); | ||
var width = Math.min(parseInt([layerSizes width]), this.maxWidth); | ||
var height = Math.min(parseInt([layerSizes height]), this.maxHeight); | ||
var imageUrl = 'https://api.mapbox.com/v4/mapbox.' + options.type + '/' + options.center + ',' + options.zoom + '/' + width + 'x' + height + '.jpg90?access_token=' + this.ak; | ||
fillLayer(context, imageUrl, layer); | ||
} | ||
|
||
Mapbox.prototype.buildOptionDialog = function (viewIndex) { | ||
var remember = getOption('remember', 0, this.prefix); | ||
|
||
var dialogWindow = COSAlertWindow.new(); | ||
dialogWindow.setMessageText('Map Creator'); | ||
dialogWindow.setInformativeText('Input your options to create a map'); | ||
|
||
dialogWindow.addTextLabelWithValue('Input your map center'); | ||
var center = createTextField(remember == 0 ? '' : getOption('center', '', this.prefix), 'Coordinate like \'116.403874,39.914888\''); | ||
dialogWindow.addAccessoryView(center); | ||
|
||
dialogWindow.addTextLabelWithValue('Select zoom level'); | ||
var zoom = createSelect(this.zoomLevels, remember == 0 ? 0 : getOption('zoom', 0, this.prefix)); | ||
dialogWindow.addAccessoryView(zoom); | ||
|
||
dialogWindow.addTextLabelWithValue('Select map type'); | ||
var type = createSelect(this.types, remember == 0 ? 0 : getOption('type', 0, this.prefix), 150); | ||
dialogWindow.addAccessoryView(type); | ||
|
||
var remember = createCheck('Remember my options', remember); | ||
dialogWindow.addAccessoryView(remember); | ||
|
||
dialogWindow.addButtonWithTitle('OK'); | ||
dialogWindow.addButtonWithTitle('Cancel'); | ||
|
||
viewIndex.push({ | ||
key: 'center', | ||
index: 1, | ||
type: 'string' | ||
}); | ||
viewIndex.push({ | ||
key: 'zoom', | ||
index: 3, | ||
type: 'select' | ||
}); | ||
viewIndex.push({ | ||
key: 'type', | ||
index: 5, | ||
type: 'select' | ||
}); | ||
viewIndex.push({ | ||
key: 'remember', | ||
index: 6, | ||
type: 'string' | ||
}); | ||
|
||
return dialogWindow; | ||
} |
Oops, something went wrong.