Skip to content

Commit d6c4850

Browse files
Merge pull request #191 from gridaco/staging
2022W48 Improved Dashboard interface & new 3D Inspector
2 parents 22e5f33 + 92fa1a8 commit d6c4850

File tree

92 files changed

+3708
-1216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+3708
-1216
lines changed

editor/scaffolds/preview-canvas/image-preview.tsx renamed to editor-packages/editor-canvas-renderer-bitmap/figma-node-bitmap-preview.tsx

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,62 @@
11
import React, { useState, useEffect } from "react";
2-
import { ReflectSceneNode } from "@design-sdk/figma-node";
2+
import type { ReflectSceneNode } from "@design-sdk/figma-node";
33
import 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";
55
import { 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
*/
1247
type 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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export {
2+
FigmaNodeBitmapPreviewServiceProvider,
3+
FigmaNodeBitmapView,
4+
} from "./figma-node-bitmap-preview";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@code-editor/canvas-renderer-bitmap",
3+
"version": "0.0.0"
4+
}

editor-packages/editor-module-icons/tsconfig.json renamed to editor-packages/editor-canvas-renderer-bitmap/tsconfig.json

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./util";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@code-editor/canvas-renderer-core",
3+
"version": "0.0.0"
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { colorFromFills } from "@design-sdk/core/utils";
2+
import type { Paint } from "@design-sdk/figma";
3+
4+
export const blurred_bg_fill = (fills: ReadonlyArray<Paint>) => {
5+
const __bg = colorFromFills(fills);
6+
const bg_color_str = __bg ? "#" + __bg.hex : "transparent";
7+
return bg_color_str;
8+
};

editor-packages/editor-canvas/canvas/canvas.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
target_of_area,
1717
boundingbox,
1818
is_point_inside_box,
19+
zoomToFit,
1920
} from "../math";
2021
import q from "@design-sdk/query";
2122
import { LazyFrame } from "@code-editor/canvas/lazy-frame";
@@ -121,6 +122,11 @@ type CanvasFocusProps = {
121122
focusRefreshkey?: string;
122123
};
123124

125+
type CanvasFocusSnap = {
126+
damping?: number;
127+
bounds?: Box;
128+
};
129+
124130
interface HovringNode {
125131
node: ReflectSceneNode;
126132
reason: "frame-title" | "raycast" | "external";
@@ -515,7 +521,8 @@ export function Canvas({
515521
}}
516522
onZoomToFit={() => {
517523
setZoom(1);
518-
// setOffset([newx, newy]); // TODO: set offset to center of the viewport
524+
// const newoffset = zoomToFit(viewbound, offset, zoom, 1);
525+
// setOffset(newoffset);
519526
_canvas_state_store.saveLastTransform(cvtransform);
520527
}}
521528
onZooming={onZooming}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from "./bounding-box";
22
export * from "./target-of-area";
33
export * from "./target-of-point";
4-
export * from "./guide-spacing"
4+
export * from "./guide-spacing";
55
export * from "./center-of";
6-
export * from "./viewbound-edge-scrolling";
6+
export * from "./viewbound-edge-scrolling";
7+
export * from "./zoom-to-fit";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Box, XY } from "../types";
2+
3+
/**
4+
* this function provides a new offset for the canvas when user executes zoom-to-fit action, which resetting the canvas zoom to 1.
5+
* the givven factors are.
6+
* @param viewbound the viewbound of the canvas.
7+
* @param offset the current offset of the canvas.
8+
* @param scale the current scale of the canvas.
9+
* @param newScale the new scale of the canvas. @default 1
10+
*
11+
* @returns the new offset of the canvas.
12+
*
13+
* @deprecated not tested
14+
*/
15+
export function zoomToFit(
16+
viewbound: Box,
17+
offset: XY,
18+
scale: number,
19+
newScale: number = 1
20+
): XY {
21+
const [x, y, w, h] = viewbound;
22+
const [ox, oy] = offset;
23+
const newOffset: XY = [
24+
(x + w / 2) * (1 - newScale / scale) + ox,
25+
(y + h / 2) * (1 - newScale / scale) + oy,
26+
];
27+
return newOffset;
28+
}

0 commit comments

Comments
 (0)