-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSidebarJavaScript.html
More file actions
267 lines (245 loc) · 7.83 KB
/
SidebarJavaScript.html
File metadata and controls
267 lines (245 loc) · 7.83 KB
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
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
/**
* Run initializations on sidebar load.
*/
$(function() {
// Assign handler functions to sidebar elements here, if needed.
$('#reformat-document').click(onReformatButtonClick);
});
// Flags to maintain function state
var flags = {
formatFont: false,
underlinesItalics: false,
formatHeading: false,
documentColors: false,
imageAltText: false,
summarize: false
};
/**
* Calls the server to modify the document.
* Master function that checks button toggle condition and performs work.
*/
function onReformatButtonClick() {
// Set flags based on the toggles
flags.formatFont = $('#toggle-format-font').is(':checked');
flags.underlinesItalics = $('#toggle-underlines-italics').is(':checked');
flags.formatHeading = $('#toggle-format-headings').is(':checked');
flags.documentColors = $('#toggle-document-color').is(':checked');
flags.imageAltText = $('#toggle-image-prompt').is(':checked');
flags.summarize = $('#toggle-ai-summary').is(':checked');
if (!checkAllOperationsComplete()) {
document.getElementById('reformat-document').setAttribute('aria-busy', 'true');
document.getElementById('reformat-document').disabled = true;
}
if (flags.formatHeading) {
autoFormatHeading();
}
if (flags.formatFont) {
autoFormatFont();
}
if (flags.underlinesItalics) {
removeUnderlineItalics();
}
if (flags.documentColors) {
optimizeColors();
}
if (flags.imageAltText) {
imageAltTextCheck()
}
if (flags.summarize) {
aiSummarize();
}
};
/**
* Mark a task as complete.
*/
function completeOperation(operation) {
flags[operation] = false;
checkAllOperationsComplete();
}
/**
* Check if all tasks are completed.
* If so, return to normal app state.
*/
function checkAllOperationsComplete() {
for (var key in flags) {
if (flags[key]) {
return false; // If any operation is still true, return false
}
}
// All operations are complete
var reformatButton = document.getElementById('reformat-document');
reformatButton.disabled = false;
reformatButton.setAttribute('aria-busy', 'false');
return true;
}
/**
* Format Document Font
*/
function autoFormatFont() {
// Directly set the font to Arial.
google.script.run
.withSuccessHandler(function(msg, element) {
// Respond to success conditions here.
console.log("Changed Font Familty and Size");
unToggleCheckbox('toggle-format-font');
completeOperation('formatFont');
})
.withFailureHandler(function(msg, element) {
// Respond to failure conditions here.
unToggleCheckbox('toggle-format-font');
completeOperation('formatFont');
})
.withUserObject(this)
.setDocumentFontToArial();
}
/**
* Replace all underline and italics with bold text.
*/
function removeUnderlineItalics() {
google.script.run
.withSuccessHandler(function(msg, element) {
// Respond to success conditions here.
console.log("Removed Underlines/Italics");
unToggleCheckbox('toggle-underlines-italics');
completeOperation('underlinesItalics');
})
.withFailureHandler(function(msg, element) {
// Respond to failure conditions here.
unToggleCheckbox('toggle-underlines-italics');
completeOperation('underlinesItalics');
})
.withUserObject(this)
.replaceWithBold();
}
/**
* Automatically create headings.
*/
function autoFormatHeading() {
google.script.run
.withSuccessHandler(function(msg, element) {
// Respond to success conditions here.
console.log("Formatted Headings");
unToggleCheckbox('toggle-format-headings');
completeOperation('formatHeading');
})
.withFailureHandler(function(msg, element) {
// Respond to failure conditions here.
unToggleCheckbox('toggle-format-headings');
completeOperation('formatHeading');
})
.withUserObject(this)
.applyHeadings();
}
/**
* Change the document colors to have a high contrast
* (black on white)
*/
function optimizeColors() {
google.script.run
.withSuccessHandler(function(msg, element) {
// Respond to success conditions here.
console.log("Colors Optimized");
unToggleCheckbox('toggle-document-color');
completeOperation('documentColors');
})
.withFailureHandler(function(msg, element) {
// Respond to failure conditions here.
unToggleCheckbox('toggle-document-color');
completeOperation('documentColors');
})
.withUserObject(this)
.setDocumentColorsToHighContrast();
}
/**
* Check for image alt tags and prompt the user
* when they are found.
*/
function imageAltTextCheck() {
google.script.run
.withSuccessHandler(function(msg, element) {
// Respond to success conditions here.
console.log("Images Checked");
unToggleCheckbox('toggle-image-prompt');
completeOperation('imageAltText');
})
.withFailureHandler(function(msg, element) {
// Respond to failure conditions here.
unToggleCheckbox('toggle-image-prompt');
completeOperation('imageAltText');
})
.withUserObject(this)
.findImagesAndPrompt();
}
/**
* Use openAI to summarize the content
*/
function aiSummarize() {
google.script.run
.withSuccessHandler(function(msg, element) {
// Respond to success conditions here.
console.log("AI Summary function");
unToggleCheckbox('toggle-ai-summary');
completeOperation('summarize');
})
.withFailureHandler(function(msg, element) {
// Respond to failure conditions here
unToggleCheckbox('toggle-ai-summary');
completeOperation('summarize');
})
.withUserObject(this)
.aiSummarizer();
}
/**
* Finds all the images located in the text
*/
function findImagesInDocument() {
var body = DocumentApp.getActiveDocument().getBody();
var numElements = body.getNumChildren();
var images = [];
for (var i = 0; i < numElements; i++) {
var element = body.getChild(i);
// Check if the element is a paragraph
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
var numChildren = element.getNumChildren();
// Iterate through all children elements of the paragraph
for (var j = 0; j < numChildren; j++) {
var child = element.getChild(j);
// Check if the child is an InlineImage
if (child.getType() === DocumentApp.ElementType.INLINE_IMAGE) {
images.push(child);
// Optional: Do something with the image, like logging its URL
Logger.log(child.getBlob().getContentType());
Logger.log(child.getBlob().getBytes().length);
}
}
}
}
Logger.log('Total images found: ' + images.length);
return images;
}
/**
* Untoggle checkboxes based on ID
*/
function unToggleCheckbox(checkboxId) {
var checkbox = document.getElementById(checkboxId);
if (checkbox) {
checkbox.checked = false;
}
}
/**
* Change the theme whenever the toggle button is clicked
*/
document.getElementById('theme-toggle-link').addEventListener('click', function(event) {
event.preventDefault(); // Prevent default link behavior
var currentTheme = document.documentElement.getAttribute('data-theme');
if (currentTheme === 'dark') {
// If current theme is dark, switch to light
document.documentElement.setAttribute('data-theme', 'light');
} else {
// If current theme is light or undefined, switch to dark
document.documentElement.setAttribute('data-theme', 'dark');
}
});
</script>