import icon from '../assets/icons/files.png' import { App } from '../types.ts' import { FlowWindow } from '../wm.ts' import { Stats } from 'fs' export default class FilesApp implements App { meta = { name: 'Files', description: 'A simple files app.', pkg: 'flow.files', version: '1.0.0', icon } async open (): Promise { const win = window.wm.createWindow({ title: this.meta.name, icon: this.meta.icon, width: 500, height: 400 }) win.content.style.display = 'flex' win.content.style.flexDirection = 'column' async function setDir (dir: string): Promise { await window.fs.readdir(dir, (e: NodeJS.ErrnoException, files: string[]) => { const back = dir === '/' ? '' : '' win.content.innerHTML = `
${back}${dir}
` if (back !== '') { (win.content.querySelector('.back') as HTMLElement).onclick = async () => { if (dir.split('/')[1] === dir.replace('/', '')) { await setDir(`/${dir.split('/')[0]}`) } else { await setDir(`/${dir.split('/')[1]}`) } } } (win.content.querySelector('.file') as HTMLElement).onclick = async () => { const title: string = prompt('Enter file name') ?? 'new-file.txt' await window.fs.promises.open(`${dir}/${title}`, 'w') await setDir(dir) } (win.content.querySelector('.folder') as HTMLElement).onclick = async () => { const title: string = prompt('Enter folder name') ?? 'new-folder' await window.fs.promises.mkdir(`${dir}/${title}`) await setDir(dir) } for (const file of files) { const separator = dir === '/' ? '' : '/' window.fs.stat(dir + separator + file, (e: NodeJS.ErrnoException, fileStat: Stats) => { const element = document.createElement('div') element.setAttribute('style', 'padding: 5px;border-bottom: 1px solid var(--text);display:flex;align-items:center;gap: 5px;') const genIcon = (): string => { switch (file.split('.').at(-1)) { case 'js': case 'mjs': case 'cjs': { return '' } case 'html': case 'htm': { return '' } case 'css': { return '' } case 'json': { return '' } case 'md': { return '' } case 'txt': case 'text': { return '' } case 'png': case 'apng': { return '' } case 'jpg': case 'jpeg': { return '' } case 'gif': { return '' } default: { return '' } } } const icon = fileStat.isDirectory() ? '' : genIcon() element.innerHTML += `${icon} ${file}` element.onclick = async () => { if (fileStat.isDirectory()) { await setDir(dir + separator + file) } else { await window.flow.openApp('flow.editor', { path: dir + separator + file }) } } win.content.querySelector('.files')?.appendChild(element) }) } }) } await setDir('/') return win } }