-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
383 additions
and
382 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,61 @@ | ||
import { readableStreamFromIterable } from "https://deno.land/[email protected]/streams/readable_stream_from_iterable.ts"; | ||
import { sumOf } from "std/collections/sum_of.ts"; | ||
import { Component, asPointer } from "webgen/mod.ts"; | ||
import { FileEntry, countFileTree, getFileStream } from "./fileHandler.ts"; | ||
|
||
const supportsFileSystemAccessAPI = | ||
'getAsFileSystemHandle' in DataTransferItem.prototype; | ||
const supportsWebkitGetAsEntry = | ||
'webkitGetAsEntry' in DataTransferItem.prototype; | ||
|
||
declare global { | ||
interface DataTransferItem { | ||
getAsFileSystemHandle: () => Promise<FileSystemHandle | FileSystemDirectoryHandle>; | ||
} | ||
} | ||
|
||
export function DropHandler(onData: (data: ReadableStream<FileEntry>, length: number) => void, component: Component) { | ||
return new class extends Component { | ||
hovering = asPointer(false); | ||
constructor() { | ||
super(); | ||
this.addClass(this.hovering.map(it => it ? "hovering" : "default"), "drop-area"); | ||
this.wrapper.ondragover = (ev) => { | ||
ev.preventDefault(); | ||
this.hovering.setValue(true); | ||
}; | ||
this.wrapper.ondragleave = (ev) => { | ||
ev.preventDefault(); | ||
console.log(ev); | ||
if (ev.target && !this.wrapper.contains(ev.relatedTarget as Node)) | ||
this.hovering.setValue(false); | ||
}; | ||
this.wrapper.ondrop = async (ev) => { | ||
ev.preventDefault(); | ||
if (!supportsFileSystemAccessAPI) { | ||
alert("Please upgrade you Browser to use the latest features"); | ||
return; | ||
} | ||
if (!supportsFileSystemAccessAPI && !supportsWebkitGetAsEntry || !ev.dataTransfer) return; | ||
|
||
this.hovering.setValue(false); | ||
const files = await Promise.all([ ...ev.dataTransfer.items ] | ||
.filter(item => item.kind === 'file') // File means file or directory | ||
.map(item => item.getAsFileSystemHandle() as Promise<FileSystemHandle | FileSystemDirectoryHandle>)); | ||
|
||
const fileSizeCount = sumOf(await Promise.all(files.filter(it => it).map(it => countFileTree(it!))), it => it); | ||
|
||
onData?.(readableStreamFromIterable(files) | ||
.pipeThrough(new TransformStream<FileSystemHandle | null, FileEntry>({ | ||
async transform(chunk, controller) { | ||
if (!chunk) return; | ||
for await (const iterator of getFileStream(chunk)) { | ||
controller.enqueue(iterator); | ||
} | ||
} | ||
})), fileSizeCount); | ||
}; | ||
this.wrapper.append(component.draw()); | ||
} | ||
}; | ||
} |
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,5 @@ | ||
export function fileTypeName(fileType: string) { | ||
const mimeType = fileType.split(";").at(0); | ||
if (!mimeType) return fileType + " File"; | ||
return mimeType.split("/").at(-1)?.toUpperCase() + " Document"; | ||
} |
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,87 @@ | ||
import { RemotePath } from "../loading.ts"; | ||
|
||
const fileNameIncludes: Record<string, string> = { | ||
"world": "globe2", | ||
"world_nether": "globe2", | ||
"world_the_end": "globe2", | ||
"logs": "body-text", | ||
"plugins": "plug", | ||
"mods": "box-seam", | ||
"config": "gear" | ||
}; | ||
|
||
const supportedFiletypes = [ | ||
'aac', | ||
'ai', | ||
'bmp', | ||
'cs', | ||
'css', | ||
'csv', | ||
'doc', | ||
'docx', | ||
'exe', | ||
'gif', | ||
'heic', | ||
'html', | ||
'java', | ||
'jpg', | ||
'js', | ||
'json', | ||
'jsx', | ||
'key', | ||
'm4p', | ||
'md', | ||
'mdx', | ||
'mov', | ||
'mp3', | ||
'mp4', | ||
'otf', | ||
'pdf', | ||
'php', | ||
'png', | ||
'ppt', | ||
'pptx', | ||
'psd', | ||
'py', | ||
'raw', | ||
'rb', | ||
'sass', | ||
'scss', | ||
'sh', | ||
'sql', | ||
'svg', | ||
'tiff', | ||
'tsx', | ||
'ttf', | ||
'txt', | ||
'wav', | ||
'woff', | ||
'xls', | ||
'xlsx', | ||
'xml', | ||
'yml', | ||
]; | ||
|
||
const otherFiletypes: Record<string, string> = { | ||
'zip': 'archive', | ||
'tar': 'archive', | ||
'gz': 'archive', | ||
'7z': 'archive', | ||
'rar': 'archive', | ||
'yaml': 'filetype-yml', | ||
'jar': 'filetype-java', | ||
}; | ||
|
||
export function mapFiletoIcon(file: RemotePath) { | ||
if (!file.fileMimeType) { | ||
return fileNameIncludes[ file.name ] ?? "folder"; | ||
} | ||
//const filetype = file.fileMimeType.split(';')[0].split('/')[1].split('-').at(-1); | ||
const filetype = file.name.split('.').at(-1); | ||
if (!filetype) return "file-earmark"; | ||
if (supportedFiletypes.includes(filetype)) | ||
return "filetype-" + filetype; | ||
if (otherFiletypes[ filetype ]) | ||
return otherFiletypes[ filetype ]; | ||
return "file-earmark"; | ||
} |
Oops, something went wrong.