mirror of
https://github.com/vale981/tridactyl
synced 2025-03-05 17:41:40 -05:00
Fix #167: Add bmarks
command for searching through bookmarks
This commit is contained in:
parent
8695552fe6
commit
9fc0e9c0cf
3 changed files with 112 additions and 0 deletions
|
@ -40,6 +40,7 @@ function enableCompletions() {
|
|||
activeCompletions = [
|
||||
new Completions.BufferCompletionSource(completionsDiv),
|
||||
new Completions.HistoryCompletionSource(completionsDiv),
|
||||
new Completions.BmarkCompletionSource(completionsDiv),
|
||||
]
|
||||
|
||||
const fragment = document.createDocumentFragment()
|
||||
|
|
|
@ -338,11 +338,109 @@ class HistoryCompletionOption extends CompletionOptionHTML implements Completion
|
|||
}
|
||||
}
|
||||
|
||||
class BmarkCompletionOption extends CompletionOptionHTML implements CompletionOptionFuse {
|
||||
public fuseKeys = []
|
||||
|
||||
constructor(public value: string, bmark: browser.bookmarks.BookmarkTreeNode) {
|
||||
super()
|
||||
if (! bmark.title) {
|
||||
bmark.title = new URL(bmark.url).host
|
||||
}
|
||||
|
||||
// Push properties we want to fuzmatch on
|
||||
this.fuseKeys.push(bmark.title, bmark.url)
|
||||
|
||||
// Create HTMLElement
|
||||
// need to download favicon
|
||||
const favIconUrl = DEFAULT_FAVICON
|
||||
// const favIconUrl = tab.favIconUrl ? tab.favIconUrl : DEFAULT_FAVICON
|
||||
this.html = html`<tr class="HistoryCompletionOption option">
|
||||
<td class="prefix">${"".padEnd(2)}</td>
|
||||
<td></td>
|
||||
<td>${bmark.title}</td>
|
||||
<td><a class="url" target="_blank" href=${bmark.url}>${bmark.url}</a></td>
|
||||
</tr>`
|
||||
}
|
||||
}
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
|
||||
export class BmarkCompletionSource extends CompletionSourceFuse {
|
||||
public options: BmarkCompletionOption[]
|
||||
|
||||
constructor(private _parent) {
|
||||
super(
|
||||
[
|
||||
"bmarks ",
|
||||
],
|
||||
"BmarkCompletionSource", "Bookmarks"
|
||||
)
|
||||
|
||||
this._parent.appendChild(this.node)
|
||||
}
|
||||
|
||||
public async filter(exstr: string) {
|
||||
this.lastExstr = exstr
|
||||
const [prefix, query] = this.splitOnPrefix(exstr)
|
||||
|
||||
// Hide self and stop if prefixes don't match
|
||||
if (prefix) {
|
||||
// Show self if prefix and currently hidden
|
||||
if (this.state === 'hidden') {
|
||||
this.state = 'normal'
|
||||
}
|
||||
} else {
|
||||
this.state = 'hidden'
|
||||
return
|
||||
}
|
||||
|
||||
this.options = (await this.scoreOptions(query, 10)).map(
|
||||
page => new BmarkCompletionOption(page.url, page)
|
||||
)
|
||||
|
||||
this.updateChain()
|
||||
}
|
||||
|
||||
updateChain() {
|
||||
// Options are pre-trimmed to the right length.
|
||||
this.options.forEach(option => option.state = 'normal')
|
||||
|
||||
// Call concrete class
|
||||
this.updateDisplay()
|
||||
}
|
||||
|
||||
onInput() {}
|
||||
|
||||
private async scoreOptions(query: string, n: number) {
|
||||
// Search bookmarks, dedupe and sort by frecency
|
||||
let bookmarks = await browserBg.bookmarks.search({query})
|
||||
bookmarks = bookmarks.filter(b=>{
|
||||
try {
|
||||
return new URL(b.url)
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
|
||||
bookmarks.sort((a, b) => b.dateAdded - a.dateAdded)
|
||||
|
||||
return bookmarks.slice(0, n)
|
||||
}
|
||||
|
||||
select(option: CompletionOption) {
|
||||
if (this.lastExstr !== undefined && option !== undefined) {
|
||||
this.completion = "open " + option.value
|
||||
option.state = 'focused'
|
||||
this.lastFocused = option
|
||||
} else {
|
||||
throw new Error("lastExstr and option must be defined!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class HistoryCompletionSource extends CompletionSourceFuse {
|
||||
public options: HistoryCompletionOption[]
|
||||
|
||||
|
|
|
@ -42,6 +42,19 @@ input {
|
|||
border-spacing: 0;
|
||||
table-layout: fixed;
|
||||
}
|
||||
|
||||
/* redundancy 2: redundancy 2: more redundancy */
|
||||
#completions .BmarkCompletionSource {
|
||||
max-height: calc(20 * var(--option-height));
|
||||
min-height: calc(10 * var(--option-height));
|
||||
}
|
||||
|
||||
#completions .BmarkCompletionSource table {
|
||||
width: 100%;
|
||||
font-size: 9pt;
|
||||
border-spacing: 0;
|
||||
table-layout: fixed;
|
||||
}
|
||||
/* redundancy ends */
|
||||
|
||||
#completions .BufferCompletionSource {
|
||||
|
|
Loading…
Add table
Reference in a new issue