From 9e8eda4531be8a24015a0d91ee3c87b236275f06 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Fri, 2 Jul 2021 22:46:38 -0700 Subject: [PATCH] background,embed-files: different icons for files and folders refactor background so you can synthetically invoke the request logic --- extension/background.js | 10 +++++++--- extension/embed-files.js | 32 +++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/extension/background.js b/extension/background.js index 76f846b..f80a124 100644 --- a/extension/background.js +++ b/extension/background.js @@ -880,6 +880,12 @@ function tryMatchRoute(path) { } throw new UnixError(unix.ENOENT); } +async function doRequest(req) { + const [route, vars] = tryMatchRoute(req.path); + const response = await route[req.op]({...req, ...vars}); + response.op = req.op; + return response; +} let port; async function onMessage(req) { @@ -896,9 +902,7 @@ async function onMessage(req) { /* console.time(req.op + ':' + req.path);*/ try { - const [route, vars] = tryMatchRoute(req.path); - response = await route[req.op]({...req, ...vars}); - response.op = req.op; + response = await doRequest(req); if (response.buf) { response.buf = btoa(response.buf); } } catch (e) { diff --git a/extension/embed-files.js b/extension/embed-files.js index 9048ab0..26a34fc 100644 --- a/extension/embed-files.js +++ b/extension/embed-files.js @@ -6,7 +6,19 @@ if (chrome.extension.getBackgroundPage) { // accept requests from the page browser.runtime.onMessage.addListener(async (request, sender) => { - const {entries} = await Routes["/tabs/by-id/#TAB_ID"].readdir({path: `/tabs/by-id/${sender.tab.id}`}); + let {entries} = await Routes["/tabs/by-id/#TAB_ID"] + .readdir({path: `/tabs/by-id/${sender.tab.id}`}); + entries = await Promise.all(entries.map(filename => { + let path = `/tabs/by-id/${sender.tab.id}/${filename}`; + if (filename === '.') { + path = `/tabs/by-id/${sender.tab.id}`; + } else if (filename === '..') { + path = `/tabs/by-id`; + } + return doRequest({op: 'getattr', path}) + .then(stat => ({ ...stat, filename })); + })); + // TODO: report back not as reply, but as general msg return entries; }); @@ -35,7 +47,13 @@ if (chrome.extension.getBackgroundPage) { } .--tabfs-file-container .--tabfs-file::before { - content: '📁'; display: block; font-size: 48px; + display: block; font-size: 48px; +} +.--tabfs-file-container .--tabfs-file::before { + content: '📄'; +} +.--tabfs-file-container .--tabfs-file--directory::before { + content: '📁'; } @@ -47,16 +65,14 @@ if (chrome.extension.getBackgroundPage) { const icons = {}; let frontierX = 0, frontierY = 0; - const addFile = function(name, x, y, file) { - // TODO: report into the extension - + const addFile = function(stat, x, y, file) { if (!x) { x = frontierX; frontierX += 64; y = 0; } container.insertAdjacentHTML('beforeend', ` -
${name}
+
${stat.filename}
`); const icon = container.lastElementChild; icon.style.left = `${x}px`; icon.style.top = `${y}px`; @@ -101,6 +117,8 @@ if (chrome.extension.getBackgroundPage) { // Remove the handlers of `mousemove` and `mouseup` document.removeEventListener('mousemove', mouseMoveHandler); document.removeEventListener('mouseup', mouseUpHandler); + + // TODO: report into extension }; icon.addEventListener('mousedown', mouseDownHandler); @@ -108,7 +126,7 @@ if (chrome.extension.getBackgroundPage) { // ask for what the files are chrome.runtime.sendMessage({hello: 'hello'}, function(response) { - response.forEach(name => addFile(name)); + response.forEach(stat => addFile(stat)); }); document.body.addEventListener('dragenter', function(e) {