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.
This commit is contained in:
dwelle committed 2026-11-15 11:03:47 +01:00
1 parent 1beab51331
commit baf95e5ce4
7 files changed
+71 -12

No files matched your search

+1 -1
View File
@@ -49,7 +49,7 @@ const isInsideTransformHandle = (
export const resizeTest = <Point extends GlobalPoint | LocalPoint>(
element: NonDeletedExcalidrawElement,
elementsMap: ElementsMap,
appState: AppState,
appState: Pick<AppState, "selectedElementIds" | "croppingElementId">,
x: number,
y: number,
zoom: Zoom,
+17 -1
View File
@@ -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,
+18 -7
View File
@@ -2728,6 +2728,9 @@ class App extends React.Component<AppProps, AppState> {
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<AppProps, AppState> {
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) {
@@ -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;
}
@@ -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,
);
}
}
@@ -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);
@@ -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