98b5c37e45
* feat: Word wrap inside rect and increase height when size exceeded
* fixes for auto increase in height
* fix height
* respect newlines when wrapping text
* shift text area when height increases beyond mid rect height until it reaches to the top
* select bound text if present when rect selected
* mutate y coord after text submit
* Add padding of 30px and update dimensions acordingly
* Don't allow selecting bound text element directly
* support deletion of bound text element when rect deleted
* trim text
* Support autoshrink and improve algo
* calculate approx line height instead of hardcoding
* use textContainerId instead of storing textContainer element itself
* rename boundTextElement -> boundTextElementId
* fix text properties not getting reflected after edit inside rect
* Support resizing
* remove ts ignore
* increase height of container when text height increases while resizing
* use original text when editing/resizing so it adjusts based on original text
* fix tests
* add util isRectangleElement
* use isTextElement util everywhere
* disable selecting text inside rect when selectAll
* Bind text to circle and diamond as well
* fix tests
* vertically center align the text always
* better vertical align
* Disable binding arrows for text inside shapes
* set min width for text container when text is binded to container
* update dimensions of container if its less than min width/ min height
* Allow selecting of text container for transparent containers when clicked inside
* fix test
* preserve whitespaces between long word exceeding width and next word
Use word break instead of whitespace no wrap for better readability and support safari
* Perf improvements for measuring text width and resizing
* Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times
* Rewrite wrapText algo to break in words appropriately and for longer words
calculate the char width in order unless max width reached. This makes the
the number of runs linear (max text length times) which was earlier
textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run
* Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line
* use console.info so eslint doesnt warn :p
* cache char width and don't call resize unless min width exceeded
* update line height and height correctly when text properties inside container updated
* improve vertical centering when text properties updated, not yet perfect though
* when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed
* Add hint when container selected
* Select container when escape key is pressed after submitting text
* fix copy/paste when using copy/paste action
* fix copy when dragged with alt pressed
* fix export to svg/png
* fix add to library
* Fix copy as png/svg
* Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec
* fix rotation jump
* moove all text utils to textElement.ts
* resize text element only after container resized so that width doesnt change when editing
* insert the remaining chars for long words once it goes beyond line
* fix typo, use string for character type
* renaming
* fix bugs in word wrap algo
* make grouping work
* set boundTextElementId only when text present else unset it
* rename textContainerId to containerId
* fix
* fix snap
* use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated
* use boundElementIds and also support binding text in images 🎉
* fix the sw/se ends when resizing from ne/nw
* fix y coord when resizing from north
* bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text
* bind when clicked on center of container
* use pre-wrap instead of normal so it works in ff
* use container boundTextElement when container present and trying to edit text
* review fixes
* make getBoundTextElementId type safe and check for existence when using this function
* fix
* don't duplicate boundElementIds when text submitted
* only remove last trailing space if present which we have added when joining words
* set width correctly when resizing to fix alignment issues
* make duplication work using cmd/ctrl+d
* set X coord correctly during resize
* don't allow resize to negative dimensions when text is bounded to container
* fix, check last char is space
* remove logs
* make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows
* add a util isTextBindableContainer to check if the container could bind text
168 lines
4.9 KiB
TypeScript
168 lines
4.9 KiB
TypeScript
import { Point } from "../types";
|
|
import { FONT_FAMILY, THEME } from "../constants";
|
|
|
|
export type ChartType = "bar" | "line";
|
|
export type FillStyle = "hachure" | "cross-hatch" | "solid";
|
|
export type FontFamilyKeys = keyof typeof FONT_FAMILY;
|
|
export type FontFamilyValues = typeof FONT_FAMILY[FontFamilyKeys];
|
|
export type Theme = typeof THEME[keyof typeof THEME];
|
|
export type FontString = string & { _brand: "fontString" };
|
|
export type GroupId = string;
|
|
export type PointerType = "mouse" | "pen" | "touch";
|
|
export type StrokeSharpness = "round" | "sharp";
|
|
export type StrokeStyle = "solid" | "dashed" | "dotted";
|
|
export type TextAlign = "left" | "center" | "right";
|
|
export type VerticalAlign = "top" | "middle";
|
|
|
|
type _ExcalidrawElementBase = Readonly<{
|
|
id: string;
|
|
x: number;
|
|
y: number;
|
|
strokeColor: string;
|
|
backgroundColor: string;
|
|
fillStyle: FillStyle;
|
|
strokeWidth: number;
|
|
strokeStyle: StrokeStyle;
|
|
strokeSharpness: StrokeSharpness;
|
|
roughness: number;
|
|
opacity: number;
|
|
width: number;
|
|
height: number;
|
|
angle: number;
|
|
/** Random integer used to seed shape generation so that the roughjs shape
|
|
doesn't differ across renders. */
|
|
seed: number;
|
|
/** Integer that is sequentially incremented on each change. Used to reconcile
|
|
elements during collaboration or when saving to server. */
|
|
version: number;
|
|
/** Random integer that is regenerated on each change.
|
|
Used for deterministic reconciliation of updates during collaboration,
|
|
in case the versions (see above) are identical. */
|
|
versionNonce: number;
|
|
isDeleted: boolean;
|
|
/** List of groups the element belongs to.
|
|
Ordered from deepest to shallowest. */
|
|
groupIds: readonly GroupId[];
|
|
/** other elements that are bound to this element */
|
|
boundElements:
|
|
| readonly Readonly<{
|
|
id: ExcalidrawLinearElement["id"];
|
|
type: "arrow" | "text";
|
|
}>[]
|
|
| null;
|
|
/** epoch (ms) timestamp of last element update */
|
|
updated: number;
|
|
}>;
|
|
|
|
export type ExcalidrawSelectionElement = _ExcalidrawElementBase & {
|
|
type: "selection";
|
|
};
|
|
|
|
export type ExcalidrawRectangleElement = _ExcalidrawElementBase & {
|
|
type: "rectangle";
|
|
};
|
|
|
|
export type ExcalidrawDiamondElement = _ExcalidrawElementBase & {
|
|
type: "diamond";
|
|
};
|
|
|
|
export type ExcalidrawEllipseElement = _ExcalidrawElementBase & {
|
|
type: "ellipse";
|
|
};
|
|
|
|
export type ExcalidrawImageElement = _ExcalidrawElementBase &
|
|
Readonly<{
|
|
type: "image";
|
|
fileId: FileId | null;
|
|
/** whether respective file is persisted */
|
|
status: "pending" | "saved" | "error";
|
|
/** X and Y scale factors <-1, 1>, used for image axis flipping */
|
|
scale: [number, number];
|
|
}>;
|
|
|
|
export type InitializedExcalidrawImageElement = MarkNonNullable<
|
|
ExcalidrawImageElement,
|
|
"fileId"
|
|
>;
|
|
|
|
/**
|
|
* These are elements that don't have any additional properties.
|
|
*/
|
|
export type ExcalidrawGenericElement =
|
|
| ExcalidrawSelectionElement
|
|
| ExcalidrawRectangleElement
|
|
| ExcalidrawDiamondElement
|
|
| ExcalidrawEllipseElement;
|
|
|
|
/**
|
|
* ExcalidrawElement should be JSON serializable and (eventually) contain
|
|
* no computed data. The list of all ExcalidrawElements should be shareable
|
|
* between peers and contain no state local to the peer.
|
|
*/
|
|
export type ExcalidrawElement =
|
|
| ExcalidrawGenericElement
|
|
| ExcalidrawTextElement
|
|
| ExcalidrawLinearElement
|
|
| ExcalidrawFreeDrawElement
|
|
| ExcalidrawImageElement;
|
|
|
|
export type NonDeleted<TElement extends ExcalidrawElement> = TElement & {
|
|
isDeleted: boolean;
|
|
};
|
|
|
|
export type NonDeletedExcalidrawElement = NonDeleted<ExcalidrawElement>;
|
|
|
|
export type ExcalidrawTextElement = _ExcalidrawElementBase &
|
|
Readonly<{
|
|
type: "text";
|
|
fontSize: number;
|
|
fontFamily: FontFamilyValues;
|
|
text: string;
|
|
baseline: number;
|
|
textAlign: TextAlign;
|
|
verticalAlign: VerticalAlign;
|
|
containerId: ExcalidrawGenericElement["id"] | null;
|
|
originalText: string;
|
|
}>;
|
|
|
|
export type ExcalidrawBindableElement =
|
|
| ExcalidrawRectangleElement
|
|
| ExcalidrawDiamondElement
|
|
| ExcalidrawEllipseElement
|
|
| ExcalidrawTextElement
|
|
| ExcalidrawImageElement;
|
|
|
|
export type ExcalidrawTextElementWithContainer = {
|
|
containerId: ExcalidrawGenericElement["id"];
|
|
} & ExcalidrawTextElement;
|
|
|
|
export type PointBinding = {
|
|
elementId: ExcalidrawBindableElement["id"];
|
|
focus: number;
|
|
gap: number;
|
|
};
|
|
|
|
export type Arrowhead = "arrow" | "bar" | "dot" | "triangle";
|
|
|
|
export type ExcalidrawLinearElement = _ExcalidrawElementBase &
|
|
Readonly<{
|
|
type: "line" | "arrow";
|
|
points: readonly Point[];
|
|
lastCommittedPoint: Point | null;
|
|
startBinding: PointBinding | null;
|
|
endBinding: PointBinding | null;
|
|
startArrowhead: Arrowhead | null;
|
|
endArrowhead: Arrowhead | null;
|
|
}>;
|
|
|
|
export type ExcalidrawFreeDrawElement = _ExcalidrawElementBase &
|
|
Readonly<{
|
|
type: "freedraw";
|
|
points: readonly Point[];
|
|
pressures: readonly number[];
|
|
simulatePressure: boolean;
|
|
lastCommittedPoint: Point | null;
|
|
}>;
|
|
|
|
export type FileId = string & { _brand: "FileId" };
|