Browse Source

refactor: community links, update imba and fzi

main
familyfriendlymikey 2 years ago
parent
commit
7a8648cd23
  1. 20
      app/api.imba
  2. 77
      app/components/app-community-links.imba
  3. 5
      app/components/app-settings.imba
  4. 28
      package-lock.json
  5. 4
      package.json

20
app/api.imba

@ -80,10 +80,14 @@ 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']) state.sorted_links = orderBy(state.links, ['is_pinned', 'frequency'], ['desc', 'desc'])
if config.data.enable_effective_names elif config.data.enable_effective_names
return state.sorted_links = fzi state.links, state.query state.sorted_links = fzi.sort state.query, state.links, do |x| x.name
state.sorted_links = fzi state.links, state.query, "display_name" else
state.sorted_links = fzi.sort state.query, state.links, do |x| x.display_name
def name_exists new_name
state.links.some! do |{name}| new_name is name
def add_initial_links def add_initial_links
let initial_links = [ let initial_links = [
@ -102,7 +106,7 @@ export default new class api
catch e catch e
err "adding link", e err "adding link", e
def create_link_from_text text def create_link_from_text text, get_icon=yes
text = text.trim! text = text.trim!
throw "Text is empty." if text is '' throw "Text is empty." if text is ''
let split_text = text.split(/\s+/) let split_text = text.split(/\s+/)
@ -110,7 +114,6 @@ export default new class api
let url = split_text.pop! let url = split_text.pop!
let host let host
{ href:url, host } = parse_url url { href:url, host } = parse_url url
let icon = await fetch_image_as_base_64 host
let name let name
if split_text[-1].startsWith "`" if split_text[-1].startsWith "`"
name = split_text.pop!.slice(1) name = split_text.pop!.slice(1)
@ -121,7 +124,10 @@ export default new class api
is_bang = yes is_bang = yes
display_name = display_name.slice(1) display_name = display_name.slice(1)
name ||= display_name name ||= display_name
{ name, display_name, is_bang, is_pinned, url, frequency:0, icon } let link = { name, display_name, is_bang, is_pinned, url, frequency:0 }
if get_icon
link.icon = await fetch_image_as_base_64 host
return link
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=='

77
app/components/app-community-links.imba

@ -1,14 +1,37 @@
let p = console.log let p = console.log
import links from '../assets/community_links' import all_links from '../assets/community_links'
import fzi from 'fzi' import fzi from 'fzi'
import api from '../api'
tag app-community-links tag app-community-links
active = yes active = no
selection_index = 0 selection_index = 0
query = ""
get render? do mounted?
def mount def mount
$cli.focus! $cli.focus!
links = await get_links!
render!
def get_links
let result = []
for link_text in all_links
let link = await api.create_link_from_text(link_text, no)
link.link_text = link_text
result.push link
result
get filtered_links
links.filter! do |link| !api.name_exists(link.name)
get sorted_links
fzi.sort query, filtered_links, do |x| x.name
get selected_link
sorted_links[selection_index]
def open def open
active = yes active = yes
@ -22,27 +45,31 @@ tag app-community-links
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 get_link_obj link_text def add_community_link
let split_text = link_text.trim!.split /\s+/ return if state.loading
let url = split_text.pop! state.loading = yes
let rest = split_text.join " " try
{ url, rest } await api.add_link selected_link.link_text
catch e
def add_community_link link err "adding link", e
await db.links.add link state.loading = no
await api.reload_db!
imba.commit!
get tips get tips
let result = [] let result = []
result.push <> result.push <>
<.tip @click=close> <.tip
@click=close
@hotkey("esc").force=close
>
<.tip-hotkey> "Esc" <.tip-hotkey> "Esc"
<.tip-content> "Exit Community Links" <.tip-content> "Exit Community Links"
result.push <> result.push <>
<.tip @click=handle_shift_return> <.tip
@click=add_community_link
@hotkey("shift+return").force=add_community_link
>
<.tip-hotkey> "Shift + Return Or Click" <.tip-hotkey> "Shift + Return Or Click"
<.tip-content> "Add To Your Links" <.tip-content> "Add To Your Links"
@ -62,28 +89,30 @@ tag app-community-links
css d:flex fld:column jc:start gap:15px fl:1 w:100% ofy:hidden css d:flex fld:column jc:start gap:15px fl:1 w:100% ofy:hidden
<div> <div>
<input$cli autofocus> <input$cli
autofocus
bind=query
>
<app-tips tips=tips> <app-tips tips=tips>
<.links> <.links>
css ofy:auto css ofy:auto
for link_text, index in links for link, index in sorted_links
<.link <.link
.selected=(selection_index == index) .selected=(selection_index == index)
@pointerover=(selection_index = index) @pointerover=(selection_index = index)
@click=(add_community_link(link_text)) @click=add_community_link
> >
css d:flex fld:row jc:space-between ai:center px:16px css d:flex fld:row jc:space-between ai:center px:16px
py:11px rd:5px cursor:pointer c:blue3 min-height:35px py:2px rd:5px cursor:pointer c:blue3 min-height:35px
let { url, rest } = get_link_obj link_text if link.is_bang
css c:#FAD4AB
<.link-left> rest <.link-left> link.name
css fl:1 css fl:1
if rest.startsWith("!")
css c:#FAD4AB
<.link-right> url <link-right.ellipsis> link.url
css fl:1 overflow-wrap:anywhere word-break:break-all css fl:1 c:inherit

5
app/components/app-settings.imba

@ -35,9 +35,6 @@ tag app-settings
def handle_click_import e def handle_click_import e
def name_exists new_name
state.links.some! do |{name}| new_name is name
def handle_import def handle_import
let errors = [] let errors = []
try try
@ -48,7 +45,7 @@ tag app-settings
for link_text in links for link_text in links
try try
let link = await api.create_link_from_text link_text let link = await api.create_link_from_text link_text
if name_exists link.name if api.name_exists link.name
throw "Name already exists, add manually if you don't mind duplicates." throw "Name already exists, add manually if you don't mind duplicates."
api.add_link link_text api.add_link link_text
catch e catch e

28
package-lock.json

@ -11,8 +11,8 @@
"dexie": "^3.2.2", "dexie": "^3.2.2",
"dexie-export-import": "^1.0.3", "dexie-export-import": "^1.0.3",
"downloadjs": "^1.4.7", "downloadjs": "^1.4.7",
"fzi": "^1.0.0", "fzi": "^1.1.0",
"imba": "^2.0.0-alpha.219", "imba": "^2.0.0-alpha.220",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mathjs": "^11.1.0", "mathjs": "^11.1.0",
"nanoid": "^4.0.0" "nanoid": "^4.0.0"
@ -516,9 +516,9 @@
} }
}, },
"node_modules/fzi": { "node_modules/fzi": {
"version": "1.0.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/fzi/-/fzi-1.0.0.tgz", "resolved": "https://registry.npmjs.org/fzi/-/fzi-1.1.0.tgz",
"integrity": "sha512-ZsF/SPkQVWrVPvhkKRivf7pM8LC1kMQmWPXPlbHIdQPL10a6Q9eiVbCJfmEVRFbZafd66DQMC8brfpORxSaKKA==" "integrity": "sha512-5bqCChh+i3RQ2Acl+FGg7JnxONXLaWDl4g/JLbRJE+zV5zX0GI0ScpKr/MzgDqzRnhkqy58J6kVy+GfDv2QSFw=="
}, },
"node_modules/glob-parent": { "node_modules/glob-parent": {
"version": "5.1.2", "version": "5.1.2",
@ -532,9 +532,9 @@
} }
}, },
"node_modules/imba": { "node_modules/imba": {
"version": "2.0.0-alpha.219", "version": "2.0.0-alpha.220",
"resolved": "https://registry.npmjs.org/imba/-/imba-2.0.0-alpha.219.tgz", "resolved": "https://registry.npmjs.org/imba/-/imba-2.0.0-alpha.220.tgz",
"integrity": "sha512-5BIUK9DBNDvprJ/hPezfg4dk6w96j/yOCWU35O/CKpLx8ce+k/u534HYrdQNGZqfTY7ImSA5uRduVbp1zKi9Wg==", "integrity": "sha512-Q2Kiqb7kLKfVZ19kje0PWgWR4K+0Nt3RmY2RD13AkhEz2Wksdr3Sj1PGJazy2gW/xC18ILB2MgPplXxzGr3dWQ==",
"dependencies": { "dependencies": {
"chokidar": "^3.4.3", "chokidar": "^3.4.3",
"esbuild": "^0.15.2" "esbuild": "^0.15.2"
@ -945,9 +945,9 @@
"optional": true "optional": true
}, },
"fzi": { "fzi": {
"version": "1.0.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/fzi/-/fzi-1.0.0.tgz", "resolved": "https://registry.npmjs.org/fzi/-/fzi-1.1.0.tgz",
"integrity": "sha512-ZsF/SPkQVWrVPvhkKRivf7pM8LC1kMQmWPXPlbHIdQPL10a6Q9eiVbCJfmEVRFbZafd66DQMC8brfpORxSaKKA==" "integrity": "sha512-5bqCChh+i3RQ2Acl+FGg7JnxONXLaWDl4g/JLbRJE+zV5zX0GI0ScpKr/MzgDqzRnhkqy58J6kVy+GfDv2QSFw=="
}, },
"glob-parent": { "glob-parent": {
"version": "5.1.2", "version": "5.1.2",
@ -958,9 +958,9 @@
} }
}, },
"imba": { "imba": {
"version": "2.0.0-alpha.219", "version": "2.0.0-alpha.220",
"resolved": "https://registry.npmjs.org/imba/-/imba-2.0.0-alpha.219.tgz", "resolved": "https://registry.npmjs.org/imba/-/imba-2.0.0-alpha.220.tgz",
"integrity": "sha512-5BIUK9DBNDvprJ/hPezfg4dk6w96j/yOCWU35O/CKpLx8ce+k/u534HYrdQNGZqfTY7ImSA5uRduVbp1zKi9Wg==", "integrity": "sha512-Q2Kiqb7kLKfVZ19kje0PWgWR4K+0Nt3RmY2RD13AkhEz2Wksdr3Sj1PGJazy2gW/xC18ILB2MgPplXxzGr3dWQ==",
"requires": { "requires": {
"chokidar": "^3.4.3", "chokidar": "^3.4.3",
"esbuild": "^0.15.2" "esbuild": "^0.15.2"

4
package.json

@ -14,8 +14,8 @@
"dexie": "^3.2.2", "dexie": "^3.2.2",
"dexie-export-import": "^1.0.3", "dexie-export-import": "^1.0.3",
"downloadjs": "^1.4.7", "downloadjs": "^1.4.7",
"fzi": "^1.0.0", "fzi": "^1.1.0",
"imba": "^2.0.0-alpha.219", "imba": "^2.0.0-alpha.220",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mathjs": "^11.1.0", "mathjs": "^11.1.0",
"nanoid": "^4.0.0" "nanoid": "^4.0.0"

Loading…
Cancel
Save