feat: Support labels for arrow 🔥 (#5723)

* feat: support arrow with text

* render arrow -> clear rect-> render text

* move bound text when linear elements move

* fix centering cursor when linear element rotated

* fix y coord when new line added and container has 3 points

* update text position when 2nd point moved

* support adding label on top of 2nd point when 3 points are present

* change linear element editor shortcut to cmd+enter and fix tests

* scale bound text points when resizing via bounding box

* ohh yeah rotation works :)

* fix coords when updating text properties

* calculate new position after rotation always from original position

* rotate the bound text by same angle as parent

* don't rotate text and make sure dimensions and coords are always calculated from original point

* hardcoding the text width for now

* Move the linear element when bound text hit

* Rotation working yaay

* consider text element angle when editing

* refactor

* update x2 coords if needed when text updated

* simplify

* consider bound text to be part of bounding box when hit

* show bounding box correctly when multiple element selected

* fix typo

* support rotating multiple elements

* support multiple element resizing

* shift bound text to mid point when odd points

* Always render linear element handles inside editor after element rendered so point is visible for bound text

* Delete bound text when point attached to it deleted

* move bound to mid segement mid point when points are even

* shift bound text when points nearby deleted and handle segment deletion

* Resize working :)

* more resize fixes

* don't update cache-its breaking delete points, look for better soln

* update mid point cache for bound elements when updated

* introduce wrapping when resizing

* wrap when resize for 2 pointer linear elements

* support adding text for linear elements with more than 3 points

* export to svg  working :)

* clip from nearest enclosing element with non transparent color if present when exporting and fill with correct color in canvas

* fix snap

* use visible elements

* Make export to svg work with Mask :)

* remove id

* mask canvas linear element area where label is added

* decide the position of bound text during render

* fix coords when editing

* fix multiple resize

* update cache when bound text version changes

* fix masking when rotated

* render text in correct position in preview

* remove unnecessary code

* fix masking when rotating linear element

* fix masking with zoom

* fix mask in preview for export

* fix offsets in export view

* fix coords on svg export

* fix mask when element rotated in svg

* enable double-click to enter text

* fix hint

* Position cursor correctly and text dimensiosn when height of element is negative

* don't allow 2 pointer linear element with bound text width to go beyond min width

* code cleanup

* fix freedraw

* Add padding

* don't show vertical align action for linear element containers

* Add specs for getBoundTextElementPosition

* more specs

* move some utils to linearElementEditor.ts

* remove only :p

* check absoulte coods in test

* Add test to hide vertical align for linear eleemnt with bound text

* improve export preview

* support labels only for arrows

* spec

* fix large texts

* fix tests

* fix zooming

* enter line editor with cmd+double click

* Allow points to move beyond min width/height for 2 pointer arrow with bound text

* fix hint for line editing

* attempt to fix arrow getting deselected

* fix hint and shortcut

* Add padding of 5px when creating bound text and add spec

* Wrap bound text when arrow binding containers moved

* Add spec

* remove

* set boundTextElementVersion to null if not present

* dont use cache when version mismatch

* Add a padding of 5px vertically when creating text

* Add box sizing content box

* Set bound elements when text element created to fix the padding

* fix zooming in editor

* fix zoom in export

* remove globalCompositeOperation and use clearRect instead of fillRect
This commit is contained in:
Aakansha Doshi 2022-12-05 21:03:13 +05:30 committed by GitHub
parent 1933116261
commit 760fd7b3a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 1668 additions and 363 deletions

View file

@ -25,11 +25,12 @@ import Stack from "./Stack";
import { ToolButton } from "./ToolButton";
import { hasStrokeColor } from "../scene/comparisons";
import { trackEvent } from "../analytics";
import { hasBoundTextElement, isBoundToContainer } from "../element/typeChecks";
import { hasBoundTextElement } from "../element/typeChecks";
import clsx from "clsx";
import { actionToggleZenMode } from "../actions";
import "./Actions.scss";
import { Tooltip } from "./Tooltip";
import { shouldAllowVerticalAlign } from "../element/textElement";
export const SelectedShapeActions = ({
appState,
@ -125,10 +126,8 @@ export const SelectedShapeActions = ({
</>
)}
{targetElements.some(
(element) =>
hasBoundTextElement(element) || isBoundToContainer(element),
) && renderAction("changeVerticalAlign")}
{shouldAllowVerticalAlign(targetElements) &&
renderAction("changeVerticalAlign")}
{(canHaveArrowheads(appState.activeTool.type) ||
targetElements.some((element) => canHaveArrowheads(element.type))) && (
<>{renderAction("changeArrowhead")}</>

View file

@ -126,6 +126,7 @@ import { mutateElement, newElementWith } from "../element/mutateElement";
import { deepCopyElement, newFreeDrawElement } from "../element/newElement";
import {
hasBoundTextElement,
isArrowElement,
isBindingElement,
isBindingElementType,
isBoundToContainer,
@ -254,6 +255,7 @@ import {
getApproxMinLineHeight,
getApproxMinLineWidth,
getBoundTextElement,
getContainerCenter,
getContainerDims,
getTextBindableContainerAtPosition,
isValidTextContainer,
@ -2049,23 +2051,23 @@ class App extends React.Component<AppProps, AppState> {
this.scene.getNonDeletedElements(),
this.state,
);
if (selectedElements.length === 1) {
const selectedElement = selectedElements[0];
if (isLinearElement(selectedElement)) {
if (
!this.state.editingLinearElement ||
this.state.editingLinearElement.elementId !==
selectedElements[0].id
) {
this.history.resumeRecording();
this.setState({
editingLinearElement: new LinearElementEditor(
selectedElement,
this.scene,
),
});
if (event[KEYS.CTRL_OR_CMD]) {
if (isLinearElement(selectedElement)) {
if (
!this.state.editingLinearElement ||
this.state.editingLinearElement.elementId !==
selectedElements[0].id
) {
this.history.resumeRecording();
this.setState({
editingLinearElement: new LinearElementEditor(
selectedElement,
this.scene,
),
});
}
}
} else if (
isTextElement(selectedElement) ||
@ -2075,9 +2077,12 @@ class App extends React.Component<AppProps, AppState> {
if (!isTextElement(selectedElement)) {
container = selectedElement as ExcalidrawTextContainer;
}
const midPoint = getContainerCenter(selectedElement, this.state);
const sceneX = midPoint.x;
const sceneY = midPoint.y;
this.startTextEditing({
sceneX: selectedElement.x + selectedElement.width / 2,
sceneY: selectedElement.y + selectedElement.height / 2,
sceneX,
sceneY,
container,
});
event.preventDefault();
@ -2521,7 +2526,12 @@ class App extends React.Component<AppProps, AppState> {
existingTextElement = this.getTextElementAtPosition(sceneX, sceneY);
}
if (!existingTextElement && shouldBindToContainer && container) {
if (
!existingTextElement &&
shouldBindToContainer &&
container &&
!isArrowElement(container)
) {
const fontString = {
fontSize: this.state.currentItemFontSize,
fontFamily: this.state.currentItemFontFamily,
@ -2574,6 +2584,14 @@ class App extends React.Component<AppProps, AppState> {
locked: false,
});
if (!existingTextElement && shouldBindToContainer && container) {
mutateElement(container, {
boundElements: (container.boundElements || []).concat({
type: "text",
id: element.id,
}),
});
}
this.setState({ editingElement: element });
if (!existingTextElement) {
@ -2625,8 +2643,9 @@ class App extends React.Component<AppProps, AppState> {
if (selectedElements.length === 1 && isLinearElement(selectedElements[0])) {
if (
!this.state.editingLinearElement ||
this.state.editingLinearElement.elementId !== selectedElements[0].id
event[KEYS.CTRL_OR_CMD] &&
(!this.state.editingLinearElement ||
this.state.editingLinearElement.elementId !== selectedElements[0].id)
) {
this.history.resumeRecording();
this.setState({
@ -2635,8 +2654,13 @@ class App extends React.Component<AppProps, AppState> {
this.scene,
),
});
return;
} else if (
this.state.editingLinearElement &&
this.state.editingLinearElement.elementId === selectedElements[0].id
) {
return;
}
return;
}
resetCursor(this.canvas);
@ -2680,9 +2704,11 @@ class App extends React.Component<AppProps, AppState> {
sceneY,
);
if (container) {
if (hasBoundTextElement(container)) {
sceneX = container.x + container.width / 2;
sceneY = container.y + container.height / 2;
if (isArrowElement(container) || hasBoundTextElement(container)) {
const midPoint = getContainerCenter(container, this.state);
sceneX = midPoint.x;
sceneY = midPoint.y;
}
}
this.startTextEditing({
@ -2783,6 +2809,7 @@ class App extends React.Component<AppProps, AppState> {
event: React.PointerEvent<HTMLCanvasElement>,
) => {
this.savePointer(event.clientX, event.clientY, this.state.cursorButton);
if (gesture.pointers.has(event.pointerId)) {
gesture.pointers.set(event.pointerId, {
x: event.clientX,
@ -3091,15 +3118,18 @@ class App extends React.Component<AppProps, AppState> {
);
} else if (
// if using cmd/ctrl, we're not dragging
!event[KEYS.CTRL_OR_CMD] &&
(hitElement ||
this.isHittingCommonBoundingBoxOfSelectedElements(
scenePointer,
selectedElements,
)) &&
!hitElement?.locked
!event[KEYS.CTRL_OR_CMD]
) {
setCursor(this.canvas, CURSOR_TYPE.MOVE);
if (
(hitElement ||
this.isHittingCommonBoundingBoxOfSelectedElements(
scenePointer,
selectedElements,
)) &&
!hitElement?.locked
) {
setCursor(this.canvas, CURSOR_TYPE.MOVE);
}
} else {
setCursor(this.canvas, CURSOR_TYPE.AUTO);
}
@ -3209,6 +3239,8 @@ class App extends React.Component<AppProps, AppState> {
linearElementEditor.elementId,
);
const boundTextElement = getBoundTextElement(element);
if (!element) {
return;
}
@ -3249,6 +3281,11 @@ class App extends React.Component<AppProps, AppState> {
)
) {
setCursor(this.canvas, CURSOR_TYPE.MOVE);
} else if (
boundTextElement &&
hitTest(boundTextElement, this.state, scenePointerX, scenePointerY)
) {
setCursor(this.canvas, CURSOR_TYPE.MOVE);
}
if (
@ -6305,8 +6342,14 @@ class App extends React.Component<AppProps, AppState> {
container?: ExcalidrawTextContainer | null,
) {
if (container) {
const elementCenterX = container.x + container.width / 2;
const elementCenterY = container.y + container.height / 2;
let elementCenterX = container.x + container.width / 2;
let elementCenterY = container.y + container.height / 2;
const elementCenter = getContainerCenter(container, appState);
if (elementCenter) {
elementCenterX = elementCenter.x;
elementCenterY = elementCenter.y;
}
const distanceToCenter = Math.hypot(
x - elementCenterX,
y - elementCenterY,

View file

@ -157,7 +157,10 @@ export const HelpDialog = ({ onClose }: { onClose?: () => void }) => {
/>
<Shortcut
label={t("helpDialog.editSelectedShape")}
shortcuts={[getShortcutKey("Enter"), t("helpDialog.doubleClick")]}
shortcuts={[
getShortcutKey("CtrlOrCmd+Enter"),
getShortcutKey(`CtrlOrCmd + ${t("helpDialog.doubleClick")}`),
]}
/>
<Shortcut
label={t("helpDialog.textNewLine")}