mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-21 21:44:59 +03:00
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:
7 files changed
+71
-12
No files matched your search
@@ -49,7 +49,7 @@ const isInsideTransformHandle = (
|
|||||||
export const resizeTest = <Point extends GlobalPoint | LocalPoint>(
|
export const resizeTest = <Point extends GlobalPoint | LocalPoint>(
|
||||||
element: NonDeletedExcalidrawElement,
|
element: NonDeletedExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
appState: AppState,
|
appState: Pick<AppState, "selectedElementIds" | "croppingElementId">,
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
zoom: Zoom,
|
zoom: Zoom,
|
||||||
|
|||||||
@@ -538,6 +538,21 @@ export class AppText {
|
|||||||
return true;
|
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
|
* 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.
|
* 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(
|
const transformHandleType = resizeTest(
|
||||||
element,
|
element,
|
||||||
this.app.scene.getNonDeletedElementsMap(),
|
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.x,
|
||||||
point.y,
|
point.y,
|
||||||
state.zoom,
|
state.zoom,
|
||||||
|
|||||||
@@ -2728,6 +2728,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
editorInterface={this.editorInterface}
|
editorInterface={this.editorInterface}
|
||||||
interactionEnabled={this.isInteractionEnabled()}
|
interactionEnabled={this.isInteractionEnabled()}
|
||||||
navigationEnabled={this.isNavigationEnabled()}
|
navigationEnabled={this.isNavigationEnabled()}
|
||||||
|
editedTextHoveredSide={
|
||||||
|
this.text.editedTextHoveredSide
|
||||||
|
}
|
||||||
renderInteractiveSceneCallback={
|
renderInteractiveSceneCallback={
|
||||||
this.renderInteractiveSceneCallback
|
this.renderInteractiveSceneCallback
|
||||||
}
|
}
|
||||||
@@ -7233,13 +7236,21 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const editedTextResizeHandle = this.text.getEditedTextResizeHandleAt(
|
if (this.state.editingTextElement) {
|
||||||
scenePointer,
|
const editedTextResizeHandle = this.text.getEditedTextResizeHandleAt(
|
||||||
event.pointerType,
|
scenePointer,
|
||||||
);
|
event.pointerType,
|
||||||
if (editedTextResizeHandle) {
|
);
|
||||||
this.cursor.set(getCursorForResizingElement(editedTextResizeHandle));
|
// (a wrapped text shows its whole box as is)
|
||||||
return;
|
this.text.setEditedTextHoveredSide(
|
||||||
|
editedTextResizeHandle?.element.autoResize
|
||||||
|
? editedTextResizeHandle.transformHandleType
|
||||||
|
: null,
|
||||||
|
);
|
||||||
|
if (editedTextResizeHandle) {
|
||||||
|
this.cursor.set(getCursorForResizingElement(editedTextResizeHandle));
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isOverScrollBar) {
|
if (!isOverScrollBar) {
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ type InteractiveCanvasProps = {
|
|||||||
app: AppClassProperties;
|
app: AppClassProperties;
|
||||||
interactionEnabled: boolean;
|
interactionEnabled: boolean;
|
||||||
navigationEnabled: boolean;
|
navigationEnabled: boolean;
|
||||||
|
/** see `AppText.editedTextHoveredSide`: here for the canvas to re-render */
|
||||||
|
editedTextHoveredSide: "e" | "w" | null;
|
||||||
renderInteractiveSceneCallback: (
|
renderInteractiveSceneCallback: (
|
||||||
data: RenderInteractiveSceneCallback,
|
data: RenderInteractiveSceneCallback,
|
||||||
) => void;
|
) => void;
|
||||||
@@ -302,7 +304,8 @@ const areEqual = (
|
|||||||
prevProps.selectedElements !== nextProps.selectedElements ||
|
prevProps.selectedElements !== nextProps.selectedElements ||
|
||||||
prevProps.renderScrollbars !== nextProps.renderScrollbars ||
|
prevProps.renderScrollbars !== nextProps.renderScrollbars ||
|
||||||
prevProps.interactionEnabled !== nextProps.interactionEnabled ||
|
prevProps.interactionEnabled !== nextProps.interactionEnabled ||
|
||||||
prevProps.navigationEnabled !== nextProps.navigationEnabled
|
prevProps.navigationEnabled !== nextProps.navigationEnabled ||
|
||||||
|
prevProps.editedTextHoveredSide !== nextProps.editedTextHoveredSide
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1478,6 +1478,8 @@ const renderTextBox = (
|
|||||||
context: CanvasRenderingContext2D,
|
context: CanvasRenderingContext2D,
|
||||||
appState: InteractiveCanvasAppState,
|
appState: InteractiveCanvasAppState,
|
||||||
selectionColor: InteractiveCanvasRenderConfig["selectionColor"],
|
selectionColor: InteractiveCanvasRenderConfig["selectionColor"],
|
||||||
|
/** the box's left ("w") or right ("e") side alone */
|
||||||
|
side?: "e" | "w",
|
||||||
) => {
|
) => {
|
||||||
context.save();
|
context.save();
|
||||||
const padding = getTextBoxPadding(appState.zoom.value);
|
const padding = getTextBoxPadding(appState.zoom.value);
|
||||||
@@ -1493,7 +1495,15 @@ const renderTextBox = (
|
|||||||
context.strokeStyle = selectionColor;
|
context.strokeStyle = selectionColor;
|
||||||
context.globalAlpha = 0.5;
|
context.globalAlpha = 0.5;
|
||||||
context.setLineDash([6 / appState.zoom.value, 4 / appState.zoom.value]);
|
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();
|
context.restore();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1713,12 +1723,20 @@ const _renderInteractiveScene = ({
|
|||||||
const textElement = allElementsMap.get(appState.editingTextElement.id) as
|
const textElement = allElementsMap.get(appState.editingTextElement.id) as
|
||||||
| ExcalidrawTextElement
|
| ExcalidrawTextElement
|
||||||
| undefined;
|
| 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(
|
renderTextBox(
|
||||||
textElement,
|
textElement,
|
||||||
context,
|
context,
|
||||||
appState,
|
appState,
|
||||||
renderConfig.selectionColor,
|
renderConfig.selectionColor,
|
||||||
|
textElement.autoResize
|
||||||
|
? app.text.editedTextHoveredSide ?? undefined
|
||||||
|
: undefined,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3742,6 +3742,13 @@ describe("textWysiwyg", () => {
|
|||||||
const { width, height } = text;
|
const { width, height } = text;
|
||||||
const x = text.x + text.width + 4;
|
const x = text.x + text.width + 4;
|
||||||
const y = text.y + text.height / 2;
|
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.downAt(x, y);
|
||||||
mouse.moveTo(x - 40, y);
|
mouse.moveTo(x - 40, y);
|
||||||
mouse.moveTo(x - 80, y);
|
mouse.moveTo(x - 80, y);
|
||||||
|
|||||||
@@ -1205,8 +1205,10 @@ export const textWysiwyg = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const cleanup = () => {
|
const cleanup = () => {
|
||||||
|
app.text.setEditedTextHoveredSide(null);
|
||||||
// remove events to ensure they don't late-fire
|
// remove events to ensure they don't late-fire
|
||||||
editable.onblur = null;
|
editable.onblur = null;
|
||||||
|
editable.onpointerenter = null;
|
||||||
editable.oninput = null;
|
editable.oninput = null;
|
||||||
editable.onkeydown = null;
|
editable.onkeydown = null;
|
||||||
|
|
||||||
@@ -1449,6 +1451,8 @@ export const textWysiwyg = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
editable.onpointerdown = (event) => event.stopPropagation();
|
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
|
// rAF (+ capture to by doubly sure) so we don't catch te pointerdown that
|
||||||
// triggered the wysiwyg
|
// triggered the wysiwyg
|
||||||
|
|||||||
Reference in new issue
Block a user