diff --git a/bun.lock b/bun.lock index dee4f53..ce7a721 100644 --- a/bun.lock +++ b/bun.lock @@ -10,7 +10,7 @@ }, "packages/cli": { "name": "figdeck", - "version": "1.4.0", + "version": "1.5.0", "bin": { "figdeck": "./bin/figdeck.js", }, @@ -45,7 +45,7 @@ }, "packages/plugin": { "name": "@figdeck/plugin", - "version": "1.4.0", + "version": "1.5.0", "dependencies": { "@figdeck/shared": "workspace:*", "highlight.js": "^11.11.1", @@ -66,12 +66,11 @@ }, "packages/vscode": { "name": "figdeck-vscode", - "version": "0.1.0", + "version": "1.5.0", "dependencies": { "yaml": "^2.8.1", }, "devDependencies": { - "@figdeck/shared": "workspace:*", "@types/bun": "^1.3.3", "@types/node": "^22.10.0", "@types/vscode": "^1.85.0", diff --git a/examples/columns.md b/examples/columns.md index ec4fbc9..7f952d7 100644 --- a/examples/columns.md +++ b/examples/columns.md @@ -16,7 +16,7 @@ Demonstration of figdeck's column layout feature. :::column ### Left Column -- Feature A +- Feature A - Feature B - Feature C diff --git a/packages/plugin/src/code.test.ts b/packages/plugin/src/code.test.ts index 37c4dd3..64b7e16 100644 --- a/packages/plugin/src/code.test.ts +++ b/packages/plugin/src/code.test.ts @@ -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 { + 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 & { 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__ = ""; @@ -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"); + }); +}); diff --git a/packages/plugin/src/code.ts b/packages/plugin/src/code.ts index 2c08c6b..4753bd4 100644 --- a/packages/plugin/src/code.ts +++ b/packages/plugin/src/code.ts @@ -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 */ @@ -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); } }