forked from zulip/zulip-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatedEs3.js
1084 lines (893 loc) · 30.8 KB
/
generatedEs3.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This is a GENERATED file -- do not edit.
* To make changes:
* 1. Edit `js.js`, which is the source for this file.
* 2. Run `tools/generate-webview-js`.
*
* @generated
* @flow strict
*/
export default `
'use strict';
var compiledWebviewJs = (function (exports) {
'use strict';
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
var makeUserId = function makeUserId(id) {
return id;
};
var sendMessage = (function (msg) {
window.ReactNativeWebView.postMessage(JSON.stringify(msg));
});
var placeholdersDivTagFromContent = function placeholdersDivTagFromContent(content) {
var match = new RegExp('<div id="message-loading" class="(?:hidden)?">').exec(content);
return match !== null ? match[0] : null;
};
var InboundEventLogger = function () {
function InboundEventLogger() {
_classCallCheck(this, InboundEventLogger);
this._isCapturing = false;
this._capturedInboundEventItems = [];
}
_createClass(InboundEventLogger, [{
key: "startCapturing",
value: function startCapturing() {
if (this._isCapturing) {
throw new Error('InboundEventLogger: Tried to call startCapturing while already capturing.');
} else if (this._capturedInboundEventItems.length > 0 || this._captureEndTime !== undefined) {
throw new Error('InboundEventLogger: Tried to call startCapturing before resetting.');
}
this._isCapturing = true;
this._captureStartTime = Date.now();
}
}, {
key: "stopCapturing",
value: function stopCapturing() {
if (!this._isCapturing) {
throw new Error('InboundEventLogger: Tried to call stopCapturing while not capturing.');
}
this._isCapturing = false;
this._captureEndTime = Date.now();
}
}, {
key: "send",
value: function send() {
var _this$_captureStartTi, _this$_captureEndTime;
if (this._isCapturing) {
throw new Error('InboundEventLogger: Tried to send captured events while still capturing.');
}
sendMessage({
type: 'warn',
details: {
startTime: (_this$_captureStartTi = this._captureStartTime) !== null && _this$_captureStartTi !== void 0 ? _this$_captureStartTi : null,
endTime: (_this$_captureEndTime = this._captureEndTime) !== null && _this$_captureEndTime !== void 0 ? _this$_captureEndTime : null,
inboundEventItems: this._capturedInboundEventItems
}
});
}
}, {
key: "reset",
value: function reset() {
this._captureStartTime = undefined;
this._captureEndTime = undefined;
this._capturedInboundEventItems = [];
this._isCapturing = false;
}
}, {
key: "maybeCaptureInboundEvent",
value: function maybeCaptureInboundEvent(event) {
if (this._isCapturing) {
var item = {
type: 'inbound',
timestamp: Date.now(),
scrubbedEvent: InboundEventLogger.scrubInboundEvent(event)
};
this._capturedInboundEventItems.push(item);
}
}
}], [{
key: "scrubInboundEvent",
value: function scrubInboundEvent(event) {
switch (event.type) {
case 'content':
{
return {
type: event.type,
scrollMessageId: event.scrollMessageId,
auth: 'redacted',
content: placeholdersDivTagFromContent(event.content),
updateStrategy: event.updateStrategy
};
}
case 'fetching':
{
return {
type: event.type,
showMessagePlaceholders: event.showMessagePlaceholders,
fetchingOlder: event.fetchingOlder,
fetchingNewer: event.fetchingNewer
};
}
case 'typing':
{
return {
type: event.type,
content: event.content !== ''
};
}
case 'ready':
{
return {
type: event.type
};
}
case 'read':
{
return {
type: event.type,
messageIds: event.messageIds
};
}
default:
{
return {
type: event.type
};
}
}
}
}]);
return InboundEventLogger;
}();
var inlineApiRoutes = ['^/user_uploads/', '^/thumbnail$', '^/avatar/'].map(function (r) {
return new RegExp(r);
});
var rewriteImageUrls = function rewriteImageUrls(auth, element) {
var realm = auth.realm;
var imageTags = [].concat(element instanceof HTMLImageElement ? [element] : [], Array.from(element.getElementsByTagName('img')));
imageTags.forEach(function (img) {
var actualSrc = img.getAttribute('src');
if (actualSrc == null) {
return;
}
var fixedSrc;
try {
fixedSrc = new URL(actualSrc, realm);
} catch (_unused) {
img.src = 'about:blank';
return;
}
if (fixedSrc.origin === realm.origin) {
if (inlineApiRoutes.some(function (regexp) {
return regexp.test(fixedSrc.pathname);
})) {
var delimiter = fixedSrc.search ? '&' : '';
fixedSrc.search += "".concat(delimiter, "api_key=").concat(auth.apiKey);
}
}
if (img.src !== fixedSrc.toString()) {
img.src = fixedSrc.toString();
}
});
};
var rewriteTime = function rewriteTime(element) {
if (typeof HTMLTimeElement !== 'function') {
return;
}
var timeElements = [].concat(element instanceof HTMLTimeElement ? [element] : [], Array.from(element.getElementsByTagName('time')));
timeElements.forEach(function (elem) {
if (!(elem instanceof HTMLTimeElement)) {
return;
}
var timeStamp = elem.dateTime;
var text = elem.innerText;
var d = new Date(timeStamp);
elem.setAttribute('original-text', text);
elem.innerText = "\\uD83D\\uDD52 ".concat(d.toLocaleString(undefined, {
dateStyle: 'full',
timeStyle: 'short'
}));
});
};
var rewriteSpoilers = function rewriteSpoilers(element) {
var spoilerHeaders = element.querySelectorAll('div.spoiler-header');
spoilerHeaders.forEach(function (e) {
var toggle_button_html = '<span class="spoiler-button" aria-expanded="false"><span class="spoiler-arrow"></span></span>';
if (e.innerText === '') {
var header_html = '<p>Spoiler</p>';
e.innerHTML = toggle_button_html + header_html;
} else {
e.innerHTML = toggle_button_html + e.innerHTML;
}
});
};
var rewriteHtml = function rewriteHtml(auth) {
var element = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document;
rewriteImageUrls(auth, element);
rewriteTime(element);
rewriteSpoilers(element);
};
var collapseSpoiler = function collapseSpoiler(spoiler) {
var computedHeight = getComputedStyle(spoiler).height;
requestAnimationFrame(function () {
spoiler.style.height = computedHeight;
spoiler.classList.remove('spoiler-content-open');
requestAnimationFrame(function () {
spoiler.style.height = '0px';
});
});
};
var expandSpoiler = function expandSpoiler(spoiler) {
var spoilerHeight = spoiler.scrollHeight;
spoiler.style.height = "".concat(spoilerHeight, "px");
spoiler.classList.add('spoiler-content-open');
var callback = function callback() {
spoiler.removeEventListener('transitionend', callback);
spoiler.style.height = '';
};
spoiler.addEventListener('transitionend', callback);
};
var toggleSpoiler = function toggleSpoiler(spoilerHeader) {
var spoilerBlock = spoilerHeader.parentElement;
if (!spoilerBlock) {
return;
}
var button = spoilerHeader.querySelector('.spoiler-button');
var arrow = spoilerBlock.querySelector('.spoiler-arrow');
var spoilerContent = spoilerBlock.querySelector('.spoiler-content');
if (!arrow || !button || !spoilerContent) {
console.warn('Malformed spoiler block');
return;
}
if (spoilerContent.classList.contains('spoiler-content-open')) {
arrow.classList.remove('spoiler-button-open');
button.setAttribute('aria-expanded', 'false');
spoilerContent.setAttribute('aria-hidden', 'true');
collapseSpoiler(spoilerContent);
} else {
arrow.classList.add('spoiler-button-open');
button.setAttribute('aria-expanded', 'true');
spoilerContent.setAttribute('aria-hidden', 'false');
expandSpoiler(spoilerContent);
}
};
if (!Array.from) {
Array.from = function from(arr) {
return Array.prototype.slice.call(arr);
};
}
if (!Element.prototype.closest) {
Element.prototype.closest = function closest(selector) {
var element = this;
while (element && !element.matches(selector)) {
element = element.parentElement;
}
return element;
};
}
if (!String.prototype.startsWith) {
String.prototype.startsWith = function startsWith(search, rawPos) {
var pos = rawPos > 0 ? rawPos | 0 : 0;
return this.substring(pos, pos + search.length) === search;
};
}
if (!String.prototype.includes) {
String.prototype.includes = function includes(search) {
var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
return this.indexOf(search, start) !== -1;
};
}
var documentBody = document.body;
if (!documentBody) {
throw new Error('No document.body element!');
}
var escapeHtml = function escapeHtml(text) {
var element = document.createElement('div');
element.innerText = text;
return element.innerHTML;
};
window.onerror = function (message, source, line, column, error) {
if (window.enableWebViewErrorDisplay) {
var elementJsError = document.getElementById('js-error-detailed');
if (elementJsError) {
elementJsError.innerHTML = ["Message: ".concat(message), "Source: ".concat(source), "Line: ".concat(line, ":").concat(column), "Error: ".concat(JSON.stringify(error)), ''].map(escapeHtml).join('<br>');
}
} else {
var _elementJsError = document.getElementById('js-error-plain');
var elementSheetGenerated = document.getElementById('generated-styles');
var elementSheetHide = document.getElementById('style-hide-js-error-plain');
if (_elementJsError && elementSheetGenerated && elementSheetHide && elementSheetHide instanceof HTMLStyleElement && elementSheetHide.sheet && elementSheetGenerated instanceof HTMLStyleElement && elementSheetGenerated.sheet) {
elementSheetHide.sheet.disabled = true;
var height = _elementJsError.offsetHeight;
elementSheetGenerated.sheet.insertRule(".header-wrapper { top: ".concat(height, "px; }"), 0);
}
}
var userAgent = window.navigator.userAgent;
sendMessage({
type: 'error',
details: {
message: message,
source: source,
line: line,
column: column,
userAgent: userAgent,
error: error
}
});
return true;
};
var eventLogger = new InboundEventLogger();
eventLogger.startCapturing();
setTimeout(function () {
var placeholdersDiv = document.getElementById('message-loading');
eventLogger.stopCapturing();
if (placeholdersDiv && !placeholdersDiv.classList.contains('hidden')) {
eventLogger.send();
}
eventLogger.reset();
}, 10000);
var showHideElement = function showHideElement(elementId, show) {
var element = document.getElementById(elementId);
if (element) {
element.classList.toggle('hidden', !show);
}
};
var viewportHeight = documentBody.clientHeight;
window.addEventListener('resize', function (event) {
var heightChange = documentBody.clientHeight - viewportHeight;
viewportHeight = documentBody.clientHeight;
var maxScrollTop = documentBody.scrollHeight - documentBody.clientHeight;
if (documentBody.scrollTop >= maxScrollTop - 1) {
return;
}
window.scrollBy({
left: 0,
top: -heightChange
});
});
function midMessagePeer(top, bottom) {
var midY = (bottom + top) / 2;
if (document.elementsFromPoint === undefined) {
var element = document.elementFromPoint(0, midY);
return element && element.closest('body > *');
}
var midElements = document.elementsFromPoint(0, midY);
if (midElements.length < 3) {
return null;
}
return midElements[midElements.length - 3];
}
function walkToMessage(start, step) {
var element = start;
while (element && !element.classList.contains('message')) {
element = element[step];
}
return element;
}
function firstMessage() {
return walkToMessage(documentBody.firstElementChild, 'nextElementSibling');
}
function lastMessage() {
return walkToMessage(documentBody.lastElementChild, 'previousElementSibling');
}
function previousMessage(start) {
return walkToMessage(start.previousElementSibling, 'previousElementSibling');
}
function nextMessage(start) {
return walkToMessage(start.nextElementSibling, 'nextElementSibling');
}
function isVisible(element, top, bottom) {
var rect = element.getBoundingClientRect();
return top < rect.bottom && rect.top < bottom;
}
var messageReadSlop = 16;
function isRead(element, top, bottom) {
return bottom + messageReadSlop >= element.getBoundingClientRect().bottom;
}
function someVisibleMessage(top, bottom) {
function checkVisible(candidate) {
return candidate && isVisible(candidate, top, bottom) ? candidate : null;
}
var midPeer = midMessagePeer(top, bottom);
return checkVisible(walkToMessage(midPeer, 'previousElementSibling')) || checkVisible(walkToMessage(midPeer, 'nextElementSibling')) || checkVisible(firstMessage()) || checkVisible(lastMessage());
}
function someVisibleReadMessage(top, bottom) {
function checkReadAndVisible(candidate) {
return candidate && isRead(candidate, top, bottom) && isVisible(candidate, top, bottom) ? candidate : null;
}
var visible = someVisibleMessage(top, bottom);
if (!visible) {
return visible;
}
return checkReadAndVisible(visible) || checkReadAndVisible(previousMessage(visible));
}
function idFromMessage(element) {
var idStr = element.getAttribute('data-msg-id');
if (idStr === null || idStr === undefined) {
throw new Error('Bad message element');
}
return +idStr;
}
function visibleReadMessageIds() {
var top = 0;
var bottom = viewportHeight;
var first = Number.MAX_SAFE_INTEGER;
var last = 0;
function walkElements(start, step) {
var element = start;
while (element && isVisible(element, top, bottom) && isRead(element, top, bottom)) {
if (element.classList.contains('message')) {
var id = idFromMessage(element);
first = Math.min(first, id);
last = Math.max(last, id);
}
element = element[step];
}
}
var start = someVisibleReadMessage(top, bottom);
walkElements(start, 'nextElementSibling');
walkElements(start, 'previousElementSibling');
return {
first: first,
last: last
};
}
var getMessageNode = function getMessageNode(node) {
var curNode = node;
while (curNode && curNode.parentNode && curNode.parentNode !== documentBody) {
curNode = curNode.parentNode;
}
return curNode;
};
var getMessageIdFromNode = function getMessageIdFromNode(node) {
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
var msgNode = getMessageNode(node);
return msgNode && msgNode instanceof Element ? +msgNode.getAttribute('data-msg-id') : defaultValue;
};
var setMessagesReadAttributes = function setMessagesReadAttributes(rangeHull) {
var element = document.querySelector("[data-msg-id='".concat(rangeHull.first, "']"));
while (element) {
if (element.classList.contains('message')) {
element.setAttribute('data-read', 'true');
if (idFromMessage(element) >= rangeHull.last) {
break;
}
}
element = element.nextElementSibling;
}
};
var prevMessageRange = visibleReadMessageIds();
var sendScrollMessage = function sendScrollMessage() {
var messageRange = visibleReadMessageIds();
var rangeHull = {
first: Math.min(prevMessageRange.first, messageRange.first),
last: Math.max(prevMessageRange.last, messageRange.last)
};
sendMessage({
type: 'scroll',
offsetHeight: documentBody.offsetHeight,
innerHeight: window.innerHeight,
scrollY: window.scrollY,
startMessageId: rangeHull.first,
endMessageId: rangeHull.last
});
setMessagesReadAttributes(rangeHull);
if (messageRange.first < messageRange.last) {
prevMessageRange = messageRange;
}
};
var sendScrollMessageIfListShort = function sendScrollMessageIfListShort() {
if (documentBody.scrollHeight === documentBody.clientHeight) {
sendScrollMessage();
}
};
var scrollEventsDisabled = true;
var hasLongPressed = false;
var longPressTimeout = undefined;
var lastTouchPositionX = -1;
var lastTouchPositionY = -1;
var handleScrollEvent = function handleScrollEvent() {
clearTimeout(longPressTimeout);
if (scrollEventsDisabled) {
return;
}
sendScrollMessage();
var nearEnd = documentBody.offsetHeight - window.scrollY - window.innerHeight > 100;
showHideElement('scroll-bottom', nearEnd);
};
window.addEventListener('scroll', handleScrollEvent);
var scrollToBottom = function scrollToBottom() {
window.scroll({
left: 0,
top: documentBody.scrollHeight,
behavior: 'smooth'
});
};
var isNearBottom = function isNearBottom() {
return documentBody.scrollHeight - 100 < documentBody.scrollTop + documentBody.clientHeight;
};
var scrollToBottomIfNearEnd = function scrollToBottomIfNearEnd() {
if (isNearBottom()) {
scrollToBottom();
}
};
var scrollToMessage = function scrollToMessage(messageId) {
var targetNode = messageId !== null ? document.getElementById("msg-".concat(messageId)) : null;
if (targetNode) {
targetNode.scrollIntoView({
block: 'start'
});
} else {
window.scroll({
left: 0,
top: documentBody.scrollHeight + 200
});
}
};
var findPreserveTarget = function findPreserveTarget() {
var message = someVisibleMessage(0, viewportHeight);
if (!message) {
return {
type: 'none'
};
}
var messageId = idFromMessage(message);
var prevBoundRect = message.getBoundingClientRect();
return {
type: 'preserve',
msgId: messageId,
prevBoundTop: prevBoundRect.top
};
};
var scrollToPreserve = function scrollToPreserve(msgId, prevBoundTop) {
var newElement = document.getElementById("msg-".concat(msgId));
if (!newElement) {
return;
}
var newBoundRect = newElement.getBoundingClientRect();
window.scrollBy(0, newBoundRect.top - prevBoundTop);
};
var runAfterLayout = function runAfterLayout(fn) {
if (platformOS === 'android') {
fn();
return;
}
requestAnimationFrame(function () {
fn();
});
};
var handleInboundEventContent = function handleInboundEventContent(uevent) {
var target;
if (uevent.updateStrategy === 'replace') {
target = {
type: 'none'
};
} else if (uevent.updateStrategy === 'scroll-to-anchor') {
target = {
type: 'anchor',
messageId: uevent.scrollMessageId
};
} else if (uevent.updateStrategy === 'scroll-to-bottom-if-near-bottom' && isNearBottom()) {
target = {
type: 'bottom'
};
} else {
target = findPreserveTarget();
}
documentBody.innerHTML = uevent.content;
rewriteHtml(uevent.auth);
runAfterLayout(function () {
if (target.type === 'bottom') {
scrollToBottom();
} else if (target.type === 'anchor') {
scrollToMessage(target.messageId);
} else if (target.type === 'preserve') {
scrollToPreserve(target.msgId, target.prevBoundTop);
}
sendScrollMessageIfListShort();
});
};
var handleInitialLoad = function handleInitialLoad(scrollMessageId, rawAuth) {
var auth = _objectSpread2(_objectSpread2({}, rawAuth), {}, {
realm: new URL(rawAuth.realm)
});
if (platformOS === 'ios') {
window.addEventListener('message', handleMessageEvent);
} else {
document.addEventListener('message', handleMessageEvent);
}
scrollToMessage(scrollMessageId);
rewriteHtml(auth);
sendScrollMessageIfListShort();
scrollEventsDisabled = false;
};
var handleInboundEventFetching = function handleInboundEventFetching(uevent) {
showHideElement('message-loading', uevent.showMessagePlaceholders);
showHideElement('spinner-older', uevent.fetchingOlder);
showHideElement('spinner-newer', uevent.fetchingNewer);
};
var handleInboundEventTyping = function handleInboundEventTyping(uevent) {
var elementTyping = document.getElementById('typing');
if (elementTyping) {
elementTyping.innerHTML = uevent.content;
runAfterLayout(function () {
return scrollToBottomIfNearEnd();
});
}
};
var handleInboundEventReady = function handleInboundEventReady(uevent) {
sendMessage({
type: 'ready'
});
};
var handleInboundEventMessagesRead = function handleInboundEventMessagesRead(uevent) {
if (uevent.messageIds.length === 0) {
return;
}
var selector = uevent.messageIds.map(function (id) {
return "[data-msg-id=\\"".concat(id, "\\"]");
}).join(',');
var messageElements = Array.from(document.querySelectorAll(selector));
messageElements.forEach(function (element) {
element.setAttribute('data-read', 'true');
});
};
var inboundEventHandlers = {
content: handleInboundEventContent,
fetching: handleInboundEventFetching,
typing: handleInboundEventTyping,
ready: handleInboundEventReady,
read: handleInboundEventMessagesRead
};
var handleMessageEvent = function handleMessageEvent(e) {
scrollEventsDisabled = true;
var decodedData = decodeURIComponent(escape(window.atob(e.data)));
var rawInboundEvents = JSON.parse(decodedData);
var inboundEvents = rawInboundEvents.map(function (inboundEvent) {
return _objectSpread2(_objectSpread2({}, inboundEvent), inboundEvent.auth ? {
auth: _objectSpread2(_objectSpread2({}, inboundEvent.auth), {}, {
realm: new URL(inboundEvent.auth.realm)
})
} : {});
});
inboundEvents.forEach(function (uevent) {
eventLogger.maybeCaptureInboundEvent(uevent);
inboundEventHandlers[uevent.type](uevent);
});
scrollEventsDisabled = false;
};
var revealMutedMessages = function revealMutedMessages(message) {
var messageNode = message;
do {
messageNode.setAttribute('data-mute-state', 'shown');
messageNode = nextMessage(messageNode);
} while (messageNode && messageNode.classList.contains('message-brief'));
};
var requireAttribute = function requireAttribute(e, name) {
var value = e.getAttribute(name);
if (value === null || value === undefined) {
throw new Error("Missing expected attribute ".concat(name));
}
return value;
};
var requireNumericAttribute = function requireNumericAttribute(e, name) {
var value = requireAttribute(e, name);
var parsedValue = parseInt(value, 10);
if (Number.isNaN(parsedValue)) {
throw new Error("Could not parse attribute ".concat(name, " value '").concat(value, "' as integer"));
}
return parsedValue;
};
documentBody.addEventListener('click', function (e) {
e.preventDefault();
clearTimeout(longPressTimeout);
if (hasLongPressed) {
hasLongPressed = false;
return;
}
var target = e.target;
if (!(target instanceof Element)) {
return;
}
if (target.matches('.scroll-bottom')) {
scrollToBottom();
return;
}
if (target.matches('.avatar-img')) {
sendMessage({
type: 'avatar',
fromUserId: makeUserId(requireNumericAttribute(target, 'data-sender-id'))
});
return;
}
if (target.matches('.header')) {
sendMessage({
type: 'narrow',
narrow: requireAttribute(target, 'data-narrow')
});
return;
}
if (target.matches('.user-mention')) {
sendMessage({
type: 'mention',
userId: makeUserId(requireNumericAttribute(target, 'data-user-id'))
});
return;
}
var inlineImageLink = target.closest('.message_inline_image a');
if (inlineImageLink && !inlineImageLink.closest('.youtube-video, .vimeo-video')) {
sendMessage({
type: 'image',
src: requireAttribute(inlineImageLink, 'href'),
messageId: getMessageIdFromNode(inlineImageLink)
});
return;
}
if (target.matches('a')) {
sendMessage({
type: 'url',
href: requireAttribute(target, 'href'),
messageId: getMessageIdFromNode(target)
});
return;
}
if (target.parentNode instanceof Element && target.parentNode.matches('a')) {
sendMessage({
type: 'url',
href: requireAttribute(target.parentNode, 'href'),
messageId: getMessageIdFromNode(target.parentNode)
});
return;
}
if (target.matches('.reaction')) {
sendMessage({
type: 'reaction',
name: requireAttribute(target, 'data-name'),
code: requireAttribute(target, 'data-code'),
reactionType: requireAttribute(target, 'data-type'),
messageId: getMessageIdFromNode(target),
voted: target.classList.contains('self-voted')
});
return;
}
if (target.matches('time')) {
var originalText = requireAttribute(target, 'original-text');
sendMessage({
type: 'time',
originalText: originalText
});
}
var spoilerHeader = target.closest('.spoiler-header');
if (spoilerHeader instanceof HTMLElement) {
toggleSpoiler(spoilerHeader);
return;
}
var messageElement = target.closest('.message-brief');