Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

951 B
Raw Blame
import {
  hasNativeFileDialog,
  openFileDialog,
} from "@editor/services/fileDialogService";
import { pendingFilePathMappings } from "@editor/services/pendingFilePathMappings";
import { getDocumentFileDialogFilter } from "@editor/utils/fileDialogUtils";

interface OpenFilesFromDiskOptions {
  multiple?: boolean;
  filters?: Array<{
    name: string;
    extensions: string[];
  }>;
  onFallbackOpen?: () => void;
}

export async function openFilesFromDisk(
  options: OpenFilesFromDiskOptions = {},
): Promise<File[]> {
  const filesWithPaths = await openFileDialog({
    multiple: options.multiple ?? true,
    filters: options.filters ?? getDocumentFileDialogFilter(),
  });

  if (filesWithPaths.length > 0) {
    for (const { file, path } of filesWithPaths) {
      pendingFilePathMappings.set(file, path);
    }
    return filesWithPaths.map((entry) => entry.file);
  }

  if (!hasNativeFileDialog) options.onFallbackOpen?.();
  return [];
}