Browse Source

refactor

main
familyfriendlymikey 2 years ago
parent
commit
d8e22509aa
  1. 14
      app/api.imba
  2. 101
      app/client.imba
  3. 308
      app/components/app-links.imba
  4. 17
      app/components/app-settings.imba
  5. 7
      app/config.imba

14
app/api.imba

@ -1,9 +1,14 @@
let p = console.log
import db from './db' import db from './db'
import state from './state' import state from './state'
import { config, save_config } from './config'
import config from './config'
p config
import { omit, orderBy } from 'lodash' import { omit, orderBy } from 'lodash'
import { parse_url } from './utils' import { parse_url } from './utils'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import fzi from 'fzi'
export default new class api export default new class api
@ -69,7 +74,7 @@ export default new class api
def sort_links def sort_links
if state.query.trim!.length <= 0 if state.query.trim!.length <= 0
return state.sorted_links = orderBy(state.links, ['is_pinned', 'frequency'], ['desc', 'desc']) return state.sorted_links = orderBy(state.links, ['is_pinned', 'frequency'], ['desc', 'desc'])
if config.enable_effective_names if config.data.enable_effective_names
return state.sorted_links = fzi state.links, state.query return state.sorted_links = fzi state.links, state.query
state.sorted_links = fzi state.links, state.query, "display_name" state.sorted_links = fzi state.links, state.query, "display_name"
@ -115,6 +120,7 @@ export default new class api
reader.readAsDataURL(blob) reader.readAsDataURL(blob)
def toggle_effective_names def toggle_effective_names
config.enable_effective_names = !config.enable_effective_names config.data.enable_effective_names = !config.data.enable_effective_names
save_config! config.save!
sort_links!

101
app/client.imba

@ -8,7 +8,6 @@ let p = console.log
# import sw from './sw.imba?serviceworker' # import sw from './sw.imba?serviceworker'
# navigator..serviceWorker..register(sw).then! do |reg| reg.update! # navigator..serviceWorker..register(sw).then! do |reg| reg.update!
import fzi from 'fzi'
import download from 'downloadjs' import download from 'downloadjs'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { evaluate as eval_math } from 'mathjs' import { evaluate as eval_math } from 'mathjs'
@ -18,7 +17,7 @@ let version = pkg.version
import db from './db' import db from './db'
import state from './state' import state from './state'
import api from './api' import api from './api'
import { config, save_config } from './config' import config from './config'
import app-community-links from './components/app-community-links' import app-community-links from './components/app-community-links'
import app-settings from './components/app-settings' import app-settings from './components/app-settings'
@ -36,6 +35,8 @@ global._fuzzyhome_delete_everything = do |prompt=yes|
delete localStorage.fuzzyhome_visited delete localStorage.fuzzyhome_visited
location.reload! location.reload!
let refs = {}
extend tag element extend tag element
get state get state
state state
@ -45,6 +46,8 @@ extend tag element
config config
get p get p
console.log console.log
get refs
refs
tag app tag app
@ -53,6 +56,9 @@ tag app
get render? do mounted? get render? do mounted?
def mount def mount
refs.settings = $as
refs.edit = $ae
refs.community-links = $acl
unless global.localStorage.fuzzyhome_visited unless global.localStorage.fuzzyhome_visited
await add_initial_links! await add_initial_links!
global.localStorage.fuzzyhome_visited = yes global.localStorage.fuzzyhome_visited = yes
@ -135,93 +141,6 @@ tag app
link_text += " {link.url}" link_text += " {link.url}"
link_text link_text
def handle_edit link
prior_query = state.query
editing_link = link
state.query = construct_link_text(link)
def make_edit link, new_link_text
def edit_link
try
await update_link link, new_link_text
catch e
return err "editing link", e
state.loading = yes
await edit_link!
state.loading = no
def handle_click_link link
if link.is_bang
state.query = ''
bang = link
else
navigate link
def handle_bang
await increment_link_frequency active_bang
window.location.href = encoded_bang_query
def handle_click_bang
handle_bang!
def navigate link
await increment_link_frequency link
window.location.href = link.url
def handle_return
return if editing_link
if bang or state.sorted_links.length < 1
return handle_bang!
let link = selected_link
if link.is_bang
state.query = ''
bang = link
else
navigate link
def handle_del
if state.query.length < 1
bang = no
sort_links!
def handle_click_delete link
return unless window.confirm "Do you really want to delete {link..display_name}?"
handle_delete link
def handle_click_edit link
handle_edit link
def handle_click_pin link
api.pin_link link
def handle_shift_backspace
if editing_link
await handle_delete editing_link
else
return unless state.sorted_links.length > 0
handle_edit selected_link
def handle_shift_return
def go
if viewing_community_links
try
await add_community_link selected_link
catch e
err "adding community link", e
elif editing_link
try
await update_link editing_link, state.query
catch e
err "updating link", e
else
handle_add!
state.loading = yes
await go!
editing_link = no
state.query = ''
sort_links!
state.loading = no
def handle_esc def handle_esc
if editing_link if editing_link
editing_link = no editing_link = no
@ -235,11 +154,11 @@ tag app
state.links.some! do |{name}| new_name is name state.links.some! do |{name}| new_name is name
def handle_paste e def handle_paste e
return unless config.enable_search_on_paste return unless config.data.enable_search_on_paste
return if state.query.length > 0 return if state.query.length > 0
global.setTimeout(&, 0) do global.setTimeout(&, 0) do
return if math_result isnt no return if math_result isnt no
bang ||= config.default_bang bang ||= config.data.default_bang
handle_bang! handle_bang!
def handle_click_copy s def handle_click_copy s

308
app/components/app-links.imba

@ -3,6 +3,10 @@ tag app-links
selection_index = 0 selection_index = 0
bang = no bang = no
def mount
p document
$links-input.focus!
def increment_selection_index def increment_selection_index
selection_index = Math.min(state.sorted_links.length - 1, selection_index + 1) selection_index = Math.min(state.sorted_links.length - 1, selection_index + 1)
@ -10,14 +14,11 @@ tag app-links
selection_index = Math.max(0, selection_index - 1) selection_index = Math.max(0, selection_index - 1)
get active_bang get active_bang
return bang or config.default_bang return bang or config.data.default_bang
get encoded_bang_query get encoded_bang_query
"{active_bang.url}{window.encodeURIComponent(state.query)}" "{active_bang.url}{window.encodeURIComponent(state.query)}"
def mount
$links-input.focus!
get math_result get math_result
try try
let result = Number(eval_math state.query) let result = Number(eval_math state.query)
@ -29,11 +30,101 @@ tag app-links
def handle_input def handle_input
selection_index = 0 selection_index = 0
sort_links! api.sort_links!
def handle_edit link
prior_query = state.query
editing_link = link
state.query = construct_link_text(link)
def make_edit link, new_link_text
def edit_link
try
await update_link link, new_link_text
catch e
return err "editing link", e
state.loading = yes
await edit_link!
state.loading = no
def handle_click_link link
if link.is_bang
state.query = ''
bang = link
else
navigate link
def handle_bang
await increment_link_frequency active_bang
window.location.href = encoded_bang_query
def handle_click_bang
handle_bang!
def navigate link
await increment_link_frequency link
window.location.href = link.url
def handle_return
return if editing_link
if bang or state.sorted_links.length < 1
return handle_bang!
let link = selected_link
if link.is_bang
state.query = ''
bang = link
else
navigate link
def handle_del
if state.query.length < 1
bang = no
api.sort_links!
def handle_click_delete link
return unless window.confirm "Do you really want to delete {link..display_name}?"
handle_delete link
def handle_click_edit link
handle_edit link
def handle_click_pin link
api.pin_link link
def handle_shift_backspace
if editing_link
await handle_delete editing_link
else
return unless state.sorted_links.length > 0
handle_edit selected_link
def handle_shift_return
def go
if viewing_community_links
try
await add_community_link selected_link
catch e
err "adding community link", e
elif editing_link
try
await update_link editing_link, state.query
catch e
err "updating link", e
else
handle_add!
state.loading = yes
await go!
editing_link = no
state.query = ''
api.sort_links!
state.loading = no
def toggle_settings
refs.settings.open!
def render def render
<self> <self[w:100%]>
css .link css .link
d:flex fld:row jc:space-between ai:center d:flex fld:row jc:space-between ai:center
@ -99,109 +190,114 @@ tag app-links
css .right css .right
d:flex jc:right d:flex jc:right
<.header> if $as.active
<app-settings$as>
else
<.header>
<.side.left@click=api.toggle_effective_names>
if config.data.enable_effective_names
<svg src="../assets/eye.svg">
else
<svg src="../assets/eye-off.svg">
<input$links-input
bind=state.query
@hotkey('return').capture.if(!state.loading)=handle_return
@hotkey('tab').capture.if(!state.loading)=api.toggle_effective_names
@hotkey('esc').capture.if(!state.loading)=toggle_settings
@hotkey('shift+return').capture.if(!state.loading)=handle_shift_return
@hotkey('esc').capture.if(!state.loading)=handle_esc
@hotkey('shift+backspace').capture.if(!state.loading)=handle_shift_backspace
@hotkey('down').capture.if(!state.loading)=increment_selection_index
@hotkey('up').capture.if(!state.loading)=decrement_selection_index
@keydown.del.if(!state.loading)=handle_del
@input.if(!state.loading)=handle_input
@paste.if(!state.loading)=handle_paste
@blur=this.focus
@cut=handle_cut
disabled=state.loading
>
<.side.left@click=api.toggle_effective_names> if (let m = math_result) isnt no
if config.enable_effective_names <.side.right[c:blue3 fs:20px ml:10px w:unset]
<svg src="../assets/eye.svg"> @click=handle_click_copy(m)
> "= {Math.round(m * 100)/100}"
else else
<svg src="../assets/eye-off.svg"> <.side.right @click.if(!state.loading)=toggle_settings>
<svg src="../assets/settings.svg">
<input$links-input
bind=state.query if config.data.enable_tips
@hotkey('return').capture.if(!state.loading)=handle_return <.middle-button>
@hotkey('tab').capture.if(!state.loading)=api.toggle_effective_names <.tip[jc:start ta:left fl:1] @click=handle_return>
@hotkey('shift+return').capture.if(!state.loading)=handle_shift_return <.tip-hotkey> "Return"
@hotkey('esc').capture.if(!state.loading)=handle_esc <.tip-content> "Navigate To Link"
@hotkey('shift+backspace').capture.if(!state.loading)=handle_shift_backspace <.tip[jc:center ta:center fl:2 px:15px]
@hotkey('down').capture.if(!state.loading)=increment_selection_index @click=handle_shift_return
@hotkey('up').capture.if(!state.loading)=decrement_selection_index >
@keydown.del.if(!state.loading)=handle_del <.tip-hotkey> "Shift + Return"
@input.if(!state.loading)=handle_input <.tip-content[of:hidden text-overflow:ellipsis white-space:nowrap]>
@paste.if(!state.loading)=handle_paste <span> "Add New Link"
@blur=this.focus <span[ws:pre]> " "
@cut=handle_cut let sq = state.query.trim!.split /\s+/
disabled=state.loading if sq.length >= 2
> let url = sq.pop!
<span> '"'
if (let m = math_result) isnt no <span> sq.join ' '
<.side.right[c:blue3 fs:20px ml:10px w:unset] <span[ws:pre]> ' '
@click=handle_click_copy(m) <span[c:blue3]> url
> "= {Math.round(m * 100)/100}" <span> '"'
else else
<.side.right @click.if(!state.loading)=toggle_settings> <span> '"'
<svg src="../assets/settings.svg"> <span> sq.join ' '
<span> '"'
if config.enable_tips and not config.enable_simplify_ui <.tip[jc:end ta:right fl:1]
<.middle-button> @click=handle_shift_backspace
<.tip[jc:start ta:left fl:1] @click=handle_return> >
<.tip-hotkey> "Return" <.tip-hotkey> "Shift + Backspace"
<.tip-content> "Navigate To Link" <.tip-content> "Edit Link"
<.tip[jc:center ta:center fl:2 px:15px]
@click=handle_shift_return
>
<.tip-hotkey> "Shift + Return"
<.tip-content[of:hidden text-overflow:ellipsis white-space:nowrap]>
<span> "Add New Link"
<span[ws:pre]> " "
let sq = state.query.trim!.split /\s+/
if sq.length >= 2
let url = sq.pop!
<span> '"'
<span> sq.join ' '
<span[ws:pre]> ' '
<span[c:blue3]> url
<span> '"'
else
<span> '"'
<span> sq.join ' '
<span> '"'
<.tip[jc:end ta:right fl:1]
@click=handle_shift_backspace
>
<.tip-hotkey> "Shift + Backspace"
<.tip-content> "Edit Link"
<div> <div>
css d:flex fld:column jc:flex-start fl:1 w:100% ofy:auto pt:15px css d:flex fld:column jc:flex-start fl:1 w:100% ofy:auto pt:15px
if not viewing_community_links and (bang or state.sorted_links.length < 1) if not viewing_community_links and (bang or state.sorted_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>
<img.link-icon src=active_bang.icon>
<.display-name.bang-text> encoded_bang_query
<.link-right[jc:flex-end]>
<.frequency> active_bang.frequency
else
for link, index in state.sorted_links
<a.link
href=link.url
@pointerover=(selection_index = index)
@click.prevent=handle_click_link(link)
.selected=(index is selection_index)
> >
<.link-left> <.link-left>
<img.link-icon src=link.icon> <img.link-icon src=active_bang.icon>
<.display-name <.display-name.bang-text> encoded_bang_query
[c:#FAD4AB]=link.is_bang <.link-right[jc:flex-end]>
> link.display_name <.frequency> active_bang.frequency
if link.display_name isnt link.name and config.enable_effective_names else
<.name> for link, index in state.sorted_links
<span.parens> "(" <a.link
<span> link.name href=link.url
<span.parens> ")" @pointerover=(selection_index = index)
<.link-right> @click.prevent=handle_click_link(link)
<.link-buttons .buttons-disabled=(not config.enable_buttons or config.enable_simplify_ui)> .selected=(index is selection_index)
<.link-button@click.prevent.stop=handle_click_edit(link)> >
<svg src='../assets/edit-2.svg'> <.link-left>
<.link-button@click.prevent.stop=handle_click_delete(link)> <img.link-icon src=link.icon>
<svg src='../assets/trash.svg'> <.display-name
<.link-button [c:#FAD4AB]=link.is_bang
@click.prevent.stop=handle_click_pin(link) > link.display_name
[visibility:visible c:purple3/50]=(link.is_pinned and (index isnt selection_index or not config.enable_buttons or config.enable_simplify_ui)) if link.display_name isnt link.name and config.data.enable_effective_names
> <.name>
<svg src='../assets/star.svg'> <span.parens> "("
<.frequency> link.frequency <span> link.name
<span.parens> ")"
<.link-right>
<.link-buttons .buttons-disabled=!config.data.enable_buttons>
<.link-button@click.prevent.stop=handle_click_edit(link)>
<svg src='../assets/edit-2.svg'>
<.link-button@click.prevent.stop=handle_click_delete(link)>
<svg src='../assets/trash.svg'>
<.link-button
@click.prevent.stop=handle_click_pin(link)
[visibility:visible c:purple3/50]=(link.is_pinned and (index isnt selection_index or not config.data.enable_buttons))
>
<svg src='../assets/star.svg'>
<.frequency> link.frequency

17
app/components/app-settings.imba

@ -2,9 +2,12 @@ tag app-settings
active = no active = no
def end def close
active = no active = no
def open
active = yes
def handle_click_github def handle_click_github
global.location.href = "https://github.com/familyfriendlymikey/fuzzyhome" global.location.href = "https://github.com/familyfriendlymikey/fuzzyhome"
@ -66,7 +69,7 @@ tag app-settings
def render def render
<self> <self[w:100%]>
css .settings-container css .settings-container
d:flex fld:row jc:space-around ai:center d:flex fld:row jc:space-around ai:center
@ -80,13 +83,13 @@ tag app-settings
bg:purple4/10 rd:5px bg:purple4/10 rd:5px
h:100% h:100%
if $community-links.active if refs.community-links.active
<community-links> <app-community-links>
else else
<.settings-container> <.settings-container>
<.settings-button <.settings-button
@click=end @click=close
@hotkey("esc")=end @hotkey("esc")=close
> "BACK" > "BACK"
<.settings-container> <.settings-container>
<.settings-button <.settings-button
@ -127,8 +130,6 @@ tag app-settings
@click=handle_click_toggle_search_on_paste @click=handle_click_toggle_search_on_paste
> >
config.enable_search_on_paste ? "DISABLE SEARCH ON PASTE" : "ENABLE SEARCH ON PASTE" config.enable_search_on_paste ? "DISABLE SEARCH ON PASTE" : "ENABLE SEARCH ON PASTE"
<.settings-container>
<.settings-button <.settings-button
@click.if(!loading)=handle_toggle_light_theme @click.if(!loading)=handle_toggle_light_theme
> >

7
app/config.imba

@ -1,4 +1,4 @@
let { data, save } = new class config export default new class config
def constructor def constructor
this.data = {} this.data = {}
@ -21,8 +21,3 @@ let { data, save } = new class config
def save def save
global.localStorage.fuzzyhome_config = JSON.stringify(this.data) global.localStorage.fuzzyhome_config = JSON.stringify(this.data)
export {
data as config
save as save_config
}

Loading…
Cancel
Save