1- import { setAssetsSrc } from "../../../../src/pdfjs/pdfjs.document.service" ;
1+ import { LoggerFactory } from "@srag/pdf-wrap/log-config" ;
2+ import { PageOverlay } from "@srag/pdf-wrap/api/storage/adapter" ;
3+
4+ import { setAssetsSrc } from "@srag/pdf-wrap/pdfjs/pdfjs.document.service" ;
5+ import { colorFromHex } from "@srag/pdf-wrap/api/draw/color" ;
6+ import { PDFjsDocumentService } from "@srag/pdf-wrap/pdfjs/pdfjs.document.service" ;
7+ import { URI } from "@srag/pdf-wrap/api/document.service" ;
8+ import { StorageRegistry } from "@srag/pdf-wrap/api/storage/adapter.registry" ;
9+ import { LogLevel } from "@srag/pdf-wrap/log-config" ;
10+
211
3- const { colorFromHex} = require ( "pdf-wrap/api/draw/color" ) ;
4- const { PDFjsDocumentService, setWorkerSrc, setCMapUrl} = require ( "pdf-wrap/pdfjs/pdfjs.document.service" ) ;
5- const { URI } = require ( "pdf-wrap/api/document.service" ) ;
6- const { StorageRegistry} = require ( "pdf-wrap/api/storage/adapter.registry" ) ;
7- const { PageOverlay} = require ( "pdf-wrap/api/storage/adapter" ) ;
8- const { LoggerFactory} = require ( "pdf-wrap/log-config" ) ;
912
1013LoggerFactory . configure ( {
1114 logGroups : [
1215 {
1316 logger : "ch/studerraimann/pdfwrap" ,
14- logLevel : 0
17+ logLevel : LogLevel . TRACE
1518 }
1619 ]
1720} ) ;
@@ -33,15 +36,15 @@ export class InMemStorageAdapter {
3336
3437 events . afterPolyLineRendered ( ) . subscribe ( it => {
3538 store . set ( it . element . id , it ) ;
36- } ) ;
39+ } , ( it ) => console . error ( it ) , ( ) => console . log ( "afterPolyLineRendered complete" ) ) ;
3740
3841 events . afterRectangleRendered ( ) . subscribe ( it => {
3942 store . set ( it . element . id , it ) ;
40- } ) ;
43+ } , ( it ) => console . error ( it ) , ( ) => console . log ( "afterRectangleRendered complete" ) ) ;
4144
4245 events . afterElementRemoved ( ) . subscribe ( it => {
4346 store . delete ( it . element . id ) ;
44- } )
47+ } , ( it ) => console . error ( it ) , ( ) => console . log ( "afterElementRemoved complete" ) )
4548 }
4649
4750 loadPage ( uri , pageNumber ) {
@@ -219,6 +222,7 @@ export class SidebarManager {
219222
220223 this . _outline . then ( outline => {
221224
225+ this . clearOutline ( ) ;
222226 outline . flatList . forEach ( it => {
223227
224228 const li = document . createElement ( "li" ) ;
@@ -235,6 +239,7 @@ export class SidebarManager {
235239
236240 renderThumbnails ( ) {
237241
242+ this . clearThumbnails ( ) ;
238243 this . _thumbails . subscribe ( it => {
239244
240245 const li = document . createElement ( "li" ) ;
@@ -249,34 +254,38 @@ export class SidebarManager {
249254 this . _thumbnailsUlElement . appendChild ( li ) ;
250255 } ) ;
251256 }
252- }
253257
254- const documentService = new PDFjsDocumentService ( ) ;
258+ clearThumbnails ( ) {
259+ while ( this . _thumbnailsUlElement . lastChild ) {
260+ this . _thumbnailsUlElement . removeChild ( this . _thumbnailsUlElement . lastChild ) ;
261+ }
262+ }
255263
264+ clearOutline ( ) {
265+ while ( this . _tableOfContents . lastChild ) {
266+ this . _tableOfContents . removeChild ( this . _tableOfContents . lastChild ) ;
267+ }
268+ }
269+ }
256270
257- new Promise ( resolve => {
271+ let documentService = null ;
272+ let pdfDoc = null ;
258273
259- const request = new XMLHttpRequest ( ) ;
260- request . open ( 'GET' , "assets/resources/chicken.pdf" , true ) ;
261- request . responseType = 'blob' ;
262- request . onload = function ( ) {
263- resolve ( request . response ) ;
264- } ;
265- request . send ( ) ;
266- } ) . then ( pdf => {
267- return documentService . loadWith ( {
274+ export async function loadPDF ( ) {
275+ documentService = new PDFjsDocumentService ( ) ;
276+ const pdf = await ( await fetch ( "assets/resources/chicken.pdf" ) ) . blob ( ) ;
277+ const pdfDocument = await documentService . loadWith ( {
268278 container : document . getElementById ( "viewerContainer" ) ,
269279 pdf : pdf ,
270280 layerStorage : URI . from ( "mem://chicken.pdf" )
271- } )
272- } ) . then ( it => {
281+ } ) ;
273282
274283 const highlightService = new HighlightService ( ) ;
275284
276285 const highlightButton = new HighlightButton ( highlightService ) ;
277286 const clearButton = new ClearButton ( highlightService ) ;
278- const penButton = new PenButton ( it . toolbox . freehand ) ;
279- const eraserButton = new EraserButton ( it . toolbox . eraser ) ;
287+ const penButton = new PenButton ( pdfDocument . toolbox . freehand ) ;
288+ const eraserButton = new EraserButton ( pdfDocument . toolbox . eraser ) ;
280289
281290 // search
282291 const searchButton = document . getElementById ( "search-button" ) ;
@@ -286,72 +295,89 @@ new Promise(resolve => {
286295 const searchField = document . getElementById ( "search-term" ) ;
287296
288297 searchButton . addEventListener ( "click" , ( ) => {
289- it . searchController . search ( searchField . value , { fuzzy : true , searchPhrase : true , highlightAll : true } ) ;
298+ pdfDocument . searchController . search ( searchField . value , { fuzzy : true , searchPhrase : true , highlightAll : true } ) ;
290299 } ) ;
291300
292301 previousSearch . addEventListener ( "click" , ( ) => {
293- it . searchController . previous ( ) ;
302+ pdfDocument . searchController . previous ( ) ;
294303 } ) ;
295304
296305 nextSearch . addEventListener ( "click" , ( ) => {
297- it . searchController . next ( ) ;
306+ pdfDocument . searchController . next ( ) ;
298307 } ) ;
299308
300309 // zoom
301310 const zoomIn = document . getElementById ( "zoom-in-button" ) ;
302311 const zoomOut = document . getElementById ( "zoom-out-button" ) ;
303312
304313 zoomIn . addEventListener ( "click" , ( ) => {
305- it . scale = it . scale * 1.5 ;
314+ pdfDocument . scale = pdfDocument . scale * 1.5 ;
306315 } ) ;
307316
308317 zoomOut . addEventListener ( "click" , ( ) => {
309- it . scale = it . scale / 1.5 ;
318+ pdfDocument . scale = pdfDocument . scale / 1.5 ;
310319 } ) ;
311320
312321 // page navigation
313322 const pageNumberInput = document . getElementById ( "page-number" ) ;
314- pageNumberInput . innerHTML = it . currentPageNumber ;
323+ pageNumberInput . innerHTML = pdfDocument . currentPageNumber . toString ( ) ;
315324
316- it . pageChange . subscribe ( it => {
317- pageNumberInput . innerHTML = it . pageNumber ;
325+ pdfDocument . pageChange . subscribe ( it => {
326+ pageNumberInput . innerHTML = it . pageNumber . toString ( ) ;
318327 } ) ;
319328
320329 const pageCountLabel = document . getElementById ( "page-count-label" ) ;
321- pageCountLabel . innerHTML = "/ " + it . pageCount ;
330+ pageCountLabel . innerHTML = "/ " + pdfDocument . pageCount ;
322331
323332 const previousPage = document . getElementById ( "previous-page" ) ;
324333 const nextPage = document . getElementById ( "next-page" ) ;
325334
326335 previousPage . addEventListener ( "click" , ( ) => {
327- it . currentPageNumber = it . currentPageNumber - 1 ;
336+ pdfDocument . currentPageNumber = pdfDocument . currentPageNumber - 1 ;
328337 } ) ;
329338
330339 nextPage . addEventListener ( "click" , ( ) => {
331- it . currentPageNumber = it . currentPageNumber + 1 ;
340+ pdfDocument . currentPageNumber = pdfDocument . currentPageNumber + 1 ;
332341 } ) ;
333342
334343 // sidebar left
335- const sidebarManager = new SidebarManager ( it ) ;
344+ const sidebarManager = new SidebarManager ( pdfDocument ) ;
336345 sidebarManager . renderOutline ( ) ;
337346 sidebarManager . renderThumbnails ( ) ;
338347
339- it . highlighting . enable ( ) ;
348+ pdfDocument . highlighting . enable ( ) ;
340349
341350 // highlighting
342- it . highlighting . onTextSelection
351+ pdfDocument . highlighting . onTextSelection
343352 . subscribe ( it => {
344353 highlightService . textSelection = it ;
345354 highlightButton . enable ( ) ;
346355 clearButton . enable ( ) ;
347356 } ) ;
348357
349- it . highlighting . onTextUnselection
358+ pdfDocument . highlighting . onTextUnselection
350359 . subscribe ( ( ) => {
351360 highlightButton . disable ( ) ;
352361 clearButton . disable ( ) ;
353362 } ) ;
354- } ) ;
363+ pdfDoc = pdfDocument ;
364+
365+ }
366+
367+ export function close ( ) {
368+ console . log ( "close" ) ;
369+ const thumbnails = document . getElementById ( "thumbnails" ) ;
370+ while ( thumbnails . lastChild ) {
371+ thumbnails . removeChild ( thumbnails . lastChild ) ;
372+ }
373+ pdfDoc . close ( ) ;
374+ pdfDoc = null ;
375+ documentService = null ;
376+ }
377+
378+ loadPDF ( ) ;
355379
380+ document . getElementById ( "close-page" ) . addEventListener ( "click" , close ) ;
381+ document . getElementById ( "load-page" ) . addEventListener ( "click" , loadPDF ) ;
356382
357383
0 commit comments