11import React , { useState , useEffect } from "react" ;
2- import { ReflectSceneNode } from "@design-sdk/figma-node" ;
2+ import type { ReflectSceneNode } from "@design-sdk/figma-node" ;
33import type { FrameOptimizationFactors } from "@code-editor/canvas/frame" ;
4- import { blurred_bg_fill } from "./util " ;
4+ import { blurred_bg_fill } from "@code-editor/canvas-renderer-core " ;
55import { CircularProgress } from "@mui/material" ;
6- import { useFigmaImageService } from "scaffolds/editor" ;
6+
7+ interface BitmapPreviewService {
8+ fetch : (
9+ id : string ,
10+ options ?: {
11+ scale ?: number ;
12+ }
13+ ) => Promise < string > ;
14+ }
15+
16+ const Context = React . createContext < BitmapPreviewService > ( null ) ;
17+
18+ const usefigmaBitmapPreviewService = ( ) => {
19+ const context = React . useContext ( Context ) ;
20+ if ( ! context || typeof context . fetch !== "function" ) {
21+ throw new Error (
22+ "Bitmap service is not available. Are you sure you have an <FigmaNodeBitmapPreviewServiceProvider> above your consumers?"
23+ ) ;
24+ }
25+ return context ;
26+ } ;
27+
28+ export function FigmaNodeBitmapPreviewServiceProvider ( {
29+ children,
30+ service,
31+ } : React . PropsWithChildren < { service : BitmapPreviewService } > ) {
32+ return < Context . Provider value = { service } > { children } </ Context . Provider > ;
33+ }
34+
35+ type TargetSceneMeta = {
36+ id : string ;
37+ filekey : ReflectSceneNode [ "filekey" ] ;
38+ name : string ;
39+ width : number ;
40+ height : number ;
41+ } ;
742
843/**
944 * 1 = 1 scale
1045 * s = 0.2 scale
1146 */
1247type ImageSizeVariant = "1" | "s" ;
1348
14- export function FigmaStaticImageFrameView ( {
49+ export function FigmaNodeBitmapView ( {
1550 target,
1651 zoom,
1752 inViewport,
1853 background,
1954} : {
20- target : ReflectSceneNode ;
55+ target : TargetSceneMeta ;
2156} & FrameOptimizationFactors & {
2257 background ?: React . CSSProperties [ "background" ] ;
2358 } ) {
24- const service = useFigmaImageService ( ) ;
59+ const service = usefigmaBitmapPreviewService ( ) ;
2560 const { filekey : _fk , id, width, height } = target ;
2661 const filekey = _fk as string ;
2762
@@ -44,19 +79,17 @@ export function FigmaStaticImageFrameView({
4479
4580 if ( service ) {
4681 service
47- . fetch ( id , {
48- debounce : true ,
49- ensure : true ,
50- } )
82+ . fetch ( id )
5183 . then ( ( res ) => {
52- const src = res [ id ] ;
84+ const src = res ;
5385 set_image ( src ) ;
5486 } )
5587 . catch ( console . error ) ;
5688 }
5789 } , [ filekey , id , service , inViewport ] ) ;
5890
59- const bg_color_str = blurred_bg_fill ( target ) ;
91+ const bg_color_str =
92+ "fills" in target ? blurred_bg_fill ( target [ "fills" ] as any ) : "white" ;
6093
6194 return (
6295 < div
0 commit comments