feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343)
* 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
This commit is contained in:
parent
7db63bd397
commit
98b5c37e45
@ -11,6 +11,7 @@ export const actionAddToLibrary = register({
|
|||||||
const selectedElements = getSelectedElements(
|
const selectedElements = getSelectedElements(
|
||||||
getNonDeletedElements(elements),
|
getNonDeletedElements(elements),
|
||||||
appState,
|
appState,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
if (selectedElements.some((element) => element.type === "image")) {
|
if (selectedElements.some((element) => element.type === "image")) {
|
||||||
return {
|
return {
|
||||||
|
@ -42,6 +42,7 @@ export const actionCopyAsSvg = register({
|
|||||||
const selectedElements = getSelectedElements(
|
const selectedElements = getSelectedElements(
|
||||||
getNonDeletedElements(elements),
|
getNonDeletedElements(elements),
|
||||||
appState,
|
appState,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
await exportCanvas(
|
await exportCanvas(
|
||||||
@ -81,6 +82,7 @@ export const actionCopyAsPng = register({
|
|||||||
const selectedElements = getSelectedElements(
|
const selectedElements = getSelectedElements(
|
||||||
getNonDeletedElements(elements),
|
getNonDeletedElements(elements),
|
||||||
appState,
|
appState,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
await exportCanvas(
|
await exportCanvas(
|
||||||
|
@ -11,6 +11,7 @@ import { newElementWith } from "../element/mutateElement";
|
|||||||
import { getElementsInGroup } from "../groups";
|
import { getElementsInGroup } from "../groups";
|
||||||
import { LinearElementEditor } from "../element/linearElementEditor";
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
||||||
import { fixBindingsAfterDeletion } from "../element/binding";
|
import { fixBindingsAfterDeletion } from "../element/binding";
|
||||||
|
import { isBoundToContainer } from "../element/typeChecks";
|
||||||
|
|
||||||
const deleteSelectedElements = (
|
const deleteSelectedElements = (
|
||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
@ -21,6 +22,12 @@ const deleteSelectedElements = (
|
|||||||
if (appState.selectedElementIds[el.id]) {
|
if (appState.selectedElementIds[el.id]) {
|
||||||
return newElementWith(el, { isDeleted: true });
|
return newElementWith(el, { isDeleted: true });
|
||||||
}
|
}
|
||||||
|
if (
|
||||||
|
isBoundToContainer(el) &&
|
||||||
|
appState.selectedElementIds[el.containerId]
|
||||||
|
) {
|
||||||
|
return newElementWith(el, { isDeleted: true });
|
||||||
|
}
|
||||||
return el;
|
return el;
|
||||||
}),
|
}),
|
||||||
appState: {
|
appState: {
|
||||||
@ -113,7 +120,6 @@ export const actionDeleteSelected = register({
|
|||||||
commitToHistory: true,
|
commitToHistory: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let { elements: nextElements, appState: nextAppState } =
|
let { elements: nextElements, appState: nextAppState } =
|
||||||
deleteSelectedElements(elements, appState);
|
deleteSelectedElements(elements, appState);
|
||||||
fixBindingsAfterDeletion(
|
fixBindingsAfterDeletion(
|
||||||
|
@ -2,11 +2,11 @@ import { KEYS } from "../keys";
|
|||||||
import { register } from "./register";
|
import { register } from "./register";
|
||||||
import { ExcalidrawElement } from "../element/types";
|
import { ExcalidrawElement } from "../element/types";
|
||||||
import { duplicateElement, getNonDeletedElements } from "../element";
|
import { duplicateElement, getNonDeletedElements } from "../element";
|
||||||
import { isSomeElementSelected } from "../scene";
|
import { getSelectedElements, isSomeElementSelected } from "../scene";
|
||||||
import { ToolButton } from "../components/ToolButton";
|
import { ToolButton } from "../components/ToolButton";
|
||||||
import { clone } from "../components/icons";
|
import { clone } from "../components/icons";
|
||||||
import { t } from "../i18n";
|
import { t } from "../i18n";
|
||||||
import { getShortcutKey } from "../utils";
|
import { arrayToMap, getShortcutKey } from "../utils";
|
||||||
import { LinearElementEditor } from "../element/linearElementEditor";
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
||||||
import {
|
import {
|
||||||
selectGroupsForSelectedElements,
|
selectGroupsForSelectedElements,
|
||||||
@ -17,6 +17,8 @@ import { AppState } from "../types";
|
|||||||
import { fixBindingsAfterDuplication } from "../element/binding";
|
import { fixBindingsAfterDuplication } from "../element/binding";
|
||||||
import { ActionResult } from "./types";
|
import { ActionResult } from "./types";
|
||||||
import { GRID_SIZE } from "../constants";
|
import { GRID_SIZE } from "../constants";
|
||||||
|
import { bindTextToShapeAfterDuplication } from "../element/textElement";
|
||||||
|
import { isBoundToContainer } from "../element/typeChecks";
|
||||||
|
|
||||||
export const actionDuplicateSelection = register({
|
export const actionDuplicateSelection = register({
|
||||||
name: "duplicateSelection",
|
name: "duplicateSelection",
|
||||||
@ -85,9 +87,12 @@ const duplicateElements = (
|
|||||||
const finalElements: ExcalidrawElement[] = [];
|
const finalElements: ExcalidrawElement[] = [];
|
||||||
|
|
||||||
let index = 0;
|
let index = 0;
|
||||||
|
const selectedElementIds = arrayToMap(
|
||||||
|
getSelectedElements(elements, appState, true),
|
||||||
|
);
|
||||||
while (index < elements.length) {
|
while (index < elements.length) {
|
||||||
const element = elements[index];
|
const element = elements[index];
|
||||||
if (appState.selectedElementIds[element.id]) {
|
if (selectedElementIds.get(element.id)) {
|
||||||
if (element.groupIds.length) {
|
if (element.groupIds.length) {
|
||||||
const groupId = getSelectedGroupForElement(appState, element);
|
const groupId = getSelectedGroupForElement(appState, element);
|
||||||
// if group selected, duplicate it atomically
|
// if group selected, duplicate it atomically
|
||||||
@ -109,7 +114,11 @@ const duplicateElements = (
|
|||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
bindTextToShapeAfterDuplication(
|
||||||
|
finalElements,
|
||||||
|
oldElements,
|
||||||
|
oldIdToDuplicatedId,
|
||||||
|
);
|
||||||
fixBindingsAfterDuplication(finalElements, oldElements, oldIdToDuplicatedId);
|
fixBindingsAfterDuplication(finalElements, oldElements, oldIdToDuplicatedId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -119,7 +128,9 @@ const duplicateElements = (
|
|||||||
...appState,
|
...appState,
|
||||||
selectedGroupIds: {},
|
selectedGroupIds: {},
|
||||||
selectedElementIds: newElements.reduce((acc, element) => {
|
selectedElementIds: newElements.reduce((acc, element) => {
|
||||||
|
if (!isBoundToContainer(element)) {
|
||||||
acc[element.id] = true;
|
acc[element.id] = true;
|
||||||
|
}
|
||||||
return acc;
|
return acc;
|
||||||
}, {} as any),
|
}, {} as any),
|
||||||
},
|
},
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { CODES, KEYS } from "../keys";
|
import { CODES, KEYS } from "../keys";
|
||||||
import { t } from "../i18n";
|
import { t } from "../i18n";
|
||||||
import { getShortcutKey } from "../utils";
|
import { arrayToMap, getShortcutKey } from "../utils";
|
||||||
import { register } from "./register";
|
import { register } from "./register";
|
||||||
import { UngroupIcon, GroupIcon } from "../components/icons";
|
import { UngroupIcon, GroupIcon } from "../components/icons";
|
||||||
import { newElementWith } from "../element/mutateElement";
|
import { newElementWith } from "../element/mutateElement";
|
||||||
@ -44,6 +44,7 @@ const enableActionGroup = (
|
|||||||
const selectedElements = getSelectedElements(
|
const selectedElements = getSelectedElements(
|
||||||
getNonDeletedElements(elements),
|
getNonDeletedElements(elements),
|
||||||
appState,
|
appState,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
return (
|
return (
|
||||||
selectedElements.length >= 2 && !allElementsInSameGroup(selectedElements)
|
selectedElements.length >= 2 && !allElementsInSameGroup(selectedElements)
|
||||||
@ -56,6 +57,7 @@ export const actionGroup = register({
|
|||||||
const selectedElements = getSelectedElements(
|
const selectedElements = getSelectedElements(
|
||||||
getNonDeletedElements(elements),
|
getNonDeletedElements(elements),
|
||||||
appState,
|
appState,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
if (selectedElements.length < 2) {
|
if (selectedElements.length < 2) {
|
||||||
// nothing to group
|
// nothing to group
|
||||||
@ -83,8 +85,9 @@ export const actionGroup = register({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const newGroupId = randomId();
|
const newGroupId = randomId();
|
||||||
|
const selectElementIds = arrayToMap(selectedElements);
|
||||||
const updatedElements = elements.map((element) => {
|
const updatedElements = elements.map((element) => {
|
||||||
if (!appState.selectedElementIds[element.id]) {
|
if (!selectElementIds.get(element.id)) {
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
return newElementWith(element, {
|
return newElementWith(element, {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { KEYS } from "../keys";
|
import { KEYS } from "../keys";
|
||||||
import { register } from "./register";
|
import { register } from "./register";
|
||||||
import { selectGroupsForSelectedElements } from "../groups";
|
import { selectGroupsForSelectedElements } from "../groups";
|
||||||
import { getNonDeletedElements } from "../element";
|
import { getNonDeletedElements, isTextElement } from "../element";
|
||||||
|
|
||||||
export const actionSelectAll = register({
|
export const actionSelectAll = register({
|
||||||
name: "selectAll",
|
name: "selectAll",
|
||||||
@ -15,7 +15,10 @@ export const actionSelectAll = register({
|
|||||||
...appState,
|
...appState,
|
||||||
editingGroupId: null,
|
editingGroupId: null,
|
||||||
selectedElementIds: elements.reduce((map, element) => {
|
selectedElementIds: elements.reduce((map, element) => {
|
||||||
if (!element.isDeleted) {
|
if (
|
||||||
|
!element.isDeleted &&
|
||||||
|
!(isTextElement(element) && element.containerId)
|
||||||
|
) {
|
||||||
map[element.id] = true;
|
map[element.id] = true;
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
|
@ -58,7 +58,8 @@ export const copyToClipboard = async (
|
|||||||
appState: AppState,
|
appState: AppState,
|
||||||
files: BinaryFiles,
|
files: BinaryFiles,
|
||||||
) => {
|
) => {
|
||||||
const selectedElements = getSelectedElements(elements, appState);
|
// select binded text elements when copying
|
||||||
|
const selectedElements = getSelectedElements(elements, appState, true);
|
||||||
const contents: ElementsClipboard = {
|
const contents: ElementsClipboard = {
|
||||||
type: EXPORT_DATA_TYPES.excalidrawClipboard,
|
type: EXPORT_DATA_TYPES.excalidrawClipboard,
|
||||||
elements: selectedElements,
|
elements: selectedElements,
|
||||||
|
@ -120,6 +120,7 @@ import {
|
|||||||
} from "../element/mutateElement";
|
} from "../element/mutateElement";
|
||||||
import { deepCopyElement, newFreeDrawElement } from "../element/newElement";
|
import { deepCopyElement, newFreeDrawElement } from "../element/newElement";
|
||||||
import {
|
import {
|
||||||
|
hasBoundTextElement,
|
||||||
isBindingElement,
|
isBindingElement,
|
||||||
isBindingElementType,
|
isBindingElementType,
|
||||||
isImageElement,
|
isImageElement,
|
||||||
@ -194,6 +195,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
debounce,
|
debounce,
|
||||||
distance,
|
distance,
|
||||||
|
getFontString,
|
||||||
getNearestScrollableContainer,
|
getNearestScrollableContainer,
|
||||||
isInputLike,
|
isInputLike,
|
||||||
isToolIcon,
|
isToolIcon,
|
||||||
@ -228,6 +230,12 @@ import {
|
|||||||
} from "../element/image";
|
} from "../element/image";
|
||||||
import throttle from "lodash.throttle";
|
import throttle from "lodash.throttle";
|
||||||
import { fileOpen, nativeFileSystemSupported } from "../data/filesystem";
|
import { fileOpen, nativeFileSystemSupported } from "../data/filesystem";
|
||||||
|
import {
|
||||||
|
bindTextToShapeAfterDuplication,
|
||||||
|
getApproxMinLineHeight,
|
||||||
|
getApproxMinLineWidth,
|
||||||
|
getBoundTextElementId,
|
||||||
|
} from "../element/textElement";
|
||||||
import { isHittingElementNotConsideringBoundingBox } from "../element/collision";
|
import { isHittingElementNotConsideringBoundingBox } from "../element/collision";
|
||||||
|
|
||||||
const IsMobileContext = React.createContext(false);
|
const IsMobileContext = React.createContext(false);
|
||||||
@ -1134,7 +1142,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
}
|
}
|
||||||
const scrolledOutside =
|
const scrolledOutside =
|
||||||
// hide when editing text
|
// hide when editing text
|
||||||
this.state.editingElement?.type === "text"
|
isTextElement(this.state.editingElement)
|
||||||
? false
|
? false
|
||||||
: !atLeastOneVisibleElement && renderingElements.length > 0;
|
: !atLeastOneVisibleElement && renderingElements.length > 0;
|
||||||
if (this.state.scrolledOutside !== scrolledOutside) {
|
if (this.state.scrolledOutside !== scrolledOutside) {
|
||||||
@ -1376,6 +1384,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
oldIdToDuplicatedId.set(element.id, newElement.id);
|
oldIdToDuplicatedId.set(element.id, newElement.id);
|
||||||
return newElement;
|
return newElement;
|
||||||
});
|
});
|
||||||
|
bindTextToShapeAfterDuplication(newElements, elements, oldIdToDuplicatedId);
|
||||||
const nextElements = [
|
const nextElements = [
|
||||||
...this.scene.getElementsIncludingDeleted(),
|
...this.scene.getElementsIncludingDeleted(),
|
||||||
...newElements,
|
...newElements,
|
||||||
@ -1394,7 +1403,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
...this.state,
|
...this.state,
|
||||||
isLibraryOpen: false,
|
isLibraryOpen: false,
|
||||||
selectedElementIds: newElements.reduce((map, element) => {
|
selectedElementIds: newElements.reduce((map, element) => {
|
||||||
|
if (isTextElement(element) && !element.containerId) {
|
||||||
map[element.id] = true;
|
map[element.id] = true;
|
||||||
|
}
|
||||||
return map;
|
return map;
|
||||||
}, {} as any),
|
}, {} as any),
|
||||||
selectedGroupIds: {},
|
selectedGroupIds: {},
|
||||||
@ -1710,9 +1721,11 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
!isLinearElement(selectedElements[0])
|
!isLinearElement(selectedElements[0])
|
||||||
) {
|
) {
|
||||||
const selectedElement = selectedElements[0];
|
const selectedElement = selectedElements[0];
|
||||||
|
|
||||||
this.startTextEditing({
|
this.startTextEditing({
|
||||||
sceneX: selectedElement.x + selectedElement.width / 2,
|
sceneX: selectedElement.x + selectedElement.width / 2,
|
||||||
sceneY: selectedElement.y + selectedElement.height / 2,
|
sceneY: selectedElement.y + selectedElement.height / 2,
|
||||||
|
shouldBind: true,
|
||||||
});
|
});
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
return;
|
return;
|
||||||
@ -1867,14 +1880,24 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
isExistingElement?: boolean;
|
isExistingElement?: boolean;
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const updateElement = (text: string, isDeleted = false) => {
|
const updateElement = (
|
||||||
|
text: string,
|
||||||
|
originalText: string,
|
||||||
|
isDeleted = false,
|
||||||
|
updateDimensions = false,
|
||||||
|
) => {
|
||||||
this.scene.replaceAllElements([
|
this.scene.replaceAllElements([
|
||||||
...this.scene.getElementsIncludingDeleted().map((_element) => {
|
...this.scene.getElementsIncludingDeleted().map((_element) => {
|
||||||
if (_element.id === element.id && isTextElement(_element)) {
|
if (_element.id === element.id && isTextElement(_element)) {
|
||||||
return updateTextElement(_element, {
|
return updateTextElement(
|
||||||
|
_element,
|
||||||
|
{
|
||||||
text,
|
text,
|
||||||
isDeleted,
|
isDeleted,
|
||||||
});
|
originalText,
|
||||||
|
},
|
||||||
|
updateDimensions,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return _element;
|
return _element;
|
||||||
}),
|
}),
|
||||||
@ -1893,27 +1916,27 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
},
|
},
|
||||||
this.state,
|
this.state,
|
||||||
);
|
);
|
||||||
return [
|
return [viewportX, viewportY];
|
||||||
viewportX - this.state.offsetLeft,
|
|
||||||
viewportY - this.state.offsetTop,
|
|
||||||
];
|
|
||||||
},
|
},
|
||||||
onChange: withBatchedUpdates((text) => {
|
onChange: withBatchedUpdates((text) => {
|
||||||
updateElement(text);
|
updateElement(text, text, false, !element.containerId);
|
||||||
if (isNonDeletedElement(element)) {
|
if (isNonDeletedElement(element)) {
|
||||||
updateBoundElements(element);
|
updateBoundElements(element);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
onSubmit: withBatchedUpdates(({ text, viaKeyboard }) => {
|
onSubmit: withBatchedUpdates(({ text, viaKeyboard, originalText }) => {
|
||||||
const isDeleted = !text.trim();
|
const isDeleted = !text.trim();
|
||||||
updateElement(text, isDeleted);
|
updateElement(text, originalText, isDeleted, true);
|
||||||
// select the created text element only if submitting via keyboard
|
// select the created text element only if submitting via keyboard
|
||||||
// (when submitting via click it should act as signal to deselect)
|
// (when submitting via click it should act as signal to deselect)
|
||||||
if (!isDeleted && viaKeyboard) {
|
if (!isDeleted && viaKeyboard) {
|
||||||
|
const elementIdToSelect = element.containerId
|
||||||
|
? element.containerId
|
||||||
|
: element.id;
|
||||||
this.setState((prevState) => ({
|
this.setState((prevState) => ({
|
||||||
selectedElementIds: {
|
selectedElementIds: {
|
||||||
...prevState.selectedElementIds,
|
...prevState.selectedElementIds,
|
||||||
[element.id]: true,
|
[elementIdToSelect]: true,
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@ -1942,7 +1965,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
|
|
||||||
// do an initial update to re-initialize element position since we were
|
// do an initial update to re-initialize element position since we were
|
||||||
// modifying element's x/y for sake of editor (case: syncing to remote)
|
// modifying element's x/y for sake of editor (case: syncing to remote)
|
||||||
updateElement(element.text);
|
updateElement(element.text, element.originalText);
|
||||||
}
|
}
|
||||||
|
|
||||||
private deselectElements() {
|
private deselectElements() {
|
||||||
@ -1957,7 +1980,9 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
): NonDeleted<ExcalidrawTextElement> | null {
|
): NonDeleted<ExcalidrawTextElement> | null {
|
||||||
const element = this.getElementAtPosition(x, y);
|
const element = this.getElementAtPosition(x, y, {
|
||||||
|
includeBoundTextElement: true,
|
||||||
|
});
|
||||||
|
|
||||||
if (element && isTextElement(element) && !element.isDeleted) {
|
if (element && isTextElement(element) && !element.isDeleted) {
|
||||||
return element;
|
return element;
|
||||||
@ -1972,9 +1997,14 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
/** if true, returns the first selected element (with highest z-index)
|
/** if true, returns the first selected element (with highest z-index)
|
||||||
of all hit elements */
|
of all hit elements */
|
||||||
preferSelected?: boolean;
|
preferSelected?: boolean;
|
||||||
|
includeBoundTextElement?: boolean;
|
||||||
},
|
},
|
||||||
): NonDeleted<ExcalidrawElement> | null {
|
): NonDeleted<ExcalidrawElement> | null {
|
||||||
const allHitElements = this.getElementsAtPosition(x, y);
|
const allHitElements = this.getElementsAtPosition(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
opts?.includeBoundTextElement,
|
||||||
|
);
|
||||||
if (allHitElements.length > 1) {
|
if (allHitElements.length > 1) {
|
||||||
if (opts?.preferSelected) {
|
if (opts?.preferSelected) {
|
||||||
for (let index = allHitElements.length - 1; index > -1; index--) {
|
for (let index = allHitElements.length - 1; index > -1; index--) {
|
||||||
@ -2005,8 +2035,16 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
private getElementsAtPosition(
|
private getElementsAtPosition(
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
|
includeBoundTextElement: boolean = false,
|
||||||
): NonDeleted<ExcalidrawElement>[] {
|
): NonDeleted<ExcalidrawElement>[] {
|
||||||
return getElementsAtPosition(this.scene.getElements(), (element) =>
|
const elements = includeBoundTextElement
|
||||||
|
? this.scene.getElements()
|
||||||
|
: this.scene
|
||||||
|
.getElements()
|
||||||
|
.filter(
|
||||||
|
(element) => !(isTextElement(element) && element.containerId),
|
||||||
|
);
|
||||||
|
return getElementsAtPosition(elements, (element) =>
|
||||||
hitTest(element, this.state, x, y),
|
hitTest(element, this.state, x, y),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -2014,17 +2052,17 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
private startTextEditing = ({
|
private startTextEditing = ({
|
||||||
sceneX,
|
sceneX,
|
||||||
sceneY,
|
sceneY,
|
||||||
|
shouldBind,
|
||||||
insertAtParentCenter = true,
|
insertAtParentCenter = true,
|
||||||
}: {
|
}: {
|
||||||
/** X position to insert text at */
|
/** X position to insert text at */
|
||||||
sceneX: number;
|
sceneX: number;
|
||||||
/** Y position to insert text at */
|
/** Y position to insert text at */
|
||||||
sceneY: number;
|
sceneY: number;
|
||||||
|
shouldBind: boolean;
|
||||||
/** whether to attempt to insert at element center if applicable */
|
/** whether to attempt to insert at element center if applicable */
|
||||||
insertAtParentCenter?: boolean;
|
insertAtParentCenter?: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const existingTextElement = this.getTextElementAtPosition(sceneX, sceneY);
|
|
||||||
|
|
||||||
const parentCenterPosition =
|
const parentCenterPosition =
|
||||||
insertAtParentCenter &&
|
insertAtParentCenter &&
|
||||||
this.getTextWysiwygSnappedToCenterPosition(
|
this.getTextWysiwygSnappedToCenterPosition(
|
||||||
@ -2035,6 +2073,43 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
window.devicePixelRatio,
|
window.devicePixelRatio,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// bind to container when shouldBind is true or
|
||||||
|
// clicked on center of container
|
||||||
|
const container =
|
||||||
|
shouldBind || parentCenterPosition
|
||||||
|
? getElementContainingPosition(
|
||||||
|
this.scene.getElements(),
|
||||||
|
sceneX,
|
||||||
|
sceneY,
|
||||||
|
"text",
|
||||||
|
)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
let existingTextElement = this.getTextElementAtPosition(sceneX, sceneY);
|
||||||
|
|
||||||
|
// consider bounded text element if container present
|
||||||
|
if (container) {
|
||||||
|
const boundTextElementId = getBoundTextElementId(container);
|
||||||
|
if (boundTextElementId) {
|
||||||
|
existingTextElement = this.scene.getElement(
|
||||||
|
boundTextElementId,
|
||||||
|
) as ExcalidrawTextElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!existingTextElement && container) {
|
||||||
|
const fontString = {
|
||||||
|
fontSize: this.state.currentItemFontSize,
|
||||||
|
fontFamily: this.state.currentItemFontFamily,
|
||||||
|
};
|
||||||
|
const minWidth = getApproxMinLineWidth(getFontString(fontString));
|
||||||
|
const minHeight = getApproxMinLineHeight(getFontString(fontString));
|
||||||
|
const newHeight = Math.max(container.height, minHeight);
|
||||||
|
const newWidth = Math.max(container.width, minWidth);
|
||||||
|
mutateElement(container, { height: newHeight, width: newWidth });
|
||||||
|
sceneX = container.x + newWidth / 2;
|
||||||
|
sceneY = container.y + newHeight / 2;
|
||||||
|
}
|
||||||
|
|
||||||
const element = existingTextElement
|
const element = existingTextElement
|
||||||
? existingTextElement
|
? existingTextElement
|
||||||
: newTextElement({
|
: newTextElement({
|
||||||
@ -2061,6 +2136,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
verticalAlign: parentCenterPosition
|
verticalAlign: parentCenterPosition
|
||||||
? "middle"
|
? "middle"
|
||||||
: DEFAULT_VERTICAL_ALIGN,
|
: DEFAULT_VERTICAL_ALIGN,
|
||||||
|
containerId: container?.id ?? undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.setState({ editingElement: element });
|
this.setState({ editingElement: element });
|
||||||
@ -2131,7 +2207,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
|
|
||||||
resetCursor(this.canvas);
|
resetCursor(this.canvas);
|
||||||
|
|
||||||
const { x: sceneX, y: sceneY } = viewportCoordsToSceneCoords(
|
let { x: sceneX, y: sceneY } = viewportCoordsToSceneCoords(
|
||||||
event,
|
event,
|
||||||
this.state,
|
this.state,
|
||||||
);
|
);
|
||||||
@ -2163,9 +2239,22 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
|
|
||||||
resetCursor(this.canvas);
|
resetCursor(this.canvas);
|
||||||
if (!event[KEYS.CTRL_OR_CMD] && !this.state.viewModeEnabled) {
|
if (!event[KEYS.CTRL_OR_CMD] && !this.state.viewModeEnabled) {
|
||||||
|
const selectedElements = getSelectedElements(
|
||||||
|
this.scene.getElements(),
|
||||||
|
this.state,
|
||||||
|
);
|
||||||
|
if (selectedElements.length === 1) {
|
||||||
|
const selectedElement = selectedElements[0];
|
||||||
|
const canBindText = hasBoundTextElement(selectedElement);
|
||||||
|
if (canBindText) {
|
||||||
|
sceneX = selectedElement.x + selectedElement.width / 2;
|
||||||
|
sceneY = selectedElement.y + selectedElement.height / 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
this.startTextEditing({
|
this.startTextEditing({
|
||||||
sceneX,
|
sceneX,
|
||||||
sceneY,
|
sceneY,
|
||||||
|
shouldBind: false,
|
||||||
insertAtParentCenter: !event.altKey,
|
insertAtParentCenter: !event.altKey,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -3036,13 +3125,25 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
// if we're currently still editing text, clicking outside
|
// if we're currently still editing text, clicking outside
|
||||||
// should only finalize it, not create another (irrespective
|
// should only finalize it, not create another (irrespective
|
||||||
// of state.elementLocked)
|
// of state.elementLocked)
|
||||||
if (this.state.editingElement?.type === "text") {
|
if (isTextElement(this.state.editingElement)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let sceneX = pointerDownState.origin.x;
|
||||||
|
let sceneY = pointerDownState.origin.y;
|
||||||
|
|
||||||
|
const element = this.getElementAtPosition(sceneX, sceneY, {
|
||||||
|
includeBoundTextElement: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const canBindText = hasBoundTextElement(element);
|
||||||
|
if (canBindText) {
|
||||||
|
sceneX = element.x + element.width / 2;
|
||||||
|
sceneY = element.y + element.height / 2;
|
||||||
|
}
|
||||||
this.startTextEditing({
|
this.startTextEditing({
|
||||||
sceneX: pointerDownState.origin.x,
|
sceneX,
|
||||||
sceneY: pointerDownState.origin.y,
|
sceneY,
|
||||||
|
shouldBind: false,
|
||||||
insertAtParentCenter: !event.altKey,
|
insertAtParentCenter: !event.altKey,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -3442,7 +3543,6 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
selectedElements,
|
selectedElements,
|
||||||
dragX,
|
dragX,
|
||||||
dragY,
|
dragY,
|
||||||
this.scene,
|
|
||||||
lockDirection,
|
lockDirection,
|
||||||
dragDistanceX,
|
dragDistanceX,
|
||||||
dragDistanceY,
|
dragDistanceY,
|
||||||
@ -3462,9 +3562,15 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
const groupIdMap = new Map();
|
const groupIdMap = new Map();
|
||||||
const oldIdToDuplicatedId = new Map();
|
const oldIdToDuplicatedId = new Map();
|
||||||
const hitElement = pointerDownState.hit.element;
|
const hitElement = pointerDownState.hit.element;
|
||||||
for (const element of this.scene.getElementsIncludingDeleted()) {
|
const elements = this.scene.getElementsIncludingDeleted();
|
||||||
|
const selectedElementIds: Array<ExcalidrawElement["id"]> =
|
||||||
|
getSelectedElements(elements, this.state, true).map(
|
||||||
|
(element) => element.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const element of elements) {
|
||||||
if (
|
if (
|
||||||
this.state.selectedElementIds[element.id] ||
|
selectedElementIds.includes(element.id) ||
|
||||||
// case: the state.selectedElementIds might not have been
|
// case: the state.selectedElementIds might not have been
|
||||||
// updated yet by the time this mousemove event is fired
|
// updated yet by the time this mousemove event is fired
|
||||||
(element.id === hitElement?.id &&
|
(element.id === hitElement?.id &&
|
||||||
@ -3492,6 +3598,11 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const nextSceneElements = [...nextElements, ...elementsToAppend];
|
const nextSceneElements = [...nextElements, ...elementsToAppend];
|
||||||
|
bindTextToShapeAfterDuplication(
|
||||||
|
nextElements,
|
||||||
|
elementsToAppend,
|
||||||
|
oldIdToDuplicatedId,
|
||||||
|
);
|
||||||
fixBindingsAfterDuplication(
|
fixBindingsAfterDuplication(
|
||||||
nextSceneElements,
|
nextSceneElements,
|
||||||
elementsToAppend,
|
elementsToAppend,
|
||||||
@ -3942,6 +4053,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
} else {
|
} else {
|
||||||
// add element to selection while
|
// add element to selection while
|
||||||
// keeping prev elements selected
|
// keeping prev elements selected
|
||||||
|
|
||||||
this.setState((_prevState) => ({
|
this.setState((_prevState) => ({
|
||||||
selectedElementIds: {
|
selectedElementIds: {
|
||||||
..._prevState.selectedElementIds,
|
..._prevState.selectedElementIds,
|
||||||
|
@ -7,6 +7,7 @@ import { AppState } from "../types";
|
|||||||
import {
|
import {
|
||||||
isImageElement,
|
isImageElement,
|
||||||
isLinearElement,
|
isLinearElement,
|
||||||
|
isTextBindableContainer,
|
||||||
isTextElement,
|
isTextElement,
|
||||||
} from "../element/typeChecks";
|
} from "../element/typeChecks";
|
||||||
import { getShortcutKey } from "../utils";
|
import { getShortcutKey } from "../utils";
|
||||||
@ -60,7 +61,8 @@ const getHints = ({ appState, elements, isMobile }: HintViewerProps) => {
|
|||||||
return t("hints.rotate");
|
return t("hints.rotate");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedElements.length === 1 && isLinearElement(selectedElements[0])) {
|
if (selectedElements.length === 1) {
|
||||||
|
if (isLinearElement(selectedElements[0])) {
|
||||||
if (appState.editingLinearElement) {
|
if (appState.editingLinearElement) {
|
||||||
return appState.editingLinearElement.selectedPointsIndices
|
return appState.editingLinearElement.selectedPointsIndices
|
||||||
? t("hints.lineEditor_pointSelected")
|
? t("hints.lineEditor_pointSelected")
|
||||||
@ -68,6 +70,10 @@ const getHints = ({ appState, elements, isMobile }: HintViewerProps) => {
|
|||||||
}
|
}
|
||||||
return t("hints.lineEditor_info");
|
return t("hints.lineEditor_info");
|
||||||
}
|
}
|
||||||
|
if (isTextBindableContainer(selectedElements[0])) {
|
||||||
|
return t("hints.bindTextToElement");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (selectedElements.length === 1 && isTextElement(selectedElements[0])) {
|
if (selectedElements.length === 1 && isTextElement(selectedElements[0])) {
|
||||||
return t("hints.text_selected");
|
return t("hints.text_selected");
|
||||||
|
@ -102,7 +102,7 @@ const ImageExportModal = ({
|
|||||||
const { exportBackground, viewBackgroundColor } = appState;
|
const { exportBackground, viewBackgroundColor } = appState;
|
||||||
|
|
||||||
const exportedElements = exportSelected
|
const exportedElements = exportSelected
|
||||||
? getSelectedElements(elements, appState)
|
? getSelectedElements(elements, appState, true)
|
||||||
: elements;
|
: elements;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -270,7 +270,7 @@ const LayerUI = ({
|
|||||||
|
|
||||||
const libraryMenu = appState.isLibraryOpen ? (
|
const libraryMenu = appState.isLibraryOpen ? (
|
||||||
<LibraryMenu
|
<LibraryMenu
|
||||||
pendingElements={getSelectedElements(elements, appState)}
|
pendingElements={getSelectedElements(elements, appState, true)}
|
||||||
onClose={closeLibrary}
|
onClose={closeLibrary}
|
||||||
onInsertShape={onInsertElements}
|
onInsertShape={onInsertElements}
|
||||||
onAddToLibrary={deselectItems}
|
onAddToLibrary={deselectItems}
|
||||||
|
@ -181,3 +181,5 @@ export const VERSIONS = {
|
|||||||
excalidraw: 2,
|
excalidraw: 2,
|
||||||
excalidrawLibrary: 2,
|
excalidrawLibrary: 2,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
export const PADDING = 30;
|
||||||
|
@ -135,6 +135,8 @@ const restoreElement = (
|
|||||||
baseline: element.baseline,
|
baseline: element.baseline,
|
||||||
textAlign: element.textAlign || DEFAULT_TEXT_ALIGN,
|
textAlign: element.textAlign || DEFAULT_TEXT_ALIGN,
|
||||||
verticalAlign: element.verticalAlign || DEFAULT_VERTICAL_ALIGN,
|
verticalAlign: element.verticalAlign || DEFAULT_VERTICAL_ALIGN,
|
||||||
|
containerId: element.containerId ?? null,
|
||||||
|
originalText: element.originalText ?? "",
|
||||||
});
|
});
|
||||||
case "freedraw": {
|
case "freedraw": {
|
||||||
return restoreElementWithProperties(element, {
|
return restoreElementWithProperties(element, {
|
||||||
|
@ -31,7 +31,9 @@ import { Point } from "../types";
|
|||||||
import { Drawable } from "roughjs/bin/core";
|
import { Drawable } from "roughjs/bin/core";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
import { getShapeForElement } from "../renderer/renderElement";
|
import { getShapeForElement } from "../renderer/renderElement";
|
||||||
import { isImageElement } from "./typeChecks";
|
import { hasBoundTextElement, isImageElement } from "./typeChecks";
|
||||||
|
import { isTextElement } from ".";
|
||||||
|
import { isTransparent } from "../utils";
|
||||||
|
|
||||||
const isElementDraggableFromInside = (
|
const isElementDraggableFromInside = (
|
||||||
element: NonDeletedExcalidrawElement,
|
element: NonDeletedExcalidrawElement,
|
||||||
@ -43,9 +45,9 @@ const isElementDraggableFromInside = (
|
|||||||
if (element.type === "freedraw") {
|
if (element.type === "freedraw") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
const isDraggableFromInside =
|
||||||
const isDraggableFromInside = element.backgroundColor !== "transparent";
|
!isTransparent(element.backgroundColor) ||
|
||||||
|
(isTransparent(element.backgroundColor) && hasBoundTextElement(element));
|
||||||
if (element.type === "line") {
|
if (element.type === "line") {
|
||||||
return isDraggableFromInside && isPathALoop(element.points);
|
return isDraggableFromInside && isPathALoop(element.points);
|
||||||
}
|
}
|
||||||
@ -90,8 +92,7 @@ export const isHittingElementNotConsideringBoundingBox = (
|
|||||||
): boolean => {
|
): boolean => {
|
||||||
const threshold = 10 / appState.zoom.value;
|
const threshold = 10 / appState.zoom.value;
|
||||||
|
|
||||||
const check =
|
const check = isTextElement(element)
|
||||||
element.type === "text"
|
|
||||||
? isStrictlyInside
|
? isStrictlyInside
|
||||||
: isElementDraggableFromInside(element)
|
: isElementDraggableFromInside(element)
|
||||||
? isInsideCheck
|
? isInsideCheck
|
||||||
|
@ -6,13 +6,13 @@ import { getPerfectElementSize } from "./sizeHelpers";
|
|||||||
import Scene from "../scene/Scene";
|
import Scene from "../scene/Scene";
|
||||||
import { NonDeletedExcalidrawElement } from "./types";
|
import { NonDeletedExcalidrawElement } from "./types";
|
||||||
import { PointerDownState } from "../types";
|
import { PointerDownState } from "../types";
|
||||||
|
import { getBoundTextElementId } from "./textElement";
|
||||||
|
|
||||||
export const dragSelectedElements = (
|
export const dragSelectedElements = (
|
||||||
pointerDownState: PointerDownState,
|
pointerDownState: PointerDownState,
|
||||||
selectedElements: NonDeletedExcalidrawElement[],
|
selectedElements: NonDeletedExcalidrawElement[],
|
||||||
pointerX: number,
|
pointerX: number,
|
||||||
pointerY: number,
|
pointerY: number,
|
||||||
scene: Scene,
|
|
||||||
lockDirection: boolean = false,
|
lockDirection: boolean = false,
|
||||||
distanceX: number = 0,
|
distanceX: number = 0,
|
||||||
distanceY: number = 0,
|
distanceY: number = 0,
|
||||||
@ -20,6 +20,43 @@ export const dragSelectedElements = (
|
|||||||
const [x1, y1] = getCommonBounds(selectedElements);
|
const [x1, y1] = getCommonBounds(selectedElements);
|
||||||
const offset = { x: pointerX - x1, y: pointerY - y1 };
|
const offset = { x: pointerX - x1, y: pointerY - y1 };
|
||||||
selectedElements.forEach((element) => {
|
selectedElements.forEach((element) => {
|
||||||
|
updateElementCoords(
|
||||||
|
lockDirection,
|
||||||
|
distanceX,
|
||||||
|
distanceY,
|
||||||
|
pointerDownState,
|
||||||
|
element,
|
||||||
|
offset,
|
||||||
|
);
|
||||||
|
if (!element.groupIds.length) {
|
||||||
|
const boundTextElementId = getBoundTextElementId(element);
|
||||||
|
if (boundTextElementId) {
|
||||||
|
const textElement =
|
||||||
|
Scene.getScene(element)!.getElement(boundTextElementId);
|
||||||
|
updateElementCoords(
|
||||||
|
lockDirection,
|
||||||
|
distanceX,
|
||||||
|
distanceY,
|
||||||
|
pointerDownState,
|
||||||
|
textElement!,
|
||||||
|
offset,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
updateBoundElements(element, {
|
||||||
|
simultaneouslyUpdated: selectedElements,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateElementCoords = (
|
||||||
|
lockDirection: boolean,
|
||||||
|
distanceX: number,
|
||||||
|
distanceY: number,
|
||||||
|
pointerDownState: PointerDownState,
|
||||||
|
element: NonDeletedExcalidrawElement,
|
||||||
|
offset: { x: number; y: number },
|
||||||
|
) => {
|
||||||
let x: number;
|
let x: number;
|
||||||
let y: number;
|
let y: number;
|
||||||
if (lockDirection) {
|
if (lockDirection) {
|
||||||
@ -37,13 +74,7 @@ export const dragSelectedElements = (
|
|||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
});
|
});
|
||||||
|
|
||||||
updateBoundElements(element, {
|
|
||||||
simultaneouslyUpdated: selectedElements,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDragOffsetXY = (
|
export const getDragOffsetXY = (
|
||||||
selectedElements: NonDeletedExcalidrawElement[],
|
selectedElements: NonDeletedExcalidrawElement[],
|
||||||
x: number,
|
x: number,
|
||||||
|
@ -11,15 +11,20 @@ import {
|
|||||||
Arrowhead,
|
Arrowhead,
|
||||||
ExcalidrawFreeDrawElement,
|
ExcalidrawFreeDrawElement,
|
||||||
FontFamilyValues,
|
FontFamilyValues,
|
||||||
|
ExcalidrawRectangleElement,
|
||||||
} from "../element/types";
|
} from "../element/types";
|
||||||
import { measureText, getFontString, getUpdatedTimestamp } from "../utils";
|
import { getFontString, getUpdatedTimestamp } from "../utils";
|
||||||
import { randomInteger, randomId } from "../random";
|
import { randomInteger, randomId } from "../random";
|
||||||
import { newElementWith } from "./mutateElement";
|
import { mutateElement, newElementWith } from "./mutateElement";
|
||||||
import { getNewGroupIdsForDuplication } from "../groups";
|
import { getNewGroupIdsForDuplication } from "../groups";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
import { getElementAbsoluteCoords } from ".";
|
import { getElementAbsoluteCoords } from ".";
|
||||||
import { adjustXYWithRotation } from "../math";
|
import { adjustXYWithRotation } from "../math";
|
||||||
import { getResizedElementAbsoluteCoords } from "./bounds";
|
import { getResizedElementAbsoluteCoords } from "./bounds";
|
||||||
|
import { measureText } from "./textElement";
|
||||||
|
import { isBoundToContainer } from "./typeChecks";
|
||||||
|
import Scene from "../scene/Scene";
|
||||||
|
import { PADDING } from "../constants";
|
||||||
|
|
||||||
type ElementConstructorOpts = MarkOptional<
|
type ElementConstructorOpts = MarkOptional<
|
||||||
Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted" | "updated">,
|
Omit<ExcalidrawGenericElement, "id" | "type" | "isDeleted" | "updated">,
|
||||||
@ -53,7 +58,8 @@ const _newElementBase = <T extends ExcalidrawElement>(
|
|||||||
boundElements = null,
|
boundElements = null,
|
||||||
...rest
|
...rest
|
||||||
}: ElementConstructorOpts & Omit<Partial<ExcalidrawGenericElement>, "type">,
|
}: ElementConstructorOpts & Omit<Partial<ExcalidrawGenericElement>, "type">,
|
||||||
) => ({
|
) => {
|
||||||
|
const element = {
|
||||||
id: rest.id || randomId(),
|
id: rest.id || randomId(),
|
||||||
type,
|
type,
|
||||||
x,
|
x,
|
||||||
@ -76,7 +82,9 @@ const _newElementBase = <T extends ExcalidrawElement>(
|
|||||||
isDeleted: false as false,
|
isDeleted: false as false,
|
||||||
boundElements,
|
boundElements,
|
||||||
updated: getUpdatedTimestamp(),
|
updated: getUpdatedTimestamp(),
|
||||||
});
|
};
|
||||||
|
return element;
|
||||||
|
};
|
||||||
|
|
||||||
export const newElement = (
|
export const newElement = (
|
||||||
opts: {
|
opts: {
|
||||||
@ -114,6 +122,7 @@ export const newTextElement = (
|
|||||||
fontFamily: FontFamilyValues;
|
fontFamily: FontFamilyValues;
|
||||||
textAlign: TextAlign;
|
textAlign: TextAlign;
|
||||||
verticalAlign: VerticalAlign;
|
verticalAlign: VerticalAlign;
|
||||||
|
containerId?: ExcalidrawRectangleElement["id"];
|
||||||
} & ElementConstructorOpts,
|
} & ElementConstructorOpts,
|
||||||
): NonDeleted<ExcalidrawTextElement> => {
|
): NonDeleted<ExcalidrawTextElement> => {
|
||||||
const metrics = measureText(opts.text, getFontString(opts));
|
const metrics = measureText(opts.text, getFontString(opts));
|
||||||
@ -131,6 +140,8 @@ export const newTextElement = (
|
|||||||
width: metrics.width,
|
width: metrics.width,
|
||||||
height: metrics.height,
|
height: metrics.height,
|
||||||
baseline: metrics.baseline,
|
baseline: metrics.baseline,
|
||||||
|
containerId: opts.containerId || null,
|
||||||
|
originalText: opts.text,
|
||||||
},
|
},
|
||||||
{},
|
{},
|
||||||
);
|
);
|
||||||
@ -147,18 +158,25 @@ const getAdjustedDimensions = (
|
|||||||
height: number;
|
height: number;
|
||||||
baseline: number;
|
baseline: number;
|
||||||
} => {
|
} => {
|
||||||
|
const maxWidth = element.containerId ? element.width : null;
|
||||||
const {
|
const {
|
||||||
width: nextWidth,
|
width: nextWidth,
|
||||||
height: nextHeight,
|
height: nextHeight,
|
||||||
baseline: nextBaseline,
|
baseline: nextBaseline,
|
||||||
} = measureText(nextText, getFontString(element));
|
} = measureText(nextText, getFontString(element), maxWidth);
|
||||||
const { textAlign, verticalAlign } = element;
|
const { textAlign, verticalAlign } = element;
|
||||||
|
|
||||||
let x: number;
|
let x: number;
|
||||||
let y: number;
|
let y: number;
|
||||||
|
if (
|
||||||
if (textAlign === "center" && verticalAlign === "middle") {
|
textAlign === "center" &&
|
||||||
const prevMetrics = measureText(element.text, getFontString(element));
|
verticalAlign === "middle" &&
|
||||||
|
!element.containerId
|
||||||
|
) {
|
||||||
|
const prevMetrics = measureText(
|
||||||
|
element.text,
|
||||||
|
getFontString(element),
|
||||||
|
maxWidth,
|
||||||
|
);
|
||||||
const offsets = getTextElementPositionOffsets(element, {
|
const offsets = getTextElementPositionOffsets(element, {
|
||||||
width: nextWidth - prevMetrics.width,
|
width: nextWidth - prevMetrics.width,
|
||||||
height: nextHeight - prevMetrics.height,
|
height: nextHeight - prevMetrics.height,
|
||||||
@ -195,6 +213,22 @@ const getAdjustedDimensions = (
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure container dimensions are set properly when
|
||||||
|
// text editor overflows beyond viewport dimensions
|
||||||
|
if (isBoundToContainer(element)) {
|
||||||
|
const container = Scene.getScene(element)!.getElement(element.containerId)!;
|
||||||
|
let height = container.height;
|
||||||
|
let width = container.width;
|
||||||
|
if (nextHeight > height - PADDING * 2) {
|
||||||
|
height = nextHeight + PADDING * 2;
|
||||||
|
}
|
||||||
|
if (nextWidth > width - PADDING * 2) {
|
||||||
|
width = nextWidth + PADDING * 2;
|
||||||
|
}
|
||||||
|
if (height !== container.height || width !== container.width) {
|
||||||
|
mutateElement(container, { height, width });
|
||||||
|
}
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
width: nextWidth,
|
width: nextWidth,
|
||||||
height: nextHeight,
|
height: nextHeight,
|
||||||
@ -206,12 +240,22 @@ const getAdjustedDimensions = (
|
|||||||
|
|
||||||
export const updateTextElement = (
|
export const updateTextElement = (
|
||||||
element: ExcalidrawTextElement,
|
element: ExcalidrawTextElement,
|
||||||
{ text, isDeleted }: { text: string; isDeleted?: boolean },
|
{
|
||||||
|
text,
|
||||||
|
isDeleted,
|
||||||
|
originalText,
|
||||||
|
}: { text: string; isDeleted?: boolean; originalText: string },
|
||||||
|
|
||||||
|
updateDimensions: boolean,
|
||||||
): ExcalidrawTextElement => {
|
): ExcalidrawTextElement => {
|
||||||
|
const dimensions = updateDimensions
|
||||||
|
? getAdjustedDimensions(element, text)
|
||||||
|
: undefined;
|
||||||
return newElementWith(element, {
|
return newElementWith(element, {
|
||||||
text,
|
text,
|
||||||
|
originalText,
|
||||||
isDeleted: isDeleted ?? element.isDeleted,
|
isDeleted: isDeleted ?? element.isDeleted,
|
||||||
...getAdjustedDimensions(element, text),
|
...dimensions,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ import {
|
|||||||
} from "./typeChecks";
|
} from "./typeChecks";
|
||||||
import { mutateElement } from "./mutateElement";
|
import { mutateElement } from "./mutateElement";
|
||||||
import { getPerfectElementSize } from "./sizeHelpers";
|
import { getPerfectElementSize } from "./sizeHelpers";
|
||||||
import { measureText, getFontString } from "../utils";
|
import { getFontString } from "../utils";
|
||||||
import { updateBoundElements } from "./binding";
|
import { updateBoundElements } from "./binding";
|
||||||
import {
|
import {
|
||||||
TransformHandleType,
|
TransformHandleType,
|
||||||
@ -33,6 +33,13 @@ import {
|
|||||||
TransformHandleDirection,
|
TransformHandleDirection,
|
||||||
} from "./transformHandles";
|
} from "./transformHandles";
|
||||||
import { Point, PointerDownState } from "../types";
|
import { Point, PointerDownState } from "../types";
|
||||||
|
import Scene from "../scene/Scene";
|
||||||
|
import {
|
||||||
|
getApproxMinLineWidth,
|
||||||
|
getBoundTextElementId,
|
||||||
|
handleBindTextResize,
|
||||||
|
measureText,
|
||||||
|
} from "./textElement";
|
||||||
|
|
||||||
export const normalizeAngle = (angle: number): number => {
|
export const normalizeAngle = (angle: number): number => {
|
||||||
if (angle >= 2 * Math.PI) {
|
if (angle >= 2 * Math.PI) {
|
||||||
@ -132,6 +139,7 @@ export const transformElements = (
|
|||||||
pointerX,
|
pointerX,
|
||||||
pointerY,
|
pointerY,
|
||||||
);
|
);
|
||||||
|
handleBindTextResize(selectedElements, transformHandleType);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -154,6 +162,11 @@ const rotateSingleElement = (
|
|||||||
}
|
}
|
||||||
angle = normalizeAngle(angle);
|
angle = normalizeAngle(angle);
|
||||||
mutateElement(element, { angle });
|
mutateElement(element, { angle });
|
||||||
|
const boundTextElementId = getBoundTextElementId(element);
|
||||||
|
if (boundTextElementId) {
|
||||||
|
const textElement = Scene.getScene(element)!.getElement(boundTextElementId);
|
||||||
|
mutateElement(textElement!, { angle });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// used in DEV only
|
// used in DEV only
|
||||||
@ -272,6 +285,7 @@ const measureFontSizeFromWH = (
|
|||||||
const metrics = measureText(
|
const metrics = measureText(
|
||||||
element.text,
|
element.text,
|
||||||
getFontString({ fontSize: nextFontSize, fontFamily: element.fontFamily }),
|
getFontString({ fontSize: nextFontSize, fontFamily: element.fontFamily }),
|
||||||
|
element.containerId ? element.width : null,
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
size: nextFontSize,
|
size: nextFontSize,
|
||||||
@ -413,6 +427,9 @@ export const resizeSingleElement = (
|
|||||||
element.width,
|
element.width,
|
||||||
element.height,
|
element.height,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const boundTextElementId = getBoundTextElementId(element);
|
||||||
|
|
||||||
const boundsCurrentWidth = esx2 - esx1;
|
const boundsCurrentWidth = esx2 - esx1;
|
||||||
const boundsCurrentHeight = esy2 - esy1;
|
const boundsCurrentHeight = esy2 - esy1;
|
||||||
|
|
||||||
@ -473,6 +490,11 @@ export const resizeSingleElement = (
|
|||||||
const newBoundsWidth = newBoundsX2 - newBoundsX1;
|
const newBoundsWidth = newBoundsX2 - newBoundsX1;
|
||||||
const newBoundsHeight = newBoundsY2 - newBoundsY1;
|
const newBoundsHeight = newBoundsY2 - newBoundsY1;
|
||||||
|
|
||||||
|
// don't allow resize to negative dimensions when text is bounded to container
|
||||||
|
if ((newBoundsWidth < 0 || newBoundsHeight < 0) && boundTextElementId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate new topLeft based on fixed corner during resize
|
// Calculate new topLeft based on fixed corner during resize
|
||||||
let newTopLeft = [...startTopLeft] as [number, number];
|
let newTopLeft = [...startTopLeft] as [number, number];
|
||||||
if (["n", "w", "nw"].includes(transformHandleDirection)) {
|
if (["n", "w", "nw"].includes(transformHandleDirection)) {
|
||||||
@ -565,9 +587,16 @@ export const resizeSingleElement = (
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
let minWidth = 0;
|
||||||
|
if (boundTextElementId) {
|
||||||
|
const boundTextElement = Scene.getScene(element)!.getElement(
|
||||||
|
boundTextElementId,
|
||||||
|
) as ExcalidrawTextElement;
|
||||||
|
minWidth = getApproxMinLineWidth(getFontString(boundTextElement));
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
resizedElement.width !== 0 &&
|
resizedElement.width > minWidth &&
|
||||||
resizedElement.height !== 0 &&
|
resizedElement.height !== 0 &&
|
||||||
Number.isFinite(resizedElement.x) &&
|
Number.isFinite(resizedElement.x) &&
|
||||||
Number.isFinite(resizedElement.y)
|
Number.isFinite(resizedElement.y)
|
||||||
@ -576,6 +605,7 @@ export const resizeSingleElement = (
|
|||||||
newSize: { width: resizedElement.width, height: resizedElement.height },
|
newSize: { width: resizedElement.width, height: resizedElement.height },
|
||||||
});
|
});
|
||||||
mutateElement(element, resizedElement);
|
mutateElement(element, resizedElement);
|
||||||
|
handleBindTextResize([element], transformHandleDirection);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -647,7 +677,7 @@ const resizeMultipleElements = (
|
|||||||
const width = element.width * scale;
|
const width = element.width * scale;
|
||||||
const height = element.height * scale;
|
const height = element.height * scale;
|
||||||
let font: { fontSize?: number; baseline?: number } = {};
|
let font: { fontSize?: number; baseline?: number } = {};
|
||||||
if (element.type === "text") {
|
if (isTextElement(element)) {
|
||||||
const nextFont = measureFontSizeFromWH(element, width, height);
|
const nextFont = measureFontSizeFromWH(element, width, height);
|
||||||
if (nextFont === null) {
|
if (nextFont === null) {
|
||||||
return null;
|
return null;
|
||||||
@ -728,6 +758,16 @@ const rotateMultipleElements = (
|
|||||||
y: element.y + (rotatedCY - cy),
|
y: element.y + (rotatedCY - cy),
|
||||||
angle: normalizeAngle(centerAngle + origAngle),
|
angle: normalizeAngle(centerAngle + origAngle),
|
||||||
});
|
});
|
||||||
|
const boundTextElementId = getBoundTextElementId(element);
|
||||||
|
if (boundTextElementId) {
|
||||||
|
const textElement =
|
||||||
|
Scene.getScene(element)!.getElement(boundTextElementId)!;
|
||||||
|
mutateElement(textElement, {
|
||||||
|
x: textElement.x + (rotatedCX - cx),
|
||||||
|
y: textElement.y + (rotatedCY - cy),
|
||||||
|
angle: normalizeAngle(centerAngle + origAngle),
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,12 +1,389 @@
|
|||||||
import { measureText, getFontString } from "../utils";
|
import { getFontString, arrayToMap } from "../utils";
|
||||||
import { ExcalidrawTextElement } from "./types";
|
import {
|
||||||
|
ExcalidrawBindableElement,
|
||||||
|
ExcalidrawElement,
|
||||||
|
ExcalidrawTextElement,
|
||||||
|
FontString,
|
||||||
|
NonDeletedExcalidrawElement,
|
||||||
|
} from "./types";
|
||||||
import { mutateElement } from "./mutateElement";
|
import { mutateElement } from "./mutateElement";
|
||||||
|
import { PADDING } from "../constants";
|
||||||
|
import { MaybeTransformHandleType } from "./transformHandles";
|
||||||
|
import Scene from "../scene/Scene";
|
||||||
|
|
||||||
export const redrawTextBoundingBox = (element: ExcalidrawTextElement) => {
|
export const redrawTextBoundingBox = (element: ExcalidrawTextElement) => {
|
||||||
const metrics = measureText(element.text, getFontString(element));
|
let maxWidth;
|
||||||
|
if (element.containerId) {
|
||||||
|
maxWidth = element.width;
|
||||||
|
}
|
||||||
|
const metrics = measureText(
|
||||||
|
element.originalText,
|
||||||
|
getFontString(element),
|
||||||
|
maxWidth,
|
||||||
|
);
|
||||||
|
|
||||||
mutateElement(element, {
|
mutateElement(element, {
|
||||||
width: metrics.width,
|
width: metrics.width,
|
||||||
height: metrics.height,
|
height: metrics.height,
|
||||||
baseline: metrics.baseline,
|
baseline: metrics.baseline,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const bindTextToShapeAfterDuplication = (
|
||||||
|
sceneElements: ExcalidrawElement[],
|
||||||
|
oldElements: ExcalidrawElement[],
|
||||||
|
oldIdToDuplicatedId: Map<ExcalidrawElement["id"], ExcalidrawElement["id"]>,
|
||||||
|
): void => {
|
||||||
|
const sceneElementMap = arrayToMap(sceneElements) as Map<
|
||||||
|
ExcalidrawElement["id"],
|
||||||
|
ExcalidrawElement
|
||||||
|
>;
|
||||||
|
oldElements.forEach((element) => {
|
||||||
|
const newElementId = oldIdToDuplicatedId.get(element.id) as string;
|
||||||
|
const boundTextElementId = getBoundTextElementId(element);
|
||||||
|
|
||||||
|
if (boundTextElementId) {
|
||||||
|
const newTextElementId = oldIdToDuplicatedId.get(boundTextElementId)!;
|
||||||
|
mutateElement(
|
||||||
|
sceneElementMap.get(newElementId) as ExcalidrawBindableElement,
|
||||||
|
{
|
||||||
|
boundElements: element.boundElements?.concat({
|
||||||
|
type: "text",
|
||||||
|
id: newTextElementId,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
mutateElement(
|
||||||
|
sceneElementMap.get(newTextElementId) as ExcalidrawTextElement,
|
||||||
|
{
|
||||||
|
containerId: newElementId,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const handleBindTextResize = (
|
||||||
|
elements: readonly NonDeletedExcalidrawElement[],
|
||||||
|
transformHandleType: MaybeTransformHandleType,
|
||||||
|
) => {
|
||||||
|
elements.forEach((element) => {
|
||||||
|
const boundTextElementId = getBoundTextElementId(element);
|
||||||
|
if (boundTextElementId) {
|
||||||
|
const textElement = Scene.getScene(element)!.getElement(
|
||||||
|
boundTextElementId,
|
||||||
|
) as ExcalidrawTextElement;
|
||||||
|
if (textElement && textElement.text) {
|
||||||
|
if (!element) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let text = textElement.text;
|
||||||
|
let nextHeight = textElement.height;
|
||||||
|
let containerHeight = element.height;
|
||||||
|
let nextBaseLine = textElement.baseline;
|
||||||
|
if (transformHandleType !== "n" && transformHandleType !== "s") {
|
||||||
|
let minCharWidthTillNow = 0;
|
||||||
|
if (text) {
|
||||||
|
minCharWidthTillNow = getMinCharWidth(getFontString(textElement));
|
||||||
|
// check if the diff has exceeded min char width needed
|
||||||
|
const diff = Math.abs(
|
||||||
|
element.width - textElement.width + PADDING * 2,
|
||||||
|
);
|
||||||
|
if (diff >= minCharWidthTillNow) {
|
||||||
|
text = wrapText(
|
||||||
|
textElement.originalText,
|
||||||
|
getFontString(textElement),
|
||||||
|
element.width,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const dimensions = measureText(
|
||||||
|
text,
|
||||||
|
getFontString(textElement),
|
||||||
|
element.width,
|
||||||
|
);
|
||||||
|
nextHeight = dimensions.height;
|
||||||
|
nextBaseLine = dimensions.baseline;
|
||||||
|
}
|
||||||
|
// increase height in case text element height exceeds
|
||||||
|
if (nextHeight > element.height - PADDING * 2) {
|
||||||
|
containerHeight = nextHeight + PADDING * 2;
|
||||||
|
const diff = containerHeight - element.height;
|
||||||
|
// fix the y coord when resizing from ne/nw/n
|
||||||
|
const updatedY =
|
||||||
|
transformHandleType === "ne" ||
|
||||||
|
transformHandleType === "nw" ||
|
||||||
|
transformHandleType === "n"
|
||||||
|
? element.y - diff
|
||||||
|
: element.y;
|
||||||
|
mutateElement(element, {
|
||||||
|
height: containerHeight,
|
||||||
|
y: updatedY,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedY = element.y + containerHeight / 2 - nextHeight / 2;
|
||||||
|
mutateElement(textElement, {
|
||||||
|
text,
|
||||||
|
// preserve padding and set width correctly
|
||||||
|
width: element.width - PADDING * 2,
|
||||||
|
height: nextHeight,
|
||||||
|
x: element.x + PADDING,
|
||||||
|
y: updatedY,
|
||||||
|
baseline: nextBaseLine,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// https://github.com/grassator/canvas-text-editor/blob/master/lib/FontMetrics.js
|
||||||
|
export const measureText = (
|
||||||
|
text: string,
|
||||||
|
font: FontString,
|
||||||
|
maxWidth?: number | null,
|
||||||
|
) => {
|
||||||
|
text = text
|
||||||
|
.split("\n")
|
||||||
|
// replace empty lines with single space because leading/trailing empty
|
||||||
|
// lines would be stripped from computation
|
||||||
|
.map((x) => x || " ")
|
||||||
|
.join("\n");
|
||||||
|
const container = document.createElement("div");
|
||||||
|
container.style.position = "absolute";
|
||||||
|
container.style.whiteSpace = "pre";
|
||||||
|
container.style.font = font;
|
||||||
|
container.style.minHeight = "1em";
|
||||||
|
|
||||||
|
if (maxWidth) {
|
||||||
|
const lineHeight = getApproxLineHeight(font);
|
||||||
|
container.style.width = `${String(maxWidth)}px`;
|
||||||
|
container.style.maxWidth = `${String(maxWidth)}px`;
|
||||||
|
container.style.overflow = "hidden";
|
||||||
|
container.style.wordBreak = "break-word";
|
||||||
|
container.style.lineHeight = `${String(lineHeight)}px`;
|
||||||
|
container.style.whiteSpace = "pre-wrap";
|
||||||
|
}
|
||||||
|
document.body.appendChild(container);
|
||||||
|
container.innerText = text;
|
||||||
|
|
||||||
|
const span = document.createElement("span");
|
||||||
|
span.style.display = "inline-block";
|
||||||
|
span.style.overflow = "hidden";
|
||||||
|
span.style.width = "1px";
|
||||||
|
span.style.height = "1px";
|
||||||
|
container.appendChild(span);
|
||||||
|
// Baseline is important for positioning text on canvas
|
||||||
|
const baseline = span.offsetTop + span.offsetHeight;
|
||||||
|
const width = container.offsetWidth;
|
||||||
|
|
||||||
|
const height = container.offsetHeight;
|
||||||
|
document.body.removeChild(container);
|
||||||
|
|
||||||
|
return { width, height, baseline };
|
||||||
|
};
|
||||||
|
|
||||||
|
const DUMMY_TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toLocaleUpperCase();
|
||||||
|
export const getApproxLineHeight = (font: FontString) => {
|
||||||
|
return measureText(DUMMY_TEXT, font, null).height;
|
||||||
|
};
|
||||||
|
|
||||||
|
let canvas: HTMLCanvasElement | undefined;
|
||||||
|
const getTextWidth = (text: string, font: FontString) => {
|
||||||
|
if (!canvas) {
|
||||||
|
canvas = document.createElement("canvas");
|
||||||
|
}
|
||||||
|
const canvas2dContext = canvas.getContext("2d")!;
|
||||||
|
canvas2dContext.font = font;
|
||||||
|
|
||||||
|
const metrics = canvas2dContext.measureText(text);
|
||||||
|
|
||||||
|
return metrics.width;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const wrapText = (
|
||||||
|
text: string,
|
||||||
|
font: FontString,
|
||||||
|
containerWidth: number,
|
||||||
|
) => {
|
||||||
|
const maxWidth = containerWidth - PADDING * 2;
|
||||||
|
|
||||||
|
const lines: Array<string> = [];
|
||||||
|
const originalLines = text.split("\n");
|
||||||
|
const spaceWidth = getTextWidth(" ", font);
|
||||||
|
originalLines.forEach((originalLine) => {
|
||||||
|
const words = originalLine.split(" ");
|
||||||
|
// This means its newline so push it
|
||||||
|
if (words.length === 1 && words[0] === "") {
|
||||||
|
lines.push(words[0]);
|
||||||
|
} else {
|
||||||
|
let currentLine = "";
|
||||||
|
let currentLineWidthTillNow = 0;
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
while (index < words.length) {
|
||||||
|
const currentWordWidth = getTextWidth(words[index], font);
|
||||||
|
|
||||||
|
// Start breaking longer words exceeding max width
|
||||||
|
if (currentWordWidth > maxWidth) {
|
||||||
|
// push current line since the current word exceeds the max width
|
||||||
|
// so will be appended in next line
|
||||||
|
if (currentLine) {
|
||||||
|
lines.push(currentLine);
|
||||||
|
}
|
||||||
|
currentLine = "";
|
||||||
|
currentLineWidthTillNow = 0;
|
||||||
|
while (words[index].length > 0) {
|
||||||
|
const currentChar = words[index][0];
|
||||||
|
const width = charWidth.calculate(currentChar, font);
|
||||||
|
currentLineWidthTillNow += width;
|
||||||
|
words[index] = words[index].slice(1);
|
||||||
|
|
||||||
|
if (currentLineWidthTillNow >= maxWidth) {
|
||||||
|
// only remove last trailing space which we have added when joining words
|
||||||
|
if (currentLine.slice(-1) === " ") {
|
||||||
|
currentLine = currentLine.slice(0, -1);
|
||||||
|
}
|
||||||
|
lines.push(currentLine);
|
||||||
|
currentLine = currentChar;
|
||||||
|
currentLineWidthTillNow = width;
|
||||||
|
if (currentLineWidthTillNow === maxWidth) {
|
||||||
|
currentLine = "";
|
||||||
|
currentLineWidthTillNow = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentLine += currentChar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// push current line if appending space exceeds max width
|
||||||
|
if (currentLineWidthTillNow + spaceWidth > maxWidth) {
|
||||||
|
lines.push(currentLine);
|
||||||
|
currentLine = "";
|
||||||
|
currentLineWidthTillNow = 0;
|
||||||
|
} else {
|
||||||
|
// space needs to be appended before next word
|
||||||
|
// as currentLine contains chars which couldn't be appended
|
||||||
|
// to previous line
|
||||||
|
currentLine += " ";
|
||||||
|
currentLineWidthTillNow += spaceWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
index++;
|
||||||
|
} else {
|
||||||
|
// Start appending words in a line till max width reached
|
||||||
|
while (currentLineWidthTillNow < maxWidth && index < words.length) {
|
||||||
|
const word = words[index];
|
||||||
|
currentLineWidthTillNow = getTextWidth(currentLine + word, font);
|
||||||
|
|
||||||
|
if (currentLineWidthTillNow >= maxWidth) {
|
||||||
|
lines.push(currentLine);
|
||||||
|
currentLineWidthTillNow = 0;
|
||||||
|
currentLine = "";
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
currentLine += `${word} `;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentLineWidthTillNow === maxWidth) {
|
||||||
|
currentLine = "";
|
||||||
|
currentLineWidthTillNow = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentLine) {
|
||||||
|
// only remove last trailing space which we have added when joining words
|
||||||
|
if (currentLine.slice(-1) === " ") {
|
||||||
|
currentLine = currentLine.slice(0, -1);
|
||||||
|
}
|
||||||
|
lines.push(currentLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return lines.join("\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const charWidth = (() => {
|
||||||
|
const cachedCharWidth: { [key: FontString]: Array<number> } = {};
|
||||||
|
|
||||||
|
const calculate = (char: string, font: FontString) => {
|
||||||
|
const ascii = char.charCodeAt(0);
|
||||||
|
if (!cachedCharWidth[font]) {
|
||||||
|
cachedCharWidth[font] = [];
|
||||||
|
}
|
||||||
|
if (!cachedCharWidth[font][ascii]) {
|
||||||
|
const width = getTextWidth(char, font);
|
||||||
|
cachedCharWidth[font][ascii] = width;
|
||||||
|
}
|
||||||
|
return cachedCharWidth[font][ascii];
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateCache = (char: string, font: FontString) => {
|
||||||
|
const ascii = char.charCodeAt(0);
|
||||||
|
|
||||||
|
if (!cachedCharWidth[font][ascii]) {
|
||||||
|
cachedCharWidth[font][ascii] = calculate(char, font);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const clearCacheforFont = (font: FontString) => {
|
||||||
|
cachedCharWidth[font] = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCache = (font: FontString) => {
|
||||||
|
return cachedCharWidth[font];
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
calculate,
|
||||||
|
updateCache,
|
||||||
|
clearCacheforFont,
|
||||||
|
getCache,
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
export const getApproxMinLineWidth = (font: FontString) => {
|
||||||
|
return measureText(DUMMY_TEXT.split("").join("\n"), font).width + PADDING * 2;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getApproxMinLineHeight = (font: FontString) => {
|
||||||
|
return getApproxLineHeight(font) + PADDING * 2;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getMinCharWidth = (font: FontString) => {
|
||||||
|
const cache = charWidth.getCache(font);
|
||||||
|
if (!cache) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
const cacheWithOutEmpty = cache.filter((val) => val !== undefined);
|
||||||
|
|
||||||
|
return Math.min(...cacheWithOutEmpty);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getApproxCharsToFitInWidth = (font: FontString, width: number) => {
|
||||||
|
// Generally lower case is used so converting to lower case
|
||||||
|
const dummyText = DUMMY_TEXT.toLocaleLowerCase();
|
||||||
|
const batchLength = 6;
|
||||||
|
let index = 0;
|
||||||
|
let widthTillNow = 0;
|
||||||
|
let str = "";
|
||||||
|
while (widthTillNow <= width) {
|
||||||
|
const batch = dummyText.substr(index, index + batchLength);
|
||||||
|
str += batch;
|
||||||
|
widthTillNow += getTextWidth(str, font);
|
||||||
|
if (index === dummyText.length - 1) {
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
index = index + batchLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (widthTillNow > width) {
|
||||||
|
str = str.substr(0, str.length - 1);
|
||||||
|
widthTillNow = getTextWidth(str, font);
|
||||||
|
}
|
||||||
|
return str.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getBoundTextElementId = (container: ExcalidrawElement | null) => {
|
||||||
|
return container?.boundElements?.filter((ele) => ele.type === "text")[0]?.id;
|
||||||
|
};
|
||||||
|
@ -1,10 +1,25 @@
|
|||||||
import { CODES, KEYS } from "../keys";
|
import { CODES, KEYS } from "../keys";
|
||||||
import { isWritableElement, getFontString } from "../utils";
|
import {
|
||||||
|
isWritableElement,
|
||||||
|
getFontString,
|
||||||
|
viewportCoordsToSceneCoords,
|
||||||
|
getFontFamilyString,
|
||||||
|
} from "../utils";
|
||||||
import Scene from "../scene/Scene";
|
import Scene from "../scene/Scene";
|
||||||
import { isTextElement } from "./typeChecks";
|
import { isTextElement } from "./typeChecks";
|
||||||
import { CLASSES } from "../constants";
|
import { CLASSES, PADDING } from "../constants";
|
||||||
import { ExcalidrawElement } from "./types";
|
import {
|
||||||
|
ExcalidrawBindableElement,
|
||||||
|
ExcalidrawElement,
|
||||||
|
ExcalidrawTextElement,
|
||||||
|
} from "./types";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
|
import { mutateElement } from "./mutateElement";
|
||||||
|
import {
|
||||||
|
getApproxLineHeight,
|
||||||
|
getBoundTextElementId,
|
||||||
|
wrapText,
|
||||||
|
} from "./textElement";
|
||||||
|
|
||||||
const normalizeText = (text: string) => {
|
const normalizeText = (text: string) => {
|
||||||
return (
|
return (
|
||||||
@ -48,26 +63,126 @@ export const textWysiwyg = ({
|
|||||||
id: ExcalidrawElement["id"];
|
id: ExcalidrawElement["id"];
|
||||||
appState: AppState;
|
appState: AppState;
|
||||||
onChange?: (text: string) => void;
|
onChange?: (text: string) => void;
|
||||||
onSubmit: (data: { text: string; viaKeyboard: boolean }) => void;
|
onSubmit: (data: {
|
||||||
|
text: string;
|
||||||
|
viaKeyboard: boolean;
|
||||||
|
originalText: string;
|
||||||
|
}) => void;
|
||||||
getViewportCoords: (x: number, y: number) => [number, number];
|
getViewportCoords: (x: number, y: number) => [number, number];
|
||||||
element: ExcalidrawElement;
|
element: ExcalidrawElement;
|
||||||
canvas: HTMLCanvasElement | null;
|
canvas: HTMLCanvasElement | null;
|
||||||
excalidrawContainer: HTMLDivElement | null;
|
excalidrawContainer: HTMLDivElement | null;
|
||||||
}) => {
|
}) => {
|
||||||
|
const textPropertiesUpdated = (
|
||||||
|
updatedElement: ExcalidrawTextElement,
|
||||||
|
editable: HTMLTextAreaElement,
|
||||||
|
) => {
|
||||||
|
const currentFont = editable.style.fontFamily.replaceAll('"', "");
|
||||||
|
if (
|
||||||
|
getFontFamilyString({ fontFamily: updatedElement.fontFamily }) !==
|
||||||
|
currentFont
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (`${updatedElement.fontSize}px` !== editable.style.fontSize) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
let originalContainerHeight: number;
|
||||||
|
let approxLineHeight = isTextElement(element)
|
||||||
|
? getApproxLineHeight(getFontString(element))
|
||||||
|
: 0;
|
||||||
|
|
||||||
const updateWysiwygStyle = () => {
|
const updateWysiwygStyle = () => {
|
||||||
const updatedElement = Scene.getScene(element)?.getElement(id);
|
const updatedElement = Scene.getScene(element)?.getElement(id);
|
||||||
if (updatedElement && isTextElement(updatedElement)) {
|
if (updatedElement && isTextElement(updatedElement)) {
|
||||||
const [viewportX, viewportY] = getViewportCoords(
|
let coordX = updatedElement.x;
|
||||||
updatedElement.x,
|
let coordY = updatedElement.y;
|
||||||
updatedElement.y,
|
let container = updatedElement?.containerId
|
||||||
|
? Scene.getScene(updatedElement)!.getElement(updatedElement.containerId)
|
||||||
|
: null;
|
||||||
|
let maxWidth = updatedElement.width;
|
||||||
|
|
||||||
|
let maxHeight = updatedElement.height;
|
||||||
|
let width = updatedElement.width;
|
||||||
|
let height = updatedElement.height;
|
||||||
|
if (container && updatedElement.containerId) {
|
||||||
|
const propertiesUpdated = textPropertiesUpdated(
|
||||||
|
updatedElement,
|
||||||
|
editable,
|
||||||
);
|
);
|
||||||
|
if (propertiesUpdated) {
|
||||||
|
const currentContainer = Scene.getScene(updatedElement)?.getElement(
|
||||||
|
updatedElement.containerId,
|
||||||
|
) as ExcalidrawBindableElement;
|
||||||
|
approxLineHeight = isTextElement(updatedElement)
|
||||||
|
? getApproxLineHeight(getFontString(updatedElement))
|
||||||
|
: 0;
|
||||||
|
if (updatedElement.height > currentContainer.height - PADDING * 2) {
|
||||||
|
const nextHeight = updatedElement.height + PADDING * 2;
|
||||||
|
originalContainerHeight = nextHeight;
|
||||||
|
mutateElement(container, { height: nextHeight });
|
||||||
|
container = { ...container, height: nextHeight };
|
||||||
|
}
|
||||||
|
editable.style.height = `${updatedElement.height}px`;
|
||||||
|
}
|
||||||
|
if (!originalContainerHeight) {
|
||||||
|
originalContainerHeight = container.height;
|
||||||
|
}
|
||||||
|
maxWidth = container.width - PADDING * 2;
|
||||||
|
maxHeight = container.height - PADDING * 2;
|
||||||
|
width = maxWidth;
|
||||||
|
height = Math.min(height, maxHeight);
|
||||||
|
// The coordinates of text box set a distance of
|
||||||
|
// 30px to preserve padding
|
||||||
|
coordX = container.x + PADDING;
|
||||||
|
|
||||||
|
// autogrow container height if text exceeds
|
||||||
|
if (editable.clientHeight > maxHeight) {
|
||||||
|
const diff = Math.min(
|
||||||
|
editable.clientHeight - maxHeight,
|
||||||
|
approxLineHeight,
|
||||||
|
);
|
||||||
|
mutateElement(container, { height: container.height + diff });
|
||||||
|
return;
|
||||||
|
} else if (
|
||||||
|
// autoshrink container height until original container height
|
||||||
|
// is reached when text is removed
|
||||||
|
container.height > originalContainerHeight &&
|
||||||
|
editable.clientHeight < maxHeight
|
||||||
|
) {
|
||||||
|
const diff = Math.min(
|
||||||
|
maxHeight - editable.clientHeight,
|
||||||
|
approxLineHeight,
|
||||||
|
);
|
||||||
|
mutateElement(container, { height: container.height - diff });
|
||||||
|
}
|
||||||
|
// Start pushing text upward until a diff of 30px (padding)
|
||||||
|
// is reached
|
||||||
|
else {
|
||||||
|
const lines = editable.clientHeight / approxLineHeight;
|
||||||
|
// For some reason the scrollHeight gets set to twice the lineHeight
|
||||||
|
// when you start typing for first time and thus line count is 2
|
||||||
|
// hence this check
|
||||||
|
if (lines > 2 || propertiesUpdated) {
|
||||||
|
// vertically center align the text
|
||||||
|
coordY =
|
||||||
|
container.y + container.height / 2 - editable.clientHeight / 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const [viewportX, viewportY] = getViewportCoords(coordX, coordY);
|
||||||
const { textAlign, angle } = updatedElement;
|
const { textAlign, angle } = updatedElement;
|
||||||
|
|
||||||
editable.value = updatedElement.text;
|
editable.value = updatedElement.originalText || updatedElement.text;
|
||||||
|
const lines = updatedElement.originalText.split("\n");
|
||||||
const lines = updatedElement.text.replace(/\r\n?/g, "\n").split("\n");
|
const lineHeight = updatedElement.containerId
|
||||||
const lineHeight = updatedElement.height / lines.length;
|
? approxLineHeight
|
||||||
const maxWidth =
|
: updatedElement.height / lines.length;
|
||||||
|
if (!container) {
|
||||||
|
maxWidth =
|
||||||
(appState.offsetLeft + appState.width - viewportX - 8) /
|
(appState.offsetLeft + appState.width - viewportX - 8) /
|
||||||
appState.zoom.value -
|
appState.zoom.value -
|
||||||
// margin-right of parent if any
|
// margin-right of parent if any
|
||||||
@ -76,27 +191,26 @@ export const textWysiwyg = ({
|
|||||||
excalidrawContainer?.parentNode as Element,
|
excalidrawContainer?.parentNode as Element,
|
||||||
).marginRight.slice(0, -2),
|
).marginRight.slice(0, -2),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
// Make sure text editor height doesn't go beyond viewport
|
||||||
|
const editorMaxHeight =
|
||||||
|
(appState.offsetTop + appState.height - viewportY) /
|
||||||
|
appState.zoom.value;
|
||||||
Object.assign(editable.style, {
|
Object.assign(editable.style, {
|
||||||
font: getFontString(updatedElement),
|
font: getFontString(updatedElement),
|
||||||
// must be defined *after* font ¯\_(ツ)_/¯
|
// must be defined *after* font ¯\_(ツ)_/¯
|
||||||
lineHeight: `${lineHeight}px`,
|
lineHeight: `${lineHeight}px`,
|
||||||
width: `${updatedElement.width}px`,
|
width: `${width}px`,
|
||||||
height: `${updatedElement.height}px`,
|
height: `${Math.max(editable.clientHeight, updatedElement.height)}px`,
|
||||||
left: `${viewportX}px`,
|
left: `${viewportX}px`,
|
||||||
top: `${viewportY}px`,
|
top: `${viewportY}px`,
|
||||||
transform: getTransform(
|
transform: getTransform(width, height, angle, appState, maxWidth),
|
||||||
updatedElement.width,
|
|
||||||
updatedElement.height,
|
|
||||||
angle,
|
|
||||||
appState,
|
|
||||||
maxWidth,
|
|
||||||
),
|
|
||||||
textAlign,
|
textAlign,
|
||||||
color: updatedElement.strokeColor,
|
color: updatedElement.strokeColor,
|
||||||
opacity: updatedElement.opacity / 100,
|
opacity: updatedElement.opacity / 100,
|
||||||
filter: "var(--theme-filter)",
|
filter: "var(--theme-filter)",
|
||||||
maxWidth: `${maxWidth}px`,
|
maxWidth: `${maxWidth}px`,
|
||||||
|
maxHeight: `${editorMaxHeight}px`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -110,6 +224,10 @@ export const textWysiwyg = ({
|
|||||||
editable.wrap = "off";
|
editable.wrap = "off";
|
||||||
editable.classList.add("excalidraw-wysiwyg");
|
editable.classList.add("excalidraw-wysiwyg");
|
||||||
|
|
||||||
|
let whiteSpace = "pre";
|
||||||
|
if (isTextElement(element)) {
|
||||||
|
whiteSpace = element.containerId ? "pre-wrap" : "pre";
|
||||||
|
}
|
||||||
Object.assign(editable.style, {
|
Object.assign(editable.style, {
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
display: "inline-block",
|
display: "inline-block",
|
||||||
@ -122,16 +240,19 @@ export const textWysiwyg = ({
|
|||||||
resize: "none",
|
resize: "none",
|
||||||
background: "transparent",
|
background: "transparent",
|
||||||
overflow: "hidden",
|
overflow: "hidden",
|
||||||
// prevent line wrapping (`whitespace: nowrap` doesn't work on FF)
|
|
||||||
whiteSpace: "pre",
|
|
||||||
// must be specified because in dark mode canvas creates a stacking context
|
// must be specified because in dark mode canvas creates a stacking context
|
||||||
zIndex: "var(--zIndex-wysiwyg)",
|
zIndex: "var(--zIndex-wysiwyg)",
|
||||||
|
wordBreak: "break-word",
|
||||||
|
// prevent line wrapping (`whitespace: nowrap` doesn't work on FF)
|
||||||
|
whiteSpace,
|
||||||
|
overflowWrap: "break-word",
|
||||||
});
|
});
|
||||||
|
|
||||||
updateWysiwygStyle();
|
updateWysiwygStyle();
|
||||||
|
|
||||||
if (onChange) {
|
if (onChange) {
|
||||||
editable.oninput = () => {
|
editable.oninput = () => {
|
||||||
|
editable.style.height = "auto";
|
||||||
|
editable.style.height = `${editable.scrollHeight}px`;
|
||||||
onChange(normalizeText(editable.value));
|
onChange(normalizeText(editable.value));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -174,7 +295,7 @@ export const textWysiwyg = ({
|
|||||||
const linesStartIndices = getSelectedLinesStartIndices();
|
const linesStartIndices = getSelectedLinesStartIndices();
|
||||||
|
|
||||||
let value = editable.value;
|
let value = editable.value;
|
||||||
linesStartIndices.forEach((startIndex) => {
|
linesStartIndices.forEach((startIndex: number) => {
|
||||||
const startValue = value.slice(0, startIndex);
|
const startValue = value.slice(0, startIndex);
|
||||||
const endValue = value.slice(startIndex);
|
const endValue = value.slice(startIndex);
|
||||||
|
|
||||||
@ -274,9 +395,63 @@ export const textWysiwyg = ({
|
|||||||
// it'd get stuck in an infinite loop of blur→onSubmit after we re-focus the
|
// it'd get stuck in an infinite loop of blur→onSubmit after we re-focus the
|
||||||
// wysiwyg on update
|
// wysiwyg on update
|
||||||
cleanup();
|
cleanup();
|
||||||
|
const updateElement = Scene.getScene(element)?.getElement(element.id);
|
||||||
|
if (!updateElement) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let wrappedText = "";
|
||||||
|
if (isTextElement(updateElement) && updateElement?.containerId) {
|
||||||
|
const container = Scene.getScene(updateElement)!.getElement(
|
||||||
|
updateElement.containerId,
|
||||||
|
) as ExcalidrawBindableElement;
|
||||||
|
|
||||||
|
if (container) {
|
||||||
|
wrappedText = wrapText(
|
||||||
|
editable.value,
|
||||||
|
getFontString(updateElement),
|
||||||
|
container.width,
|
||||||
|
);
|
||||||
|
const { x, y } = viewportCoordsToSceneCoords(
|
||||||
|
{
|
||||||
|
clientX: Number(editable.style.left.slice(0, -2)),
|
||||||
|
clientY: Number(editable.style.top.slice(0, -2)),
|
||||||
|
},
|
||||||
|
appState,
|
||||||
|
);
|
||||||
|
if (isTextElement(updateElement) && updateElement.containerId) {
|
||||||
|
if (editable.value) {
|
||||||
|
mutateElement(updateElement, {
|
||||||
|
y,
|
||||||
|
height: Number(editable.style.height.slice(0, -2)),
|
||||||
|
width: Number(editable.style.width.slice(0, -2)),
|
||||||
|
x,
|
||||||
|
});
|
||||||
|
const boundTextElementId = getBoundTextElementId(container);
|
||||||
|
if (!boundTextElementId || boundTextElementId !== element.id) {
|
||||||
|
mutateElement(container, {
|
||||||
|
boundElements: (container.boundElements || []).concat({
|
||||||
|
type: "text",
|
||||||
|
id: element.id,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mutateElement(container, {
|
||||||
|
boundElements: container.boundElements?.filter(
|
||||||
|
(ele) => ele.type !== "text",
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
wrappedText = editable.value;
|
||||||
|
}
|
||||||
|
|
||||||
onSubmit({
|
onSubmit({
|
||||||
text: normalizeText(editable.value),
|
text: normalizeText(wrappedText),
|
||||||
viaKeyboard: submittedViaKeyboard,
|
viaKeyboard: submittedViaKeyboard,
|
||||||
|
originalText: editable.value,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import { ExcalidrawElement, PointerType } from "./types";
|
|||||||
import { getElementAbsoluteCoords, Bounds } from "./bounds";
|
import { getElementAbsoluteCoords, Bounds } from "./bounds";
|
||||||
import { rotate } from "../math";
|
import { rotate } from "../math";
|
||||||
import { Zoom } from "../types";
|
import { Zoom } from "../types";
|
||||||
|
import { isTextElement } from ".";
|
||||||
|
|
||||||
export type TransformHandleDirection =
|
export type TransformHandleDirection =
|
||||||
| "n"
|
| "n"
|
||||||
@ -242,7 +243,7 @@ export const getTransformHandles = (
|
|||||||
omitSides = OMIT_SIDES_FOR_LINE_BACKSLASH;
|
omitSides = OMIT_SIDES_FOR_LINE_BACKSLASH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (element.type === "text") {
|
} else if (isTextElement(element)) {
|
||||||
omitSides = OMIT_SIDES_FOR_TEXT_ELEMENT;
|
omitSides = OMIT_SIDES_FOR_TEXT_ELEMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import {
|
|||||||
ExcalidrawFreeDrawElement,
|
ExcalidrawFreeDrawElement,
|
||||||
InitializedExcalidrawImageElement,
|
InitializedExcalidrawImageElement,
|
||||||
ExcalidrawImageElement,
|
ExcalidrawImageElement,
|
||||||
|
ExcalidrawTextElementWithContainer,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
export const isGenericElement = (
|
export const isGenericElement = (
|
||||||
@ -86,7 +87,17 @@ export const isBindableElement = (
|
|||||||
element.type === "diamond" ||
|
element.type === "diamond" ||
|
||||||
element.type === "ellipse" ||
|
element.type === "ellipse" ||
|
||||||
element.type === "image" ||
|
element.type === "image" ||
|
||||||
element.type === "text")
|
(element.type === "text" && !element.containerId))
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isTextBindableContainer = (element: ExcalidrawElement | null) => {
|
||||||
|
return (
|
||||||
|
element != null &&
|
||||||
|
(element.type === "rectangle" ||
|
||||||
|
element.type === "diamond" ||
|
||||||
|
element.type === "ellipse" ||
|
||||||
|
element.type === "image")
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -101,3 +112,20 @@ export const isExcalidrawElement = (element: any): boolean => {
|
|||||||
element?.type === "line"
|
element?.type === "line"
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const hasBoundTextElement = (
|
||||||
|
element: ExcalidrawElement | null,
|
||||||
|
): element is ExcalidrawBindableElement => {
|
||||||
|
return (
|
||||||
|
isBindableElement(element) &&
|
||||||
|
!!element.boundElements?.some(({ type }) => type === "text")
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const isBoundToContainer = (
|
||||||
|
element: ExcalidrawElement | null,
|
||||||
|
): element is ExcalidrawTextElementWithContainer => {
|
||||||
|
return (
|
||||||
|
element !== null && isTextElement(element) && element.containerId !== null
|
||||||
|
);
|
||||||
|
};
|
||||||
|
@ -121,6 +121,8 @@ export type ExcalidrawTextElement = _ExcalidrawElementBase &
|
|||||||
baseline: number;
|
baseline: number;
|
||||||
textAlign: TextAlign;
|
textAlign: TextAlign;
|
||||||
verticalAlign: VerticalAlign;
|
verticalAlign: VerticalAlign;
|
||||||
|
containerId: ExcalidrawGenericElement["id"] | null;
|
||||||
|
originalText: string;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
export type ExcalidrawBindableElement =
|
export type ExcalidrawBindableElement =
|
||||||
@ -130,6 +132,10 @@ export type ExcalidrawBindableElement =
|
|||||||
| ExcalidrawTextElement
|
| ExcalidrawTextElement
|
||||||
| ExcalidrawImageElement;
|
| ExcalidrawImageElement;
|
||||||
|
|
||||||
|
export type ExcalidrawTextElementWithContainer = {
|
||||||
|
containerId: ExcalidrawGenericElement["id"];
|
||||||
|
} & ExcalidrawTextElement;
|
||||||
|
|
||||||
export type PointBinding = {
|
export type PointBinding = {
|
||||||
elementId: ExcalidrawBindableElement["id"];
|
elementId: ExcalidrawBindableElement["id"];
|
||||||
focus: number;
|
focus: number;
|
||||||
|
@ -207,7 +207,8 @@
|
|||||||
"lineEditor_pointSelected": "Press Delete to remove point(s),\nCtrlOrCmd+D to duplicate, or drag to move",
|
"lineEditor_pointSelected": "Press Delete to remove point(s),\nCtrlOrCmd+D to duplicate, or drag to move",
|
||||||
"lineEditor_nothingSelected": "Select a point to edit (hold SHIFT to select multiple),\nor hold Alt and click to add new points",
|
"lineEditor_nothingSelected": "Select a point to edit (hold SHIFT to select multiple),\nor hold Alt and click to add new points",
|
||||||
"placeImage": "Click to place the image, or click and drag to set its size manually",
|
"placeImage": "Click to place the image, or click and drag to set its size manually",
|
||||||
"publishLibrary": "Publish your own library"
|
"publishLibrary": "Publish your own library",
|
||||||
|
"bindTextToElement": "Press enter to add text"
|
||||||
},
|
},
|
||||||
"canvasError": {
|
"canvasError": {
|
||||||
"cannotShowPreview": "Cannot show preview",
|
"cannotShowPreview": "Cannot show preview",
|
||||||
|
@ -22,6 +22,7 @@ import { RoughCanvas } from "roughjs/bin/canvas";
|
|||||||
import { Drawable, Options } from "roughjs/bin/core";
|
import { Drawable, Options } from "roughjs/bin/core";
|
||||||
import { RoughSVG } from "roughjs/bin/svg";
|
import { RoughSVG } from "roughjs/bin/svg";
|
||||||
import { RoughGenerator } from "roughjs/bin/generator";
|
import { RoughGenerator } from "roughjs/bin/generator";
|
||||||
|
|
||||||
import { RenderConfig } from "../scene/types";
|
import { RenderConfig } from "../scene/types";
|
||||||
import { distance, getFontString, getFontFamilyString, isRTL } from "../utils";
|
import { distance, getFontString, getFontFamilyString, isRTL } from "../utils";
|
||||||
import { isPathALoop } from "../math";
|
import { isPathALoop } from "../math";
|
||||||
@ -30,6 +31,7 @@ import { AppState, BinaryFiles, Zoom } from "../types";
|
|||||||
import { getDefaultAppState } from "../appState";
|
import { getDefaultAppState } from "../appState";
|
||||||
import { MAX_DECIMALS_FOR_SVG_EXPORT, MIME_TYPES, SVG_NS } from "../constants";
|
import { MAX_DECIMALS_FOR_SVG_EXPORT, MIME_TYPES, SVG_NS } from "../constants";
|
||||||
import { getStroke, StrokeOptions } from "perfect-freehand";
|
import { getStroke, StrokeOptions } from "perfect-freehand";
|
||||||
|
import { getApproxLineHeight } from "../element/textElement";
|
||||||
|
|
||||||
// using a stronger invert (100% vs our regular 93%) and saturate
|
// using a stronger invert (100% vs our regular 93%) and saturate
|
||||||
// as a temp hack to make images in dark theme look closer to original
|
// as a temp hack to make images in dark theme look closer to original
|
||||||
@ -252,7 +254,9 @@ const drawElementOnCanvas = (
|
|||||||
|
|
||||||
// Canvas does not support multiline text by default
|
// Canvas does not support multiline text by default
|
||||||
const lines = element.text.replace(/\r\n?/g, "\n").split("\n");
|
const lines = element.text.replace(/\r\n?/g, "\n").split("\n");
|
||||||
const lineHeight = element.height / lines.length;
|
const lineHeight = element.containerId
|
||||||
|
? getApproxLineHeight(getFontString(element))
|
||||||
|
: element.height / lines.length;
|
||||||
const verticalOffset = element.height - element.baseline;
|
const verticalOffset = element.height - element.baseline;
|
||||||
const horizontalOffset =
|
const horizontalOffset =
|
||||||
element.textAlign === "center"
|
element.textAlign === "center"
|
||||||
|
@ -75,6 +75,7 @@ export const getElementContainingPosition = (
|
|||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
|
excludedType?: ExcalidrawElement["type"],
|
||||||
) => {
|
) => {
|
||||||
let hitElement = null;
|
let hitElement = null;
|
||||||
// We need to to hit testing from front (end of the array) to back (beginning of the array)
|
// We need to to hit testing from front (end of the array) to back (beginning of the array)
|
||||||
@ -83,7 +84,13 @@ export const getElementContainingPosition = (
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const [x1, y1, x2, y2] = getElementAbsoluteCoords(elements[index]);
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(elements[index]);
|
||||||
if (x1 < x && x < x2 && y1 < y && y < y2) {
|
if (
|
||||||
|
x1 < x &&
|
||||||
|
x < x2 &&
|
||||||
|
y1 < y &&
|
||||||
|
y < y2 &&
|
||||||
|
elements[index].type !== excludedType
|
||||||
|
) {
|
||||||
hitElement = elements[index];
|
hitElement = elements[index];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import {
|
|||||||
} from "../element/types";
|
} from "../element/types";
|
||||||
import { getElementAbsoluteCoords, getElementBounds } from "../element";
|
import { getElementAbsoluteCoords, getElementBounds } from "../element";
|
||||||
import { AppState } from "../types";
|
import { AppState } from "../types";
|
||||||
|
import { isBoundToContainer } from "../element/typeChecks";
|
||||||
|
|
||||||
export const getElementsWithinSelection = (
|
export const getElementsWithinSelection = (
|
||||||
elements: readonly NonDeletedExcalidrawElement[],
|
elements: readonly NonDeletedExcalidrawElement[],
|
||||||
@ -17,6 +18,7 @@ export const getElementsWithinSelection = (
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
element.type !== "selection" &&
|
element.type !== "selection" &&
|
||||||
|
!isBoundToContainer(element) &&
|
||||||
selectionX1 <= elementX1 &&
|
selectionX1 <= elementX1 &&
|
||||||
selectionY1 <= elementY1 &&
|
selectionY1 <= elementY1 &&
|
||||||
selectionX2 >= elementX2 &&
|
selectionX2 >= elementX2 &&
|
||||||
@ -53,7 +55,21 @@ export const getCommonAttributeOfSelectedElements = <T>(
|
|||||||
export const getSelectedElements = (
|
export const getSelectedElements = (
|
||||||
elements: readonly NonDeletedExcalidrawElement[],
|
elements: readonly NonDeletedExcalidrawElement[],
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
) => elements.filter((element) => appState.selectedElementIds[element.id]);
|
includeBoundTextElement: boolean = false,
|
||||||
|
) =>
|
||||||
|
elements.filter((element) => {
|
||||||
|
if (appState.selectedElementIds[element.id]) {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
includeBoundTextElement &&
|
||||||
|
isBoundToContainer(element) &&
|
||||||
|
appState.selectedElementIds[element?.containerId]
|
||||||
|
) {
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
export const getTargetElements = (
|
export const getTargetElements = (
|
||||||
elements: readonly NonDeletedExcalidrawElement[],
|
elements: readonly NonDeletedExcalidrawElement[],
|
||||||
|
@ -256,6 +256,7 @@ Object {
|
|||||||
"backgroundColor": "transparent",
|
"backgroundColor": "transparent",
|
||||||
"baseline": 0,
|
"baseline": 0,
|
||||||
"boundElements": Array [],
|
"boundElements": Array [],
|
||||||
|
"containerId": null,
|
||||||
"fillStyle": "hachure",
|
"fillStyle": "hachure",
|
||||||
"fontFamily": 1,
|
"fontFamily": 1,
|
||||||
"fontSize": 14,
|
"fontSize": 14,
|
||||||
@ -264,6 +265,7 @@ Object {
|
|||||||
"id": "id-text01",
|
"id": "id-text01",
|
||||||
"isDeleted": false,
|
"isDeleted": false,
|
||||||
"opacity": 100,
|
"opacity": 100,
|
||||||
|
"originalText": "text",
|
||||||
"roughness": 1,
|
"roughness": 1,
|
||||||
"seed": Any<Number>,
|
"seed": Any<Number>,
|
||||||
"strokeColor": "#000000",
|
"strokeColor": "#000000",
|
||||||
@ -289,6 +291,7 @@ Object {
|
|||||||
"backgroundColor": "transparent",
|
"backgroundColor": "transparent",
|
||||||
"baseline": 0,
|
"baseline": 0,
|
||||||
"boundElements": Array [],
|
"boundElements": Array [],
|
||||||
|
"containerId": null,
|
||||||
"fillStyle": "hachure",
|
"fillStyle": "hachure",
|
||||||
"fontFamily": 1,
|
"fontFamily": 1,
|
||||||
"fontSize": 10,
|
"fontSize": 10,
|
||||||
@ -297,6 +300,7 @@ Object {
|
|||||||
"id": "id-text01",
|
"id": "id-text01",
|
||||||
"isDeleted": false,
|
"isDeleted": false,
|
||||||
"opacity": 100,
|
"opacity": 100,
|
||||||
|
"originalText": "test",
|
||||||
"roughness": 1,
|
"roughness": 1,
|
||||||
"seed": Any<Number>,
|
"seed": Any<Number>,
|
||||||
"strokeColor": "#000000",
|
"strokeColor": "#000000",
|
||||||
|
31
src/utils.ts
31
src/utils.ts
@ -90,37 +90,6 @@ export const getFontString = ({
|
|||||||
return `${fontSize}px ${getFontFamilyString({ fontFamily })}` as FontString;
|
return `${fontSize}px ${getFontFamilyString({ fontFamily })}` as FontString;
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://github.com/grassator/canvas-text-editor/blob/master/lib/FontMetrics.js
|
|
||||||
export const measureText = (text: string, font: FontString) => {
|
|
||||||
const line = document.createElement("div");
|
|
||||||
const body = document.body;
|
|
||||||
line.style.position = "absolute";
|
|
||||||
line.style.whiteSpace = "pre";
|
|
||||||
line.style.font = font;
|
|
||||||
body.appendChild(line);
|
|
||||||
line.innerText = text
|
|
||||||
.split("\n")
|
|
||||||
// replace empty lines with single space because leading/trailing empty
|
|
||||||
// lines would be stripped from computation
|
|
||||||
.map((x) => x || " ")
|
|
||||||
.join("\n");
|
|
||||||
const width = line.offsetWidth;
|
|
||||||
const height = line.offsetHeight;
|
|
||||||
// Now creating 1px sized item that will be aligned to baseline
|
|
||||||
// to calculate baseline shift
|
|
||||||
const span = document.createElement("span");
|
|
||||||
span.style.display = "inline-block";
|
|
||||||
span.style.overflow = "hidden";
|
|
||||||
span.style.width = "1px";
|
|
||||||
span.style.height = "1px";
|
|
||||||
line.appendChild(span);
|
|
||||||
// Baseline is important for positioning text on canvas
|
|
||||||
const baseline = span.offsetTop + span.offsetHeight;
|
|
||||||
document.body.removeChild(line);
|
|
||||||
|
|
||||||
return { width, height, baseline };
|
|
||||||
};
|
|
||||||
|
|
||||||
export const debounce = <T extends any[]>(
|
export const debounce = <T extends any[]>(
|
||||||
fn: (...args: T) => void,
|
fn: (...args: T) => void,
|
||||||
timeout: number,
|
timeout: number,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user