From 7a747312eec7c36b02bd8602e10848de3239b669 Mon Sep 17 00:00:00 2001 From: Oliver Blanthorn Date: Thu, 23 Nov 2017 14:20:44 +0000 Subject: [PATCH] Start work on history completions --- src/commandline_background.ts | 4 ++ src/commandline_frame.ts | 1 + src/completions.ts | 77 +++++++++++++++++++++++++++++++++++ src/static/commandline.css | 14 +++++++ 4 files changed, 96 insertions(+) diff --git a/src/commandline_background.ts b/src/commandline_background.ts index de3500b5..f7d7fc0b 100644 --- a/src/commandline_background.ts +++ b/src/commandline_background.ts @@ -27,6 +27,9 @@ async function currentWindowTabs(): Promise { return await browser.tabs.query({currentWindow:true}) } +async function history(): Promise { + return await browser.history.search({text:"",maxResults:100}) +} async function allWindowTabs(): Promise { let allTabs: browser.tabs.Tab[] = [] for (const window of await browser.windows.getAll()) { @@ -49,6 +52,7 @@ export async function hide() { Messaging.addListener("commandline_background", Messaging.attributeCaller({ currentWindowTabs, + history, recvExStr, show, hide, diff --git a/src/commandline_frame.ts b/src/commandline_frame.ts index 3bb02a21..0e5423cc 100644 --- a/src/commandline_frame.ts +++ b/src/commandline_frame.ts @@ -39,6 +39,7 @@ function enableCompletions() { if (! activeCompletions) { activeCompletions = [ new Completions.BufferCompletionSource(completionsDiv), + new Completions.HistoryCompletionSource(completionsDiv), ] const fragment = document.createDocumentFragment() diff --git a/src/completions.ts b/src/completions.ts index 02fc3ba8..4fd5ac61 100644 --- a/src/completions.ts +++ b/src/completions.ts @@ -258,6 +258,83 @@ abstract class CompletionSourceFuse extends CompletionSource { // {{{ IMPLEMENTATIONS +class HistoryCompletionOption extends CompletionOptionHTML implements CompletionOptionFuse { + public fuseKeys = [] + + constructor(public value: string, page: browser.history.HistoryItem) { + super() + // Two character buffer properties prefix + let pre = "" + // Push prefix before padding so we don't match on whitespace + this.fuseKeys.push(pre) + + // Push properties we want to fuzmatch on + this.fuseKeys.push(page.title, page.url) // weight by page.visitCount + + // Create HTMLElement + // need to download favicon + const favIconUrl = DEFAULT_FAVICON + // const favIconUrl = tab.favIconUrl ? tab.favIconUrl : DEFAULT_FAVICON + this.html = html` + ${pre.padEnd(2)} + + $${page.title} + ${page.url} + ` + } +} + +export class HistoryCompletionSource extends CompletionSourceFuse { + public options: HistoryCompletionOption[] + + // TODO: + // - store the exstr and trigger redraws on user or data input without + // callback faffery + // - sort out the element redrawing. + + constructor(private _parent) { + super( + [ + "open ", + "tabopen ", + "winopen ", + ], + "HistoryCompletionSource", "History" + ) + + this.updateOptions() + this._parent.appendChild(this.node) + } + + private async updateOptions(exstr?: string) { + /* console.log('updateOptions', this.optionContainer) */ + const history: browser.history.HistoryItem[] = + await Messaging.message("commandline_background", "history") + + const options = [] + + // Get alternative tab, defined as last accessed tab. + history.sort((a, b) => { return a.lastVisitTime < b.lastVisitTime ? 1 : -1 }) + + for (const page of history) { + options.push(new HistoryCompletionOption( + page.url, + page, + )) + } + + /* console.log('updateOptions end', this.waiting, this.optionContainer) */ + this.options = options + this.updateChain() + } + + async onInput(exstr) { + // Schedule an update, if you like. Not very useful for buffers, but + // will be for other things. + this.updateOptions() + } +} + class BufferCompletionOption extends CompletionOptionHTML implements CompletionOptionFuse { public fuseKeys = [] diff --git a/src/static/commandline.css b/src/static/commandline.css index 32388d5f..0d874779 100644 --- a/src/static/commandline.css +++ b/src/static/commandline.css @@ -32,6 +32,20 @@ input { border-top: 0.5px solid grey; } +/* Olie doesn't know how CSS inheritance works */ +#completions .HistoryCompletionSource { + max-height: calc(20 * var(--option-height)); + min-height: calc(10 * var(--option-height)); +} + +#completions .HistoryCompletionSource table { + width: 100%; + font-size: 9pt; + border-spacing: 0; + table-layout: fixed; +} +/* redundancy ends */ + #completions .BufferCompletionSource { max-height: calc(20 * var(--option-height)); min-height: calc(10 * var(--option-height));