calculate coords based on container viewport position (#1955)

* feat: calculate coords based on parent left and top so it renders correctly in host App

* fix text

* move offsets to state & fix bugs

* fix text jumping

* account for zoom in textWysiwyg & undo incorrect offsetting

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Aakansha Doshi 2020-07-27 17:18:49 +05:30 committed by GitHub
parent 63edbb9517
commit 7eff6893c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 361 additions and 179 deletions

View file

@ -2,28 +2,13 @@ import { getDefaultAppState, cleanAppStateForExport } from "../appState";
import { restore } from "./restore";
import { t } from "../i18n";
import { AppState } from "../types";
import { calculateScrollCenter } from "../scene";
export const loadFromBlob = async (blob: any) => {
const updateAppState = (contents: string) => {
const defaultAppState = getDefaultAppState();
let elements = [];
let appState = defaultAppState;
try {
const data = JSON.parse(contents);
if (data.type !== "excalidraw") {
throw new Error(t("alerts.couldNotLoadInvalidFile"));
}
elements = data.elements || [];
appState = {
...defaultAppState,
...cleanAppStateForExport(data.appState as Partial<AppState>),
};
} catch {
throw new Error(t("alerts.couldNotLoadInvalidFile"));
}
return { elements, appState };
};
/**
* @param blob
* @param appState if provided, used for centering scroll to restored scene
*/
export const loadFromBlob = async (blob: any, appState?: AppState) => {
if (blob.handle) {
(window as any).handle = blob.handle;
}
@ -42,6 +27,23 @@ export const loadFromBlob = async (blob: any) => {
});
}
const { elements, appState } = updateAppState(contents);
return restore(elements, appState, { scrollToContent: true });
const defaultAppState = getDefaultAppState();
let elements = [];
let _appState = appState || defaultAppState;
try {
const data = JSON.parse(contents);
if (data.type !== "excalidraw") {
throw new Error(t("alerts.couldNotLoadInvalidFile"));
}
elements = data.elements || [];
_appState = {
...defaultAppState,
...cleanAppStateForExport(data.appState as Partial<AppState>),
...(appState ? calculateScrollCenter(elements, appState, null) : {}),
};
} catch {
throw new Error(t("alerts.couldNotLoadInvalidFile"));
}
return restore(elements, _appState);
};