-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
348 lines (319 loc) · 12.5 KB
/
functions.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
const signatureSetting = 'calendarSignature';
let existingBody = '';
let requestHeader = undefined;
Office.onReady(function(info) {
// note: used in both the add signature button and the standalone taskpane, so we need to check objects exist
if (info.host === Office.HostType.Outlook) {
if (window.jQuery) {
$(document).ready(function() {
const textColours = [
'000000', '262626', '595959', '8c8c8c', 'bfbfbf', 'd9d9d9', 'e9e9e9', 'f5f5f5', 'fafafa', 'ffffff',
'fbe9e6', 'fcede1', 'fcefd4', 'fcfbcf', 'e7f6d5', 'daf4f0', 'd9edfa', 'e0e8fa', 'ede1f8', 'f6e2ea',
'ffa39e', 'ffbb96', 'ffd591', 'fffb8f', 'b7eb8f', '87e8de', '91d5ff', 'adc6ff', 'd3adf7', 'ffadd2',
'ff4d4f', 'ff7a45', 'ffa940', 'ffec3d', '73d13d', '36cfc9', '40a9ff', '597ef7', '9254de', 'f759ab',
'e13c39', 'e75f33', 'eb903a', 'f5db4d', '72c040', '59bfc0', '4290f7', '3658e2', '6a39c9', 'd84493',
'cf1322', 'd4380d', 'd46b08', 'd4b106', '389e0d', '08979c', '096dd9', '1d39c4', '531dab', 'c41d7f',
'820014', '871400', '873800', '614700', '135200', '00474f', '003a8c', '061178', '22075e', '780650'
];
const trumbowygElement = $('#signatureText');
trumbowygElement.trumbowyg({
btns: [ // note: carefully arranged to avoid overly limiting the colour pane's width
['fontfamily', 'fontsize'], ['bold', 'italic', 'underline', 'del'], ['link'],
['foreColor', 'backColor', 'unorderedList', 'orderedList', 'horizontalRule'], ['viewHTML']
],
semantic: false,
plugins: {
colors: {
foreColorList: textColours,
allowCustomForeColor: true,
backColorList: textColours,
allowCustomBackColor: true,
}
},
autogrow: false
});
if (Office.context.roamingSettings.get(signatureSetting)) {
trumbowygElement.trumbowyg('html',
Base64.decode(Office.context.roamingSettings.get(signatureSetting)));
}
let height = 100; // to avoid button overlap at small sizes
trumbowygElement.closest('.trumbowyg-box').css({minHeight: height});
height -= $('.trumbowyg-button-pane').height() - 1; // button bar is 36, but can wrap; 1 is the border
trumbowygElement.prev('.trumbowyg-editor').css({minHeight: height});
trumbowygElement.css({minHeight: height}); // fixes the raw HTML editor
showInterface();
$(window).on('resize', resizeEditor);
resizeEditor();
});
} else {
showInterface();
}
}
});
function showInterface() {
// elements are hidden by default (in case Office is not present)
const signatureBox = document.getElementById('signatureText');
if (signatureBox) {
signatureBox.style.display = 'block';
}
const saveButton = document.getElementById('saveSignature');
if (saveButton) {
saveButton.style.display = 'block';
saveButton.onclick = saveSignature;
}
}
function showTaskPaneMessage(messageText, autoHide) {
if (window.jQuery) {
$('#messageBox').text(messageText).show().delay(2500).queue(function (next) {
if (autoHide) {
$(this).hide();
}
next();
});
} else {
console.log(messageText);
}
}
function resizeEditor() {
const saveButton = $('#saveSignature');
const trumbowygElement = $('#signatureText');
const closestBox = trumbowygElement.closest('.trumbowyg-box');
// the first launch popup is shown in a different style, so needs a slight visual edit
if (window.location.search.indexOf('?firstlaunch') !== -1) {
$('body').css({padding: 0});
saveButton.css({margin: 0, marginRight: 6});
}
// make the editor fill the available height
let newHeight = saveButton.position().top - closestBox.position().top - 12; // 12 is padding between button and box
closestBox.css({height: newHeight});
newHeight = newHeight - $('.trumbowyg-button-pane').height() - 1; // button bar is 36, but can wrap; 1 is the border
trumbowygElement.prev('.trumbowyg-editor').css({height: newHeight}); // the main editor box
trumbowygElement.css({height: newHeight}); // the textarea (used for HTML editing)
}
function cleanIDAndClassValues(bodyContent) {
// need to remove any id="[id]" (+ class="") because Outlook replaces it with id="x_[id]", which breaks detection
return bodyContent.replaceAll(/(id|class)="x_(.*?)"/gi, '$1="$2"');
}
function saveSignature() {
let hasSavedSignature = Office.context.roamingSettings.get(signatureSetting);
// we need to remove any id="[id]" (+ class="") because Outlook replaces it with id="x_[id]", which breaks detection
let signature;
if (window.jQuery) {
signature = cleanIDAndClassValues($('#signatureText').trumbowyg('html'));
} else {
signature = cleanIDAndClassValues(document.getElementById('signatureText').value);
}
showTaskPaneMessage('✎ Saving…', false);
Office.context.roamingSettings.set(signatureSetting, Base64.encode(signature));
Office.context.roamingSettings.saveAsync(function(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
showTaskPaneMessage('✓ Saved', true);
if (!hasSavedSignature) {
if (typeof Office.context.ui.messageParent == 'function') {
Office.context.ui.messageParent(true.toString()); // first time - close dialog and re-add signature
}
}
// Office.addin.hide(); // not supported on the web
} else {
showTaskPaneMessage('✗ Save failed', false);
console.error('saveSignature', asyncResult.status, ':', asyncResult.error.message);
}
});
}
function addSignature(firstTime) {
// if triggered from the ribbon button (i.e., not a taskpane) we get an anonymous callback and notify once complete
if (typeof firstTime === 'object') {
try {
if (typeof firstTime.completed === 'function') {
requestHeader = firstTime;
}
} catch (error) {
}
firstTime = true;
}
if (!Office.context.roamingSettings.get(signatureSetting)) {
console.log('addSignature: no saved signature found; opening save dialog');
// we need a separate dialog box on first load because Office.addin.showAsTaskpane() isn't supported in Outlook
Office.context.ui.displayDialogAsync(
'https://simonrob.github.io/ocsa/taskpane.html?firstlaunch',
{width: 60, height: 70, displayInIframe: true},
function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
const dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, function (message) {
dialog.close(); // the only message we ever send is "signature saved"; no need to check content
// it would be better to call addSignature (with requestHeader) to add the signature straight
// away, but there is no way (without a page refresh) to reload the roamingSettings object; if
// we instead call addSignature from the save callback, Office.context.mailbox is undefined...
if (typeof requestHeader === 'object') {
requestHeader.completed();
requestHeader = undefined;
}
});
} else {
console.log('displayDialogAsync', asyncResult.status, ':', asyncResult.error.message);
}
}
);
return;
}
// append the signature to any existing body content - note that calendar invitations are always HTML:
// https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/insert-data-in-the-body
// note: we need to do this because while there is a setSignatureAsync method, it doesn't work with calendar events
// ("The operation is not supported"): https://learn.microsoft.com/en-us/javascript/api/outlook/office.body?view=
// outlook-js-preview#outlook-office-body-setsignatureasync-member(1)
Office.context.mailbox.item.body.getAsync(
Office.CoercionType.Html,
function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
if (asyncResult.value.indexOf('</body>') !== -1) {
// most versions of Outlook handle appending to HTML with no problem; the old macOS interface needs help
appendSignature(false, asyncResult.value.replace('</body>',
Base64.decode(Office.context.roamingSettings.get(signatureSetting)) + '</body>'));
} else {
appendSignature(false, asyncResult.value +
Base64.decode(Office.context.roamingSettings.get(signatureSetting)));
}
/*
// note: removed this more complex approach as it needs more testing in desktop apps
if (firstTime) {
// first step: get the existing body to later check whether it has a signature already
existingBody = asyncResult.value;
appendSignature(false, Base64.decode(Office.context.roamingSettings.get(signatureSetting)));
} else {
// third step: asyncResult.value is now the signature as re-formatted by Outlook
appendSignature(false, cleanIDAndClassValues(existingBody.replace(asyncResult.value, '')) +
asyncResult.value);
}
*/
} else {
console.log('addSignature', asyncResult.status, ':', asyncResult.error.message);
}
}
);
}
function appendSignature(firstTime, bodyContent) {
Office.context.mailbox.item.body.setAsync(
bodyContent,
{ coercionType: Office.CoercionType.Html },
function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
if (firstTime) {
// second step: add just the signature, replacing the existing body
addSignature(false);
} else {
// final step: hide the "working on your request" message
if (typeof requestHeader === 'object') {
requestHeader.completed();
requestHeader = undefined;
}
}
} else{
console.log('appendSignature', asyncResult.status, ':', asyncResult.error.message);
}
}
);
}
/**
*
* UTF-8-safe Base64 encode / decode utility
* https://www.webtoolkit.info/javascript_base64.html
*
**/
var Base64 = {
// private property
_keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
// public method for encoding
encode: function(input) {
var output = '';
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
} // Whend
return output;
}, // End Function encode
// public method for decoding
decode: function(input) {
var output = '';
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
} // Whend
output = Base64._utf8_decode(output);
return output;
}, // End Function decode
// private method for UTF-8 encoding
_utf8_encode: function(string) {
var utftext = '';
string = string.replace(/\r\n/g, '\n');
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
} // Next n
return utftext;
}, // End Function _utf8_encode
// private method for UTF-8 decoding
_utf8_decode: function(utftext) {
var string = '';
var i = 0;
var c, c1, c2, c3;
c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
} // Whend
return string;
} // End Function _utf8_decode
}