fix: Always bind to container selected by user (#5880)

* fix: Always bind to container selected by user

* Don't bind to container when using text tool

* adjust z-index for bound text

* fix

* Add spec

* Add test

* Allow double click on transparent container and add spec

* fix spec

* adjust z-index only when binding

* update index

* fix

* add index check

* Update src/scene/Scene.ts

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Aakansha Doshi 2022-11-25 15:45:34 +05:30 committed by GitHub
parent 1f117995d9
commit d2181847be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 172 additions and 110 deletions

View file

@ -150,6 +150,24 @@ class Scene {
// (I guess?)
this.callbacks.clear();
}
insertElementAtIndex(element: ExcalidrawElement, index: number) {
if (!Number.isFinite(index) || index < 0) {
throw new Error(
"insertElementAtIndex can only be called with index >= 0",
);
}
const nextElements = [
...this.elements.slice(0, index),
element,
...this.elements.slice(index),
];
this.replaceAllElements(nextElements);
}
getElementIndex(elementId: string) {
return this.elements.findIndex((element) => element.id === elementId);
}
}
export default Scene;

View file

@ -1,11 +1,4 @@
import {
ExcalidrawElement,
ExcalidrawTextContainer,
NonDeletedExcalidrawElement,
} from "../element/types";
import { getElementAbsoluteCoords } from "../element";
import { isTextBindableContainer } from "../element/typeChecks";
import { NonDeletedExcalidrawElement } from "../element/types";
export const hasBackground = (type: string) =>
type === "rectangle" ||
@ -73,23 +66,3 @@ export const getElementsAtPosition = (
(element) => !element.isDeleted && isAtPositionFn(element),
);
};
export const getTextBindableContainerAtPosition = (
elements: readonly ExcalidrawElement[],
x: number,
y: number,
): ExcalidrawTextContainer | null => {
let hitElement = null;
// We need to to hit testing from front (end of the array) to back (beginning of the array)
for (let index = elements.length - 1; index >= 0; --index) {
if (elements[index].isDeleted) {
continue;
}
const [x1, y1, x2, y2] = getElementAbsoluteCoords(elements[index]);
if (x1 < x && x < x2 && y1 < y && y < y2) {
hitElement = elements[index];
break;
}
}
return isTextBindableContainer(hitElement, false) ? hitElement : null;
};

View file

@ -14,7 +14,6 @@ export {
canHaveArrowheads,
canChangeSharpness,
getElementAtPosition,
getTextBindableContainerAtPosition,
hasText,
getElementsAtPosition,
} from "./comparisons";