wip: remade history tree

This commit is contained in:
ivcz 2021-10-26 14:46:23 +03:00
parent 1c6a471dc3
commit 12430ac16e
2 changed files with 44 additions and 62 deletions

View file

@ -44,39 +44,51 @@ export class TabHistoryCompletionSource extends Completions.CompletionSourceFuse
return this.updateOptions(exstr) return this.updateOptions(exstr)
} }
private traverseChildren(tree, node) { private makeTree(nodes, parentId, level = 0) {
return nodes
.filter(node => node["parent"] === parentId)
.reduce(
(tree, node) => [
...tree,
{
...node,
children: this.makeTree(nodes, node["id"], level + 1),
level,
},
],
[],
)
}
private flattenTree(node, flat = []) {
flat.push({
title: node["title"],
href: node["href"],
parent: node["parent"],
id: node["id"],
level: node["level"] === 0 ? node["level"] : node["level"] - 1,
})
for (const child of node["children"]) { for (const child of node["children"]) {
const newNode = tree[child] this.flattenTree(child, flat)
this.traverseChildren(tree, newNode)
this.addIndex(tree, newNode)
} }
return flat
} }
private addIndex(tree, node) { private addIndicies(tree) {
const parentCount = this.countParents(tree, node) for (const node of tree) {
node["index"] = parentCount const parentCount = node["level"]
let string = "" let string = parentCount + " "
for (let i = 0; i <= parentCount; ++i) { for (let i = 0; i <= parentCount; ++i) {
if (i === parentCount) { if (i === parentCount) {
string += "├─" string += "├─"
} else if (i === 0) { } else if (i === 0) {
string += "| " string += "| "
} else { } else {
string += "| " string += "| "
}
} }
node["prefix"] = string
} }
node["prefix"] = string
}
private countParents(tree, node): number {
let prev = null
if (tree[node["parent"]]) prev = tree[node["parent"]]
let counter = 0
while (prev) {
prev = tree[prev["parent"]]
counter += 1
}
return counter - 1
} }
private async updateOptions(exstr = "") { private async updateOptions(exstr = "") {
@ -88,26 +100,9 @@ export class TabHistoryCompletionSource extends Completions.CompletionSourceFuse
}) })
let history = await browserBg.sessions.getTabValue(tab[0].id, "history") let history = await browserBg.sessions.getTabValue(tab[0].id, "history")
if (!history) history = { list: [] } if (!history) history = { list: [] }
const tree = this.makeTree(history["list"], null)
let jump = history["list"][history["current"]] history["list"] = this.flattenTree(tree[0])
let counter = 0 this.addIndicies(history["list"])
if (jump) {
history["list"][history["current"]]["index"] = "%"
history["list"][history["current"]]["prefix"] = "|"
}
while (jump && history["list"][jump["parent"]]) {
counter -= 1
history["list"][jump["parent"]]["index"] = counter
history["list"][jump["parent"]]["prefix"] = "|"
jump = history["list"][jump["parent"]]
}
jump = history["list"][history["current"]]
this.traverseChildren(history["list"], jump)
history["list"] = history["list"].filter(el =>
Object.prototype.hasOwnProperty.call(el, "index"),
)
this.options = this.scoreOptions( this.options = this.scoreOptions(
history["list"].map( history["list"].map(
@ -117,7 +112,7 @@ export class TabHistoryCompletionSource extends Completions.CompletionSourceFuse
id: item.index, id: item.index,
title: item.title, title: item.title,
prefix: item.prefix, prefix: item.prefix,
index: item.index, index: item.level,
}), }),
), ),
) )
@ -125,17 +120,6 @@ export class TabHistoryCompletionSource extends Completions.CompletionSourceFuse
} }
private scoreOptions(options: TabHistoryCompletionOption[]) { private scoreOptions(options: TabHistoryCompletionOption[]) {
options.sort((el1, el2) => {
if (el1["index"] && el2["index"] && el1["index"] < el2["index"])
return -1
if (el1["index"] && el2["index"] && el1["index"] > el2["index"])
return 1
if (el1["parent"] && el2["parent"] && el1["parent"] < el2["parent"])
return -1
if (el1["parent"] && el2["parent"] && el1["parent"] > el2["parent"])
return 1
return 0
})
return options return options
} }
} }

View file

@ -1116,17 +1116,15 @@ export async function addTabHistory() {
const link = getJumpPageId() const link = getJumpPageId()
const current = pages["list"].findIndex(item => item.href === link) const current = pages["list"].findIndex(item => item.href === link)
if (pages["list"][current]) { if (pages["list"][current]) {
if (pages["list"][pages["list"][current]["parent"]]) pages["list"][pages["list"][current]["parent"]]["children"].push(current)
pages["current"] = current pages["current"] = current
} else { } else {
pages["list"].push({ pages["list"].push({
children: [],
parent: pages["current"], parent: pages["current"],
href: link, href: link,
title: document.title, title: document.title,
id: pages["list"].length,
}) })
pages["current"] = pages["list"].length - 1 pages["current"] = pages["list"].length - 1
if (pages["list"][pages["list"][pages["current"]]["parent"]]) pages["list"][pages["list"][pages["current"]]["parent"]]["children"].push(pages["current"])
} }
saveTabHistory(pages) saveTabHistory(pages)
} }