fix: refresh wysiwyg position on canvas resize (#3008)

This commit is contained in:
David Luzar 2021-02-11 12:24:26 +01:00 committed by GitHub
parent 4fdddb518a
commit 4b253c7362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 3 deletions

6
package-lock.json generated
View File

@ -3356,6 +3356,12 @@
"@types/react": "*"
}
},
"@types/resize-observer-browser": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/@types/resize-observer-browser/-/resize-observer-browser-0.1.5.tgz",
"integrity": "sha512-8k/67Z95Goa6Lznuykxkfhq9YU3l1Qe6LNZmwde1u7802a3x8v44oq0j91DICclxatTr0rNnhXx7+VTIetSrSQ==",
"dev": true
},
"@types/resolve": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-0.0.8.tgz",

View File

@ -51,6 +51,7 @@
"devDependencies": {
"@types/lodash.throttle": "4.1.6",
"@types/pako": "1.0.1",
"@types/resize-observer-browser": "0.1.5",
"eslint-config-prettier": "7.2.0",
"eslint-plugin-prettier": "3.3.1",
"firebase-tools": "9.3.0",

View File

@ -1551,6 +1551,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
textWysiwyg({
id: element.id,
appState: this.state,
canvas: this.canvas,
getViewportCoords: (x, y) => {
const { x: viewportX, y: viewportY } = sceneCoordsToViewportCoords(
{

View File

@ -38,6 +38,7 @@ export const textWysiwyg = ({
onSubmit,
getViewportCoords,
element,
canvas,
}: {
id: ExcalidrawElement["id"];
appState: AppState;
@ -45,6 +46,7 @@ export const textWysiwyg = ({
onSubmit: (text: string) => void;
getViewportCoords: (x: number, y: number) => [number, number];
element: ExcalidrawElement;
canvas: HTMLCanvasElement | null;
}) => {
const updateWysiwygStyle = () => {
const updatedElement = Scene.getScene(element)?.getElement(id);
@ -151,6 +153,10 @@ export const textWysiwyg = ({
editable.oninput = null;
editable.onkeydown = null;
if (observer) {
observer.disconnect();
}
window.removeEventListener("resize", updateWysiwygStyle);
window.removeEventListener("wheel", stopEvent, true);
window.removeEventListener("pointerdown", onPointerDown);
@ -197,9 +203,19 @@ export const textWysiwyg = ({
let isDestroyed = false;
editable.onblur = handleSubmit;
// reposition wysiwyg in case of window resize. Happens on mobile when
// device keyboard is opened.
// reposition wysiwyg in case of canvas is resized. Using ResizeObserver
// is preferred so we catch changes from host, where window may not resize.
let observer: ResizeObserver | null = null;
if (canvas && "ResizeObserver" in window) {
observer = new window.ResizeObserver(() => {
updateWysiwygStyle();
});
observer.observe(canvas);
} else {
window.addEventListener("resize", updateWysiwygStyle);
}
window.addEventListener("pointerdown", onPointerDown);
window.addEventListener("wheel", stopEvent, {
passive: false,