Browse Source

polish bang history

main
familyfriendlymikey 2 years ago
parent
commit
3ac14d7dca
  1. 46
      app/api.imba
  2. 74
      app/components/app-bang.imba
  3. 3
      app/components/app-home.imba
  4. 5
      app/components/app-settings.imba
  5. 1
      app/config.imba

46
app/api.imba

@ -5,7 +5,7 @@ import db from './db'
import state from './state'
import config from './config'
import { omit, orderBy } from 'lodash'
import { find, omit, orderBy } from 'lodash'
import { nanoid } from 'nanoid'
import fzi from 'fzi'
import { evaluate as eval_math } from 'mathjs'
@ -36,7 +36,7 @@ export default new class api
def put_link link
try
await db.links.update link.id, link
if config.data.default_bang.id is link.id
if link.is_bang and config.data.default_bang.id is link.id
config.set_default_bang link
await reload_db!
catch e
@ -69,6 +69,14 @@ export default new class api
def reload_db
state.links = await db.links.toArray()
if state.active_bang
let id = state.active_bang.id
state.active_bang = find state.links, { id }
let id = config.data.default_bang.id
let link = find state.links, { id }
if link
config.data.default_bang = link
config.save!
sort_links!
def increment_link_frequency link
@ -235,12 +243,10 @@ export default new class api
state.active_bang or config.data.default_bang
get encoded_bang_query
let history_item = sorted_bang_history[state.bang_selection_index]
"{bang.url}{window.encodeURIComponent(history_item or state.query)}"
"{bang.url}{window.encodeURIComponent(state.query)}"
get encoded_bang_query_nourl
let history_item = sorted_bang_history[state.bang_selection_index]
"{window.encodeURIComponent(history_item or state.query)}"
"{window.encodeURIComponent(state.query)}"
def update_history bang
let text
@ -265,14 +271,19 @@ export default new class api
bang.history.splice(i, 1)
try
await put_link bang
state.bang_selection_index = Math.min state.bang_selection_index, sorted_bang_history.length - 1
catch e
err "updating bang history", e
def handle_bang
return if state.loading
if state.bang_selection_index > -1
state.query = sorted_bang_history[state.bang_selection_index]
state.bang_selection_index = -1
return
await increment_link_frequency bang
let to_navigate = encoded_bang_query
await update_history bang
window.location.href = to_navigate
window.location.href = encoded_bang_query
def unset_active_bang
state.active_bang = no
@ -286,3 +297,22 @@ export default new class api
get sorted_bang_history
fzi.sort state.query, bang.history
def delete_bang_history
bang.history = []
try
await put_link bang
state.bang_selection_index = -1
catch e
err "deleting bang history", e
config.data.default_bang.history = []
config.save!
def delete_all_bang_history
return unless window.confirm "Are you sure you want to delete all bang history?"
try
await db.links.toCollection!.modify! do |link| link.history = []
await reload_db!
catch e
err "deleting some link histories", e
imba.commit!

74
app/components/app-bang.imba

@ -10,32 +10,23 @@ tag app-bang
result.push <>
<.tip
@click=api.handle_bang
@hotkey('return').capture.if(!state.loading)=api.handle_bang
@hotkey('return').force=api.handle_bang
>
<.tip-hotkey> "Return"
<.tip-content> "Search History Item"
<.tip-content> "Use History Item"
else
result.push <>
<.tip
@click=api.handle_bang
@hotkey('return').capture.if(!state.loading)=api.handle_bang
@hotkey('return').force=api.handle_bang
>
<.tip-hotkey> "Return"
<.tip-content> "Search"
if state.bang_selection_index > -1
result.push <>
<.tip
@click=api.delete_bang_history_item
@hotkey('shift+backspace').capture.if(!state.loading)=api.delete_bang_history_item
>
<.tip-hotkey> "Shift + Backspace"
<.tip-content> "Delete History Item"
result.push <>
<.tip.ellipsis
@click=api.handle_add_link
@hotkey('shift+return').capture.if(!state.loading)=api.handle_add_link
@hotkey('shift+return').force=api.handle_add_link
>
<.tip-hotkey> "Shift + Return"
<.tip-content.ellipsis>
@ -50,21 +41,31 @@ tag app-bang
else
<span> "\"{sq.join " "}\""
result.push <>
if state.active_bang
if state.bang_selection_index > -1
result.push <>
<.tip
@click=api.delete_bang_history_item
@hotkey('shift+backspace').force=api.delete_bang_history_item
>
<.tip-hotkey> "Shift + Backspace"
<.tip-content> "Delete History Item"
if state.active_bang
result.push <>
<.tip
@click=api.unset_active_bang
@hotkey('esc').capture.if(!state.loading)=api.unset_active_bang
@hotkey('esc').force=api.unset_active_bang
>
<.tip-hotkey> "Esc"
<.tip-content> "Back"
else
<.tip.noclick>
<.tip-hotkey> "Paste (If Input Empty)"
<.tip-content> "Instant Search"
result.push <>
<.tip @click.if(!loading)=api.handle_cut>
<.tip@click=(api.delete_bang_history! and $tips.show_more = no)>
<.tip-hotkey> "Click"
<.tip-content> "Delete Bang History"
result.push <>
<.tip @click=api.handle_cut>
if api.math_result
<.tip-hotkey> "Cut (Math, If No Selection)"
<.tip-content> "Cut Math Result"
@ -72,11 +73,10 @@ tag app-bang
<.tip-hotkey> "Cut (If No Selection)"
<.tip-content> "Cut All Text"
if state.active_bang
result.push <>
<.tip.noclick>
<.tip-hotkey> "Paste (If Input Empty)"
<.tip-content> "Instant Search"
result.push <>
<.tip.noclick>
<.tip-hotkey> "Paste (If Input Empty)"
<.tip-content> "Instant Search"
result
@ -88,17 +88,20 @@ tag app-bang
@hotkey("down").force=api.increment_bang_selection_index
@hotkey("shift+tab").force=api.decrement_bang_selection_index
>
css w:100% d:flex fld:column gap:15px
css w:100% d:flex fld:column gap:15px ofy:hidden
<app-tips$tips tips=tips>
unless $tips.show_more
<.bang.selected
<.bang
.selected=(state.bang_selection_index is -1)
[c:#FAD4AB]=(state.bang_selection_index is -1)
@pointerover=(state.bang_selection_index = -1)
@click=api.handle_bang
>
css d:flex fld:row jc:space-between ai:center
px:16px py:11px rd:5px cursor:pointer c:#FAD4AB
px:16px py:11px rd:5px cursor:pointer c:blue3
<.link-left>
css d:flex fl:1 ofy:hidden
@ -106,7 +109,7 @@ tag app-bang
<img.link-icon src=api.bang.icon>
css w:20px h:20px mr:10px rd:3px
<.display-name> api.encoded_bang_query_nourl
<.display-name> "...{api.encoded_bang_query_nourl}"
css fs:20px of:hidden text-overflow:ellipsis
<.link-right>
@ -116,9 +119,14 @@ tag app-bang
css fs:15px ml:7px
<.history>
css d:flex fld:column jc:start ai:center
css d:flex fld:column jc:start ai:center ofy:auto
for item, index in api.sorted_bang_history
<.item [c:#FAD4AB]=(state.bang_selection_index is index)> item
<.item
@pointerover=(state.bang_selection_index = index)
@click=api.handle_bang
[c:#FAD4AB]=(state.bang_selection_index is index)
.selected=(state.bang_selection_index is index)
> item
css w:100% fs:17px c:blue3 rd:5px p:10px 10px
box-sizing:border-box
box-sizing:border-box cursor:pointer

3
app/components/app-home.imba

@ -4,7 +4,8 @@ tag app-home
$home-input.focus!
def blur
setTimeout(&, 100) do $home-input.focus!
# setTimeout(&, 100) do $home-input.focus!
p!
def handle_paste e
return unless config.data.enable_search_on_paste

5
app/components/app-settings.imba

@ -144,3 +144,8 @@ tag app-settings
<.settings-button @click.if(!loading)=handle_toggle_light_theme>
config.data.enable_dark_theme ? "DISABLE DARK THEME" : "ENABLE DARK THEME"
<.settings-container>
<.settings-button @click=api.delete_all_bang_history>
"DELETE ALL BANG HISTORY"

1
app/config.imba

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