-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMForce365.js
More file actions
298 lines (252 loc) · 10.2 KB
/
MForce365.js
File metadata and controls
298 lines (252 loc) · 10.2 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
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
window.getWindowDimensions = function () {
return {
width: window.innerWidth,
height: window.innerHeight
};
};
window.mforce365 = window.mforce365 || {};
window.mforce365.focusNotesInput = function () {
const selectors = [
"#meeting-pre-notes textarea",
"#meeting-notes-compose-input",
"#meeting-notes-compose-rich [contenteditable='true']"
];
for (const selector of selectors) {
const element = document.querySelector(selector);
if (!element) {
continue;
}
element.scrollIntoView({ behavior: "smooth", block: "center" });
if (typeof element.focus === "function") {
element.focus();
}
if (typeof element.selectionStart === "number" && typeof element.selectionEnd === "number") {
const end = element.value ? element.value.length : 0;
element.selectionStart = end;
element.selectionEnd = end;
}
return;
}
};
window.mforce365.copyTextToClipboard = async function (text) {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
return;
}
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.setAttribute("readonly", "readonly");
textArea.style.position = "fixed";
textArea.style.opacity = "0";
textArea.style.pointerEvents = "none";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
const copied = document.execCommand("copy");
if (!copied) {
throw new Error("Clipboard copy command was rejected.");
}
} finally {
document.body.removeChild(textArea);
}
};
window.mforce365.getBrowserCulture = function () {
if (navigator.languages && navigator.languages.length > 0) {
for (const language of navigator.languages) {
if (language) {
return language;
}
}
}
return navigator.language || Intl.DateTimeFormat().resolvedOptions().locale || "";
};
window.mforce365.tryAcquireNewUserFollowUpLock = function (storageKey, lockId, lockExpiresAtUtc) {
const currentStateJson = window.localStorage.getItem(storageKey);
if (!currentStateJson) {
return false;
}
let currentState;
try {
currentState = JSON.parse(currentStateJson);
} catch {
return false;
}
if (!currentState || !currentState.FirstSeenUtc || currentState.SentAtUtc) {
return false;
}
if (currentState.SendLockId && currentState.SendLockExpiresAtUtc) {
const existingLockExpiry = Date.parse(currentState.SendLockExpiresAtUtc);
if (!Number.isNaN(existingLockExpiry) && existingLockExpiry > Date.now()) {
return false;
}
}
currentState.SendLockId = lockId;
currentState.SendLockExpiresAtUtc = lockExpiresAtUtc;
window.localStorage.setItem(storageKey, JSON.stringify(currentState));
return true;
};
const b64toBlob = (b64Data, contentType = '', sliceSize = 512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, { type: contentType });
return blob;
}
// Convert a base64 string to a Uint8Array. This is needed to create a blob object from the base64 string.
// The code comes from: https://developer.mozilla.org/fr/docs/Web/API/WindowBase64/D%C3%A9coder_encoder_en_base64
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91 ? nChr - 65 : nChr > 96 && nChr < 123 ? nChr - 71 : nChr > 47 && nChr < 58 ? nChr + 4 : nChr === 43 ? 62 : nChr === 47 ? 63 : 0;
}
function base64DecToArr(sBase64, nBlocksSize) {
var
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""),
nInLen = sB64Enc.length,
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2,
taBytes = new Uint8Array(nOutLen);
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0;
}
}
return taBytes;
}
function jsOpenIntoNewTab(filename, byteBase64) {
var blob = b64toBlob(byteBase64);
blob.type = "application.pdf";
var blobURL = URL.createObjectURL(blob);
window.open(blobURL);
}
function BlazorDownloadFile(filename, contentType, content) {
// Blazor marshall byte[] to a base64 string, so we first need to convert the string (content) to a Uint8Array to create the File
const data = base64DecToArr(content);
// Create the URL
const file = new File([data], filename, { type: contentType });
const exportUrl = URL.createObjectURL(file);
// Create the <a> element and click on it
const a = document.createElement("a");
document.body.appendChild(a);
a.href = exportUrl;
a.download = filename;
a.target = "_self";
a.click();
// We don't need to keep the url, let's release the memory
// On Safari it seems you need to comment this line... (please let me know if you know why)
URL.revokeObjectURL(exportUrl);
}
// Convert a base64 string to a Uint8Array. This is needed to create a blob object from the base64 string.
// The code comes from: https://developer.mozilla.org/fr/docs/Web/API/WindowBase64/D%C3%A9coder_encoder_en_base64
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91 ? nChr - 65 : nChr > 96 && nChr < 123 ? nChr - 71 : nChr > 47 && nChr < 58 ? nChr + 4 : nChr === 43 ? 62 : nChr === 47 ? 63 : 0;
}
function synchronizeFileWithIndexedDb(filename) {
return new Promise((res, rej) => {
const db = window.indexedDB.open('SqliteStorage', 1);
db.onupgradeneeded = () => {
db.result.createObjectStore('Files', { keypath: 'id' });
};
db.onsuccess = () => {
const req = db.result.transaction('Files', 'readonly').objectStore('Files').get('file');
req.onsuccess = () => {
Module.FS_createDataFile('/', filename, req.result, true, true, true);
res();
};
};
let lastModifiedTime = new Date();
setInterval(() => {
const path = `/${filename}`;
if (FS.analyzePath(path).exists) {
const mtime = FS.stat(path).mtime;
if (mtime.valueOf() !== lastModifiedTime.valueOf()) {
lastModifiedTime = mtime;
const data = FS.readFile(path);
db.result.transaction('Files', 'readwrite').objectStore('Files').put(data, 'file');
}
}
}, 1000);
});
}
window.bufferToCanvas = function (elem, buffer, width, height) {
let imageData = new ImageData(new Uint8ClampedArray(buffer.buffer, 0, width * height * 4), width, height);
elem.width = width;
elem.height = height;
elem.getContext('2d').putImageData(imageData, 0, 0);
}
function BlazorDownloadFileFast(name, contentType, content) {
// Convert the parameters to actual JS types
const nameStr = BINDING.conv_string(name);
const contentTypeStr = BINDING.conv_string(contentType);
const contentArray = Blazor.platform.toUint8Array(content);
// Create the URL
const file = new File([contentArray], nameStr, { type: contentTypeStr });
const exportUrl = URL.createObjectURL(file);
// Create the <a> element and click on it
const a = document.createElement("a");
document.body.appendChild(a);
a.href = exportUrl;
a.download = nameStr;
a.target = "_self";
a.click();
// We don't need to keep the url, let's release the memory
// On Safari it seems you need to comment this line... (please let me know if you know why)
URL.revokeObjectURL(exportUrl);
}
function BlazorDownloadFile(filename, bytesBase64) {
var link = document.createElement('a');
link.download = filename;
link.href = "data:application/octet-stream;base64," + bytesBase64;
document.body.appendChild(link); // Needed for Firefox
link.click();
document.body.removeChild(link);
}
function base64DecToArr(sBase64, nBlocksSize) {
var
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""),
nInLen = sB64Enc.length,
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2,
taBytes = new Uint8Array(nOutLen);
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0;
}
}
return taBytes;
}
function saveAsFile(filename, bytesBase64) {
if (navigator.msSaveBlob) {
//Download document in Edge browser
var data = window.atob(bytesBase64);
var bytes = new Uint8Array(data.length);
for (var i = 0; i < data.length; i++) {
bytes[i] = data.charCodeAt(i);
}
var blob = new Blob([bytes.buffer], { type: "application/octet-stream" });
navigator.msSaveBlob(blob, filename);
}
else {
var link = document.createElement('a');
link.download = filename;
link.href = "data:application/octet-stream;base64," + bytesBase64;
document.body.appendChild(link); // Needed for Firefox
link.click();
document.body.removeChild(link);
}
}