import { CSSResult, html, TemplateResult, LitElement, nothing, PropertyValues } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import type { PDFDocumentProxy, RenderTask } from 'pdfjs-dist';
import { getTranslations, ThumbnailsTranslations } from '../i18n/i18n';
// @ts-ignore
import style from './lit-pdf-thumbnails.scss';
/** Target CSS width (px) a thumbnail canvas is rendered at, before device-pixel scaling. */
const THUMBNAIL_WIDTH = 110;
/**
* Sidebar panel for `lit-pdf-viewer` listing a thumbnail per page. Shown/
* hidden via the `open` property. Thumbnails render once, the first time the
* panel is opened for a given document, rather than upfront while hidden.
*/
@customElement('lit-pdf-thumbnails')
export class LitPdfThumbnails extends LitElement {
@property({ attribute: false }) public pdfDocument: PDFDocumentProxy | null = null;
@property({ type: Boolean, reflect: true }) public open = false;
@property({ type: Number }) public currentPage = 1;
/** Locale to translate the panel into (defaults to the browser's language). */
@property({ type: String }) public locale: string;
/** Overrides for individual translation strings. */
@property({ type: Object }) public translations: Partial = {};
private _renderedPages = new Set();
private _renderTasks = new Map();
public static get styles(): CSSResult[] {
return [style];
}
private get _t(): ThumbnailsTranslations {
return getTranslations(this.locale, { thumbnails: this.translations }).thumbnails;
}
private get _pageCount(): number {
return this.pdfDocument?.numPages ?? 0;
}
public connectedCallback(): void {
super.connectedCallback();
this.setAttribute('role', 'navigation');
this.setAttribute('aria-label', this._t.thumbnailsLabel);
}
public disconnectedCallback(): void {
super.disconnectedCallback();
this._cancelPendingRenders();
}
public render(): TemplateResult {
const t = this._t;
if (!this._pageCount) {
return html``;
}
return html`