Skip to content

Commit

Permalink
feat(deno): add filtering if types are specifiered
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Aug 26, 2024
1 parent ba3f317 commit 59c9c8a
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/deno/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class FileDialog {
}

addFilter(_: string, extensions: string[]): FileDialog {
// const decode = new TextEncoder().encode(description);
const extensionsStr = JSON.stringify(extensions);
const ext = new TextEncoder().encode(extensionsStr);

Expand Down Expand Up @@ -102,11 +101,13 @@ export function openFileDialog(
fileDialog = fileDialog.setDirectory(options.startingDirectory);
}

// const types = options?.types ?? [];
// const allExts = types.flatMap(getAllExts);
// const exts = allExts.map((ext) => ext.slice(1));
for (const [description, exts] of options.acceptsOptions) {
const extsWithoutDot = exts.map((ext) =>
ext.startsWith(".") ? ext.slice(1) : ext
);

// fileDialog = fileDialog.addFilter("<unknown>", exts);
fileDialog = fileDialog.addFilter(description, extsWithoutDot);
}

if (options?.multiple) {
const paths = fileDialog.pickFiles();
Expand All @@ -133,17 +134,21 @@ export function openSaveFileDialog(
fileDialog = fileDialog.setFileName(options.suggestedName);
}

for (const [description, exts] of options.acceptsOptions) {
const extsWithoutDot = exts.map((ext) =>
ext.startsWith(".") ? ext.slice(1) : ext
);

fileDialog = fileDialog.addFilter(description, extsWithoutDot);
}

const path = fileDialog.saveFile();

Deno.writeFileSync(path, new Uint8Array());

return toLoc(path);
}

// function getAllExts(accept: FilePickerAcceptType): string[] {
// return Object.values(accept.accept).flatMap((value) => value);
// }

function toLoc(path: string): FileLocation {
const { base: name, dir: root } = parse(path);

Expand Down

0 comments on commit 59c9c8a

Please sign in to comment.