feat: Expose the API to calculate offsets and remove offsetTop and offsetLeft props (#3265)

* feat: Expose the API to calculate offsets and remove offsetTop and offsetLeft props

* update

* fix tests

* fix

* update readme and changelog

* fix

* better
This commit is contained in:
Aakansha Doshi 2021-03-20 13:00:49 +05:30 committed by GitHub
parent add1785ace
commit de99484a1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 73 deletions

View file

@ -15,26 +15,40 @@ describe("appState", () => {
const ELEM_WIDTH = 100;
const ELEM_HEIGHT = 60;
await render(
<Excalidraw
width={WIDTH}
height={HEIGHT}
offsetLeft={OFFSET_LEFT}
offsetTop={OFFSET_TOP}
initialData={{
elements: [
API.createElement({
type: "rectangle",
id: "A",
width: ELEM_WIDTH,
height: ELEM_HEIGHT,
}),
],
scrollToContent: true,
}}
/>,
);
const originalGetBoundingClientRect =
global.window.HTMLDivElement.prototype.getBoundingClientRect;
// override getBoundingClientRect as by default it will always return all values as 0 even if customized in html
global.window.HTMLDivElement.prototype.getBoundingClientRect = () => ({
top: OFFSET_TOP,
left: OFFSET_LEFT,
bottom: 10,
right: 10,
width: 100,
x: 10,
y: 20,
height: 100,
toJSON: () => {},
});
await render(
<div>
<Excalidraw
width={WIDTH}
height={HEIGHT}
initialData={{
elements: [
API.createElement({
type: "rectangle",
id: "A",
width: ELEM_WIDTH,
height: ELEM_HEIGHT,
}),
],
scrollToContent: true,
}}
/>
</div>,
);
await waitFor(() => {
expect(h.state.width).toBe(WIDTH);
expect(h.state.height).toBe(HEIGHT);
@ -45,5 +59,6 @@ describe("appState", () => {
expect(h.state.scrollX).toBe(WIDTH / 2 - ELEM_WIDTH / 2);
expect(h.state.scrollY).toBe(HEIGHT / 2 - ELEM_HEIGHT / 2);
});
global.window.HTMLDivElement.prototype.getBoundingClientRect = originalGetBoundingClientRect;
});
});