snapshot of https://github.com/excalidraw/excalidraw.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Tests / test (push) Failing after 15s
Some checks failed
Tests / test (push) Failing after 15s
This commit is contained in:
75
packages/excalidraw/data/filesystem.ts
Normal file
75
packages/excalidraw/data/filesystem.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import {
|
||||
fileOpen as _fileOpen,
|
||||
fileSave as _fileSave,
|
||||
supported as nativeFileSystemSupported,
|
||||
} from "browser-fs-access";
|
||||
|
||||
import { MIME_TYPES } from "@excalidraw/common";
|
||||
|
||||
import { normalizeFile } from "./blob";
|
||||
|
||||
type FILE_EXTENSION = Exclude<keyof typeof MIME_TYPES, "binary">;
|
||||
|
||||
export const fileOpen = async <M extends boolean | undefined = false>(opts: {
|
||||
extensions?: FILE_EXTENSION[];
|
||||
description: string;
|
||||
multiple?: M;
|
||||
}): Promise<M extends false | undefined ? File : File[]> => {
|
||||
// an unsafe TS hack, alas not much we can do AFAIK
|
||||
type RetType = M extends false | undefined ? File : File[];
|
||||
|
||||
const mimeTypes = opts.extensions?.reduce((mimeTypes, type) => {
|
||||
mimeTypes.push(MIME_TYPES[type]);
|
||||
|
||||
return mimeTypes;
|
||||
}, [] as string[]);
|
||||
|
||||
const extensions = opts.extensions?.reduce((acc, ext) => {
|
||||
if (ext === "jpg") {
|
||||
return acc.concat(".jpg", ".jpeg");
|
||||
}
|
||||
return acc.concat(`.${ext}`);
|
||||
}, [] as string[]);
|
||||
|
||||
const files = await _fileOpen({
|
||||
description: opts.description,
|
||||
extensions,
|
||||
mimeTypes,
|
||||
multiple: opts.multiple ?? false,
|
||||
});
|
||||
|
||||
if (Array.isArray(files)) {
|
||||
return (await Promise.all(
|
||||
files.map((file) => normalizeFile(file)),
|
||||
)) as RetType;
|
||||
}
|
||||
return (await normalizeFile(files)) as RetType;
|
||||
};
|
||||
|
||||
export const fileSave = (
|
||||
blob: Blob | Promise<Blob>,
|
||||
opts: {
|
||||
/** supply without the extension */
|
||||
name: string;
|
||||
/** file extension */
|
||||
extension: FILE_EXTENSION;
|
||||
mimeTypes?: string[];
|
||||
description: string;
|
||||
/** existing FileSystemFileHandle */
|
||||
fileHandle?: FileSystemFileHandle | null;
|
||||
},
|
||||
) => {
|
||||
return _fileSave(
|
||||
blob,
|
||||
{
|
||||
fileName: `${opts.name}.${opts.extension}`,
|
||||
description: opts.description,
|
||||
extensions: [`.${opts.extension}`],
|
||||
mimeTypes: opts.mimeTypes,
|
||||
},
|
||||
opts.fileHandle,
|
||||
false,
|
||||
);
|
||||
};
|
||||
|
||||
export { nativeFileSystemSupported };
|
||||
Reference in New Issue
Block a user