diff --git a/background.js b/background.js index 3eed2c1..92a8784 100644 --- a/background.js +++ b/background.js @@ -6,23 +6,28 @@ const homeKey = 'esc_win_content'; const homeURL = api.runtime.getURL('home.html'); const commandKey = 'open-escape'; -/* - -Weird thing: +const VIEW_GROUPS = 1; +const VIEW_PAGES = 2; +const VIEW_URL = 3; -- 'panel' gets us the featurelessness we want -- but the resulting window has type of 'popup' -- so you have to search by 'popup' when getting it - -*/ +// 'panel' gets us the featurelessness we want but the resulting window has type +// of 'popup' so you have to search by 'popup' when getting it. //const windowType = 'normal'; +//const windowType = 'detached_panel'; //const windowType = 'popup'; const windowType = 'panel'; -//const windowType = 'detached_panel'; + +/* +const sa = signal([]); +sa.effect(e => console.log('signal effect', sa.value)); +sa.value = [1]; +*/ let port = null; +const store = await datastore.init(); + // home page first load opens the connection api.runtime.onConnect.addListener(p => { console.log('port connected', p) @@ -37,7 +42,8 @@ api.runtime.onConnect.addListener(p => { const escapeHandler = async msg => { if (msg.ESC === 1) { const win = await findEscapeWindow(); - if (msg.address && msg.address != homeURL) { + if (msg.url && msg.url != homeURL) { + store.setCurrentView(VIEW_URL); await openContentWindow(homeURL, homeKey); } } @@ -45,32 +51,70 @@ const escapeHandler = async msg => { api.runtime.onMessage.addListener(escapeHandler); -const onNavigate = async msg => { - console.log('onNavigate', msg); - if (msg.type === 'navigate' && msg.url) { - console.log('Navigating to:', msg.url); - // if the message is a navigation request, open the content window - // with the specified address - const win = await openContentWindow(msg.url, homeKey); - console.log(win); - - const tab = win.tabs[0]; - console.log('taking screenshot for:', tab); - const screenshot = await api.tabs.captureTab(tab.id); - if (screenshot) { - store.addScreenshot(url, screenshot); - console.log('Screenshot captured for:', msg.url); - } - - /* - if (port) { - port.postMessage({ type: 'windowOpened', address: msg.url, windowId: win.id }); - } - */ +const addPageToGroup = (groupId, url) => { + const group = store.getGroupById(groupId); + const index = group.pages.indexOf(url); + console.log('aPTG', groupId, url); + if (index === -1) { + console.log('adding', url, 'to', groupId); + group.pages.push(url); + store.updateGroup(group); + } + else { + console.log('url already in group') + } +}; + +const onMessage = msg => { + console.log('onMessage', msg); + switch(msg.type) { + default: + onNavigate(msg.url); } }; -api.runtime.onMessage.addListener(onNavigate); +const onNavigate = async url => { + if (!url) { + console.error('onNavigate, NO URL'); + return; + } + + console.log('Navigating to:', url); + + console.log('getting group id...'); + const groupId = store.getLastActiveGroupId(); + console.log('got group', groupId); + + // TODO + addPageToGroup(groupId, url); + console.log('group updated:', groupId); + + store.setCurrentView(VIEW_URL); + console.log('current view set to:', VIEW_URL); + + // if the message is a navigation request, open the content window + // with the specified address + const win = await openContentWindow(url, homeKey); + //console.log('opened win', win); + + // + const tab = win.tabs[0]; + console.log('taking screenshot for:', tab); + const screenshot = await api.tabs.captureTab(tab.id); + if (screenshot) { + store.setScreenshot(url, screenshot); + console.log('Screenshot captured for:', url); + } + // + + /* + if (port) { + port.postMessage({ type: 'windowOpened', url: msg.url, windowId: win.id }); + } + */ +}; + +api.runtime.onMessage.addListener(onMessage); api.commands.onCommand.addListener(async command => { if (command === commandKey) { @@ -89,13 +133,13 @@ const findEscapeWindow = async () => { }); }; -const openContentWindow = async (address, key = null) => { +const openContentWindow = async (url, key = null) => { // attempt to find open window, and focus it if (key != null) { const foundWin = await findEscapeWindow(); if (foundWin != null) { const tab = foundWin.tabs[0]; - api.tabs.update(tab.id, {url: address}); + api.tabs.update(tab.id, {url}); api.windows.update(foundWin.id, { focused: true }) return foundWin; } @@ -104,7 +148,7 @@ const openContentWindow = async (address, key = null) => { // make new window const opts = { type: windowType, - url: address + url }; // open window and get info @@ -127,7 +171,4 @@ const executeContentScript = async name => { return results[0] }; -await datastore.init(); - await openContentWindow(homeURL, homeKey); - diff --git a/content-script.js b/content-script.js index 5c7f72f..3cc8ce2 100644 --- a/content-script.js +++ b/content-script.js @@ -17,7 +17,7 @@ const handleEsc = evt => { browser.runtime.sendMessage({ ESC: 1, - address: document.location.href + url: document.location.href }); }; diff --git a/datastore.js b/datastore.js index 636c4c8..026fffa 100644 --- a/datastore.js +++ b/datastore.js @@ -1,6 +1,6 @@ import api from './api.js'; -const FLUSH_MS = 10000; // how often to flush state to storage +const FLUSH_MS = 5000; // how often to flush state to storage // keys for extension-level data const EXT_DATA_KEY = 'state'; @@ -28,22 +28,26 @@ const initializeStore = state => { else { state.groups[i] = group; } + updateStorage(state); }, getLastActiveGroupId: () => { return state.lastGroupId; }, setLastActiveGroupId: id => { state.lastGroupId = id; + updateStorage(state); }, getGroupById: id => state.groups.find(group => group.id === id), - getLastActiveGroup: () => state.groups.find(id => group.id === state.lastGroupId), + getLastActiveGroup: () => state.groups.find(group => group.id === state.lastGroupId), getCurrentView: () => state.currentView, setCurrentView: view => { state.currentView = view; + updateStorage(state); }, getScreenshot: url => state.screenshots[url] || null, setScreenshot: (url, screenshot) => { state.screenshots[url] = screenshot; + updateStorage(state); } }; }; @@ -69,16 +73,48 @@ const init = async () => { // If state doesn't exist, initialize it else if (!state) { state = initialState; + } + + const store = initializeStore(state); + + if (store.groups().length === 0) { // first write updateStorage(state); + const DEFAULT_GROUP_PREFIX = 'Group'; + + const newGroupTitle = () => { + return `${DEFAULT_GROUP_PREFIX} ${Object.keys(store.groups()).length + 1}`; + }; + + const newGroupData = (title, urls) => { + console.log('newGroup', title); + + const id = Math.random().toString(16).slice(2); + const now = Date.now(); + + const group = { + id, + title: title || newGroupTitle(), + pages: urls || [], + created: now, + updated: now + }; + + return group; + }; + + const group = newGroupData(newGroupTitle(), []); + store.updateGroup(group); + store.setLastActiveGroupId(group.id); } // Flush state to storage every `FLUSH_MS` milliseconds + /* setInterval(() => { updateStorage(state); }, FLUSH_MS); - - return initializeStore(state); + */ + return store; }; export default { diff --git a/home.css b/home.css index 7362671..9d791aa 100644 --- a/home.css +++ b/home.css @@ -80,14 +80,10 @@ h2 { background-color: var(--background); color: var(--text); border: none; + border-bottom: 0.01px solid var(--text); outline: none; font-size: 1.5rem; font-weight: 500; - /* - height: 1.5rem; - margin: 0; - padding: 0; - */ -webkit-app-region: no-drag; /* Allow interaction */ } @@ -115,10 +111,6 @@ h2 { } .card { - /* - min-height: 100%; - min-width: 100%; - */ display: flex; flex-direction: column; text-decoration: none; @@ -126,11 +118,6 @@ h2 { padding-bottom: 1rem; padding-top: 1rem; cursor: pointer; - /* - position: relative; - top: 0; - transition: all .1s ease-in; - */ } .card:hover { @@ -145,7 +132,8 @@ h2 { flex-direction: column; } -.card .thumb { +/*.card .thumb {*/ +.card { background-size: cover; background-position: center center; } @@ -155,6 +143,10 @@ h2 { line-height: 1.4; } +.card title { + background-color: var(--primary); +} + .card span { font-size: 12px; font-weight: bold; diff --git a/home.html b/home.html index 7481e7a..6cc7af1 100644 --- a/home.html +++ b/home.html @@ -27,9 +27,7 @@