|
| 1 | +import type { |
| 2 | + ReflectFrameNode, |
| 3 | + ReflectGroupNode, |
| 4 | + ReflectRectangleNode, |
| 5 | + ReflectSceneNode, |
| 6 | +} from "@design-sdk/figma-node"; |
| 7 | +import type { Container } from "@reflect-ui/core"; |
| 8 | +import { tokenizeLayout } from "../../token-layout"; |
| 9 | +import { tokenizeContainer } from "../../token-container"; |
| 10 | +import { unwrappedChild } from "../../wrappings"; |
| 11 | +import type { CameraDisplayFlag } from "@code-features/flags"; |
| 12 | +import { WrappingContainer, XCameraDisplayView } from "../../tokens"; |
| 13 | +import assert from "assert"; |
| 14 | +import { keyFromNode } from "../../key"; |
| 15 | + |
| 16 | +export function tokenize_flagged_camera_view( |
| 17 | + node: ReflectSceneNode, |
| 18 | + flag: CameraDisplayFlag |
| 19 | +) { |
| 20 | + if (!flag.value) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + node = validate_input(node); |
| 26 | + const _key = keyFromNode(node); |
| 27 | + |
| 28 | + const container = unwrappedChild( |
| 29 | + node.type === "RECTANGLE" |
| 30 | + ? tokenizeContainer.fromRectangle(node) |
| 31 | + : tokenizeLayout.fromFrameOrGroup(node, [], { is_root: false }, {}) |
| 32 | + ) as Container; |
| 33 | + |
| 34 | + return new WrappingContainer({ |
| 35 | + ...container, |
| 36 | + key: keyFromNode(node), |
| 37 | + child: new XCameraDisplayView({ |
| 38 | + key: _key, |
| 39 | + ...container, |
| 40 | + }), |
| 41 | + }); |
| 42 | + } catch (e) { |
| 43 | + throw new Error(`failed to tokenize camera display view: ${e.message}`); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +type CameraDisplayCompatNodeType = |
| 48 | + | ReflectFrameNode |
| 49 | + | ReflectGroupNode |
| 50 | + | ReflectRectangleNode; |
| 51 | + |
| 52 | +function validate_input(node: ReflectSceneNode): CameraDisplayCompatNodeType { |
| 53 | + assert( |
| 54 | + node.type === "FRAME" || node.type === "GROUP" || node.type === "RECTANGLE", |
| 55 | + `camera display view target input must be a frame, group or rectangle, but "${node.type}" was given` |
| 56 | + ); |
| 57 | + |
| 58 | + return node; |
| 59 | +} |
0 commit comments