Browse Source

remove search engine config in favor of default bangs, populate edit prompt with link text

main
familyfriendlymikey 2 years ago
parent
commit
1bd3170ecc
  1. 129
      app/client.imba
  2. 9
      app/config.imba

129
app/client.imba

@ -7,6 +7,7 @@ import fzi from 'fzi'
import download from 'downloadjs' import download from 'downloadjs'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { parse_url } from './utils' import { parse_url } from './utils'
import initial_config from './config'
let state = { let state = {
query: '' query: ''
@ -14,10 +15,6 @@ let state = {
scored_links: [] scored_links: []
} }
let config = {
search_engine: {}
}
global._fuzzyhome_delete_everything = do global._fuzzyhome_delete_everything = do
return unless window.confirm "This will delete everything. Are you sure?" return unless window.confirm "This will delete everything. Are you sure?"
indexedDB.deleteDatabase("fuzzyhome") indexedDB.deleteDatabase("fuzzyhome")
@ -45,15 +42,15 @@ tag app
fatal_error = yes fatal_error = yes
return return
unless global.localStorage.fuzzyhome_visited unless global.localStorage.fuzzyhome_visited
add_initial_links! await add_initial_links!
global.localStorage.fuzzyhome_visited = yes global.localStorage.fuzzyhome_visited = yes
await load_config! await load_config!
def add_initial_links def add_initial_links
let initial_links = [ let initial_links = [
"click here to learn how to use this tool effectively github.com/familyfriendlymikey/fuzzyhome" "tutorial github.com/familyfriendlymikey/fuzzyhome"
"google google.com" "!brave search search.brave.com/search?q="
"youtube youtube.com" "!youtube search youtube.com/results?search_query="
"photopea photopea.com" "photopea photopea.com"
"twitch twitch.tv" "twitch twitch.tv"
"messenger messenger.com" "messenger messenger.com"
@ -67,16 +64,15 @@ tag app
err "adding link", e err "adding link", e
def validate_config def validate_config
throw 'config error' unless config..search_engine.hasOwnProperty 'url' throw _ if config.default_bang.id === null
throw 'config error' unless config..search_engine.hasOwnProperty 'icon' throw _ if config.default_bang.url === null
throw 'config error' unless config..search_engine.hasOwnProperty 'frequency' throw _ if config.default_bang.img === null
throw _ if config.default_bang.name === null
throw _ if config.default_bang.frequency === null
def reset_config def reset_config
p "resetting config" p "resetting config"
let url = 'https://www.google.com/search?q=' config = initial_config
let frequency = 0
let icon = await fetch_image_as_base_64 'google.com'
config.search_engine = { url, icon, frequency }
save_config! save_config!
def save_config def save_config
@ -87,7 +83,7 @@ tag app
config = JSON.parse(global.localStorage.fuzzyhome_config) config = JSON.parse(global.localStorage.fuzzyhome_config)
validate_config! validate_config!
catch catch
await reset_config! reset_config!
def err s, e def err s, e
p e p e
@ -118,19 +114,11 @@ tag app
def decrement_selection_index def decrement_selection_index
selection_index = Math.max(0, selection_index - 1) selection_index = Math.max(0, selection_index - 1)
def increment_search_engine_frequency get active_bang
config.search_engine.frequency += 1 return bang or config.default_bang
save_config!
get encoded_search_query
"{config.search_engine.url}{window.encodeURIComponent(state.query)}"
get encoded_bang_query get encoded_bang_query
"{bang.url}{window.encodeURIComponent(state.query)}" "{active_bang.url}{window.encodeURIComponent(state.query)}"
def use_search_engine
increment_search_engine_frequency!
window.location.href = encoded_search_query
def fetch_image_as_base_64 host def fetch_image_as_base_64 host
let fallback = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAH0lEQVR42mO8seXffwYqAsZRA0cNHDVw1MBRA0eqgQCDRkbJSQHxEQAAAABJRU5ErkJggg==' let fallback = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAH0lEQVR42mO8seXffwYqAsZRA0cNHDVw1MBRA0eqgQCDRkbJSQHxEQAAAABJRU5ErkJggg=='
@ -189,10 +177,11 @@ tag app
await db.links.add link await db.links.add link
await reload_db! await reload_db!
imba.commit! imba.commit!
return link
def handle_edit link def handle_edit link
def edit_link def edit_link
let input = window.prompt "Enter the new link name and url:" let input = window.prompt "Enter the new link name and url:", "{link.name} {link.url}"
return if input === null return if input === null
try try
await update_link link, input await update_link link, input
@ -209,38 +198,37 @@ tag app
throw "link id not found" if result === 0 throw "link id not found" if result === 0
await reload_db! await reload_db!
imba.commit! imba.commit!
return new_link
def handle_click_link link def handle_click_link link
await increment_link_frequency link await increment_link_frequency link
window.location.href = link.url window.location.href = link.url
def handle_click_search
increment_search_engine_frequency!
def handle_bang def handle_bang
return unless bang await increment_link_frequency active_bang
let url = encoded_bang_query window.location.href = encoded_bang_query
await increment_link_frequency bang
window.location.href = url
def handle_click_bang def handle_click_bang
handle_bang! handle_bang!
def handle_return def navigate link
if bang
handle_bang!
elif state.scored_links.length < 1
use_search_engine!
else
let link = state.scored_links[selection_index]
await increment_link_frequency link await increment_link_frequency link
window.location.href = link.url window.location.href = link.url
def handle_tab def handle_return
return bang = no if bang if bang or state.scored_links.length < 1
return unless state.scored_links.length > 0 return handle_bang!
let link = state.scored_links[selection_index]
if link.name.startsWith '!'
state.query = '' state.query = ''
bang = state.scored_links[selection_index] bang = link
else
navigate link
def handle_del
if state.query.length < 1
bang = no
sort_links!
def handle_click_delete link def handle_click_delete link
handle_delete link handle_delete link
@ -306,31 +294,14 @@ tag app
settings_active = no settings_active = no
loading = no loading = no
def handle_click_config
def edit_config
let input = window.prompt "Please enter the URL of your search engine."
return if input === null
try
var { href: url, host } = parse_url input
catch e
return err "changing search engine", e
let icon = await fetch_image_as_base_64 host
Object.assign config.search_engine, { url, icon }
save_config!
loading = yes
await edit_config!
settings_active = no
loading = no
def handle_click_github def handle_click_github
global.location.href = "https://github.com/familyfriendlymikey/fuzzyhome" global.location.href = "https://github.com/familyfriendlymikey/fuzzyhome"
def handle_paste e def handle_paste e
return if state.query.length > 0 return if state.query.length > 0
global.setTimeout(&, 0) do global.setTimeout(&, 0) do
use_search_engine! bang = config.default_bang
handle_bang!
get pretty_date get pretty_date
Date!.toString!.split(" ").slice(0, 4).join(" ") Date!.toString!.split(" ").slice(0, 4).join(" ")
@ -407,7 +378,7 @@ tag app
tt:capitalize fs:20px tt:capitalize fs:20px
overflow-wrap:anywhere overflow-wrap:anywhere
css .search css .bang-text
tt:none word-break:break-all tt:none word-break:break-all
css .link-right css .link-right
@ -451,10 +422,6 @@ tag app
.disabled=loading .disabled=loading
@click.if(!loading)=handle_click_export @click.if(!loading)=handle_click_export
> "EXPORT" > "EXPORT"
<.settings-button
.disabled=loading
@click.if(!loading)=handle_click_config
> "CONFIG"
<.settings-button <.settings-button
.disabled=loading .disabled=loading
@click.if(!loading)=handle_click_github @click.if(!loading)=handle_click_github
@ -468,7 +435,7 @@ tag app
@hotkey('shift+backspace').capture=handle_shift_backspace @hotkey('shift+backspace').capture=handle_shift_backspace
@hotkey('down').capture=increment_selection_index @hotkey('down').capture=increment_selection_index
@hotkey('up').capture=decrement_selection_index @hotkey('up').capture=decrement_selection_index
@hotkey('tab').capture=handle_tab @keydown.del=handle_del
@input=handle_input @input=handle_input
@paste=handle_paste @paste=handle_paste
@blur=this.focus @blur=this.focus
@ -488,17 +455,17 @@ tag app
<.middle-button.disabled> "+" <.middle-button.disabled> "+"
<.links> <.links>
if bang if bang or state.scored_links.length < 1
<a.link.selected <a.link.selected
href=encoded_bang_query href=encoded_bang_query
@click=handle_click_bang @click=handle_click_bang
> >
<.link-left> <.link-left>
<img.link-icon src=bang.img> <img.link-icon src=active_bang.img>
<.name.search> encoded_bang_query <.name.bang-text> encoded_bang_query
<.link-right[jc:flex-end]> <.link-right[jc:flex-end]>
<.frequency> bang.frequency <.frequency> active_bang.frequency
elif state.scored_links.length > 0 else
for link, index in state.scored_links for link, index in state.scored_links
<a.link <a.link
href=link.url href=link.url
@ -514,16 +481,6 @@ tag app
<.link-button[fs:12px]@click.prevent.stop=handle_click_edit(link)> "✎" <.link-button[fs:12px]@click.prevent.stop=handle_click_edit(link)> "✎"
<.link-button@click.prevent.stop=handle_click_delete(link)> "x" <.link-button@click.prevent.stop=handle_click_delete(link)> "x"
<.frequency> link.frequency <.frequency> link.frequency
else
<a.link.selected
href=encoded_search_query
@click=handle_click_search
>
<.link-left>
<img.link-icon src=config.search_engine.icon>
<.name.search> encoded_search_query
<.link-right[jc:flex-end]>
<.frequency> config.search_engine.frequency
$main-input.focus! $main-input.focus!
imba.mount <app> imba.mount <app>

9
app/config.imba

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save