158 lines
6.0 KiB
JavaScript
158 lines
6.0 KiB
JavaScript
import { PROJECT_MANIFEST } from './format.js';
|
|
import { manifestFromSnapshot } from './project-filesystem.js';
|
|
|
|
export function mountFileBrowser(panel, toggle, {session, bridge, text, locale, elements}) {
|
|
const {make, icon, button} = elements;
|
|
const nav = make('nav', null, 'isa-project-path');
|
|
nav.setAttribute('aria-label', text.fileLocation);
|
|
const body = make('div', null, 'isa-project-browser-body');
|
|
panel.append(nav, body);
|
|
panel.id = 'isa-project-files-' + crypto.randomUUID();
|
|
panel.hidden = true;
|
|
toggle.setAttribute('aria-controls', panel.id);
|
|
toggle.setAttribute('aria-expanded', 'false');
|
|
let generation = 0;
|
|
|
|
function displayName(path) {
|
|
if (!path) return text.projectFiles;
|
|
if (path === PROJECT_MANIFEST) return text.projectDetails;
|
|
if (path === 'extensions') return text.extensionData;
|
|
if (path === 'assets') return text.assets;
|
|
return path.split('/').at(-1);
|
|
}
|
|
|
|
function showPath(path, focus) {
|
|
const segments = path ? path.split('/') : [];
|
|
nav.replaceChildren();
|
|
for (let index = 0; index <= segments.length; index++) {
|
|
const current = segments.slice(0, index).join('/');
|
|
if (index) nav.append(icon('chevron'));
|
|
if (index < segments.length) {
|
|
nav.append(button(displayName(current), () => open(current)));
|
|
} else {
|
|
const name = make('span', displayName(current));
|
|
name.setAttribute('aria-current', 'location');
|
|
name.tabIndex = -1;
|
|
nav.append(name);
|
|
if (focus) name.focus();
|
|
}
|
|
}
|
|
}
|
|
|
|
function clearBody() {
|
|
for (const media of body.querySelectorAll('audio, video')) {
|
|
media.pause();
|
|
media.removeAttribute('src');
|
|
media.load();
|
|
}
|
|
body.replaceChildren();
|
|
}
|
|
|
|
function list(directory, entries) {
|
|
const parent = directory ? session.path(directory) : session.project.path;
|
|
const files = entries.filter(entry => entry.path !== parent)
|
|
.sort((a, b) => Number(b.kind === 'folder') - Number(a.kind === 'folder') || a.name.localeCompare(b.name, locale));
|
|
if (!files.length) return make('p', text.empty, 'isa-project-message');
|
|
const rows = make('ul', null, 'isa-project-file-list');
|
|
for (const file of files) {
|
|
const path = file.path.slice(session.project.path.length + 1);
|
|
const folder = file.kind === 'folder';
|
|
const row = button(null, () => open(path, folder ? null : file), folder ? 'folder' : 'file');
|
|
row.append(make('span', displayName(path)), icon('chevron'));
|
|
row.title = file.name;
|
|
const item = make('li');
|
|
item.append(row);
|
|
rows.append(item);
|
|
}
|
|
return rows;
|
|
}
|
|
|
|
function manifestPreview(snapshot) {
|
|
let manifest;
|
|
try {
|
|
manifest = manifestFromSnapshot(snapshot);
|
|
} catch (error) {
|
|
if (typeof snapshot.text === 'string') return make('pre', snapshot.text);
|
|
throw error;
|
|
}
|
|
const format = new Intl.DateTimeFormat(locale, {dateStyle: 'medium', timeStyle: 'short'});
|
|
const result = make('div', null, 'isa-project-metadata');
|
|
const fields = make('dl');
|
|
for (const [label, value] of [
|
|
[text.name, manifest.name], [text.created, format.format(new Date(manifest.createdAt))],
|
|
[text.updated, format.format(new Date(manifest.updatedAt))], [text.projectId, manifest.id],
|
|
[text.formatVersion, manifest.formatVersion]
|
|
]) {
|
|
const field = make('div');
|
|
field.append(make('dt', label), make('dd', value));
|
|
fields.append(field);
|
|
}
|
|
const source = make('details');
|
|
source.append(make('summary', PROJECT_MANIFEST), make('pre', snapshot.text ?? JSON.stringify(manifest, null, 2)));
|
|
result.append(fields, source);
|
|
return result;
|
|
}
|
|
|
|
async function preview(path, file, ticket) {
|
|
const mime = file.contentType ?? '';
|
|
if (/^(audio|video|image)\//.test(mime)) {
|
|
if (!bridge) throw new Error(text.previewUnavailable);
|
|
const reply = await bridge.invoke('filesystem_get_download_link_file', {...session.scope, path: session.path(path)});
|
|
session.assertCurrent();
|
|
if (ticket !== generation) return;
|
|
const url = new URL(reply.downloadUrl);
|
|
if (!['http:', 'https:'].includes(url.protocol)) throw new Error(text.previewUnavailable);
|
|
const media = make(mime.startsWith('image/') ? 'img' : mime.startsWith('audio/') ? 'audio' : 'video');
|
|
if (media.tagName === 'IMG') media.alt = file.name;
|
|
else media.controls = true;
|
|
media.src = url.toString();
|
|
return media;
|
|
}
|
|
const reply = await session.readFile(path);
|
|
session.assertCurrent();
|
|
if (ticket !== generation) return;
|
|
if (reply.status !== 'found') throw new Error(text.previewUnavailable);
|
|
if (path === PROJECT_MANIFEST) return manifestPreview(reply);
|
|
if (typeof reply.text !== 'string') throw new Error(text.previewUnavailable);
|
|
return make('pre', reply.text);
|
|
}
|
|
|
|
async function open(path, file = null, focus = true) {
|
|
const ticket = ++generation;
|
|
clearBody();
|
|
showPath(path, focus);
|
|
body.setAttribute('aria-busy', 'true');
|
|
try {
|
|
if (file) {
|
|
const node = await preview(path, file, ticket);
|
|
if (ticket === generation) body.replaceChildren(node);
|
|
} else {
|
|
const entries = await session.listFiles(path, false);
|
|
if (ticket === generation) body.replaceChildren(list(path, entries));
|
|
}
|
|
} catch (error) {
|
|
if (ticket !== generation || error?.name === 'AbortError') return;
|
|
const notice = make('p', String(error.message ?? error), 'isa-extension-notice isa-extension-notice--error');
|
|
notice.setAttribute('role', 'alert');
|
|
body.replaceChildren(notice, button(text.retry, () => open(path, file)));
|
|
} finally {
|
|
if (ticket === generation) body.removeAttribute('aria-busy');
|
|
}
|
|
}
|
|
|
|
function close() {
|
|
generation++;
|
|
clearBody();
|
|
body.removeAttribute('aria-busy');
|
|
panel.hidden = true;
|
|
toggle.setAttribute('aria-expanded', 'false');
|
|
}
|
|
toggle.onclick = () => {
|
|
if (!panel.hidden) return close();
|
|
panel.hidden = false;
|
|
toggle.setAttribute('aria-expanded', 'true');
|
|
void open('', null, false);
|
|
};
|
|
return close;
|
|
}
|