AI-native design editor. Open-source Figma alternative.
fix(text): preserve auto-height bounds after editing
Danila Poyarkov2f81a7407a09
diff --git a/packages/core/src/editor/text.ts b/packages/core/src/editor/text.tsindex d4a59777f..175941e85 100644--- a/packages/core/src/editor/text.ts+++ b/packages/core/src/editor/text.ts@@ -1,5 +1,6 @@import {createTextEditSession,+ resizeTextNodeForEdit,snapshotTextNode,textSnapshotChanged,type TextEditSession@@ -30,6 +31,7 @@ export function createTextActions(ctx: EditorContext) {activeSession = nullreturn}+ const paragraph = te.state?.paragraph ?? nullconst result = te.stop()if (!result) {ctx.state.editingTextId = null@@ -37,11 +39,17 @@ export function createTextActions(ctx: EditorContext) {ctx.requestRender()return}- const before = activeSession?.before ?? { text: '', styleRuns: [] }+ const before = activeSession?.before ?? { text: '', styleRuns: [], size: {} }const node = ctx.graph.getNode(result.nodeId)const after = snapshotTextNode(node, result.text)after.text = result.text- ctx.graph.updateNode(result.nodeId, { text: after.text, styleRuns: after.styleRuns })+ const sizeChanges = before.text !== after.text ? resizeTextNodeForEdit(node, paragraph) : {}+ if (Object.keys(sizeChanges).length > 0) after.size = sizeChanges+ ctx.graph.updateNode(result.nodeId, {+ text: after.text,+ styleRuns: after.styleRuns,+ ...sizeChanges+ })ctx.state.editingTextId = nullactiveSession = null@@ -49,10 +57,18 @@ export function createTextActions(ctx: EditorContext) {ctx.undo.push({label: 'Edit text',forward: () => {- ctx.graph.updateNode(result.nodeId, { text: after.text, styleRuns: after.styleRuns })+ ctx.graph.updateNode(result.nodeId, {+ text: after.text,+ styleRuns: after.styleRuns,+ ...after.size+ })},inverse: () => {- ctx.graph.updateNode(result.nodeId, { text: before.text, styleRuns: before.styleRuns })+ ctx.graph.updateNode(result.nodeId, {+ text: before.text,+ styleRuns: before.styleRuns,+ ...before.size+ })}})}diff --git a/packages/core/src/editor/text/session.ts b/packages/core/src/editor/text/session.tsindex 2cc602bb5..70bc71a1b 100644--- a/packages/core/src/editor/text/session.ts+++ b/packages/core/src/editor/text/session.ts@@ -1,9 +1,14 @@+import type { Paragraph } from 'canvaskit-wasm'+import type { SceneNode, StyleRun } from '#core/scene-graph'import { copyStyleRuns } from '#core/scene-graph/copy'+export type TextEditSizeSnapshot = Partial<Pick<SceneNode, 'width' | 'height'>>+export type TextEditSnapshot = {text: stringstyleRuns: StyleRun[]+ size?: TextEditSizeSnapshot}export type TextEditSession = {@@ -16,7 +21,8 @@ export function createTextEditSession(node: SceneNode): TextEditSession {nodeId: node.id,before: {text: node.text,- styleRuns: copyStyleRuns(node.styleRuns)+ styleRuns: copyStyleRuns(node.styleRuns),+ size: { width: node.width, height: node.height }}}}@@ -24,12 +30,38 @@ export function createTextEditSession(node: SceneNode): TextEditSession {export function snapshotTextNode(node: SceneNode | undefined, fallbackText = ''): TextEditSnapshot {return {text: node?.text ?? fallbackText,- styleRuns: node ? copyStyleRuns(node.styleRuns) : []+ styleRuns: node ? copyStyleRuns(node.styleRuns) : [],+ size: node ? { width: node.width, height: node.height } : undefined+ }+}++export function resizeTextNodeForEdit(+ node: SceneNode | undefined,+ paragraph: Paragraph | null+): TextEditSizeSnapshot {+ if (!node || !paragraph) return {}+ const changes: TextEditSizeSnapshot = {}+ if (node.textAutoResize === 'WIDTH_AND_HEIGHT') {+ const width = Math.ceil(paragraph.getLongestLine())+ if (width > 0 && width !== node.width) changes.width = width}+ if (node.textAutoResize === 'HEIGHT' || node.textAutoResize === 'WIDTH_AND_HEIGHT') {+ const height = Math.ceil(paragraph.getHeight())+ if (height > 0 && height !== node.height) changes.height = height+ }+ return changes}export function textSnapshotChanged(before: TextEditSnapshot, after: TextEditSnapshot): boolean {- return before.text !== after.text || !styleRunsEqual(before.styleRuns, after.styleRuns)+ return (+ before.text !== after.text ||+ !styleRunsEqual(before.styleRuns, after.styleRuns) ||+ (after.size !== undefined && !sizeEqual(before.size ?? {}, after.size))+ )+}++function sizeEqual(a: TextEditSizeSnapshot, b: TextEditSizeSnapshot): boolean {+ return a.width === b.width && a.height === b.height}function styleRunsEqual(a: StyleRun[], b: StyleRun[]): boolean {diff --git a/tests/engine/text/edit-undo.test.ts b/tests/engine/text/edit-undo.test.tsindex ad82265b7..384ab5492 100644--- a/tests/engine/text/edit-undo.test.ts+++ b/tests/engine/text/edit-undo.test.ts@@ -53,6 +53,14 @@ function setup() {return { graph, undo, textEditor, state, textNode, actions }}+function paragraphWithHeight(height: number) {+ return {+ getHeight: () => height,+ getLongestLine: () => 0,+ delete: () => undefined+ }+}+describe('text edit undo', () => {test('commitTextEdit pushes undo entry when text changed', () => {const { graph, undo, textEditor, textNode, actions } = setup()@@ -78,6 +86,24 @@ describe('text edit undo', () => {expect(getNodeOrThrow(graph, textNode.id).text).toBe('Hello World')})+ test('commitTextEdit preserves auto-height text bounds', () => {+ const { graph, undo, textEditor, textNode, actions } = setup()+ graph.updateNode(textNode.id, { textAutoResize: 'HEIGHT', height: 18 })++ actions.startTextEditing(textNode.id)+ const state = expectDefined(textEditor.state, 'text editor state')+ state.paragraph = paragraphWithHeight(42) as NonNullable<typeof state.paragraph>+ textEditor.insert(' World', getNodeOrThrow(graph, textNode.id))++ actions.commitTextEdit()++ expect(getNodeOrThrow(graph, textNode.id).height).toBe(42)+ undo.undo()+ expect(getNodeOrThrow(graph, textNode.id).height).toBe(18)+ undo.redo()+ expect(getNodeOrThrow(graph, textNode.id).height).toBe(42)+ })+test('commitTextEdit does not push undo when text unchanged', () => {const { undo, actions, textNode } = setup()