1
- import type { INodeProps , NodeLoadedPayload } from '@lightningjs/renderer' ;
1
+ import type {
2
+ INode ,
3
+ INodeProps ,
4
+ NodeLoadedPayload ,
5
+ RendererMain ,
6
+ } from '@lightningjs/renderer' ;
7
+ import type { Fiber } from 'react-reconciler' ;
8
+ import type { Plugin } from '../render/Plugin' ;
2
9
import {
10
+ type LightningElement ,
3
11
LightningElementType ,
4
12
type LightningImageElementProps ,
5
13
type LightningImageElementStyle ,
6
- type LightningTextElementStyle ,
7
- type LightningViewElementProps ,
8
14
type RendererNode ,
9
15
} from '../types' ;
10
16
import { LightningViewElement } from './LightningViewElement' ;
11
17
12
- export class LightningImageElement extends LightningViewElement <
13
- LightningImageElementStyle ,
14
- LightningImageElementProps < LightningImageElementStyle >
15
- > {
18
+ function getImageType ( src ?: string | null ) : INode [ 'imageType' ] {
19
+ if ( ! src ) {
20
+ return null ;
21
+ }
22
+
23
+ const srcLower = src . toLowerCase ( ) ;
24
+ const isSvg =
25
+ srcLower . endsWith ( '.svg' ) || srcLower . startsWith ( 'data:image/svg+xml,' ) ;
26
+
27
+ return isSvg ? 'svg' : null ;
28
+ }
29
+
30
+ export class LightningImageElement <
31
+ TStyleProps extends LightningImageElementStyle = LightningImageElementStyle ,
32
+ TProps extends
33
+ LightningImageElementProps < TStyleProps > = LightningImageElementProps < TStyleProps > ,
34
+ > extends LightningViewElement < TStyleProps , TProps > {
16
35
public override get type ( ) {
17
36
return LightningElementType . Image ;
18
37
}
@@ -30,16 +49,22 @@ export class LightningImageElement extends LightningViewElement<
30
49
public set src ( v ) {
31
50
this . node . src = v ;
32
51
33
- // Attempt to set the svg imageType for data urls if not already set.
34
- // Lightning doesn't handle svgs that are data urls, so this adds support
35
- // for that.
36
- if ( this . node . imageType == null && v ) {
37
- const src = v . toLowerCase ( ) ;
38
- const isSvg =
39
- src . endsWith ( '.svg' ) || src . startsWith ( 'data:image/svg+xml,' ) ;
52
+ if ( ! this . node . imageType ) {
53
+ this . node . imageType = getImageType ( v ) ;
54
+ }
55
+ }
40
56
41
- this . setNodeProp ( 'imageType' , isSvg ? 'svg' : null ) ;
57
+ public constructor (
58
+ initialProps : TProps ,
59
+ renderer : RendererMain ,
60
+ plugins : Plugin < LightningElement > [ ] ,
61
+ fiber : Fiber ,
62
+ ) {
63
+ if ( ! initialProps . imageType ) {
64
+ initialProps . imageType = getImageType ( initialProps . src ) ;
42
65
}
66
+
67
+ super ( initialProps , renderer , plugins , fiber ) ;
43
68
}
44
69
45
70
protected override _handleTextureLoaded ( event : NodeLoadedPayload ) : void {
@@ -58,7 +83,7 @@ export class LightningImageElement extends LightningViewElement<
58
83
}
59
84
60
85
public override _toLightningNodeProps (
61
- props : LightningViewElementProps < LightningTextElementStyle > & {
86
+ props : TProps & {
62
87
text ?: string ;
63
88
} & Record < string , unknown > ,
64
89
initial ?: boolean ,
0 commit comments