Skip to content

Commit

Permalink
feat: create and expose serializeLibraryAsJSON (#5009)
Browse files Browse the repository at this point in the history
Co-authored-by: David Luzar <luzar.david@gmail.com>
  • Loading branch information
pomdtr and dwelle committed Apr 5, 2022
1 parent 880e4fe commit 873afda
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/data/json.ts
Expand Up @@ -123,14 +123,18 @@ export const isValidLibrary = (json: any) => {
);
};

export const saveLibraryAsJSON = async (libraryItems: LibraryItems) => {
export const serializeLibraryAsJSON = (libraryItems: LibraryItems) => {
const data: ExportedLibraryData = {
type: EXPORT_DATA_TYPES.excalidrawLibrary,
version: VERSIONS.excalidrawLibrary,
source: EXPORT_SOURCE,
libraryItems,
};
const serialized = JSON.stringify(data, null, 2);
return JSON.stringify(data, null, 2);
};

export const saveLibraryAsJSON = async (libraryItems: LibraryItems) => {
const serialized = serializeLibraryAsJSON(libraryItems);
await fileSave(
new Blob([serialized], {
type: MIME_TYPES.excalidrawlib,
Expand Down
1 change: 1 addition & 0 deletions src/packages/excalidraw/CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@ Please add the latest change on the top under the correct section.
#### Refactor

- Rename `appState.elementLocked` to `appState.activeTool.locked` [#4983](https://github.com/excalidraw/excalidraw/pull/4983).
- Expose [`serializeLibraryAsJSON`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#serializeLibraryAsJSON) helper that we use when saving Excalidraw Library to a file.

##### BREAKING CHANGE

Expand Down
11 changes: 11 additions & 0 deletions src/packages/excalidraw/README_NEXT.md
Expand Up @@ -906,6 +906,17 @@ serializeAsJSON({

Takes the scene elements and state and returns a JSON string. Deleted `elements`as well as most properties from `AppState` are removed from the resulting JSON. (see [`serializeAsJSON()`](https://github.com/excalidraw/excalidraw/blob/master/src/data/json.ts#L16) source for details).

#### `serializeLibraryAsJSON`

**_Signature_**

<pre>
serializeLibraryAsJSON({
libraryItems: <a href="https://github.com/excalidraw/excalidraw/blob/master/src/types.ts#L191">LibraryItems[]</a>,
</pre>

Takes the library items and returns a JSON string.

#### `getSceneVersion`

**How to use**
Expand Down
2 changes: 1 addition & 1 deletion src/packages/utils.ts
Expand Up @@ -139,6 +139,6 @@ export const exportToSvg = async ({
);
};

export { serializeAsJSON } from "../data/json";
export { serializeAsJSON, serializeLibraryAsJSON } from "../data/json";
export { loadFromBlob, loadLibraryFromBlob } from "../data/blob";
export { getFreeDrawSvgPath } from "../renderer/renderElement";

2 comments on commit 873afda

@vercel
Copy link

@vercel vercel bot commented on 873afda Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 873afda Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

excalidraw-package-example – ./src/packages/excalidraw

excalidraw-package-example-excalidraw.vercel.app
excalidraw-package-example-git-master-excalidraw.vercel.app
excalidraw-package-example.vercel.app

Please sign in to comment.