You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json.ts 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { fileOpen, fileSave } from "browser-nativefs";
  2. import { cleanAppStateForExport } from "../appState";
  3. import { MIME_TYPES } from "../constants";
  4. import { clearElementsForExport } from "../element";
  5. import { ExcalidrawElement } from "../element/types";
  6. import { AppState } from "../types";
  7. import { loadFromBlob } from "./blob";
  8. import { Library } from "./library";
  9. export const serializeAsJSON = (
  10. elements: readonly ExcalidrawElement[],
  11. appState: AppState,
  12. ): string =>
  13. JSON.stringify(
  14. {
  15. type: "excalidraw",
  16. version: 2,
  17. source: window.location.origin,
  18. elements: clearElementsForExport(elements),
  19. appState: cleanAppStateForExport(appState),
  20. },
  21. null,
  22. 2,
  23. );
  24. export const saveAsJSON = async (
  25. elements: readonly ExcalidrawElement[],
  26. appState: AppState,
  27. ) => {
  28. const serialized = serializeAsJSON(elements, appState);
  29. const blob = new Blob([serialized], {
  30. type: "application/json",
  31. });
  32. const fileHandle = await fileSave(
  33. blob,
  34. {
  35. fileName: appState.name,
  36. description: "Excalidraw file",
  37. extensions: [".excalidraw"],
  38. },
  39. appState.fileHandle,
  40. );
  41. return { fileHandle };
  42. };
  43. export const loadFromJSON = async (localAppState: AppState) => {
  44. const blob = await fileOpen({
  45. description: "Excalidraw files",
  46. extensions: [".json", ".excalidraw", ".png", ".svg"],
  47. mimeTypes: ["application/json", "image/png", "image/svg+xml"],
  48. });
  49. return loadFromBlob(blob, localAppState);
  50. };
  51. export const isValidLibrary = (json: any) => {
  52. return (
  53. typeof json === "object" &&
  54. json &&
  55. json.type === "excalidrawlib" &&
  56. json.version === 1
  57. );
  58. };
  59. export const saveLibraryAsJSON = async () => {
  60. const library = await Library.loadLibrary();
  61. const serialized = JSON.stringify(
  62. {
  63. type: "excalidrawlib",
  64. version: 1,
  65. library,
  66. },
  67. null,
  68. 2,
  69. );
  70. const fileName = "library.excalidrawlib";
  71. const blob = new Blob([serialized], {
  72. type: MIME_TYPES.excalidrawlib,
  73. });
  74. await fileSave(blob, {
  75. fileName,
  76. description: "Excalidraw library file",
  77. extensions: [".excalidrawlib"],
  78. });
  79. };
  80. export const importLibraryFromJSON = async () => {
  81. const blob = await fileOpen({
  82. description: "Excalidraw library files",
  83. extensions: [".json", ".excalidrawlib"],
  84. mimeTypes: ["application/json"],
  85. });
  86. Library.importLibrary(blob);
  87. };