|
1 | 1 | const Settings = require('sketch/settings');
|
2 | 2 | const UI = require('sketch/ui');
|
| 3 | +const sketch = require('sketch'); |
| 4 | +const BrowserWindow = require('sketch-module-web-view'); |
3 | 5 |
|
4 | 6 | export function uploadSelect(context) {
|
5 | 7 | onRun(context, false);
|
6 | 8 | }
|
7 | 9 | export function setting(e) {
|
8 | 10 | onRun(context, true);
|
9 | 11 | }
|
| 12 | +export function uploadAll(context) { |
| 13 | + const url = Settings.settingForKey('url'); |
| 14 | + const doc = context.document; |
| 15 | + |
| 16 | + if (!url) { |
| 17 | + UI.getInputFromUser( |
| 18 | + 'Input the server address.', |
| 19 | + { |
| 20 | + initialValue: url || 'https://' |
| 21 | + }, |
| 22 | + (err, value) => { |
| 23 | + if (err) { |
| 24 | + return; |
| 25 | + } |
| 26 | + return Settings.setSettingForKey('url', value); |
| 27 | + } |
| 28 | + ); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const document = require('sketch/dom').getSelectedDocument(); |
| 33 | + const options = { |
| 34 | + identifier: 'unique.id1', |
| 35 | + width: 540, |
| 36 | + height: 300 |
| 37 | + }; |
| 38 | + |
| 39 | + const artBoards = document.selectedPage.layers.filter( |
| 40 | + layer => layer.name === '切图' || layer.name === 'slices' |
| 41 | + )[0]; |
10 | 42 |
|
11 |
| -function exportLayerAsBitmap(document, layer, scale) { |
| 43 | + if (!artBoards) { |
| 44 | + return doc.showMessage( |
| 45 | + 'Select page doesn\'t include artBoard with name "切图" or slices.' |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + const browserWindow = new BrowserWindow(options); |
| 50 | + |
| 51 | + const layers = artBoards.layers.map(layer => layer.name); |
| 52 | + |
| 53 | + browserWindow.webContents |
| 54 | + .executeJavaScript('sendUrl("' + url + '")') |
| 55 | + .then(res => { |
| 56 | + console.log(res); |
| 57 | + }); |
| 58 | + |
| 59 | + browserWindow.webContents |
| 60 | + .executeJavaScript('sendLayer(' + JSON.stringify(layers) + ')') |
| 61 | + .then(res => { |
| 62 | + console.log(res); |
| 63 | + }); |
| 64 | + |
| 65 | + browserWindow.webContents.on('nativeLog', function(s, id) { |
| 66 | + let layer = document.selectedPage.layers |
| 67 | + .filter(layer => layer.name === '切图')[0] |
| 68 | + .layers.filter(layer => layer.name === s)[0]; |
| 69 | + if (!layer) { |
| 70 | + return sketch.UI.message(s + ' not found!'); |
| 71 | + } |
| 72 | + const layerName = layer.name; |
| 73 | + const bitmap = sketch |
| 74 | + .export( |
| 75 | + [ |
| 76 | + document.selectedPage.layers |
| 77 | + .filter(layer => layer.name === '切图')[0] |
| 78 | + .layers.filter(layer => layer.name === s)[0] |
| 79 | + ], |
| 80 | + { formats: 'png', output: false } |
| 81 | + )[0] |
| 82 | + .toString('base64'); |
| 83 | + |
| 84 | + fetch(url + '/sketch/' + id, { |
| 85 | + method: 'POST', |
| 86 | + headers: { |
| 87 | + Accept: 'application/json', |
| 88 | + 'Content-Type': 'application/json' |
| 89 | + }, |
| 90 | + body: { image: bitmap, name: layerName } |
| 91 | + }) |
| 92 | + .then(response => response.text()) |
| 93 | + .then(text => { |
| 94 | + browserWindow.webContents |
| 95 | + .executeJavaScript('uploadSuccess()') |
| 96 | + .then(res => { |
| 97 | + console.log(res); |
| 98 | + }); |
| 99 | + }) |
| 100 | + .catch(e => { |
| 101 | + browserWindow.webContents |
| 102 | + .executeJavaScript('uploadFail()') |
| 103 | + .then(res => { |
| 104 | + console.log(res); |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + browserWindow.loadURL(require('./ui.html')); |
| 110 | +} |
| 111 | + |
| 112 | +function exportSelectAsBitmap(document, layer, scale) { |
12 | 113 | var slice,
|
13 | 114 | result = {},
|
14 | 115 | rect = layer.absoluteRect(),
|
@@ -48,23 +149,23 @@ const onRun = function(context, setting) {
|
48 | 149 | if (err) {
|
49 | 150 | return;
|
50 | 151 | }
|
51 |
| - return Settings.setSettingForKey('url', value); |
| 152 | + return Settings.setSettingForKey('url', value); |
52 | 153 | }
|
53 | 154 | );
|
54 |
| - return ; |
| 155 | + return; |
55 | 156 | }
|
56 | 157 |
|
57 | 158 | const doc = context.document;
|
58 | 159 | const selection = context.selection;
|
| 160 | + |
59 | 161 | if (selection.count() == 0) {
|
60 | 162 | doc.showMessage('Please select the layer to upload.');
|
61 | 163 | } else {
|
62 | 164 | for (let i = 0; i < selection.count(); i++) {
|
63 | 165 | const layer = selection[i];
|
64 | 166 | const layerName = layer.name();
|
65 |
| - const { bitmap } = exportLayerAsBitmap(doc, layer, 1); |
66 |
| - |
67 |
| - fetch(url, { |
| 167 | + const { bitmap } = exportSelectAsBitmap(doc, layer, 1); |
| 168 | + fetch(url + '/sketch/arroast', { |
68 | 169 | method: 'POST',
|
69 | 170 | headers: {
|
70 | 171 | Accept: 'application/json',
|
|
0 commit comments