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
ash / webui / scanning / resources / scan_preview.ts [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import './action_toolbar.js';
import './scanning_fonts.css.js';
import './scanning_shared.css.js';
import '/strings.m.js';
import 'chrome://resources/ash/common/cr_elements/cr_dialog/cr_dialog.js';
import 'chrome://resources/polymer/v3_0/paper-progress/paper-progress.js';
import {assert} from 'chrome://resources/ash/common/assert.js';
import {CrButtonElement} from 'chrome://resources/ash/common/cr_elements/cr_button/cr_button.js';
import {CrDialogElement} from 'chrome://resources/ash/common/cr_elements/cr_dialog/cr_dialog.js';
import {I18nMixin} from 'chrome://resources/ash/common/cr_elements/i18n_mixin.js';
import {DomRepeatEvent, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {ForceHiddenElementsVisibleObserverInterface, ForceHiddenElementsVisibleObserverReceiver} from './accessibility_features.mojom-webui.js';
import {getAccessibilityFeaturesInterface} from './mojo_interface_provider.js';
import {getTemplate} from './scan_preview.html.js';
import {AppState} from './scanning_app_types.js';
import {ScanningBrowserProxyImpl} from './scanning_browser_proxy.js';
const PROGRESS_TIMER_MS = 3000;
/**
* The bottom margin of each scanned image in pixels.
*/
const SCANNED_IMG_MARGIN_BOTTOM_PX = 12;
/**
* The bottom margin for the action toolbar from the bottom edge of the
* viewport.
*/
const ACTION_TOOLBAR_BOTTOM_MARGIN_PX = 40;
/**
* @fileoverview
* 'scan-preview' shows a preview of a scanned document.
*/
const ScanPreviewElementBase = I18nMixin(PolymerElement);
type DialogAction = 'rescan-page'|'remove-page';
export class ScanPreviewElement extends ScanPreviewElementBase implements
ForceHiddenElementsVisibleObserverInterface {
static get is() {
return 'scan-preview' as const;
}
static get template() {
return getTemplate();
}
static get properties() {
return {
appState: {
type: Number,
observer: ScanPreviewElement.prototype.appStateChanged,
},
/**
* The object URLs of the scanned images.
*/
objectUrls: {
type: Array,
observer: ScanPreviewElement.prototype.objectUrlsChanged,
},
pageNumber: {
type: Number,
observer: ScanPreviewElement.prototype.pageNumberChanged,
},
progressPercent: Number,
showHelpOrProgress: {
type: Boolean,
value: true,
},
showScannedImages: {
type: Boolean,
value: false,
},
showHelperText: {
type: Boolean,
value: true,
},
showScanProgress: {
type: Boolean,
value: false,
},
showCancelingProgress: {
type: Boolean,
value: false,
},
progressTextString: String,
previewAriaLabel: String,
progressTimer: {
type: Number,
value: null,
},
isMultiPageScan: {
type: Boolean,
observer: ScanPreviewElement.prototype.isMultiPageScanChanged,
},
/**
* The index of the page currently focused on.
*/
currentPageIndexInView: {
type: Number,
value: 0,
},
/**
* Set to true once the first scanned image from a scan is loaded. This is
* needed to prevent checking the dimensions of every scanned image. The
* assumption is that all scanned images share the same dimensions.
*/
scannedImagesLoaded: {
type: Boolean,
value: false,
},
showActionToolbar: Boolean,
dialogTitleText: String,
dialogConfirmationText: String,
dialogButtonText: String,
/**
* True when |appState| is MULTI_PAGE_SCANNING.
*/
multiPageScanning: {
type: Boolean,
value: false,
reflectToAttribute: true,
},
showSingleImageFocus: {
type: Boolean,
reflectToAttribute: true,
},
/**
* True when the ChromeVox, Switch, or Screen Magnifier accessibility
* features are turned on that require the action toolbar to always be
* visible during multi-page scan sessions. Only used for CSS selector
* logic.
*/
forceActionToolbarVisible: {
type: Boolean,
value: false,
reflectToAttribute: true,
},
};
}
static get observers() {
return [
'setPreviewAriaLabel(showScannedImages, showCancelingProgress,' +
' showHelperText, objectUrls.length)',
'setScanProgressTimer(showScanProgress, progressPercent)',
];
}
appState: AppState;
objectUrls: string[];
private pageNumber: number;
private progressPercent: number;
private showHelpOrProgress: boolean;
private showScannedImages: boolean;
private showHelperText: boolean;
private showScanProgress: boolean;
private showCancelingProgress: boolean;
private progressTextString: string;
private previewAriaLabel: string;
private progressTimer: number|null;
private isMultiPageScan: boolean;
private currentPageIndexInView: number;
private scannedImagesLoaded: boolean;
private showActionToolbar: boolean;
private dialogTitleText: string;
private dialogConfirmationText: string;
private dialogButtonText: string;
private multiPageScanning: boolean;
private showSingleImageFocus: boolean;
private forceActionToolbarVisible: boolean;
private actionToolbarHeight: number;
private actionToolbarWidth: number;
private forceHiddenElementsVisibleObserverReceiver:
ForceHiddenElementsVisibleObserverReceiver;
private onDialogActionClick: EventListenerOrEventListenerObject;
private onWindowResized: EventListenerOrEventListenerObject;
private previewAreaResizeObserver: ResizeObserver;
// ScanningBrowserProxy is initialized when scanning_app.js is created.
private browserProxy = ScanningBrowserProxyImpl.getInstance();
constructor() {
super();
this.onWindowResized = () => this.setActionToolbarPosition();
this.previewAreaResizeObserver =
new ResizeObserver(() => this.updatePreviewElements());
}
override ready(): void {
super.ready();
this.style.setProperty(
'--scanned-image-margin-bottom', SCANNED_IMG_MARGIN_BOTTOM_PX + 'px');
// parseFloat() is used to convert the string returned by
// styleMap.get() into a number ("642px" --> 642).
const styleMap = (this as unknown as Element).computedStyleMap();
this.actionToolbarHeight =
parseFloat(styleMap.get('--action-toolbar-height')!.toString());
this.actionToolbarWidth =
parseFloat(styleMap.get('--action-toolbar-width')!.toString());
this.forceHiddenElementsVisibleObserverReceiver =
new ForceHiddenElementsVisibleObserverReceiver(this);
getAccessibilityFeaturesInterface()
.observeForceHiddenElementsVisible(
this.forceHiddenElementsVisibleObserverReceiver.$
.bindNewPipeAndPassRemote())
.then(
response => this.forceActionToolbarVisible = response.forceVisible);
}
override disconnectedCallback(): void {
super.disconnectedCallback();
if (this.isMultiPageScan) {
window.removeEventListener('resize', this.onWindowResized);
this.previewAreaResizeObserver.disconnect();
}
if (this.forceHiddenElementsVisibleObserverReceiver) {
this.forceHiddenElementsVisibleObserverReceiver.$.close();
}
}
/**
* Overrides ForceHiddenElementsVisibleObserverReceiver.
*/
onForceHiddenElementsVisibleChange(forceVisible: boolean): void {
this.forceActionToolbarVisible = forceVisible;
}
private appStateChanged(): void {
this.showScannedImages = this.appState === AppState.DONE ||
this.appState === AppState.MULTI_PAGE_NEXT_ACTION ||
this.appState === AppState.MULTI_PAGE_SCANNING;
this.showScanProgress = this.appState === AppState.SCANNING ||
this.appState === AppState.MULTI_PAGE_SCANNING;
this.showCancelingProgress = this.appState === AppState.CANCELING ||
this.appState === AppState.MULTI_PAGE_CANCELING;
this.showHelperText = !this.showScanProgress &&
!this.showCancelingProgress && !this.showScannedImages;
this.showHelpOrProgress = !this.showScannedImages ||
this.appState === AppState.MULTI_PAGE_SCANNING;
this.multiPageScanning = this.appState === AppState.MULTI_PAGE_SCANNING;
this.showSingleImageFocus =
this.appState === AppState.MULTI_PAGE_NEXT_ACTION;
this.showActionToolbar = this.appState === AppState.MULTI_PAGE_NEXT_ACTION;
// If no longer showing the scanned images, reset |scannedImagesLoaded_| so
// it can be used again for the next scan job.
if (this.showHelpOrProgress) {
this.scannedImagesLoaded = false;
}
}
private pageNumberChanged(): void {
this.progressTextString =
this.i18n('scanPreviewProgressText', this.pageNumber);
}
/**
* Sets the ARIA label used by the preview area based on the app state and the
* current page showing. In the initial state, use the scan preview
* instructions from the page as the label. When the scan completes, announce
* the total number of pages scanned.
*
*/
private setPreviewAriaLabel(): void {
if (this.showScannedImages) {
this.browserProxy
.getPluralString('scannedImagesAriaLabel', this.objectUrls.length)
.then((pluralString) => this.previewAriaLabel = pluralString);
return;
}
if (this.showCancelingProgress) {
this.previewAriaLabel = this.i18n('cancelingScanningText');
return;
}
if (this.showHelperText) {
this.previewAriaLabel = this.i18n('scanPreviewHelperText');
return;
}
}
/**
* When receiving progress updates from an ongoing scan job, only update the
* preview section aria label after a timer elapses to prevent successive
* progress updates from spamming ChromeVox.
*/
private setScanProgressTimer(): void {
// Only set the timer if scanning is still in progress.
if (!this.showScanProgress) {
return;
}
// Always announce when a page is completed. Bypass and clear any existing
// timer and immediately update the aria label.
if (this.progressPercent === 100) {
if (this.progressTimer) {
clearTimeout(this.progressTimer);
}
this.onScanProgressTimerComplete();
return;
}
// If a timer is already in progress, do not set another timer.
if (this.progressTimer) {
return;
}
this.progressTimer =
setTimeout(() => this.onScanProgressTimerComplete(), PROGRESS_TIMER_MS);
}
private onScanProgressTimerComplete(): void {
// Only update the aria label if scanning is still in progress.
if (!this.showScanProgress) {
return;
}
this.previewAriaLabel = this.i18n(
'scanningImagesAriaLabel', this.pageNumber, this.progressPercent);
this.progressTimer = null;
}
/**
* While scrolling, if the current page in view would change, update it and
* set the focus CSS variable accordingly.
*/
private onScannedImagesScroll(): void {
if (!this.isMultiPageScan ||
this.appState != AppState.MULTI_PAGE_NEXT_ACTION) {
return;
}
const scannedImagesDiv: HTMLDivElement =
this.shadowRoot!.querySelector<HTMLDivElement>('#scannedImages')!;
const scannedImages: HTMLCollection =
scannedImagesDiv.getElementsByClassName('scanned-image');
if (scannedImages.length === 0) {
return;
}
// If the current page in view stays the same, do nothing.
const pageIndexInView = this.getCurrentPageInView(scannedImages);
if (pageIndexInView === this.currentPageIndexInView) {
return;
}
this.setFocusedScannedImage(scannedImages, pageIndexInView);
}
/**
* Calculates the index of the current page in view based on the scroll
* position. This algorithm allows for every scanned image to be focusable
* via scrolling. It starts by waiting until the previous image is scrolled
* halfway outside the viewport before the page index changes, but then
* changes behavior once the end of the scroll area is reached and no more
* images can be scrolled up. In that case, the remaining scroll area is
* divided evenly between the final images in the viewport.
*/
private getCurrentPageInView(scannedImages: HTMLCollection): number {
assert(this.isMultiPageScan);
if (scannedImages.length === 1) {
return 0;
}
// Assumes the scanned images share the same dimensions.
const imageHeight = scannedImages[0].getBoundingClientRect().height +
SCANNED_IMG_MARGIN_BOTTOM_PX;
// The first step is to calculate the number of images that will be visible
// in the viewport when scrolled to the bottom. That is how to calculate the
// "crossover" point where the algorithm needs to change.
const numImagesVisibleAtEnd = Math.ceil(
this.shadowRoot!.querySelector<HTMLElement>(
'#previewDiv')!.offsetHeight /
imageHeight);
const numImagesBeforeCrossover =
scannedImages.length - numImagesVisibleAtEnd;
// Calculate the point where the last images in the scroll area are visible
// and the scrolling algorithm needs to change.
const crossoverBreakpoint = numImagesBeforeCrossover == 0 ?
Number.MIN_VALUE :
(scannedImages[numImagesBeforeCrossover] as HTMLElement).offsetTop -
(imageHeight / 2);
// Before the "crossover", update the page index based on when the previous
// image is scrolled halfway outside the viewport.
if (this.shadowRoot!.querySelector<HTMLElement>('#previewDiv')!.scrollTop <
crossoverBreakpoint) {
// Subtract half the image height so |scrollTop| = 0 when the first page
// is scrolled halfway outside the viewport. That way each page index will
// be the current scroll divided by the image height.
const scrollTop =
this.shadowRoot!.querySelector<HTMLElement>(
'#previewDiv')!.scrollTop -
(imageHeight / 2) -
/*imageFocusBorder=*/ 2;
if (scrollTop < 0) {
return 0;
}
return 1 + Math.floor(scrollTop / imageHeight);
}
// After the "crossover", the remaining amount of scroll left in the
// scrollbar is divided evenly to the remaining images. This allows every
// image to be scrolled to.
const maxScrollTop =
this.shadowRoot!.querySelector<HTMLElement>(
'#previewDiv')!.scrollHeight -
this.shadowRoot!.querySelector<HTMLElement>(
'#previewDiv')!.offsetHeight;
const scrollRemainingAfterCrossover =
Math.max(maxScrollTop - crossoverBreakpoint, 0);
const imageScrollProportion =
scrollRemainingAfterCrossover / numImagesVisibleAtEnd;
// Calculate the new page index.
const scrollTop =
this.shadowRoot!.querySelector<HTMLElement>('#previewDiv')!.scrollTop -
crossoverBreakpoint;
const index = Math.floor(scrollTop / imageScrollProportion);
return Math.min(numImagesBeforeCrossover + index, scannedImages.length - 1);
}
/**
* Sets the CSS class for the current scanned image in view so the blue border
* will show on the correct page when hovered.
*/
private setFocusedScannedImage(
scannedImages: HTMLCollection, pageIndexInView: number): void {
assert(this.isMultiPageScan);
this.removeFocusFromScannedImage(scannedImages);
assert(pageIndexInView >= 0 && pageIndexInView < scannedImages.length);
scannedImages[pageIndexInView].classList.add('focused-scanned-image');
this.currentPageIndexInView = pageIndexInView;
}
/**
* Removes the focus CSS class from the scanned image which already has it
* then resets |currentPageInView_|.
*/
private removeFocusFromScannedImage(scannedImages: HTMLCollection): void {
// This condition is only true when the user chooses to remove a page from
// the multi-page scan session. When a page gets removed, the focus is
// cleared and not immediately set again.
if (this.currentPageIndexInView < 0) {
return;
}
assert(
this.currentPageIndexInView >= 0 &&
this.currentPageIndexInView < scannedImages.length);
scannedImages[this.currentPageIndexInView].classList.remove(
'focused-scanned-image');
// Set to -1 because the focus has been removed from the current page and no
// other page has it.
this.currentPageIndexInView = -1;
}
/**
* Runs when a new scanned image is loaded.
*/
private onScannedImageLoaded(e: DomRepeatEvent<string>): void {
if (!this.isMultiPageScan) {
return;
}
const scannedImages =
this.shadowRoot!.querySelector<HTMLElement>('#scannedImages')!
.getElementsByClassName('scanned-image');
this.setFocusedScannedImage(
scannedImages, this.getCurrentPageInView(scannedImages));
this.updatePreviewElements();
// Scrolling to a page is only needed for the first scanned image load.
if (this.scannedImagesLoaded) {
return;
}
this.scannedImagesLoaded = true;
// |e.model| is populated by the dom-repeat element.
this.scrollToPage(e.model.index);
}
/**
* Set the focus to the clicked scanned image.
*/
private onScannedImageClick(e: DomRepeatEvent<string>): void {
if (!this.isMultiPageScan) {
return;
}
// |e.model| is populated by the dom-repeat element.
const scannedImages =
this.shadowRoot!.querySelector<HTMLDivElement>('#scannedImages')!
.getElementsByClassName('scanned-image');
this.setFocusedScannedImage(scannedImages, e.model.index);
}
/**
* Set the position of the action toolbar based on the size of the scanned
* images and the current size of the app window.
*/
private setActionToolbarPosition(): void {
assert(this.isMultiPageScan);
const scannedImage =
this.shadowRoot!.querySelector<HTMLImageElement>('.scanned-image')!;
if (!scannedImage) {
return;
}
const scannedImageRect = scannedImage.getBoundingClientRect();
// Set the toolbar position from the bottom edge of the viewport.
const topPosition =
this.shadowRoot!.querySelector<HTMLDivElement>(
'#previewDiv')!.offsetHeight -
ACTION_TOOLBAR_BOTTOM_MARGIN_PX - (this.actionToolbarHeight / 2);
this.style.setProperty('--action-toolbar-top', topPosition + 'px');
// Position the toolbar in the middle of the viewport.
const leftPosition = scannedImageRect.x + (scannedImageRect.width / 2) -
(this.actionToolbarWidth / 2);
this.style.setProperty('--action-toolbar-left', leftPosition + 'px');
}
/**
* Called when the "show-remove-page-dialog" event fires from the action
* toolbar button click.
*/
private onShowRemovePageDialog(e: CustomEvent<number>): void {
this.showRemoveOrRescanDialog(/* isRemovePageDialog */ true, e.detail);
}
/**
* Called when the "show-rescan-page-dialog" event fires from the action
* toolbar button click.
*/
private onShowRescanPageDialog(e: CustomEvent<number>): void {
this.showRemoveOrRescanDialog(/* isRemovePageDialog */ false, e.detail);
}
/**
* |isRemovePageDialog| determines whether to show the 'Remove Page' or
* 'Rescan Page' dialog.
*/
private showRemoveOrRescanDialog(
isRemovePageDialog: boolean, pageIndex: number): void {
// Configure the on-click action.
this.onDialogActionClick = () => {
this.fireDialogAction(
isRemovePageDialog ? 'remove-page' : 'rescan-page', pageIndex);
};
this.shadowRoot!.querySelector<CrButtonElement>('#actionButton')!
.addEventListener('click', this.onDialogActionClick, {once: true});
// Configure the dialog strings for the requested mode (Remove or Rescan).
this.dialogButtonText = this.i18n(
isRemovePageDialog ? 'removePageButtonLabel' : 'rescanPageButtonLabel');
this.dialogConfirmationText = this.i18n(
isRemovePageDialog ? 'removePageConfirmationText' :
'rescanPageConfirmationText');
this.browserProxy
.getPluralString(
isRemovePageDialog ? 'removePageDialogTitle' :
'rescanPageDialogTitle',
this.objectUrls.length === 1 ? 0 : pageIndex + 1)
.then((pluralString: string): void => {
// When removing a page while more than one page exists, leave the
// title empty and move the title text into the body.
const isRemoveFromMultiplePages =
isRemovePageDialog && this.objectUrls.length > 1;
this.dialogTitleText = isRemoveFromMultiplePages ? '' : pluralString;
if (isRemoveFromMultiplePages) {
this.dialogConfirmationText = pluralString;
}
// Once strings are loaded, open the dialog.
this.shadowRoot!.querySelector<CrDialogElement>(
'#scanPreviewDialog')!.showModal();
});
}
/**
* Filrs either the 'remove-page' or 'rescan-page' event.
*/
private fireDialogAction(event: DialogAction, pageIndex: number): void {
const scannedImages =
this.shadowRoot!.querySelector<HTMLDivElement>('#scannedImages')!
.getElementsByClassName('scanned-image');
this.removeFocusFromScannedImage(scannedImages);
assert(pageIndex >= 0);
this.dispatchEvent(new CustomEvent(
event, {bubbles: true, composed: true, detail: pageIndex}));
this.closeDialog();
}
private closeDialog(): void {
this.shadowRoot!.querySelector<CrDialogElement>(
'#scanPreviewDialog')!.close();
this.shadowRoot!.querySelector<CrButtonElement>('#actionButton')!
.removeEventListener('click', this.onDialogActionClick);
}
/**
* Scrolls the image specified by |pageIndex| into view.
*/
private scrollToPage(pageIndex: number): void {
assert(this.isMultiPageScan);
const scannedImages =
this.shadowRoot!.querySelector<HTMLDivElement>('#scannedImages')!
.getElementsByClassName('scanned-image');
if (scannedImages.length === 0) {
return;
}
assert(pageIndex >= 0 && pageIndex < scannedImages.length);
this.shadowRoot!.querySelector<HTMLElement>('#previewDiv')!.scrollTop =
(scannedImages[pageIndex] as HTMLElement).offsetTop -
/*imageFocusBorder=*/ 2;
}
private isMultiPageScanChanged(): void {
// Listen for window size changes during multi-page scan sessions so the
// position of the action toolbar can be updated.
if (this.isMultiPageScan) {
window.addEventListener('resize', this.onWindowResized);
// Observe changes to the preview area during multi-page scan sessions so
// the scan progress div height can be updated when images are
// added/removed.
this.previewAreaResizeObserver.observe(
(this.shadowRoot!.querySelector<HTMLDivElement>('#previewDiv')!));
} else {
window.removeEventListener('resize', this.onWindowResized);
this.previewAreaResizeObserver.disconnect();
}
}
/**
* Make the scan progress height match the preview area height.
*
*/
private setMultiPageScanProgressHeight(): void {
this.style.setProperty(
'--multi-page-scan-progress-height',
this.shadowRoot!.querySelector<HTMLDivElement>(
'#previewDiv')!.offsetHeight +
'px');
}
private objectUrlsChanged(): void {
if (!this.isMultiPageScan) {
return;
}
// Set to -1 when no pages exist after a scan is saved.
if (this.objectUrls.length === 0) {
this.currentPageIndexInView = -1;
}
}
/**
* Sets the size and positioning of elements that depend on the size of the
* scan preview area.
*
*/
private updatePreviewElements(): void {
this.setMultiPageScanProgressHeight();
this.setActionToolbarPosition();
}
/**
* Hide the action toolbar if it's page is not currently in view.
*/
private showActionToolbarByIndex(index: number): boolean {
return index === this.currentPageIndexInView && this.showActionToolbar;
}
/**
* Set |currentPageIndexInView_| to the page focused on (via ChromeVox).
*/
private onScannedImageInFocus(e: DomRepeatEvent<string>): void {
if (!this.isMultiPageScan) {
return;
}
// |e.model| is populated by the dom-repeat element.
const scannedImages =
this.shadowRoot!.querySelector<HTMLDivElement>('#scannedImages')!
.getElementsByClassName('scanned-image');
this.setFocusedScannedImage(scannedImages, e.model.index);
}
private getScannedImageAriaLabel(index: number): string {
return this.i18n(
'multiPageImageAriaLabel', index + 1, this.objectUrls.length);
}
setIsMultiPageScanForTesting(isMultiPageScan: boolean): void {
this.isMultiPageScan = isMultiPageScan;
}
setPageNumberForTesting(pageNumber: number): void {
this.pageNumber = pageNumber;
}
}
declare global {
interface HTMLElementEventMap {
'rescan-page': CustomEvent<number>;
'remove-page': CustomEvent<number>;
}
interface HTMLElementTagNameMap {
[ScanPreviewElement.is]: ScanPreviewElement;
}
}
customElements.define(ScanPreviewElement.is, ScanPreviewElement);