-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.js
325 lines (281 loc) · 12 KB
/
Code.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
/**
* PlantUML Gizmo project - (c) 2014 Christopher Fuhrman
* fuhrmanator@gmail.com
*/
/**
* Limit the access to
* @OnlyCurrentDoc
* By default, it asks for access to all Google Docs
*/
// TODO find a way to do Google Analytics properly - currently, it logs IPs of Google Servers as the GA script runs inside a server
var ADD_ON_TITLE = 'PlantUML Gizmo';
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
*
* @param {object} e The event parameter for a simple onOpen trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode.
*/
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Start', 'showSidebar')
.addSeparator()
.addItem('About', 'showAbout')
.addItem('Settings', 'showSettings')
.addToUi();
}
/**
* Runs when the add-on is installed.
*
* @param {object} e The event parameter for a simple onInstall trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode. (In practice, onInstall triggers always
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or
* AuthMode.NONE.)
*/
function onInstall(e) {
onOpen(e);
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
*/
function showSidebar() {
var ui = HtmlService.createTemplateFromFile('Sidebar')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME) // https://developers.google.com/apps-script/migration/iframe?utm_campaign=app+invites_deprication_google_apps_script_101315&utm_source=gdev&utm_medium=blog#setting_the_link_target_attribute
.setTitle(ADD_ON_TITLE)
DocumentApp.getUi().showSidebar(ui);
// code past here doesn't seem to run (filtered by CAJA, or showSidebar() never returns?)
// Logger.log("checking selection");
// // load source of image if it's selected
// var selection = DocumentApp.getActiveDocument().getSelection();
// if (selection) {
// recoverUrlFromImage();
// } else {
// Logger.log("No image selected");
// }
//
}
/**
* Re-opens (after preferences dialog) sidebar
*/
function reshowSidebar() {
var ui = HtmlService.createTemplateFromFile('Sidebar')
.evaluate()
.setTitle(ADD_ON_TITLE)
DocumentApp.getUi().showSidebar(ui);
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
*/
function showSettings() {
var ui = HtmlService.createTemplateFromFile('Settings')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('PlantUML Gizmo Settings')
.setWidth(500)
.setHeight(315);
DocumentApp.getUi().showDialog(ui); // does this call block???
return;
}
/**
* Opens a dialog showing information about the Add-on
*/
function showAbout() {
var ui = DocumentApp.getUi();
var result = ui.alert(
'About',
'PlantUML Gizmo was written for use in the OO Analysis and Design courses at École de technologie supérieure, and has been used by Google Engineers on Android and Google Pay.\n\nIt uses JavaScript API Client Code described at http://plantuml.sourceforge.net/codejavascript.html as well as inflating routines at http://www.planttext.com/javascript/jquery-plantuml/plantuml.js \n\nFind me on twitter @thefuhrmanator. Version 15 (2019-11-22)',
ui.ButtonSet.OK);
}
/**
* Save PlantUML source text (from sidebar)
*/
function saveSource(source) {
// Logger.log("Saving source " + source );
var properties = PropertiesService.getUserProperties(); // User properties hold source code, since collaborators could conceivably modify the same Document
var result = properties.setProperty("PlantUMLSource", source);
// Logger.log("Result = " + result );
}
/**
* Load PlantUML source text (from sidebar)
*/
function loadSource() {
// Logger.log("Loading source");
var properties = PropertiesService.getUserProperties();
var source = properties.getProperty("PlantUMLSource");
// Logger.log("source = " + source );
if (!source) {
source = "Bob -> Alice : hello";
}
return source;
}
/**
* Save PlantUML source text (from sidebar)
*/
function clearSource() {
// Logger.log("Clearing source");
var properties = PropertiesService.getUserProperties();
properties.deleteProperty("PlantUMLSource");
}
/**
* Gets the prefs
*/
function getPrefs() {
var serverUrl = "";
var useDataUrl = false;
var useDataUrlProperty;
var theProperties = PropertiesService.getDocumentProperties();
serverUrl = theProperties.getProperty('PLANTUML_SERVER_URL');
if ( serverUrl === null ) {
// initialize the property to default
theProperties.setProperty('PLANTUML_SERVER_URL', 'https://www.plantuml.com/plantuml/');
serverUrl = theProperties.getProperty('PLANTUML_SERVER_URL');
}
useDataUrlProperty = theProperties.getProperty('PLANTUML_SERVER_USE_DATA_URL');
if ( useDataUrlProperty === null ) {
// initialize the property to default
theProperties.setProperty('PLANTUML_SERVER_USE_DATA_URL', false);
}
useDataUrl = theProperties.getProperty('PLANTUML_SERVER_USE_DATA_URL') == "true";
var prefs = {serverPrefix:serverUrl,
useDataURL:useDataUrl};
return prefs;
}
/**
* Sets the prefs
*/
function setPrefs(prefs) {
// Logger.log("setting prefs: " + prefs.serverPrefix + ", useDataURL=" + prefs.useDataURL + " (type is " + typeof prefs.useDataURL + ")");
// TODO verify it's a valid URL - try to get an image? -- maybe try this in the JavaScript settings first
var theProperties = PropertiesService.getDocumentProperties();
theProperties.setProperty('PLANTUML_SERVER_URL', prefs.serverPrefix);
theProperties.setProperty('PLANTUML_SERVER_USE_DATA_URL', prefs.useDataURL);
return prefs;
}
/**
* Recovers URL from selected image
*/
function recoverUrlFromImage() {
var selection = DocumentApp.getActiveDocument().getSelection();
var url = "not set";
if (selection) {
/* make sure selection is an image */
var elements = selection.getSelectedElements();
// Logger.log("selection = " + selection);
if (elements.length == 1 &&
elements[0].getElement().getType() ==
DocumentApp.ElementType.INLINE_IMAGE) {
url = elements[0].getElement().asInlineImage().getLinkUrl();
if (!url) {
throw 'Invalid image - must have a PlantUML URL linked to it. See this <a href="https://sites.google.com/site/plantumlgizmo/learn#TOC-Can-I-update-the-source-of-PlantUML-diagrams-">FAQ</a>.';
}
// Logger.log("recoveredURL = " + url);
} else {
throw "Must select a PlantUML diagram.";
}
} else {
throw 'Must select an image that was inserted with PlantUML Gizmo. See this <a href="https://sites.google.com/site/plantumlgizmo/learn#TOC-Can-I-update-the-source-of-PlantUML-diagrams-">FAQ</a>.';
}
return url;
}
/**
* Inserts an Image at the selection
*
* @param {string} imageUrl The image URL to insert.
*/
function insertImage(imageDataUrl, imageUrl) {
// imageDataUrl = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAjCAIAAAB6jJ8NAAAANXRFWHRjb3B5bGVmdABHZW5lcmF0ZWQgYnkgaHR0cDovL3BsYW50dW1sLnNvdXJjZWZvcmdlLm5ldDpnVRsAAADXelRYdHBsYW50dW1sAAB4nC2NQWuDQBhE7wv+hznqwaCrpsVDCQkhISiEmqTHsupCluhu2P3Wtv++VnqZw8wb3saRsOTHIWDuofRTWDHC0c8g57Sq+586MRcZx5fq6R4wgfgNLUp0AdtI3S8QOw9C07WuMEnrlNF4TRIefsgeJz+AZ0jXZZGXWY7rZQeepHnEwsO5gjPedhK9+lO2nuZvxE5iEuGljtDs8e41qVFirydljR6lpmXH0VDzNLRw6zzeKkIj7ezHrWbp6mWVfPIibtOCVUr7718MmUnBsuCmqAAAAYtJREFUeNpj+A8G+52S9zvF4kVp/5HAfqcEQuqTISoZ4Bb8+3cOD8K0gJD6UQsQFjx+vOP58900tGD58o4tWyaRYMG1a+vKyxNSUgJPn15KpAVxcT6ZmaFTplQQZcGfP2fWretduLA5IcGPSAuWLm0DMiIi3ImyoLEx8+TJxd++nYiJ8SLSgg0b+oEMoD+IsmDx4paampS6ujTiLQgMdJw0qRwYsMRG8q9fp0lNpsCAHc1oFFqQst8pDh9yzkC1IJGQ+lQUC9DAy/1n/5MC8KjHbsHl+lkkWYBH/QBZQEUwQBY8WX+AJFPwqB+gOLjZv5wkC/CoHxyRvG/fvt27d9Mqkvv7+48cOXLz5k1aRXJKSgr1I/ne/M1w9s6dO5uammbOnInHAmT15MTBXzCAc4+GVNzoXvL78zda5WRggKxkttimEXrYv+T10YvkWHAqpfVK4xxcaJ9D5goGMwhaxWq1XS/qzqz11MzJQFmg0StZLLdphj1YtJXSSMYEwDg4ElT++eYDqkXyoC6uAXsM0J5rp8HfAAAAAElFTkSuQmCC"; // hack for a test
/*
* For debugging cursor info
*/
// var cursor = DocumentApp.getActiveDocument().getCursor();
// Logger.log(cursor.getElement().getParent().getType());
// throw "cursor info: " + cursor.getElement().getType() + " offset = " + cursor.getOffset() + " surrounding text = '" + cursor.getSurroundingText().getText() + "' parent's type = " +
// cursor.getElement().getParent().getType();
/*
* end debug
*/
// Logger.log("insertImage got imageDataURL of '" + imageDataUrl + "'");
// Logger.log("insertImage got imageURL of '" + imageUrl + "'");
var doc = DocumentApp.getActiveDocument();
var selection = doc.getSelection();
var replaced = false;
//Logger.log("insertImage()");
/// TODO handle replacement cases
if (selection) {
var elements = selection.getSelectedElements();
// delete the selected image (to be replaced)
if (elements.length == 1 &&
elements[0].getElement().getType() ==
DocumentApp.ElementType.INLINE_IMAGE) {
var parentElement = elements[0].getElement().getParent(); // so we can re-insert cursor
elements[0].getElement().removeFromParent();
replaced = true;
// move cursor to just before deleted image
doc.setCursor(DocumentApp.getActiveDocument().newPosition(parentElement, 0));
} else {
throw "Please select only one image (image replacement) or nothing (image insertion)"
}
}
var cursor = doc.getCursor();
var blob;
if (imageDataUrl != "") {
blob = getBlobFromBase64(imageDataUrl);
} else {
blob = getBlobViaFetch(imageUrl);
}
var image = cursor.insertInlineImage(blob);
image.setLinkUrl(imageUrl);
// move the cursor to after the image
var position = doc.newPosition(cursor.getElement(), cursor.getOffset()+1);
doc.setCursor(position);
// resize to width
if (cursor.getElement().getType() == DocumentApp.ElementType.PARAGRAPH) {
// Logger.log("Resizing");
var currentParagraph = DocumentApp.getActiveDocument().getCursor().getElement().asParagraph();
var originalImageWidth = image.getWidth(); // pixels
var documentWidthPoints = DocumentApp.getActiveDocument().getBody().getPageWidth() - DocumentApp.getActiveDocument().getBody().getMarginLeft() - DocumentApp.getActiveDocument().getBody().getMarginRight();
var documentWidth = documentWidthPoints * 96 / 72; // convert to pixels (a guess)
var paragraphWidthPoints = documentWidthPoints - currentParagraph.getIndentStart() - currentParagraph.getIndentEnd();
var paragraphWidth = paragraphWidthPoints * 96 / 72; // convert to pixels (a guess)
if (originalImageWidth > paragraphWidth) {
image.setWidth(paragraphWidth);
// scale proportionally
image.setHeight(image.getHeight() * image.getWidth() / originalImageWidth);
}
}
// re-select inserted image
if (selection) {
var rangeBuilder = doc.newRange().addElement(image);
doc.setSelection(rangeBuilder.build());
}
}
function getBlobViaFetch(imageDataUrl) {
// Logger.log("UrlFetchApp.fetch");
var resp = UrlFetchApp.fetch(imageDataUrl); // raw data
// Logger.log("resp code: " + resp.getResponseCode());
// Logger.log("fetch resp: " + resp.getContent());
if (resp.getResponseCode() != 200) {
throw "HTTPResponse returned code of " + resp.getResponseCode() + " for URL " + imageUrl;
}
// Inserts the image and sets its link
var blob = resp.getBlob();
//var blob = resp.getAs('image/png');
if (blob == null) {
throw "Blob is null ";
}
blob.setContentType('image/png'); // since it's null
// Logger.log("blob type: " + blob.getContentType() + ", bytes = '" + blob.getBytes() + "'");
return blob;
}
function getBlobFromBase64(imageDataUrl) {
var base64String = imageDataUrl.replace("data:image/png;base64,","");
var blob = Utilities.newBlob(Utilities.base64Decode(base64String));
blob.setContentType("image/png")
if (blob == null) {
throw "getBlobFromBase64: Blob is null ";
}
// Logger.log("getBlobFromBase64: blob type: " + blob.getContentType() + ", bytes = '" + blob.getBytes() + "'");
return blob;
}