1
+ import { image } from './image.js' ;
1
2
import { audio } from './audio.js' ;
2
3
import { progress } from './progress.js' ;
3
4
import { util } from '../../common/util.js' ;
@@ -83,7 +84,8 @@ export const guest = (() => {
83
84
div . classList . add ( 'm-2' ) ;
84
85
div . innerHTML = `
85
86
<p class="mt-0 mb-1 mx-0 p-0" style="font-size: 0.95rem;">${ guest ?. getAttribute ( 'data-message' ) } </p>
86
- <h2 class="m-0 p-0">${ util . escapeHtml ( name ) } </h2>` ;
87
+ <h2 class="m-0 p-0">${ util . escapeHtml ( name ) } </h2>
88
+ ` ;
87
89
88
90
guest ?. appendChild ( div ) ;
89
91
}
@@ -147,96 +149,12 @@ export const guest = (() => {
147
149
/**
148
150
* @returns {void }
149
151
*/
150
- const normalize = ( ) => {
152
+ const normalizeArabicFont = ( ) => {
151
153
document . querySelectorAll ( '.font-arabic' ) . forEach ( ( el ) => {
152
154
el . innerHTML = String ( el . innerHTML ) . normalize ( 'NFC' ) ;
153
155
} ) ;
154
156
} ;
155
157
156
- /**
157
- * @returns {void }
158
- */
159
- const imageProgress = ( ) => {
160
- /**
161
- * @type {Map<string, string> }
162
- */
163
- const uniqueUrl = new Map ( ) ;
164
-
165
- /**
166
- * @param {HTMLImageElement } el
167
- * @returns {Promise<void> }
168
- */
169
- const getByFetch = async ( el ) => {
170
- // 6 hour TTL
171
- const ttl = 1000 * 60 * 60 * 6 ;
172
- const url = el . getAttribute ( 'data-src' ) ;
173
- const exp = 'x-expiration-time' ;
174
- const cacheName = 'image_cache' ;
175
-
176
- if ( uniqueUrl . has ( url ) ) {
177
- el . src = uniqueUrl . get ( url ) ;
178
- progress . complete ( 'image' ) ;
179
- return ;
180
- }
181
-
182
- /**
183
- * @param {Cache } c
184
- * @returns {Promise<blob> }
185
- */
186
- const fetchPut = ( c ) => {
187
- return fetch ( url ) . then ( ( res ) => res . blob ( ) . then ( ( b ) => {
188
- const headers = new Headers ( res . headers ) ;
189
- headers . append ( exp , String ( Date . now ( ) + ttl ) ) ;
190
-
191
- return c . put ( url , new Response ( b , { headers } ) ) . then ( ( ) => b ) ;
192
- } ) ) ;
193
- } ;
194
-
195
- await caches . open ( cacheName ) . then ( ( c ) => {
196
- return c . match ( url ) . then ( ( res ) => {
197
- if ( ! res ) {
198
- return fetchPut ( c ) ;
199
- }
200
-
201
- if ( Date . now ( ) <= parseInt ( res . headers . get ( exp ) ) ) {
202
- return res . blob ( ) ;
203
- }
204
-
205
- return c . delete ( url ) . then ( ( s ) => s ? fetchPut ( c ) : res . blob ( ) ) ;
206
- } ) . then ( ( b ) => {
207
- el . src = URL . createObjectURL ( b ) ;
208
- uniqueUrl . set ( url , el . src ) ;
209
- progress . complete ( 'image' ) ;
210
- } )
211
- } ) . catch ( ( ) => progress . invalid ( 'image' ) ) ;
212
- } ;
213
-
214
- /**
215
- * @param {HTMLImageElement } el
216
- * @returns {void }
217
- */
218
- const getByDefault = ( el ) => {
219
- el . onerror = ( ) => progress . invalid ( 'image' ) ;
220
- el . onload = ( ) => progress . complete ( 'image' ) ;
221
-
222
- if ( el . complete && el . naturalWidth !== 0 && el . naturalHeight !== 0 ) {
223
- progress . complete ( 'image' ) ;
224
- } else if ( el . complete ) {
225
- progress . invalid ( 'image' ) ;
226
- }
227
- } ;
228
-
229
- ( async ( els ) => {
230
- for ( const el of els ) {
231
- if ( el . hasAttribute ( 'data-src' ) ) {
232
- await getByFetch ( el ) ;
233
- } else {
234
- getByDefault ( el ) ;
235
- }
236
- }
237
- } ) ( document . querySelectorAll ( 'img' ) ) ;
238
- } ;
239
-
240
158
/**
241
159
* @returns {object }
242
160
*/
@@ -246,13 +164,12 @@ export const guest = (() => {
246
164
offline . init ( ) ;
247
165
progress . init ( ) ;
248
166
249
- normalize ( ) ;
250
167
countDownDate ( ) ;
168
+ normalizeArabicFont ( ) ;
251
169
information = storage ( 'information' ) ;
252
- document . addEventListener ( 'progressDone' , ( ) => {
253
- showGuestName ( ) ;
254
- window . AOS . init ( ) ;
255
- } ) ;
170
+
171
+ document . addEventListener ( 'progressDone' , showGuestName ) ;
172
+ document . addEventListener ( 'progressDone' , window . AOS . init ) ;
256
173
257
174
if ( session . isAdmin ( ) ) {
258
175
storage ( 'user' ) . clear ( ) ;
@@ -272,12 +189,9 @@ export const guest = (() => {
272
189
info . remove ( ) ;
273
190
}
274
191
275
- // add total image.
276
- document . querySelectorAll ( 'img' ) . forEach ( progress . add ) ;
277
-
278
192
const token = document . body . getAttribute ( 'data-key' ) ;
279
193
if ( ! token || token . length === 0 ) {
280
- imageProgress ( ) ;
194
+ image . init ( ) . load ( ) ;
281
195
document . getElementById ( 'comment' ) ?. remove ( ) ;
282
196
document . querySelector ( 'a.nav-link[href="#comment"]' ) ?. closest ( 'li.nav-item' ) ?. remove ( ) ;
283
197
}
@@ -287,31 +201,28 @@ export const guest = (() => {
287
201
progress . add ( ) ;
288
202
progress . add ( ) ;
289
203
290
- const hasDataSrc = Array . from ( document . querySelectorAll ( ' img' ) ) . some ( ( i ) => i . hasAttribute ( 'data-src' ) ) ;
291
- if ( ! hasDataSrc ) {
292
- imageProgress ( ) ;
204
+ const img = image . init ( ) ;
205
+ if ( ! img . hasDataSrc ( ) ) {
206
+ img . load ( ) ;
293
207
}
294
208
295
209
session . setToken ( token ) ;
296
- session . guest ( )
297
- . then ( ( res ) => {
298
- if ( res . code !== 200 ) {
299
- progress . invalid ( 'config' ) ;
300
- return ;
301
- }
302
-
303
- progress . complete ( 'config' ) ;
304
-
305
- if ( hasDataSrc ) {
306
- imageProgress ( ) ;
307
- }
308
-
309
- comment . init ( ) ;
310
- comment . comment ( )
311
- . then ( ( ) => progress . complete ( 'comment' ) )
312
- . catch ( ( ) => progress . invalid ( 'comment' ) ) ;
313
- } )
314
- . catch ( ( ) => progress . invalid ( 'config' ) ) ;
210
+ session . guest ( ) . then ( ( res ) => {
211
+ if ( res . code !== 200 ) {
212
+ progress . invalid ( 'config' ) ;
213
+ return ;
214
+ }
215
+
216
+ progress . complete ( 'config' ) ;
217
+ if ( img . hasDataSrc ( ) ) {
218
+ img . load ( ) ;
219
+ }
220
+
221
+ comment . init ( ) ;
222
+ comment . comment ( )
223
+ . then ( ( ) => progress . complete ( 'comment' ) )
224
+ . catch ( ( ) => progress . invalid ( 'comment' ) ) ;
225
+ } ) . catch ( ( ) => progress . invalid ( 'config' ) ) ;
315
226
}
316
227
317
228
return {
0 commit comments