1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-07 01:19:57 +00:00

feat: hide server autocomplete when you enter a valid URL (#1009)

Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
closes https://github.com/elk-zone/elk/issues/1005
This commit is contained in:
Tom Sherman 2023-01-12 18:14:34 +00:00 committed by GitHub
parent cb2e39e854
commit 11f1f62523
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,14 +47,6 @@ async function oauth() {
}
}
async function handleInput() {
if (server.startsWith('https://'))
server = server.replace('https://', '')
if (server?.length)
displayError = false
}
let fuse = $shallowRef(new Fuse([] as string[]))
const filteredServers = $computed(() => {
@ -68,6 +60,35 @@ const filteredServers = $computed(() => {
return results
})
function isValidUrl(str: string) {
try {
// eslint-disable-next-line no-new
new URL(str)
return true
}
catch (err) {
return false
}
}
async function handleInput() {
if (server.startsWith('https://'))
server = server.replace('https://', '')
if (server?.length)
displayError = false
if (
isValidUrl(`https://${server.trim()}`)
&& server.trim().match(/^[a-z0-9-]+(\.[a-z0-9-]+)+(:[0-9]+)?$/i)
// Do not hide the autocomplete if a result has an exact substring match on the input
&& !filteredServers.some(s => s.includes(server.trim()))
)
autocompleteShow = false
else
autocompleteShow = true
}
function toSelector(server: string) {
return server.replace(/[^\w-]/g, '-')
}