-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
189 lines (161 loc) · 4.78 KB
/
Code.gs
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
// Note: highlights coordinated with the frontend colors in index.css
var colors = {
concision: {highlight: '#ffb0fa', emphasis: '#e469dd'},
clarity: {highlight: '#f9be8b', emphasis: '#fd9842'},
logic: {highlight: '#8ed6f9', emphasis: '#23b7fd'},
grammar: {highlight: '#9decb0', emphasis: '#38e562'},
};
// Through `ui` we access the menu, the sidebar, etc.
var ui = DocumentApp.getUi();
/*
* Init
*/
function onOpen() {
ui.createMenu('Comments')
.addItem('Start', 'index')
.addToUi();
}
/*
* Handle menu item clicks
*/
function index() {
renderToSidebar('index', 'Revise');
}
/*
* API
*/
var production_api_website = 'https://www.writelab.com';
var production_api_root = production_api_website + '/api/';
var production_api_auth = PropertiesService
.getScriptProperties()
.getProperty('WRITELAB_BASIC_AUTH');
// Supplies the necessary header, content type and body parameters
// for making requests using `UrlFetchApp.fetch`.
function prepareParams(options) {
var params = {
contentType: "application/json",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Basic " + Utilities.base64Encode(production_api_auth)
}
};
if (options) {
if (options.text)
params.payload = JSON.stringify({text: options.text});
params.method = options.method || "get";
}
return params;
}
// Submit text for analysis and return the status resource location.
function api_submitText(text) {
var params = prepareParams({method: "post", text: '' + text});
var response = UrlFetchApp.fetch(production_api_root, params);
return response.getAllHeaders().Location;
}
// Given a status resource location, check if the resource is ready.
// If it is, return the comments. If it's not ready, return `undefined`.
function api_getCommentsOrUndefined(location) {
var params = prepareParams();
var response = UrlFetchApp.fetch(production_api_website + location, params);
var status = response.getResponseCode();
// Still parsing...
if (status !== 200) {
return;
}
// Comments are ready
return {
status: status,
comments: JSON.parse(response.getContentText()).comments
};
}
/*
* Highlighting
*/
function hilite(options) {
var text = options.text || DocumentApp.getActiveDocument().getBody().editAsText();
text.setBackgroundColor(
options.start,
options.end,
options.color || '#FF0000');
}
function emphasize(options) {
var text = options.text || DocumentApp.getActiveDocument().getBody().editAsText();
text.setBackgroundColor(options.start, options.end, options.color);
}
function unhighlight() {
var body = DocumentApp.getActiveDocument().getBody();
var n = body.getNumChildren();
var i;
for (i = 0; i < n; i++) {
// Warning: passing `null` as a way of making bg transparent is an undocumented
// feature. The previous alternative was '#FFFFFF' (white), but that's intrusive.
body.getChild(i).asText().editAsText().setBackgroundColor(null);
}
}
function unemphasize(comments) {
// Highlighting has the effect of unemphasizing by replacing highlight
// colors with emphasis colors
highlightComments(comments);
}
function highlightComments(comments) {
var comment;
var i, j;
var indices;
var index;
var module;
for (i = 0; i < comments.length; i++) {
comment = comments[i];
indices = comment.indices;
color = colors[comment.module].highlight;
for (j = 0; j < indices.length; j++) {
index = indices[j];
hilite({start: index[0], end: index[1], color: color});
}
}
}
/*
* Ajax
*/
// Analyze the entire document
function analyze_all() {
var doc = DocumentApp.getActiveDocument();
return api_submitText(doc.getBody().getText());
}
// Make API request to WL and if successful, highlight the returned comments.
// Note: functionality combined to reduce the number of requests.
function checkStatus(location) {
var resp = api_getCommentsOrUndefined(location);
if (resp) {
highlightComments(resp.comments);
}
return resp;
}
// Highlight indices
function highlightByIndices(indices, module) {
indices = indices.split(',').map(function(str) {
return parseInt(str, 10);
});
unhighlight();
hilite({start: indices[0], end: indices[1], color: colors[module].highlight});
}
function emphasizeByIndices(indices, module, comments) {
unemphasize(comments);
indices = indices.split(',').map(function(str) {
return parseInt(str, 10);
});
emphasize({start: indices[0], end: indices[1], color: colors[module].emphasis});
}
/*
* Helpers
*/
// Evaluates a given template and assigns its contents
// to the sidebar.
function renderToSidebar(template, title) {
ui.showSidebar(HtmlService
.createTemplateFromFile(template)
.evaluate()
.setTitle(title)
.setWidth(300)
.setSandboxMode(HtmlService.SandboxMode.IFRAME));
}