@@ -2,20 +2,21 @@ import { v4 as uuidv4 } from 'uuid';
2
2
3
3
import * as React from 'react' ;
4
4
5
- // Note: this import is successful only if "moduleResolution" is set to "Node"
6
- // in tsconfig.json. However if this is set to "bundler" instead and this can't
7
- // be resolved, the entire code is still type-valid with `Parser` being of type
8
- // `any`.
9
- import type { Parser } from '@frameright/image-display-control-metadata-parser/dist/index.d' ;
5
+ import { Parser } from '@frameright/image-display-control-metadata-parser' ;
10
6
11
7
// If true, we are either running on the server at request-time, or at
12
8
// build time, building a static (e.g. Next.js) page.
13
9
const isServerOrStatic = typeof window === 'undefined' ;
14
10
15
11
const isBrowser = ! isServerOrStatic ;
16
12
17
- // See https://stackoverflow.com/questions/50940640/how-to-determine-if-jest-is-running-the-code-or-not
18
- const isRunningTests = process . env . JEST_WORKER_ID !== undefined ;
13
+ let isRunningTests = false ;
14
+ try {
15
+ // See https://stackoverflow.com/questions/50940640/how-to-determine-if-jest-is-running-the-code-or-not
16
+ isRunningTests = process . env . JEST_WORKER_ID !== undefined ;
17
+ } catch ( e ) {
18
+ // Probably "ReferenceError: process is not defined", ignore.
19
+ }
19
20
20
21
// See https://docs.frameright.io/web-component/importing
21
22
if ( isBrowser && ! isRunningTests ) {
@@ -25,67 +26,24 @@ if (isBrowser && !isRunningTests) {
25
26
) ;
26
27
}
27
28
28
- type ParserConstructor = {
29
- new ( buffer : Buffer ) : Parser ;
30
- } ;
31
-
32
- // In the browser, importing image-display-control-metadata-parser-standalone
33
- // will define window.ImageDisplayControl and window.Buffer.
34
- interface ExtendedWindow extends Window {
35
- Buffer : { Buffer : BufferConstructor } | undefined ;
36
- ImageDisplayControl :
37
- | {
38
- Parser : ParserConstructor ;
39
- }
40
- | undefined ;
41
- }
42
-
43
29
interface FsModule {
44
30
readFileSync : ( path : string ) => Buffer ;
45
31
}
46
32
47
33
let fs : FsModule | null = null ;
48
- let parserConstructor : ParserConstructor | null = null ;
49
- if ( isBrowser ) {
50
- // Defines window.ImageDisplayControl and window.Buffer.
51
-
52
- // Note: this has no .d.ts file, so we have to use `"noImplicitAny": false`
53
- // in tsconfig.json for this import to work. Ideally we should get rid of
54
- // this special standalone file, as it ships Node polyfills for the browser.
55
- // Instead, the library should not require Node polyfills. See
56
- // https://github.com/Frameright/image-display-control-metadata-parser/issues/3
57
- import (
58
- '@frameright/image-display-control-metadata-parser/dist/image-display-control-metadata-parser-standalone.min.js'
59
- ) . then ( ( ) => {
60
- const extendedWindow = window as unknown as ExtendedWindow ;
61
- if ( extendedWindow . ImageDisplayControl ) {
62
- parserConstructor = extendedWindow . ImageDisplayControl . Parser ;
63
- }
64
- } ) ;
65
- } else {
66
- // We need to use `require()` here with vite-plugin-ssr in prod as otherwise
34
+ if ( ! isBrowser ) {
35
+ // We need to use `require()` here with Vite/Vike in prod as otherwise
67
36
// `import()` will happen later than the server-side rendering. However
68
37
// `require()` isn't defined in dev mode, so then we fall back to `import()`.
69
38
if ( require ) {
70
39
// FIXME: for this to work on Next.js, we need to set
71
40
// `config.resolve.fallback = { fs: false };` in next.config.js. See
72
- // * https://github.com/Frameright/image-display-control-metadata-parser/issues/3
73
- // * https://stackoverflow.com/questions/64926174/module-not-found-cant-resolve-fs-in-next-js-application
41
+ // https://stackoverflow.com/questions/64926174/module-not-found-cant-resolve-fs-in-next-js-application
74
42
fs = require ( 'fs' ) ;
75
-
76
- // eslint-disable-next-line @typescript-eslint/no-var-requires
77
- const parserModule = require ( '@frameright/image-display-control-metadata-parser' ) ;
78
-
79
- parserConstructor = parserModule . Parser ;
80
43
} else {
81
44
import ( 'fs' ) . then ( ( fsModule ) => {
82
45
fs = fsModule ;
83
46
} ) ;
84
- import ( '@frameright/image-display-control-metadata-parser' ) . then (
85
- ( parserModule ) => {
86
- parserConstructor = parserModule . Parser ;
87
- }
88
- ) ;
89
47
}
90
48
}
91
49
@@ -145,7 +103,16 @@ export function ImageDisplayControl({
145
103
146
104
++ numImgChildren ;
147
105
148
- const newAttrs = {
106
+ const newAttrs : {
107
+ is : string ;
108
+ class : string ;
109
+ ref : React . MutableRefObject < null > ;
110
+ 'data-idc-uuid' : string ;
111
+ suppressHydrationWarning : boolean ;
112
+ 'data-src-prop' : string ;
113
+ 'data-path-on-server' ?: string ;
114
+ 'data-image-regions' ?: string ;
115
+ } = {
149
116
// Note: thanks to this, react will recognize this as a custom element.
150
117
// https://react.dev/reference/react-dom/components#custom-html-elements
151
118
is : 'image-display-control' ,
@@ -337,7 +304,7 @@ async function _populateImageRegionsMap(
337
304
}
338
305
339
306
await _waitForImports ( debug ) ;
340
- const regions = _readImageRegionsJsonFromArrayBuffer ( arrayBuffer ) ;
307
+ const regions = _readImageRegionsJsonFromBuffer ( arrayBuffer ) ;
341
308
if ( regions ) {
342
309
_traceIfDebug (
343
310
debug ,
@@ -431,42 +398,8 @@ function _readImageRegionsJsonFromDisk(
431
398
return result ;
432
399
}
433
400
434
- // To be used only on client-side.
435
- function _readImageRegionsJsonFromArrayBuffer (
436
- arrayBuffer : ArrayBuffer
437
- ) : string {
438
- let result = '' ;
439
- try {
440
- const extendedWindow = window as unknown as ExtendedWindow ;
441
- if ( ! extendedWindow . Buffer ) {
442
- // See
443
- // https://github.com/Frameright/image-display-control-metadata-parser/issues/3
444
- _warn (
445
- "Buffer module not available, can't read image regions from" ,
446
- 'ArrayBuffer.'
447
- ) ;
448
- } else {
449
- const buffer = extendedWindow . Buffer . Buffer . from ( arrayBuffer ) ;
450
- result = _readImageRegionsJsonFromBuffer ( buffer ) ;
451
- }
452
- } catch ( error ) {
453
- _warn ( 'Error while reading image regions from array buffer:' , error ) ;
454
- }
455
- return result ;
456
- }
457
-
458
- function _readImageRegionsJsonFromBuffer ( buffer : Buffer ) : string {
459
- if ( ! parserConstructor ) {
460
- // See
461
- // https://github.com/Frameright/image-display-control-metadata-parser/issues/3
462
- _warn (
463
- "parserConstructor module not available, can't read image regions from" ,
464
- 'Buffer.'
465
- ) ;
466
- return '' ;
467
- }
468
-
469
- const parser = new parserConstructor ( buffer ) ;
401
+ function _readImageRegionsJsonFromBuffer ( buffer : Buffer | ArrayBuffer ) : string {
402
+ const parser = new Parser ( buffer ) ;
470
403
const regions = parser . getIdcMetadata ( 'rectangle' , 'crop' ) ;
471
404
return JSON . stringify ( regions ) ;
472
405
}
@@ -509,8 +442,8 @@ function _getImageSource(
509
442
510
443
result = {
511
444
src :
512
- element . attributes [ 'data-src-prop' ] ?. value ||
513
- element . attributes [ 'src' ] ?. value ||
445
+ element . attributes . getNamedItem ( 'data-src-prop' ) ?. value ||
446
+ element . attributes . getNamedItem ( 'src' ) ?. value ||
514
447
element . src ,
515
448
} ;
516
449
}
@@ -528,21 +461,15 @@ function _isImgElement(
528
461
return ! ! element . props . src ;
529
462
} else if ( 'attributes' in element ) {
530
463
// HTML element
531
- return ! ! element . attributes [ 'src' ] . value ;
464
+ return ! ! element . attributes . getNamedItem ( 'src' ) ? .value ;
532
465
}
533
466
return false ;
534
467
}
535
468
536
- // Workaround for
537
- // https://github.com/Frameright/image-display-control-metadata-parser/issues/3
469
+ // FIXME: workaround for async import() on Vite/Vike in dev mode.
538
470
async function _waitForImports ( debug : boolean ) {
539
- const extendedWindow = window as unknown as ExtendedWindow ;
540
471
for ( const waitMs of [ 100 , 200 , 500 , 1000 , 1000 ] ) {
541
- if (
542
- ( fs || isBrowser ) &&
543
- ( extendedWindow . Buffer || isServerOrStatic ) &&
544
- parserConstructor
545
- ) {
472
+ if ( fs || isBrowser ) {
546
473
_traceIfDebug ( debug , 'All imports have resolved.' ) ;
547
474
return ;
548
475
}
@@ -554,11 +481,11 @@ async function _waitForImports(debug: boolean) {
554
481
555
482
const consolePrefix = '[idc]' ;
556
483
557
- function _warn ( ...args ) {
484
+ function _warn ( ...args : any [ ] ) {
558
485
console . warn ( consolePrefix , '[warn]' , ...args ) ;
559
486
}
560
487
561
- function _traceIfDebug ( debug : boolean , ...args ) {
488
+ function _traceIfDebug ( debug : boolean , ...args : any [ ] ) {
562
489
if ( debug ) {
563
490
console . log ( consolePrefix , '[debug]' , ...args ) ;
564
491
}
0 commit comments