Browse Source

refactor, client.imba done

main
familyfriendlymikey 2 years ago
parent
commit
eb5bf6d94d
  1. 41
      app/api.imba
  2. 135
      app/client.imba
  3. 2
      app/components/app-edit.imba
  4. 49
      app/components/app-links.imba
  5. 7
      app/components/app-settings.imba
  6. 2
      app/db.imba
  7. 23
      app/utils.imba

41
app/api.imba

@ -1,4 +1,5 @@
let p = console.log let p = console.log
import { err } from './utils'
import db from './db' import db from './db'
import state from './state' import state from './state'
@ -6,7 +7,6 @@ import state from './state'
import config from './config' import config from './config'
p config p config
import { omit, orderBy } from 'lodash' import { omit, orderBy } from 'lodash'
import { parse_url } from './utils'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import fzi from 'fzi' import fzi from 'fzi'
@ -78,6 +78,23 @@ export default new class api
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"
def add_initial_links
let initial_links = [
"tutorial github.com/familyfriendlymikey/fuzzyhome"
"!brave search `b search.brave.com/search?q="
"!youtube youtube.com/results?search_query="
"photopea photopea.com"
"twitch twitch.tv"
"messenger `me messenger.com"
"instagram `in instagram.com"
"localhost `3000 http://localhost:3000"
]
for link_text in initial_links
try
add_link link_text
catch e
err "adding link", e
def create_link_from_text text def create_link_from_text text
text = text.trim! text = text.trim!
throw "Text is empty." if text is '' throw "Text is empty." if text is ''
@ -124,3 +141,25 @@ export default new class api
config.save! config.save!
sort_links! sort_links!
def construct_link_text link
let link_text = ""
link_text += "!" if link.is_bang
link_text += link.display_name
link_text += " `{link.name}" if link.name isnt link.display_name
link_text += " {link.url}"
link_text
def parse_url url
throw "invalid url" if url === null
let get_url = do |s|
let url = new URL s
throw _ unless (url.host and url.href)
url
try
return get_url url
try
return get_url "https://{url}"
throw "invalid url"
def get_pretty_date
Date!.toString!.split(" ").slice(0, 4).join(" ")

135
app/client.imba

@ -1,8 +1,3 @@
# TODO, deal with these 3:
# editing_link = no
# prior_query = ''
# viewing_community_links = yes
let p = console.log let p = console.log
# import sw from './sw.imba?serviceworker' # import sw from './sw.imba?serviceworker'
@ -10,12 +5,14 @@ let p = console.log
import download from 'downloadjs' import download from 'downloadjs'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import { evaluate as eval_math } from 'mathjs' import { err } from './utils'
import pkg from '../package.json' import pkg from '../package.json'
let version = pkg.version let version = pkg.version
p "fuzzyhome version {version}"
import db from './db' import db from './db'
import state from './state' import state from './state'
let refs = {}
import api from './api' import api from './api'
import config from './config' import config from './config'
@ -28,17 +25,6 @@ import app-link from './components/app-link'
import app-bang from './components/app-bang' import app-bang from './components/app-bang'
import './styles' import './styles'
p "fuzzyhome version {version}"
global._fuzzyhome_delete_everything = do |prompt=yes|
return if prompt and window.confirm "This will delete everything. Are you sure?"
indexedDB.deleteDatabase("fuzzyhome")
delete localStorage.fuzzyhome_config
delete localStorage.fuzzyhome_visited
location.reload!
let refs = {}
extend tag element extend tag element
get state get state
state state
@ -50,6 +36,8 @@ extend tag element
console.log console.log
get refs get refs
refs refs
get err
err
tag app tag app
@ -58,12 +46,15 @@ tag app
get render? do mounted? get render? do mounted?
def mount def mount
refs.settings = $as refs.settings = $as
refs.edit = $ae refs.edit = $ae
refs.community-links = $acl refs.community-links = $acl
unless global.localStorage.fuzzyhome_visited unless global.localStorage.fuzzyhome_visited
await add_initial_links! await api.add_initial_links!
global.localStorage.fuzzyhome_visited = yes global.localStorage.fuzzyhome_visited = yes
try try
await api.reload_db! await api.reload_db!
p "links:", state.links p "links:", state.links
@ -72,111 +63,6 @@ tag app
fatal_error = yes fatal_error = yes
return return
def add_initial_links
let initial_links = [
"tutorial github.com/familyfriendlymikey/fuzzyhome"
"!brave search `b search.brave.com/search?q="
"!youtube youtube.com/results?search_query="
"photopea photopea.com"
"twitch twitch.tv"
"messenger `me messenger.com"
"instagram `in instagram.com"
"localhost `3000 http://localhost:3000"
]
for link_text in initial_links
try
api.add_link link_text
catch e
err "adding link", e
def err s, e
p "error:"
p e
window.alert("Error {s}:\n\n{e}")
get selected_link
state.sorted_links[selection_index]
get tip_url
let split_query = state.query.trim!.split /\s+/
if split_query.length >= 2
return ' https://' + split_query.pop!
else
return ''
get tip_name
let split_query = state.query.trim!.split /\s+/
let name = split_query.join ' '
if split_query.length >= 2
split_query.pop!
if split_query[-1].startsWith '~'
split_query.pop!
name = split_query.join ' '
if name.startsWith '!'
name = name.slice(1)
name
get can_add
return no if state.loading
return no if settings_active
let query = state.query.trim!
return no if query is ''
let split_query = query.split /\s+/
return no if split_query.length < 2
yes
def handle_add
state.loading = yes
try
await api.add_link state.query
state.query = ''
sort_links!
catch e
err "adding link", e
state.loading = no
def construct_link_text link
let link_text = ""
link_text += "!" if link.is_bang
link_text += link.display_name
link_text += " `{link.name}" if link.name isnt link.display_name
link_text += " {link.url}"
link_text
def handle_esc
if editing_link
editing_link = no
state.query = prior_query
prior_query = ''
elif viewing_community_links
viewing_community_links = no
sort_links!
def name_exists new_name
state.links.some! do |{name}| new_name is name
def handle_paste e
return unless config.data.enable_search_on_paste
return if state.query.length > 0
global.setTimeout(&, 0) do
return if math_result isnt no
bang ||= config.data.default_bang
handle_bang!
def handle_click_copy s
try
await window.navigator.clipboard.writeText(s)
state.query = ''
sort_links!
def handle_cut e
return unless e.target.selectionStart == e.target.selectionEnd
let s = math_result
s ||= state.query
await window.navigator.clipboard.writeText(s)
state.query = ''
sort_links!
def render def render
<self.disabled=state.loading> <self.disabled=state.loading>
@ -186,7 +72,8 @@ tag app
box-sizing:border-box p:30px rd:10px mt:10vh box-sizing:border-box p:30px rd:10px mt:10vh
if fatal_error if fatal_error
<[c:blue2]> <.fatal>
css c:blue2
""" """
There was an error state.loading the database. There was an error state.loading the database.
This could be due to a user setting This could be due to a user setting

2
app/components/app-edit.imba

@ -14,7 +14,7 @@ tag app-edit < app-prompt
<self> <self>
<.tips> <.tips>
<.tip <.tip
@click=handle_esc @click=(active = no)
> >
<.tip-hotkey> "Esc" <.tip-hotkey> "Esc"
<.tip-content> "Cancel Edits" <.tip-content> "Cancel Edits"

49
app/components/app-links.imba

@ -1,3 +1,5 @@
import { evaluate as eval_math } from 'mathjs'
tag app-links tag app-links
selection_index = 0 selection_index = 0
@ -7,12 +9,44 @@ tag app-links
p document p document
$links-input.focus! $links-input.focus!
def handle_paste e
return unless config.data.enable_search_on_paste
return if state.query.length > 0
global.setTimeout(&, 0) do
return if math_result isnt no
bang ||= config.data.default_bang
handle_bang!
def handle_click_copy s
try
await window.navigator.clipboard.writeText(s)
state.query = ''
sort_links!
def handle_cut e
return unless e.target.selectionStart == e.target.selectionEnd
let s = math_result
s ||= state.query
await window.navigator.clipboard.writeText(s)
state.query = ''
sort_links!
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)
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 handle_add
state.loading = yes
try
await api.add_link state.query
state.query = ''
sort_links!
catch e
err "adding link", e
state.loading = no
get math_result get math_result
try try
let result = Number(eval_math state.query) let result = Number(eval_math state.query)
@ -29,7 +63,7 @@ tag app-links
def handle_edit link def handle_edit link
prior_query = state.query prior_query = state.query
editing_link = link editing_link = link
state.query = construct_link_text(link) state.query = api.construct_link_text(link)
def make_edit link, new_link_text def make_edit link, new_link_text
def edit_link def edit_link
@ -117,10 +151,11 @@ tag app-links
<.header> <.header>
css d:flex fld:row w:100% css d:flex fld:row w:100%
css .side c:purple3/90 fs:15px d:flex ja:center w:30px cursor:pointer css .side c:purple3/90 fs:15px d:flex ja:center w:30px cursor:pointer
css .side svg w:15px css .side svg w:15px d:flex
css .left jc:left
css .right jc:right
<.side@click=api.toggle_effective_names> <.side.left@click=api.toggle_effective_names>
css d:flex jc:left
if config.data.enable_effective_names if config.data.enable_effective_names
<svg src="../assets/eye.svg"> <svg src="../assets/eye.svg">
@ -147,9 +182,9 @@ tag app-links
> >
if (let m = math_result) isnt no if (let m = math_result) isnt no
<.side @click=handle_click_copy(m)> <.side.right@click=handle_click_copy(m)>
"= {Math.round(m * 100)/100}" "= {Math.round(m * 100)/100}"
css d:flex jc:right c:blue3 fs:20px ml:10px w:unset css c:blue3 fs:20px ml:10px w:unset
else else
<.side.right @click.if(!state.loading)=toggle_settings> <.side.right @click.if(!state.loading)=toggle_settings>
@ -214,7 +249,7 @@ tag app-links
<span[c:blue3 ws:pre]> " {url}" <span[c:blue3 ws:pre]> " {url}"
<span> '"' <span> '"'
else else
<span> '"{sq.join " "}"' <span> "\"{sq.join " "}\""
<.tip @click=handle_shift_backspace> <.tip @click=handle_shift_backspace>
<.tip-hotkey> "Shift + Backspace" <.tip-hotkey> "Shift + Backspace"

7
app/components/app-settings.imba

@ -32,6 +32,10 @@ tag app-settings
settings_active = no settings_active = no
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
@ -49,6 +53,7 @@ tag app-settings
errors.push "{link_text}\n{e}" errors.push "{link_text}\n{e}"
if errors.length > 0 if errors.length > 0
err "importing some links", errors.join("\n\n") err "importing some links", errors.join("\n\n")
loading = yes loading = yes
await handle_import! await handle_import!
settings_active = no settings_active = no
@ -58,7 +63,7 @@ tag app-settings
loading = yes loading = yes
await reload_db! await reload_db!
let links = state.links.map do |link| let links = state.links.map do |link|
construct_link_text link api.construct_link_text link
let datetime = new Date!.toString!.split(" ") let datetime = new Date!.toString!.split(" ")
let date = datetime.slice(1, 4).join("-").toLowerCase! let date = datetime.slice(1, 4).join("-").toLowerCase!
let time = datetime[4].split(":").join("-") let time = datetime[4].split(":").join("-")

2
app/db.imba

@ -2,6 +2,7 @@ let p = console.log
import Dexie from 'dexie' import Dexie from 'dexie'
import 'dexie-export-import' import 'dexie-export-import'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import api from './api'
let db = new Dexie 'fuzzyhome' let db = new Dexie 'fuzzyhome'
@ -21,7 +22,6 @@ db.version(2).stores({
let img = link.img let img = link.img
this.value = { id, name, url, frequency, img } this.value = { id, name, url, frequency, img }
import { parse_url } from './utils'
db.version(3).stores({ db.version(3).stores({
links: "++id,name,url,frequency,img" links: "++id,name,url,frequency,img"
}).upgrade! do |trans| }).upgrade! do |trans|

23
app/utils.imba

@ -1,14 +1,11 @@
export def parse_url url export def err s, e
throw "invalid url" if url === null p "error:"
let get_url = do |s| p e
let url = new URL s window.alert("Error {s}:\n\n{e}")
throw _ unless (url.host and url.href)
url
try
return get_url url
try
return get_url "https://{url}"
throw "invalid url"
def get_pretty_date global._fuzzyhome_delete_everything = do |prompt=yes|
Date!.toString!.split(" ").slice(0, 4).join(" ") return if prompt and window.confirm "This will delete everything. Are you sure?"
indexedDB.deleteDatabase("fuzzyhome")
delete localStorage.fuzzyhome_config
delete localStorage.fuzzyhome_visited
location.reload!

Loading…
Cancel
Save