-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Import): import local files by dropping in editor
- Loading branch information
1 parent
e0342ef
commit 09c69f6
Showing
3 changed files
with
78 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { createEventsManager } from '../events'; | ||
import type { ContentConfig } from '../models'; | ||
import type { populateConfig as populateConfigFn, SourceFile } from './utils'; | ||
import { importFromZip } from './zip'; | ||
|
||
export const importFromFiles = async ( | ||
files: FileList, | ||
populateConfig: typeof populateConfigFn, | ||
eventsManager: ReturnType<typeof createEventsManager>, | ||
) => { | ||
const loadFiles = (files: FileList) => | ||
new Promise<Partial<ContentConfig>>((resolve, reject) => { | ||
const sourceFiles: SourceFile[] = []; | ||
|
||
for (const file of files) { | ||
// Max 100 MB allowed | ||
const maxSizeAllowed = 100 * 1024 * 1024; | ||
if (file.size > maxSizeAllowed) { | ||
reject('Error: Exceeded size 100 MB'); | ||
return; | ||
} | ||
|
||
const reader = new FileReader(); | ||
eventsManager.addEventListener(reader, 'load', (event: any) => { | ||
const text = (event.target?.result as string) || ''; | ||
sourceFiles.push({ | ||
filename: file.name, | ||
content: text, | ||
}); | ||
|
||
if (sourceFiles.length === files.length) { | ||
resolve(populateConfig(sourceFiles, {})); | ||
} | ||
}); | ||
|
||
eventsManager.addEventListener(reader, 'error', () => { | ||
reject('Error: Failed to read file'); | ||
}); | ||
|
||
reader.readAsText(file); | ||
} | ||
}); | ||
|
||
const loadZipFile = (files: FileList) => importFromZip(files[0], populateConfig); | ||
|
||
if (!files?.length) return {}; | ||
|
||
const getConfigFromFiles = | ||
files?.length === 1 && files[0].name.endsWith('.zip') ? loadZipFile : loadFiles; | ||
|
||
return getConfigFromFiles(files); | ||
}; |