From 5722deeb01632e7de3bed88bcbe612225510b88f Mon Sep 17 00:00:00 2001 From: Marek Piasecki Date: Fri, 27 Jan 2023 09:07:03 +0100 Subject: [PATCH] use localStorage instead of Chrome bookmarks; possibility to add and remove link --- chrome/manifest.json | 13 -------- package-lock.json | 20 +++++++++++- package.json | 7 ++-- src/api.imba | 24 +++++++++++--- src/components/app-home.imba | 61 +++++++++++++++++++++++++++++++++-- src/components/app-links.imba | 9 +++++- src/config.imba | 5 +-- src/main.imba | 25 +++++++------- src/state.imba | 7 +++- vite.config.js | 3 +- 10 files changed, 131 insertions(+), 43 deletions(-) delete mode 100644 chrome/manifest.json diff --git a/chrome/manifest.json b/chrome/manifest.json deleted file mode 100644 index 55cb3b0..0000000 --- a/chrome/manifest.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "fuzzyhome", - "description": "A power user oriented new-tab page that enables lightning speed navigation through the dark magic of fuzzy finding.", - "version": "2.2.0", - "manifest_version": 3, - "permissions": [ - "bookmarks", - "storage" - ], - "chrome_url_overrides": { - "newtab": "dist/index.html" - } -} diff --git a/package-lock.json b/package-lock.json index efc898e..eef812f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,9 @@ "math-expression-evaluator": "^2.0.0", "vite": "^3.2.5", "vite-plugin-imba": "^0.10.1" + }, + "devDependencies": { + "vite-plugin-singlefile": "^0.13.2" } }, "node_modules/@adobe/css-tools": { @@ -2030,7 +2033,6 @@ "version": "4.0.5", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", - "peer": true, "dependencies": { "braces": "^3.0.2", "picomatch": "^2.3.1" @@ -2750,6 +2752,22 @@ "node": ">=6" } }, + "node_modules/vite-plugin-singlefile": { + "version": "0.13.2", + "resolved": "https://registry.npmjs.org/vite-plugin-singlefile/-/vite-plugin-singlefile-0.13.2.tgz", + "integrity": "sha512-HAvrU9mxasNMn/YF0Hb9NjsWDstCWe4iLQ6IR5ppOiNMvXjcyqU3C9SDQ32xnonx3Y04JUGjD2bGiT6q0S9T8w==", + "dev": true, + "dependencies": { + "micromatch": "^4.0.5" + }, + "engines": { + "node": "^14.18.0 || >=16.0.0" + }, + "peerDependencies": { + "rollup": ">=2.79.0", + "vite": ">=3.2.0" + } + }, "node_modules/vitest": { "version": "0.27.1", "resolved": "https://registry.npmjs.org/vitest/-/vitest-0.27.1.tgz", diff --git a/package.json b/package.json index 1815165..71fb496 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "homepage": "https://fuzzyho.me/", "author": "Mikey Oz (https://github.com/familyfriendlymikey)", "scripts": { - "dev": "vite build -w --outDir chrome/dist --minify false", - "build": "vite build --outDir chrome/dist" + "dev": "vite", + "build": "vite build" }, "dependencies": { "fzi": "^1.5.0", @@ -15,5 +15,8 @@ "math-expression-evaluator": "^2.0.0", "vite": "^3.2.5", "vite-plugin-imba": "^0.10.1" + }, + "devDependencies": { + "vite-plugin-singlefile": "^0.13.2" } } diff --git a/src/api.imba b/src/api.imba index 6216b30..157f866 100644 --- a/src/api.imba +++ b/src/api.imba @@ -1,4 +1,4 @@ -import state from './state.imba' +import state, { storage } from './state.imba' import config from './config.imba' import { find, omit, orderBy } from 'lodash' @@ -8,17 +8,24 @@ import Mexp from 'math-expression-evaluator' let mexp = new Mexp export default new class api + #home_input = #link_input = null def pin_link link Pins[link.url] ^= 1 sort_links! - global.chrome.storage.sync.set {pins:Pins} + storage.set 'pins', Pins imba.commit! def increment_link_frequency link Frequencies[link.url] ??= 0 Frequencies[link.url] += 1 - global.chrome.storage.sync.set {frequencies:Frequencies} + storage.set 'frequencies', Frequencies + + def refresh_links + let links = [] + links.push(title: k, url: v) for own k,v of Links + state.links = self.traverse(links) + self.sort_links! def get-link-from-node node return unless let url = node..url @@ -74,16 +81,23 @@ export default new class api Date!.toString!.split(" ").slice(0, 4).join(" ") get selected_link - state.sorted_links[state.link_selection_index] + state.sorted_links[state.link_selection_index] || { name: "", alias: "", is_bang:no, url: "" } + + def select_name(name) + set_link_selection_index(0) + while selected_link.name != name + increment_link_selection_index! def set_link_selection_index index state.link_selection_index = index def increment_link_selection_index set_link_selection_index Math.min(state.sorted_links.length - 1, state.link_selection_index + 1) + #home_input.focus! def decrement_link_selection_index - set_link_selection_index Math.max(0, state.link_selection_index - 1) + let i = set_link_selection_index Math.max(-1, state.link_selection_index - 1) + #link_input.focus! if i == -1 def navigate link await increment_link_frequency link diff --git a/src/components/app-home.imba b/src/components/app-home.imba index d1151ce..806d78d 100644 --- a/src/components/app-home.imba +++ b/src/components/app-home.imba @@ -2,9 +2,38 @@ tag app-home def mount $home-input.focus! + api.#home_input = $home-input + api.#link_input = $link-input-url def blur - setTimeout(&, 100) do $home-input.focus! + setTimeout(&, 100) do + if document.activeElement != $link-input-url and document.activeElement != $link-input-title + $home-input.focus! + + def edd_link + let title = $link-input-title.value + let url = $link-input-url.value + + if !title + $link-input-title.focus! + elif !url + $link-input-url.focus! + else + if api.selected_link.name != title + delete Links[api.selected_link.name] + Links[title] =? url + storage.set 'links', Links + api.refresh_links! + api.select_name(title) + $home-input.focus! + + def delete_link + delete Links[$link-input-title.value] + storage.set 'links', Links + $link-input-title.value = "" + $link-input-url.value = "" + api.refresh_links! + $home-input.focus! def handle_click_copy s try @@ -18,6 +47,31 @@ tag app-home css w:100% d:flex fld:column ofy:hidden gap:20px + <.header> + css + d:hcl filter:grayscale(80%) + > input + h:34px px:20px + fs:14px + bd:1px solid $input-bc + outline:none rd:5px + bg:$input-bg c:$text-c + caret-color:$input-caret-c + <[c:$button-c] @click=delete_link!> "тип" + + <.header> css @@ -69,9 +123,10 @@ tag app-home css e:400ms of:hidden @off o:0 - if state.active_bang or state.sorted_links.length < 1 + if state.active_bang or (state.sorted_links.length < 1 and state.links.length) - else + if !state.links.length + "Add first link" diff --git a/src/components/app-links.imba b/src/components/app-links.imba index 7b80d60..0b28880 100644 --- a/src/components/app-links.imba +++ b/src/components/app-links.imba @@ -38,7 +38,14 @@ tag app-links hotkey_handler: api.decrement_link_selection_index.bind(api) hotkey: 'up' hotkey_display_name: "Up Arrow" - content: "Move Selection Up" + content: state.link_selection_index > 0 ? "Move Selection Up" : "Add New Link" + } + result.push temp + + temp = { + click_handler: do api.#link_input.focus! + hotkey_display_name: "Shift+Tab" + content: "Edit Link" } result.push temp diff --git a/src/config.imba b/src/config.imba index 18f2bb2..9597607 100644 --- a/src/config.imba +++ b/src/config.imba @@ -1,13 +1,14 @@ +import {storage} from './state.imba' export default new class config def save - global.localStorage.fuzzyhome_config = JSON.stringify(data) + storage.set 'fuzzyhome_config', data def constructor data = {} try - data = JSON.parse(global.localStorage.fuzzyhome_config) + data = storage.get 'fuzzyhome_config' data.focus ??= yes diff --git a/src/main.imba b/src/main.imba index 35db6cf..35bafa6 100644 --- a/src/main.imba +++ b/src/main.imba @@ -4,7 +4,7 @@ import pkg from '../package.json' let version = pkg.version L "fuzzyhome version {version}" -import state from './state.imba' +import state, { storage } from './state.imba' import api from './api.imba' import config from './config.imba' @@ -23,26 +23,23 @@ extend tag element api get config config + get storage + storage if config.data.focus and location.search =? "?x" throw new Error global.Pins = {} global.Frequencies = {} +global.Links = {} def init - let { pins } = await global.chrome.storage.sync.get 'pins' - Pins = pins or {} - - let { frequencies } = await global.chrome.storage.sync.get 'frequencies' - Frequencies = frequencies or {} - - global.chrome.bookmarks.getTree! do(bookmarks) - const bookmarks-bar = bookmarks[0].children[0].children - state.links = api.traverse bookmarks-bar - api.sort_links! - state.loaded = yes - imba.commit! + Pins = storage.get('pins') || {} + Frequencies = storage.get('frequencies') || {} + Links = storage.get('links') || {} + api.refresh_links! + state.loaded = yes + imba.commit! init! @@ -66,7 +63,7 @@ tag app bg:$appbg w:80vw max-width:700px max-height:80vh bxs:0px 0px 10px rgba(0,0,0,0.35) - box-sizing:border-box p:30px rd:10px mt:10vh + box-sizing:border-box p:30px rd:10px mt:7vh if state.view is 'settings' diff --git a/src/state.imba b/src/state.imba index 59d4fd6..e7f1e01 100644 --- a/src/state.imba +++ b/src/state.imba @@ -1,3 +1,8 @@ +export const storage = { + set: do |k, v| global.localStorage.setItem(k, JSON.stringify(v)) + get: do |k| JSON.parse global.localStorage.getItem(k) +} + export default { view: 'home' query: '' @@ -7,4 +12,4 @@ export default { link_selection_index: 0 active_bang: no loaded:no -} +} \ No newline at end of file diff --git a/vite.config.js b/vite.config.js index 06e3bdf..0e3ed4f 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,7 +1,8 @@ import { imba } from 'vite-plugin-imba'; import { defineConfig } from 'vite'; +import { viteSingleFile } from "vite-plugin-singlefile" export default defineConfig({ base: '', - plugins: [imba()], + plugins: [imba(), viteSingleFile({removeViteModuleLoader: true})], });