Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Demonstration of figdeck's column layout feature.
:::column
### Left Column

- Feature A
- Feature A
- Feature B
- Feature C

Expand Down
123 changes: 123 additions & 0 deletions packages/plugin/src/code.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import { beforeEach, describe, expect, it, mock } from "bun:test";
import type { BulletItem } from "@figdeck/shared";

// Mock for TextNode
function createMockTextNode(overrides: Partial<TextNode> = {}): TextNode {
return {
type: "TEXT",
width: 800,
height: 20,
layoutSizingHorizontal: "HUG",
textAutoResize: "WIDTH_AND_HEIGHT",
resize: mock(() => {}),
...overrides,
} as unknown as TextNode;
}

// Mock for FrameNode
function createMockFrameNode(
overrides: Partial<FrameNode> & { children?: SceneNode[] } = {},
): FrameNode {
const children = overrides.children || [];
return {
type: "FRAME",
width: 400,
height: 100,
layoutMode: "VERTICAL",
layoutSizingHorizontal: "HUG",
layoutSizingVertical: "HUG",
children: children,
...overrides,
} as unknown as FrameNode;
}

// Minimal figma + __html__ stubs for importing the plugin entrypoint
beforeEach(() => {
(globalThis as { __html__?: string }).__html__ = "";
Expand Down Expand Up @@ -187,3 +217,96 @@ describe("validateAndSanitizeSlides", () => {
expect(block.items.length).toBeLessThanOrEqual(100);
});
});

describe("constrainNodeToWidth", () => {
it("sets text node properties for wrapping", async () => {
const { constrainNodeToWidth } = await import("./code");

const textNode = createMockTextNode();
constrainNodeToWidth(textNode as unknown as SceneNode, 400);

expect(textNode.layoutSizingHorizontal).toBe("FIXED");
expect(textNode.textAutoResize).toBe("HEIGHT");
expect(textNode.resize).toHaveBeenCalledWith(400, 20);
});

it("sets frame node properties and processes children", async () => {
const { constrainNodeToWidth } = await import("./code");

const childText = createMockTextNode();
const frameNode = createMockFrameNode({
layoutMode: "VERTICAL",
children: [childText as unknown as SceneNode],
});

constrainNodeToWidth(frameNode as unknown as SceneNode, 400);

expect(frameNode.layoutSizingHorizontal).toBe("FILL");
expect(frameNode.layoutSizingVertical).toBe("HUG");
// Child text node should also be constrained
expect(childText.layoutSizingHorizontal).toBe("FIXED");
expect(childText.textAutoResize).toBe("HEIGHT");
});

it("does not modify non-auto-layout frames", async () => {
const { constrainNodeToWidth } = await import("./code");

const frameNode = createMockFrameNode({
layoutMode: "NONE",
width: 300,
});

constrainNodeToWidth(frameNode as unknown as SceneNode, 400);

// Should not change layoutSizingHorizontal for non-auto-layout frames
expect(frameNode.layoutSizingHorizontal).toBe("HUG");
});
});

describe("constrainTextNodesInFrame", () => {
it("constrains all text nodes in frame", async () => {
const { constrainTextNodesInFrame } = await import("./code");

const textNode1 = createMockTextNode();
const textNode2 = createMockTextNode();
const frameNode = createMockFrameNode({
children: [
textNode1 as unknown as SceneNode,
textNode2 as unknown as SceneNode,
],
});

constrainTextNodesInFrame(frameNode, 350);

expect(textNode1.layoutSizingHorizontal).toBe("FIXED");
expect(textNode1.textAutoResize).toBe("HEIGHT");
expect(textNode1.resize).toHaveBeenCalledWith(350, 20);

expect(textNode2.layoutSizingHorizontal).toBe("FIXED");
expect(textNode2.textAutoResize).toBe("HEIGHT");
expect(textNode2.resize).toHaveBeenCalledWith(350, 20);
});

it("recursively processes nested frames", async () => {
const { constrainTextNodesInFrame } = await import("./code");

const nestedText = createMockTextNode();
const nestedFrame = createMockFrameNode({
layoutMode: "VERTICAL",
children: [nestedText as unknown as SceneNode],
});
const outerFrame = createMockFrameNode({
children: [nestedFrame as unknown as SceneNode],
});

constrainTextNodesInFrame(outerFrame, 300);

// Nested frame should be configured
expect(nestedFrame.layoutSizingHorizontal).toBe("FILL");
expect(nestedFrame.layoutSizingVertical).toBe("HUG");

// Text inside nested frame should be constrained
expect(nestedText.layoutSizingHorizontal).toBe("FIXED");
expect(nestedText.textAutoResize).toBe("HEIGHT");
});
});
79 changes: 74 additions & 5 deletions packages/plugin/src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,78 @@ async function renderBlockItemToNode(
}
}

/**
* Constrain a node's width for column layouts, enabling proper text wrapping.
*/
export function constrainNodeToWidth(
node: SceneNode,
targetWidth: number,
): void {
if (node.type === "TEXT") {
// For text nodes in auto-layout: enable wrapping and constrain to column width
const textNode = node as TextNode;
// 1. Set layout sizing to FIXED first (prevents auto-layout from overriding width)
textNode.layoutSizingHorizontal = "FIXED";
// 2. Set text auto-resize to HEIGHT (enables text wrapping with fixed width)
textNode.textAutoResize = "HEIGHT";
// 3. Now resize to target width - text will wrap
textNode.resize(targetWidth, textNode.height);
} else if (node.type === "FRAME") {
// For frame nodes (bullet lists, blockquotes, etc.)
const frameNode = node as FrameNode;
if (frameNode.layoutMode !== "NONE") {
// Auto-layout frame: fill width, hug height, ensure children also fill
frameNode.layoutSizingHorizontal = "FILL";
frameNode.layoutSizingVertical = "HUG";
// Recursively constrain text children for proper wrapping
constrainTextNodesInFrame(frameNode, targetWidth);
} else if (node.width > targetWidth) {
// Non-auto-layout frame, resize maintaining aspect ratio
const aspectRatio = node.height / node.width;
frameNode.resize(targetWidth, targetWidth * aspectRatio);
}
} else if (
"resize" in node &&
typeof (node as { resize?: (w: number, h: number) => void }).resize ===
"function" &&
node.width > targetWidth
) {
// For other nodes (images, etc.), constrain width maintaining aspect ratio
const aspectRatio = node.height / node.width;
(node as { resize: (w: number, h: number) => void }).resize(
targetWidth,
targetWidth * aspectRatio,
);
}
}

/**
* Recursively constrain text nodes within a frame for proper text wrapping.
*/
export function constrainTextNodesInFrame(
frame: FrameNode,
maxWidth: number,
): void {
for (const child of frame.children) {
if (child.type === "TEXT") {
const textNode = child as TextNode;
// 1. Set layout sizing to FIXED first
textNode.layoutSizingHorizontal = "FIXED";
// 2. Set text auto-resize to HEIGHT (enables wrapping)
textNode.textAutoResize = "HEIGHT";
// 3. Resize to max width
textNode.resize(maxWidth, textNode.height);
} else if (child.type === "FRAME") {
const childFrame = child as FrameNode;
if (childFrame.layoutMode !== "NONE") {
childFrame.layoutSizingHorizontal = "FILL";
childFrame.layoutSizingVertical = "HUG";
constrainTextNodesInFrame(childFrame, maxWidth);
}
}
}
}

/**
* Render a columns block as a horizontal auto-layout frame
*/
Expand Down Expand Up @@ -500,11 +572,8 @@ async function renderColumnsBlock(
const node = await renderBlockItemToNode(item, styles);
if (node) {
columnFrame.appendChild(node);
// Constrain node width to column width
if ("resize" in node && node.width > columnWidth) {
const aspectRatio = node.height / node.width;
node.resize(columnWidth, columnWidth * aspectRatio);
}
// Constrain node width to column width with proper text wrapping
constrainNodeToWidth(node, columnWidth);
}
}

Expand Down