ng2-pdfjs-viewer
Version:
The most comprehensive Angular PDF viewer, powered by Mozilla PDF.js 6 — view, annotate, sign, fill forms, search, and read aloud from one component. 8.3M+ downloads, mobile-first, production-ready.
75 lines (71 loc) • 3.35 kB
JavaScript
import { computed } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
// Secondary entry point: ng2-pdfjs-viewer/signals
//
// Read-only Angular signals projected from the viewer's @Output() events, for
// zoneless and OnPush apps that prefer reading state from signals over wiring up
// a handful of (event)="..." bindings and EventEmitter subscriptions.
//
// It reads the same outputs the component already emits - it adds no new viewer
// behaviour and sends nothing anywhere. Each signal holds the latest value from
// its source output and starts as `undefined` until that output first fires
// (`loaded`/`totalPages` are derived from page initialization).
//
// Requires Angular 16+ (signals + `@angular/core/rxjs-interop`). The base package
// keeps its `>=10` peer range; only this entry point needs 16+, so import it from
// the subpath and apps on older Angular never load it:
//
// import { pdfViewerSignals } from "ng2-pdfjs-viewer/signals";
//
// A @ViewChild viewer is only populated in ngAfterViewInit, which is NOT an
// injection context, so pass an Injector captured earlier:
//
// @ViewChild('viewer') viewer!: PdfJsViewerComponent;
// private injector = inject(Injector);
// signals!: PdfViewerSignals;
//
// ngAfterViewInit() {
// this.signals = pdfViewerSignals(this.viewer, { injector: this.injector });
// }
// // template: {{ signals.page() }} / {{ signals.totalPages() }} / {{ signals.loaded() }}
//
// Called from a constructor or field initializer (where an injection context
// exists), the `injector` option can be omitted. Subscriptions are torn down with
// the injector's DestroyRef - i.e. when the host component is destroyed.
/**
* Project the viewer's outputs into read-only signals. See the file header for
* usage and the injection-context rules. The returned signals are live: each
* updates when its source output next fires.
*/
function pdfViewerSignals(viewer, options = {}) {
const { injector } = options;
const latest = (source) => toSignal(source, { initialValue: undefined, injector });
const pages = latest(viewer.onPagesInit);
return {
page: latest(viewer.onPageChange),
scale: latest(viewer.onScaleChange),
totalPages: computed(() => pages()?.pagesCount),
loaded: computed(() => pages() !== undefined),
error: latest(viewer.onDocumentError),
rotation: latest(viewer.onRotationChange),
findMatches: latest(viewer.onUpdateFindMatchesCount),
readAloud: latest(viewer.onReadAloudStateChange),
annotationEditor: latest(viewer.onAnnotationEditorStateChange),
annotationEditorMode: latest(viewer.annotationEditorChange),
sidebar: latest(viewer.onSidebarViewChanged),
metadata: latest(viewer.onMetadataLoaded),
outline: latest(viewer.onOutlineLoaded),
formData: latest(viewer.formDataChange),
presentationMode: latest(viewer.onPresentationModeChanged),
zoom: latest(viewer.zoomChange),
cursor: latest(viewer.cursorChange),
scroll: latest(viewer.scrollChange),
spread: latest(viewer.spreadChange),
pageMode: latest(viewer.pageModeChange),
};
}
/**
* Generated bundle index. Do not edit.
*/
export { pdfViewerSignals };
//# sourceMappingURL=ng2-pdfjs-viewer-signals.mjs.map