From baf95e5ce4ca9ceb232a6fabe8f08e20e2bbcb29 Mon Sep 17 00:00:00 2001 From: dwelle <5153846+dwelle@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:03:47 +0100 Subject: [PATCH] feat(editor): show the side of an edited text that can be dragged to wrap it A text that doesn't wrap yet has no box showing its sides while it's edited: the side under the pointer is now drawn (dashed, as the box of a wrapped text), along with the resize cursor. --- packages/element/src/resizeTest.ts | 2 +- packages/excalidraw/components/App.text.ts | 18 ++++++++++++- packages/excalidraw/components/App.tsx | 25 +++++++++++++------ .../components/canvases/InteractiveCanvas.tsx | 5 +++- .../excalidraw/renderer/interactiveScene.ts | 22 ++++++++++++++-- .../excalidraw/wysiwyg/textWysiwyg.test.tsx | 7 ++++++ packages/excalidraw/wysiwyg/textWysiwyg.tsx | 4 +++ 7 files changed, 71 insertions(+), 12 deletions(-) diff --git a/packages/element/src/resizeTest.ts b/packages/element/src/resizeTest.ts index a124707d28..de425bc3ab 100644 --- a/packages/element/src/resizeTest.ts +++ b/packages/element/src/resizeTest.ts @@ -49,7 +49,7 @@ const isInsideTransformHandle = ( export const resizeTest = ( element: NonDeletedExcalidrawElement, elementsMap: ElementsMap, - appState: AppState, + appState: Pick, x: number, y: number, zoom: Zoom, diff --git a/packages/excalidraw/components/App.text.ts b/packages/excalidraw/components/App.text.ts index e78b846d86..f90122349a 100644 --- a/packages/excalidraw/components/App.text.ts +++ b/packages/excalidraw/components/App.text.ts @@ -538,6 +538,21 @@ export class AppText { return true; }; + /** + * The side handle of the text being edited under the pointer (see + * `getEditedTextResizeHandleAt()`), drawn as the affordance for dragging + * it: a text that doesn't wrap yet has no box showing its sides. Not app + * state — the interactive canvas takes it as a prop. + */ + public editedTextHoveredSide: "e" | "w" | null = null; + + public setEditedTextHoveredSide = (side: "e" | "w" | null) => { + if (this.editedTextHoveredSide !== side) { + this.editedTextHoveredSide = side; + this.app.triggerRender(); + } + }; + /** * The side handle, at a scene point, of the free text being edited: dragging * it sets the width the text wraps at, as it does for a selected text. @@ -564,7 +579,8 @@ export class AppText { const transformHandleType = resizeTest( element, this.app.scene.getNonDeletedElementsMap(), - { ...state, selectedElementIds: { [element.id]: true } }, + // (the edited text isn't necessarily selected) + { selectedElementIds: { [element.id]: true }, croppingElementId: null }, point.x, point.y, state.zoom, diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index b16f28a2b8..b48cf227fe 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -2728,6 +2728,9 @@ class App extends React.Component { editorInterface={this.editorInterface} interactionEnabled={this.isInteractionEnabled()} navigationEnabled={this.isNavigationEnabled()} + editedTextHoveredSide={ + this.text.editedTextHoveredSide + } renderInteractiveSceneCallback={ this.renderInteractiveSceneCallback } @@ -7233,13 +7236,21 @@ class App extends React.Component { return; } - const editedTextResizeHandle = this.text.getEditedTextResizeHandleAt( - scenePointer, - event.pointerType, - ); - if (editedTextResizeHandle) { - this.cursor.set(getCursorForResizingElement(editedTextResizeHandle)); - return; + if (this.state.editingTextElement) { + const editedTextResizeHandle = this.text.getEditedTextResizeHandleAt( + scenePointer, + event.pointerType, + ); + // (a wrapped text shows its whole box as is) + this.text.setEditedTextHoveredSide( + editedTextResizeHandle?.element.autoResize + ? editedTextResizeHandle.transformHandleType + : null, + ); + if (editedTextResizeHandle) { + this.cursor.set(getCursorForResizingElement(editedTextResizeHandle)); + return; + } } if (!isOverScrollBar) { diff --git a/packages/excalidraw/components/canvases/InteractiveCanvas.tsx b/packages/excalidraw/components/canvases/InteractiveCanvas.tsx index c5e656221f..0d4fc23417 100644 --- a/packages/excalidraw/components/canvases/InteractiveCanvas.tsx +++ b/packages/excalidraw/components/canvases/InteractiveCanvas.tsx @@ -48,6 +48,8 @@ type InteractiveCanvasProps = { app: AppClassProperties; interactionEnabled: boolean; navigationEnabled: boolean; + /** see `AppText.editedTextHoveredSide`: here for the canvas to re-render */ + editedTextHoveredSide: "e" | "w" | null; renderInteractiveSceneCallback: ( data: RenderInteractiveSceneCallback, ) => void; @@ -302,7 +304,8 @@ const areEqual = ( prevProps.selectedElements !== nextProps.selectedElements || prevProps.renderScrollbars !== nextProps.renderScrollbars || prevProps.interactionEnabled !== nextProps.interactionEnabled || - prevProps.navigationEnabled !== nextProps.navigationEnabled + prevProps.navigationEnabled !== nextProps.navigationEnabled || + prevProps.editedTextHoveredSide !== nextProps.editedTextHoveredSide ) { return false; } diff --git a/packages/excalidraw/renderer/interactiveScene.ts b/packages/excalidraw/renderer/interactiveScene.ts index 10ea01c786..dd21d11361 100644 --- a/packages/excalidraw/renderer/interactiveScene.ts +++ b/packages/excalidraw/renderer/interactiveScene.ts @@ -1478,6 +1478,8 @@ const renderTextBox = ( context: CanvasRenderingContext2D, appState: InteractiveCanvasAppState, selectionColor: InteractiveCanvasRenderConfig["selectionColor"], + /** the box's left ("w") or right ("e") side alone */ + side?: "e" | "w", ) => { context.save(); const padding = getTextBoxPadding(appState.zoom.value); @@ -1493,7 +1495,15 @@ const renderTextBox = ( context.strokeStyle = selectionColor; context.globalAlpha = 0.5; context.setLineDash([6 / appState.zoom.value, 4 / appState.zoom.value]); - context.strokeRect(shiftX, shiftY, width, height); + if (side) { + const x = side === "w" ? shiftX : shiftX + width; + context.beginPath(); + context.moveTo(x, shiftY); + context.lineTo(x, shiftY + height); + context.stroke(); + } else { + context.strokeRect(shiftX, shiftY, width, height); + } context.restore(); }; @@ -1713,12 +1723,20 @@ const _renderInteractiveScene = ({ const textElement = allElementsMap.get(appState.editingTextElement.id) as | ExcalidrawTextElement | undefined; - if (textElement && !textElement.autoResize) { + // a wrapped text gets its box; one that grows with its content, the + // side under the pointer, which wraps it when dragged + if ( + textElement && + (!textElement.autoResize || app.text.editedTextHoveredSide) + ) { renderTextBox( textElement, context, appState, renderConfig.selectionColor, + textElement.autoResize + ? app.text.editedTextHoveredSide ?? undefined + : undefined, ); } } diff --git a/packages/excalidraw/wysiwyg/textWysiwyg.test.tsx b/packages/excalidraw/wysiwyg/textWysiwyg.test.tsx index 97f84a8d25..bc5b40a77f 100644 --- a/packages/excalidraw/wysiwyg/textWysiwyg.test.tsx +++ b/packages/excalidraw/wysiwyg/textWysiwyg.test.tsx @@ -3742,6 +3742,13 @@ describe("textWysiwyg", () => { const { width, height } = text; const x = text.x + text.width + 4; const y = text.y + text.height / 2; + + // the side under the pointer is the one drawn, to show it can be dragged + mouse.moveTo(x, y); + expect(h.app.text.editedTextHoveredSide).toBe("e"); + mouse.moveTo(x + 40, y); + expect(h.app.text.editedTextHoveredSide).toBe(null); + mouse.downAt(x, y); mouse.moveTo(x - 40, y); mouse.moveTo(x - 80, y); diff --git a/packages/excalidraw/wysiwyg/textWysiwyg.tsx b/packages/excalidraw/wysiwyg/textWysiwyg.tsx index 07ce395e7f..96f5e66715 100644 --- a/packages/excalidraw/wysiwyg/textWysiwyg.tsx +++ b/packages/excalidraw/wysiwyg/textWysiwyg.tsx @@ -1205,8 +1205,10 @@ export const textWysiwyg = ({ }; const cleanup = () => { + app.text.setEditedTextHoveredSide(null); // remove events to ensure they don't late-fire editable.onblur = null; + editable.onpointerenter = null; editable.oninput = null; editable.onkeydown = null; @@ -1449,6 +1451,8 @@ export const textWysiwyg = ({ } editable.onpointerdown = (event) => event.stopPropagation(); + // (the pointer left the canvas, and with it the text's side handles) + editable.onpointerenter = () => app.text.setEditedTextHoveredSide(null); // rAF (+ capture to by doubly sure) so we don't catch te pointerdown that // triggered the wysiwyg