manhteky123's picture
Upload 213 files
60f878e verified
import { PlaitBoard, PlaitElement } from '@plait/core';
import { MIME_TYPES, VERSIONS } from '../constants';
import { fileOpen, fileSave } from './filesystem';
import { DrawnixExportedData, DrawnixExportedType } from './types';
import { loadFromBlob, normalizeFile } from './blob';
export const getDefaultName = () => {
const time = new Date().getTime();
return time.toString();
};
export const saveAsJSON = async (
board: PlaitBoard,
name: string = getDefaultName()
) => {
const serialized = serializeAsJSON(board);
const blob = new Blob([serialized], {
type: MIME_TYPES.drawnix,
});
const fileHandle = await fileSave(blob, {
name,
extension: 'drawnix',
description: 'Drawnix file',
});
return { fileHandle };
};
export const loadFromJSON = async (board: PlaitBoard) => {
const file = await fileOpen({
description: 'Drawnix files',
// ToDo: Be over-permissive until https://bugs.webkit.org/show_bug.cgi?id=34442
// gets resolved. Else, iOS users cannot open `.drawnix` files.
// extensions: ["json", "drawnix", "png", "svg"],
});
return loadFromBlob(board, await normalizeFile(file));
};
export const isValidDrawnixData = (data?: any): data is DrawnixExportedData => {
return (
data &&
data.type === DrawnixExportedType.drawnix &&
Array.isArray(data.elements) &&
typeof data.viewport === 'object'
);
};
export const serializeAsJSON = (board: PlaitBoard): string => {
const data = {
type: DrawnixExportedType.drawnix,
version: VERSIONS.drawnix,
source: 'web',
elements: board.children,
viewport: board.viewport,
};
return JSON.stringify(data, null, 2);
};