@@ -20,33 +20,10 @@ export async function init(id, invoke, options) {
2020 return ;
2121 }
2222
23- const loadingTask = pdfjsLib . getDocument ( options ) ;
24- loadingTask . onProgress = function ( progressData ) {
25-
26- } ;
27-
28- loadingTask . onPassword = function ( updatePassword , reason ) {
29- if ( reason === pdfjsLib . PasswordResponses . NEED_PASSWORD ) {
30-
31- }
32- else if ( reason === pdfjsLib . PasswordResponses . INCORRECT_PASSWORD ) {
33-
34- }
35- } ;
36-
37- const container = el . querySelector ( ".bb-view-container" ) ;
38- const eventBus = new pdfjsViewer . EventBus ( ) ;
39- const pdfViewer = new pdfjsViewer . PDFViewer ( {
40- container,
41- eventBus
42- } ) ;
43-
44- addEventListener ( el , pdfViewer , eventBus , invoke , options ) ;
45-
46- const pdfDocument = await loadingTask . promise ;
47- pdfViewer . setDocument ( pdfDocument ) ;
23+ const pdfViewer = await loadPdf ( el , invoke , options ) ;
24+ const observer = setObserver ( el ) ;
4825
49- Data . set ( id , { el, pdfViewer } ) ;
26+ Data . set ( id , { el, pdfViewer, observer } ) ;
5027}
5128
5229export function setScaleValue ( id , value ) {
@@ -86,6 +63,107 @@ export function resetThumbnails(id) {
8663 }
8764}
8865
66+ const loadPdf = async ( el , invoke , options ) => {
67+ const loadingTask = pdfjsLib . getDocument ( options ) ;
68+ loadingTask . onProgress = function ( progressData ) {
69+
70+ } ;
71+
72+ loadingTask . onPassword = function ( updatePassword , reason ) {
73+ if ( reason === pdfjsLib . PasswordResponses . NEED_PASSWORD ) {
74+
75+ }
76+ else if ( reason === pdfjsLib . PasswordResponses . INCORRECT_PASSWORD ) {
77+
78+ }
79+ } ;
80+
81+ const container = el . querySelector ( ".bb-view-container" ) ;
82+ const eventBus = new pdfjsViewer . EventBus ( ) ;
83+ const pdfViewer = new pdfjsViewer . PDFViewer ( {
84+ container,
85+ eventBus
86+ } ) ;
87+
88+ addEventListener ( el , pdfViewer , eventBus , invoke , options ) ;
89+
90+ const pdfDocument = await loadingTask . promise ;
91+ pdfViewer . setDocument ( pdfDocument ) ;
92+
93+ return pdfViewer ;
94+ }
95+
96+ const setObserver = el => {
97+ const title = el . querySelector ( ".bb-view-title" ) ;
98+ const subject = el . querySelector ( ".bb-view-subject" ) ;
99+ const groupPage = el . querySelector ( ".bb-view-group-page" ) ;
100+ const groupScale = el . querySelector ( ".bb-view-group-scale" ) ;
101+ const groupRotate = el . querySelector ( ".bb-view-group-rotate" ) ;
102+ const controls = el . querySelector ( ".bb-view-controls" ) ;
103+
104+ el . widths = [ subject . offsetWidth , groupRotate . offsetWidth , groupScale . offsetWidth , groupPage . offsetWidth , controls . offsetWidth ] ;
105+
106+ const increaseSpace = toolbar => {
107+ if ( controls . classList . contains ( 'd-none' ) ) {
108+ if ( title . offsetWidth + el . widths [ 4 ] + groupPage . offsetWidth + groupScale . offsetWidth + groupRotate . offsetWidth + controls . offsetWidth < toolbar . offsetWidth ) {
109+ controls . classList . remove ( "d-none" ) ;
110+ }
111+ }
112+ else if ( groupPage . classList . contains ( 'd-none' ) ) {
113+ if ( title . offsetWidth + el . widths [ 3 ] + groupPage . offsetWidth + groupScale . offsetWidth + groupRotate . offsetWidth + controls . offsetWidth < toolbar . offsetWidth ) {
114+ groupPage . classList . remove ( "d-none" ) ;
115+ }
116+ }
117+ else if ( groupScale . classList . contains ( 'd-none' ) ) {
118+ if ( title . offsetWidth + el . widths [ 2 ] + groupPage . offsetWidth + groupScale . offsetWidth + groupRotate . offsetWidth + controls . offsetWidth < toolbar . offsetWidth ) {
119+ groupScale . classList . remove ( "d-none" ) ;
120+ }
121+ }
122+ else if ( groupRotate . classList . contains ( 'd-none' ) ) {
123+ if ( title . offsetWidth + el . widths [ 1 ] + groupPage . offsetWidth + groupScale . offsetWidth + groupRotate . offsetWidth + controls . offsetWidth < toolbar . offsetWidth ) {
124+ groupRotate . classList . remove ( "d-none" ) ;
125+ }
126+ }
127+ else if ( subject . classList . contains ( "d-none" ) ) {
128+ if ( title . offsetWidth + el . widths [ 0 ] + groupPage . offsetWidth + groupScale . offsetWidth + groupRotate . offsetWidth + controls . offsetWidth < toolbar . offsetWidth ) {
129+ subject . classList . remove ( "d-none" ) ;
130+ }
131+ }
132+ }
133+ const decreaseSpace = toolbar => {
134+ while ( title . offsetWidth + groupPage . offsetWidth + groupScale . offsetWidth + groupRotate . offsetWidth + controls . offsetWidth > toolbar . offsetWidth ) {
135+ if ( subject . classList . contains ( "d-none" ) === false ) {
136+ subject . classList . add ( "d-none" ) ;
137+ }
138+ else if ( groupRotate . classList . contains ( 'd-none' ) === false ) {
139+ groupRotate . classList . add ( "d-none" ) ;
140+ }
141+ else if ( groupScale . classList . contains ( 'd-none' ) === false ) {
142+ groupScale . classList . add ( "d-none" ) ;
143+ }
144+ else if ( groupPage . classList . contains ( 'd-none' ) === false ) {
145+ groupPage . classList . add ( "d-none" ) ;
146+ }
147+ else if ( controls . classList . contains ( 'd-none' ) === false ) {
148+ controls . classList . add ( "d-none" ) ;
149+ }
150+ }
151+ }
152+
153+ const observer = new ResizeObserver ( entries => {
154+ const toolbar = el . querySelector ( ".bb-view-toolbar" ) ;
155+ if ( toolbar === null ) {
156+ return ;
157+ }
158+
159+ decreaseSpace ( toolbar ) ;
160+ increaseSpace ( toolbar ) ;
161+ } ) ;
162+
163+ observer . observe ( el ) ;
164+ return observer ;
165+ }
166+
89167const addEventListener = ( el , pdfViewer , eventBus , invoke , options ) => {
90168 eventBus . on ( "pagesinit" , async ( ) => {
91169 if ( options . fitMode ) {
@@ -303,9 +381,14 @@ const printPdf = url => {
303381}
304382
305383export function dispose ( id ) {
384+ const { el, observer } = Data . get ( id ) ;
306385 Data . remove ( id ) ;
307386
308- const el = document . getElementById ( id ) ;
387+ if ( observer ) {
388+ observer . disconnect ( ) ;
389+ observer = null ;
390+ }
391+
309392 if ( el ) {
310393 const minus = el . querySelector ( ".bb-page-minus" ) ;
311394 const plus = el . querySelector ( ".bb-page-plus" ) ;
0 commit comments