Fix library dnd (#2314)
This commit is contained in:
parent
8a50916ef2
commit
ba3f548b91
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,3 +14,4 @@ yarn-error.log*
|
|||||||
yarn.lock
|
yarn.lock
|
||||||
.idea
|
.idea
|
||||||
dist/
|
dist/
|
||||||
|
.eslintcache
|
||||||
|
@ -2,7 +2,7 @@ import { register } from "./register";
|
|||||||
import { getSelectedElements } from "../scene";
|
import { getSelectedElements } from "../scene";
|
||||||
import { getNonDeletedElements } from "../element";
|
import { getNonDeletedElements } from "../element";
|
||||||
import { deepCopyElement } from "../element/newElement";
|
import { deepCopyElement } from "../element/newElement";
|
||||||
import { loadLibrary, saveLibrary } from "../data/localStorage";
|
import { Library } from "../data/library";
|
||||||
|
|
||||||
export const actionAddToLibrary = register({
|
export const actionAddToLibrary = register({
|
||||||
name: "addToLibrary",
|
name: "addToLibrary",
|
||||||
@ -12,8 +12,8 @@ export const actionAddToLibrary = register({
|
|||||||
appState,
|
appState,
|
||||||
);
|
);
|
||||||
|
|
||||||
loadLibrary().then((items) => {
|
Library.loadLibrary().then((items) => {
|
||||||
saveLibrary([...items, selectedElements.map(deepCopyElement)]);
|
Library.saveLibrary([...items, selectedElements.map(deepCopyElement)]);
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -145,7 +145,6 @@ import {
|
|||||||
isBindingElementType,
|
isBindingElementType,
|
||||||
} from "../element/typeChecks";
|
} from "../element/typeChecks";
|
||||||
import { actionFinalize, actionDeleteSelected } from "../actions";
|
import { actionFinalize, actionDeleteSelected } from "../actions";
|
||||||
import { loadLibrary } from "../data/localStorage";
|
|
||||||
|
|
||||||
import throttle from "lodash.throttle";
|
import throttle from "lodash.throttle";
|
||||||
import { LinearElementEditor } from "../element/linearElementEditor";
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
||||||
@ -1266,7 +1265,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
|
|||||||
history.resumeRecording();
|
history.resumeRecording();
|
||||||
this.scene.replaceAllElements(this.scene.getElements());
|
this.scene.replaceAllElements(this.scene.getElements());
|
||||||
|
|
||||||
this.initializeSocketClient({ showLoadingState: false });
|
await this.initializeSocketClient({ showLoadingState: false });
|
||||||
};
|
};
|
||||||
|
|
||||||
closePortal = () => {
|
closePortal = () => {
|
||||||
@ -3729,7 +3728,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const libraryShapes = event.dataTransfer.getData(MIME_TYPES.excalidraw);
|
const libraryShapes = event.dataTransfer.getData(MIME_TYPES.excalidrawlib);
|
||||||
if (libraryShapes !== "") {
|
if (libraryShapes !== "") {
|
||||||
this.addElementsFromPasteOrLibrary(
|
this.addElementsFromPasteOrLibrary(
|
||||||
JSON.parse(libraryShapes),
|
JSON.parse(libraryShapes),
|
||||||
@ -4040,7 +4039,7 @@ declare global {
|
|||||||
setState: React.Component<any, AppState>["setState"];
|
setState: React.Component<any, AppState>["setState"];
|
||||||
history: SceneHistory;
|
history: SceneHistory;
|
||||||
app: InstanceType<typeof App>;
|
app: InstanceType<typeof App>;
|
||||||
library: ReturnType<typeof loadLibrary>;
|
library: typeof Library;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4064,7 +4063,7 @@ if (
|
|||||||
get: () => history,
|
get: () => history,
|
||||||
},
|
},
|
||||||
library: {
|
library: {
|
||||||
get: () => loadLibrary(),
|
value: Library,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -39,12 +39,12 @@ import { Tooltip } from "./Tooltip";
|
|||||||
|
|
||||||
import "./LayerUI.scss";
|
import "./LayerUI.scss";
|
||||||
import { LibraryUnit } from "./LibraryUnit";
|
import { LibraryUnit } from "./LibraryUnit";
|
||||||
import { loadLibrary, saveLibrary } from "../data/localStorage";
|
|
||||||
import { ToolButton } from "./ToolButton";
|
import { ToolButton } from "./ToolButton";
|
||||||
import { saveLibraryAsJSON, importLibraryFromJSON } from "../data/json";
|
import { saveLibraryAsJSON, importLibraryFromJSON } from "../data/json";
|
||||||
import { muteFSAbortError } from "../utils";
|
import { muteFSAbortError } from "../utils";
|
||||||
import { BackgroundPickerAndDarkModeToggle } from "./BackgroundPickerAndDarkModeToggle";
|
import { BackgroundPickerAndDarkModeToggle } from "./BackgroundPickerAndDarkModeToggle";
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
|
import { Library } from "../data/library";
|
||||||
|
|
||||||
interface LayerUIProps {
|
interface LayerUIProps {
|
||||||
actionManager: ActionManager;
|
actionManager: ActionManager;
|
||||||
@ -223,7 +223,7 @@ const LibraryMenu = ({
|
|||||||
resolve("loading");
|
resolve("loading");
|
||||||
}, 100);
|
}, 100);
|
||||||
}),
|
}),
|
||||||
loadLibrary().then((items) => {
|
Library.loadLibrary().then((items) => {
|
||||||
setLibraryItems(items);
|
setLibraryItems(items);
|
||||||
setIsLoading("ready");
|
setIsLoading("ready");
|
||||||
}),
|
}),
|
||||||
@ -238,18 +238,18 @@ const LibraryMenu = ({
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const removeFromLibrary = useCallback(async (indexToRemove) => {
|
const removeFromLibrary = useCallback(async (indexToRemove) => {
|
||||||
const items = await loadLibrary();
|
const items = await Library.loadLibrary();
|
||||||
const nextItems = items.filter((_, index) => index !== indexToRemove);
|
const nextItems = items.filter((_, index) => index !== indexToRemove);
|
||||||
saveLibrary(nextItems);
|
Library.saveLibrary(nextItems);
|
||||||
setLibraryItems(nextItems);
|
setLibraryItems(nextItems);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const addToLibrary = useCallback(
|
const addToLibrary = useCallback(
|
||||||
async (elements: LibraryItem) => {
|
async (elements: LibraryItem) => {
|
||||||
const items = await loadLibrary();
|
const items = await Library.loadLibrary();
|
||||||
const nextItems = [...items, elements];
|
const nextItems = [...items, elements];
|
||||||
onAddToLibrary();
|
onAddToLibrary();
|
||||||
saveLibrary(nextItems);
|
Library.saveLibrary(nextItems);
|
||||||
setLibraryItems(nextItems);
|
setLibraryItems(nextItems);
|
||||||
},
|
},
|
||||||
[onAddToLibrary],
|
[onAddToLibrary],
|
||||||
|
@ -89,3 +89,10 @@ export const MIME_TYPES = {
|
|||||||
excalidraw: "application/vnd.excalidraw+json",
|
excalidraw: "application/vnd.excalidraw+json",
|
||||||
excalidrawlib: "application/vnd.excalidrawlib+json",
|
excalidrawlib: "application/vnd.excalidrawlib+json",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const STORAGE_KEYS = {
|
||||||
|
LOCAL_STORAGE_ELEMENTS: "excalidraw",
|
||||||
|
LOCAL_STORAGE_APP_STATE: "excalidraw-state",
|
||||||
|
LOCAL_STORAGE_COLLAB: "excalidraw-collab",
|
||||||
|
LOCAL_STORAGE_LIBRARY: "excalidraw-library",
|
||||||
|
};
|
||||||
|
@ -55,13 +55,24 @@ export const parseFileContents = async (blob: Blob | File) => {
|
|||||||
return contents;
|
return contents;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMimeType = (blob: Blob): string => {
|
export const getMimeType = (blob: Blob | string): string => {
|
||||||
if (blob.type) {
|
let name: string;
|
||||||
return blob.type;
|
if (typeof blob === "string") {
|
||||||
|
name = blob;
|
||||||
|
} else {
|
||||||
|
if (blob.type) {
|
||||||
|
return blob.type;
|
||||||
|
}
|
||||||
|
name = blob.name || "";
|
||||||
}
|
}
|
||||||
const name = blob.name || "";
|
|
||||||
if (/\.(excalidraw|json)$/.test(name)) {
|
if (/\.(excalidraw|json)$/.test(name)) {
|
||||||
return "application/json";
|
return "application/json";
|
||||||
|
} else if (/\.png$/.test(name)) {
|
||||||
|
return "image/png";
|
||||||
|
} else if (/\.jpe?g$/.test(name)) {
|
||||||
|
return "image/jpeg";
|
||||||
|
} else if (/\.svg$/.test(name)) {
|
||||||
|
return "image/svg+xml";
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
|
@ -4,7 +4,6 @@ import { cleanAppStateForExport } from "../appState";
|
|||||||
|
|
||||||
import { fileOpen, fileSave } from "browser-nativefs";
|
import { fileOpen, fileSave } from "browser-nativefs";
|
||||||
import { loadFromBlob } from "./blob";
|
import { loadFromBlob } from "./blob";
|
||||||
import { loadLibrary } from "./localStorage";
|
|
||||||
import { Library } from "./library";
|
import { Library } from "./library";
|
||||||
import { MIME_TYPES } from "../constants";
|
import { MIME_TYPES } from "../constants";
|
||||||
|
|
||||||
@ -65,7 +64,7 @@ export const isValidLibrary = (json: any) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const saveLibraryAsJSON = async () => {
|
export const saveLibraryAsJSON = async () => {
|
||||||
const library = await loadLibrary();
|
const library = await Library.loadLibrary();
|
||||||
const serialized = JSON.stringify(
|
const serialized = JSON.stringify(
|
||||||
{
|
{
|
||||||
type: "excalidrawlib",
|
type: "excalidrawlib",
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
import { loadLibraryFromBlob } from "./blob";
|
import { loadLibraryFromBlob } from "./blob";
|
||||||
import { LibraryItems, LibraryItem } from "../types";
|
import { LibraryItems, LibraryItem } from "../types";
|
||||||
import { loadLibrary, saveLibrary } from "./localStorage";
|
import { restoreElements } from "./restore";
|
||||||
|
import { STORAGE_KEYS } from "../constants";
|
||||||
|
|
||||||
export class Library {
|
export class Library {
|
||||||
|
private static libraryCache: LibraryItems | null = null;
|
||||||
|
|
||||||
|
static resetLibrary = () => {
|
||||||
|
Library.libraryCache = null;
|
||||||
|
localStorage.removeItem(STORAGE_KEYS.LOCAL_STORAGE_LIBRARY);
|
||||||
|
};
|
||||||
|
|
||||||
/** imports library (currently merges, removing duplicates) */
|
/** imports library (currently merges, removing duplicates) */
|
||||||
static async importLibrary(blob: Blob) {
|
static async importLibrary(blob: Blob) {
|
||||||
const libraryFile = await loadLibraryFromBlob(blob);
|
const libraryFile = await loadLibraryFromBlob(blob);
|
||||||
@ -34,10 +42,51 @@ export class Library {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const existingLibraryItems = await loadLibrary();
|
const existingLibraryItems = await Library.loadLibrary();
|
||||||
const filtered = libraryFile.library!.filter((libraryItem) =>
|
const filtered = libraryFile.library!.filter((libraryItem) =>
|
||||||
isUniqueitem(existingLibraryItems, libraryItem),
|
isUniqueitem(existingLibraryItems, libraryItem),
|
||||||
);
|
);
|
||||||
saveLibrary([...existingLibraryItems, ...filtered]);
|
Library.saveLibrary([...existingLibraryItems, ...filtered]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static loadLibrary = (): Promise<LibraryItems> => {
|
||||||
|
return new Promise(async (resolve) => {
|
||||||
|
if (Library.libraryCache) {
|
||||||
|
return resolve(JSON.parse(JSON.stringify(Library.libraryCache)));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_LIBRARY);
|
||||||
|
if (!data) {
|
||||||
|
return resolve([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const items = (JSON.parse(data) as LibraryItems).map((elements) =>
|
||||||
|
restoreElements(elements),
|
||||||
|
) as Mutable<LibraryItems>;
|
||||||
|
|
||||||
|
// clone to ensure we don't mutate the cached library elements in the app
|
||||||
|
Library.libraryCache = JSON.parse(JSON.stringify(items));
|
||||||
|
|
||||||
|
resolve(items);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
resolve([]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
static saveLibrary = (items: LibraryItems) => {
|
||||||
|
const prevLibraryItems = Library.libraryCache;
|
||||||
|
try {
|
||||||
|
const serializedItems = JSON.stringify(items);
|
||||||
|
// cache optimistically so that consumers have access to the latest
|
||||||
|
// immediately
|
||||||
|
Library.libraryCache = JSON.parse(serializedItems);
|
||||||
|
localStorage.setItem(STORAGE_KEYS.LOCAL_STORAGE_LIBRARY, serializedItems);
|
||||||
|
} catch (e) {
|
||||||
|
Library.libraryCache = prevLibraryItems;
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,59 +1,12 @@
|
|||||||
import { ExcalidrawElement } from "../element/types";
|
import { ExcalidrawElement } from "../element/types";
|
||||||
import { AppState, LibraryItems } from "../types";
|
import { AppState } from "../types";
|
||||||
import { clearAppStateForLocalStorage, getDefaultAppState } from "../appState";
|
import { clearAppStateForLocalStorage, getDefaultAppState } from "../appState";
|
||||||
import { restoreElements } from "./restore";
|
import { STORAGE_KEYS } from "../constants";
|
||||||
|
|
||||||
const LOCAL_STORAGE_KEY = "excalidraw";
|
|
||||||
const LOCAL_STORAGE_KEY_STATE = "excalidraw-state";
|
|
||||||
const LOCAL_STORAGE_KEY_COLLAB = "excalidraw-collab";
|
|
||||||
const LOCAL_STORAGE_KEY_LIBRARY = "excalidraw-library";
|
|
||||||
|
|
||||||
let _LATEST_LIBRARY_ITEMS: LibraryItems | null = null;
|
|
||||||
export const loadLibrary = (): Promise<LibraryItems> => {
|
|
||||||
return new Promise(async (resolve) => {
|
|
||||||
if (_LATEST_LIBRARY_ITEMS) {
|
|
||||||
return resolve(JSON.parse(JSON.stringify(_LATEST_LIBRARY_ITEMS)));
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = localStorage.getItem(LOCAL_STORAGE_KEY_LIBRARY);
|
|
||||||
if (!data) {
|
|
||||||
return resolve([]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const items = (JSON.parse(data) as LibraryItems).map((elements) =>
|
|
||||||
restoreElements(elements),
|
|
||||||
) as Mutable<LibraryItems>;
|
|
||||||
|
|
||||||
// clone to ensure we don't mutate the cached library elements in the app
|
|
||||||
_LATEST_LIBRARY_ITEMS = JSON.parse(JSON.stringify(items));
|
|
||||||
|
|
||||||
resolve(items);
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
resolve([]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const saveLibrary = (items: LibraryItems) => {
|
|
||||||
const prevLibraryItems = _LATEST_LIBRARY_ITEMS;
|
|
||||||
try {
|
|
||||||
const serializedItems = JSON.stringify(items);
|
|
||||||
// cache optimistically so that consumers have access to the latest
|
|
||||||
// immediately
|
|
||||||
_LATEST_LIBRARY_ITEMS = JSON.parse(serializedItems);
|
|
||||||
localStorage.setItem(LOCAL_STORAGE_KEY_LIBRARY, serializedItems);
|
|
||||||
} catch (e) {
|
|
||||||
_LATEST_LIBRARY_ITEMS = prevLibraryItems;
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const saveUsernameToLocalStorage = (username: string) => {
|
export const saveUsernameToLocalStorage = (username: string) => {
|
||||||
try {
|
try {
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
LOCAL_STORAGE_KEY_COLLAB,
|
STORAGE_KEYS.LOCAL_STORAGE_COLLAB,
|
||||||
JSON.stringify({ username }),
|
JSON.stringify({ username }),
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -64,7 +17,7 @@ export const saveUsernameToLocalStorage = (username: string) => {
|
|||||||
|
|
||||||
export const importUsernameFromLocalStorage = (): string | null => {
|
export const importUsernameFromLocalStorage = (): string | null => {
|
||||||
try {
|
try {
|
||||||
const data = localStorage.getItem(LOCAL_STORAGE_KEY_COLLAB);
|
const data = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_COLLAB);
|
||||||
if (data) {
|
if (data) {
|
||||||
return JSON.parse(data).username;
|
return JSON.parse(data).username;
|
||||||
}
|
}
|
||||||
@ -82,11 +35,11 @@ export const saveToLocalStorage = (
|
|||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
LOCAL_STORAGE_KEY,
|
STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS,
|
||||||
JSON.stringify(elements.filter((element) => !element.isDeleted)),
|
JSON.stringify(elements.filter((element) => !element.isDeleted)),
|
||||||
);
|
);
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
LOCAL_STORAGE_KEY_STATE,
|
STORAGE_KEYS.LOCAL_STORAGE_APP_STATE,
|
||||||
JSON.stringify(clearAppStateForLocalStorage(appState)),
|
JSON.stringify(clearAppStateForLocalStorage(appState)),
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -100,8 +53,8 @@ export const importFromLocalStorage = () => {
|
|||||||
let savedState = null;
|
let savedState = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
savedElements = localStorage.getItem(LOCAL_STORAGE_KEY);
|
savedElements = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS);
|
||||||
savedState = localStorage.getItem(LOCAL_STORAGE_KEY_STATE);
|
savedState = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_APP_STATE);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Unable to access localStorage
|
// Unable to access localStorage
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
@ -28,12 +28,20 @@ describe("appState", () => {
|
|||||||
expect(h.state.viewBackgroundColor).toBe("#F00");
|
expect(h.state.viewBackgroundColor).toBe("#F00");
|
||||||
});
|
});
|
||||||
|
|
||||||
API.dropFile({
|
API.drop(
|
||||||
appState: {
|
new Blob(
|
||||||
viewBackgroundColor: "#000",
|
[
|
||||||
},
|
JSON.stringify({
|
||||||
elements: [API.createElement({ type: "rectangle", id: "A" })],
|
type: "excalidraw",
|
||||||
});
|
appState: {
|
||||||
|
viewBackgroundColor: "#000",
|
||||||
|
},
|
||||||
|
elements: [API.createElement({ type: "rectangle", id: "A" })],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
{ type: "application/json" },
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
|
||||||
|
@ -29,6 +29,17 @@ jest.mock("../data/firebase.ts", () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
jest.mock("socket.io-client", () => {
|
||||||
|
return () => {
|
||||||
|
return {
|
||||||
|
close: () => {},
|
||||||
|
on: () => {},
|
||||||
|
off: () => {},
|
||||||
|
emit: () => {},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
describe("collaboration", () => {
|
describe("collaboration", () => {
|
||||||
it("creating room should reset deleted elements", async () => {
|
it("creating room should reset deleted elements", async () => {
|
||||||
render(
|
render(
|
||||||
@ -50,7 +61,7 @@ describe("collaboration", () => {
|
|||||||
expect(API.getStateHistory().length).toBe(1);
|
expect(API.getStateHistory().length).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
h.app.openPortal();
|
await h.app.openPortal();
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
|
||||||
expect(API.getStateHistory().length).toBe(1);
|
expect(API.getStateHistory().length).toBe(1);
|
||||||
|
@ -9,12 +9,6 @@ import {
|
|||||||
} from "../data/image";
|
} from "../data/image";
|
||||||
import { serializeAsJSON } from "../data/json";
|
import { serializeAsJSON } from "../data/json";
|
||||||
|
|
||||||
import fs from "fs";
|
|
||||||
import util from "util";
|
|
||||||
import path from "path";
|
|
||||||
|
|
||||||
const readFile = util.promisify(fs.readFile);
|
|
||||||
|
|
||||||
const { h } = window;
|
const { h } = window;
|
||||||
|
|
||||||
const testElements = [
|
const testElements = [
|
||||||
@ -43,22 +37,18 @@ Object.defineProperty(window, "TextDecoder", {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("appState", () => {
|
describe("export", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
render(<App />);
|
render(<App />);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("export embedded png and reimport", async () => {
|
it("export embedded png and reimport", async () => {
|
||||||
const pngBlob = new Blob(
|
const pngBlob = await API.loadFile("./fixtures/smiley.png");
|
||||||
[await readFile(path.resolve(__dirname, "./fixtures/smiley.png"))],
|
|
||||||
{ type: "image/png" },
|
|
||||||
);
|
|
||||||
|
|
||||||
const pngBlobEmbedded = await encodePngMetadata({
|
const pngBlobEmbedded = await encodePngMetadata({
|
||||||
blob: pngBlob,
|
blob: pngBlob,
|
||||||
metadata: serializeAsJSON(testElements, h.state),
|
metadata: serializeAsJSON(testElements, h.state),
|
||||||
});
|
});
|
||||||
API.dropFile(pngBlobEmbedded);
|
API.drop(pngBlobEmbedded);
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(h.elements).toEqual([
|
expect(h.elements).toEqual([
|
||||||
@ -78,17 +68,7 @@ describe("appState", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("import embedded png (legacy v1)", async () => {
|
it("import embedded png (legacy v1)", async () => {
|
||||||
const pngBlob = new Blob(
|
API.drop(await API.loadFile("./fixtures/test_embedded_v1.png"));
|
||||||
[
|
|
||||||
await readFile(
|
|
||||||
path.resolve(__dirname, "./fixtures/test_embedded_v1.png"),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
{ type: "image/png" },
|
|
||||||
);
|
|
||||||
|
|
||||||
API.dropFile(pngBlob);
|
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(h.elements).toEqual([
|
expect(h.elements).toEqual([
|
||||||
expect.objectContaining({ type: "text", text: "test" }),
|
expect.objectContaining({ type: "text", text: "test" }),
|
||||||
@ -97,17 +77,7 @@ describe("appState", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("import embedded png (v2)", async () => {
|
it("import embedded png (v2)", async () => {
|
||||||
const pngBlob = new Blob(
|
API.drop(await API.loadFile("./fixtures/smiley_embedded_v2.png"));
|
||||||
[
|
|
||||||
await readFile(
|
|
||||||
path.resolve(__dirname, "./fixtures/smiley_embedded_v2.png"),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
{ type: "image/png" },
|
|
||||||
);
|
|
||||||
|
|
||||||
API.dropFile(pngBlob);
|
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(h.elements).toEqual([
|
expect(h.elements).toEqual([
|
||||||
expect.objectContaining({ type: "text", text: "😀" }),
|
expect.objectContaining({ type: "text", text: "😀" }),
|
||||||
@ -116,17 +86,7 @@ describe("appState", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("import embedded svg (legacy v1)", async () => {
|
it("import embedded svg (legacy v1)", async () => {
|
||||||
const svgBlob = new Blob(
|
API.drop(await API.loadFile("./fixtures/test_embedded_v1.svg"));
|
||||||
[
|
|
||||||
await readFile(
|
|
||||||
path.resolve(__dirname, "./fixtures/test_embedded_v1.svg"),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
{ type: "image/svg+xml" },
|
|
||||||
);
|
|
||||||
|
|
||||||
API.dropFile(svgBlob);
|
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(h.elements).toEqual([
|
expect(h.elements).toEqual([
|
||||||
expect.objectContaining({ type: "text", text: "test" }),
|
expect.objectContaining({ type: "text", text: "test" }),
|
||||||
@ -135,17 +95,7 @@ describe("appState", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("import embedded svg (v2)", async () => {
|
it("import embedded svg (v2)", async () => {
|
||||||
const svgBlob = new Blob(
|
API.drop(await API.loadFile("./fixtures/smiley_embedded_v2.svg"));
|
||||||
[
|
|
||||||
await readFile(
|
|
||||||
path.resolve(__dirname, "./fixtures/smiley_embedded_v2.svg"),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
{ type: "image/svg+xml" },
|
|
||||||
);
|
|
||||||
|
|
||||||
API.dropFile(svgBlob);
|
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(h.elements).toEqual([
|
expect(h.elements).toEqual([
|
||||||
expect.objectContaining({ type: "text", text: "😀" }),
|
expect.objectContaining({ type: "text", text: "😀" }),
|
||||||
|
31
src/tests/fixtures/fixture_library.excalidrawlib
vendored
Normal file
31
src/tests/fixtures/fixture_library.excalidrawlib
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"type": "excalidrawlib",
|
||||||
|
"version": 1,
|
||||||
|
"library": [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"type": "rectangle",
|
||||||
|
"version": 38,
|
||||||
|
"versionNonce": 1046419680,
|
||||||
|
"isDeleted": false,
|
||||||
|
"id": "A",
|
||||||
|
"fillStyle": "hachure",
|
||||||
|
"strokeWidth": 1,
|
||||||
|
"strokeStyle": "solid",
|
||||||
|
"roughness": 1,
|
||||||
|
"opacity": 100,
|
||||||
|
"angle": 0,
|
||||||
|
"x": 21801,
|
||||||
|
"y": 719.5,
|
||||||
|
"strokeColor": "#c92a2a",
|
||||||
|
"backgroundColor": "#e64980",
|
||||||
|
"width": 50,
|
||||||
|
"height": 30,
|
||||||
|
"seed": 117297479,
|
||||||
|
"groupIds": [],
|
||||||
|
"strokeSharpness": "sharp",
|
||||||
|
"boundElementIds": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
@ -8,7 +8,12 @@ import { newElement, newTextElement, newLinearElement } from "../../element";
|
|||||||
import { DEFAULT_VERTICAL_ALIGN } from "../../constants";
|
import { DEFAULT_VERTICAL_ALIGN } from "../../constants";
|
||||||
import { getDefaultAppState } from "../../appState";
|
import { getDefaultAppState } from "../../appState";
|
||||||
import { GlobalTestState, createEvent, fireEvent } from "../test-utils";
|
import { GlobalTestState, createEvent, fireEvent } from "../test-utils";
|
||||||
import { ImportedDataState } from "../../data/types";
|
import fs from "fs";
|
||||||
|
import util from "util";
|
||||||
|
import path from "path";
|
||||||
|
import { getMimeType } from "../../data/blob";
|
||||||
|
|
||||||
|
const readFile = util.promisify(fs.readFile);
|
||||||
|
|
||||||
const { h } = window;
|
const { h } = window;
|
||||||
|
|
||||||
@ -138,30 +143,48 @@ export class API {
|
|||||||
return element as any;
|
return element as any;
|
||||||
};
|
};
|
||||||
|
|
||||||
static dropFile(data: ImportedDataState | Blob) {
|
static readFile = async <T extends "utf8" | null>(
|
||||||
|
filepath: string,
|
||||||
|
encoding?: T,
|
||||||
|
): Promise<T extends "utf8" ? string : Buffer> => {
|
||||||
|
filepath = path.isAbsolute(filepath)
|
||||||
|
? filepath
|
||||||
|
: path.resolve(path.join(__dirname, "../", filepath));
|
||||||
|
return readFile(filepath, { encoding }) as any;
|
||||||
|
};
|
||||||
|
|
||||||
|
static loadFile = async (filepath: string) => {
|
||||||
|
const { base, ext } = path.parse(filepath);
|
||||||
|
return new File([await API.readFile(filepath, null)], base, {
|
||||||
|
type: getMimeType(ext),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
static drop = async (blob: Blob) => {
|
||||||
const fileDropEvent = createEvent.drop(GlobalTestState.canvas);
|
const fileDropEvent = createEvent.drop(GlobalTestState.canvas);
|
||||||
const file =
|
const text = await new Promise<string>((resolve, reject) => {
|
||||||
data instanceof Blob
|
try {
|
||||||
? data
|
const reader = new FileReader();
|
||||||
: new Blob(
|
reader.onload = () => {
|
||||||
[
|
resolve(reader.result as string);
|
||||||
JSON.stringify({
|
};
|
||||||
type: "excalidraw",
|
reader.readAsText(blob);
|
||||||
...data,
|
} catch (error) {
|
||||||
}),
|
reject(error);
|
||||||
],
|
}
|
||||||
{
|
});
|
||||||
type: "application/json",
|
|
||||||
},
|
|
||||||
);
|
|
||||||
Object.defineProperty(fileDropEvent, "dataTransfer", {
|
Object.defineProperty(fileDropEvent, "dataTransfer", {
|
||||||
value: {
|
value: {
|
||||||
files: [file],
|
files: [blob],
|
||||||
getData: (_type: string) => {
|
getData: (type: string) => {
|
||||||
|
if (type === blob.type) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
return "";
|
return "";
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
fireEvent(GlobalTestState.canvas, fileDropEvent);
|
fireEvent(GlobalTestState.canvas, fileDropEvent);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
@ -78,13 +78,21 @@ describe("history", () => {
|
|||||||
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]),
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]),
|
||||||
);
|
);
|
||||||
|
|
||||||
API.dropFile({
|
API.drop(
|
||||||
appState: {
|
new Blob(
|
||||||
...getDefaultAppState(),
|
[
|
||||||
viewBackgroundColor: "#000",
|
JSON.stringify({
|
||||||
},
|
type: "excalidraw",
|
||||||
elements: [API.createElement({ type: "rectangle", id: "B" })],
|
appState: {
|
||||||
});
|
...getDefaultAppState(),
|
||||||
|
viewBackgroundColor: "#000",
|
||||||
|
},
|
||||||
|
elements: [API.createElement({ type: "rectangle", id: "B" })],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
{ type: "application/json" },
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
await waitFor(() => expect(API.getStateHistory().length).toBe(2));
|
await waitFor(() => expect(API.getStateHistory().length).toBe(2));
|
||||||
expect(h.state.viewBackgroundColor).toBe("#000");
|
expect(h.state.viewBackgroundColor).toBe("#000");
|
||||||
|
43
src/tests/library.test.tsx
Normal file
43
src/tests/library.test.tsx
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { render, waitFor } from "./test-utils";
|
||||||
|
import App from "../components/App";
|
||||||
|
import { API } from "./helpers/api";
|
||||||
|
import { MIME_TYPES } from "../constants";
|
||||||
|
import { LibraryItem } from "../types";
|
||||||
|
|
||||||
|
const { h } = window;
|
||||||
|
|
||||||
|
describe("library", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
h.library.resetLibrary();
|
||||||
|
render(<App />);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("import library via drag&drop", async () => {
|
||||||
|
expect(await h.library.loadLibrary()).toEqual([]);
|
||||||
|
await API.drop(
|
||||||
|
await API.loadFile("./fixtures/fixture_library.excalidrawlib"),
|
||||||
|
);
|
||||||
|
await waitFor(async () => {
|
||||||
|
expect(await h.library.loadLibrary()).toEqual([
|
||||||
|
[expect.objectContaining({ id: "A" })],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// NOTE: mocked to test logic, not actual drag&drop via UI
|
||||||
|
it("drop library item onto canvas", async () => {
|
||||||
|
expect(h.elements).toEqual([]);
|
||||||
|
const libraryItems: LibraryItem = JSON.parse(
|
||||||
|
await API.readFile("./fixtures/fixture_library.excalidrawlib", "utf8"),
|
||||||
|
).library[0];
|
||||||
|
await API.drop(
|
||||||
|
new Blob([JSON.stringify(libraryItems)], {
|
||||||
|
type: MIME_TYPES.excalidrawlib,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(h.elements).toEqual([expect.objectContaining({ id: "A_copy" })]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user