fix: expansion state cleaning and logseq new version compat

This commit is contained in:
Seth 2023-10-31 14:06:05 +08:00
parent 784c8107e4
commit a09596c24d
3 changed files with 24 additions and 18 deletions

View File

@ -1,6 +1,6 @@
{
"name": "logseq-plugin-favorite-tree",
"version": "1.1.0",
"version": "1.1.1",
"main": "dist/index.html",
"logseq": {
"id": "_sethyuan-logseq-favorite-tree",

View File

@ -1,10 +1,11 @@
const storage = logseq.Assets.makeSandboxStorage()
export async function readExpansionState(key: string, readKeys?: Set<string>) {
readKeys?.add(key)
key = `expansion-${key}.json`
const hasItem = await storage.hasItem(key)
const str = hasItem ? (await storage.getItem(key))! : "{}"
const graphKey = await getGraphKey(key)
readKeys?.add(graphKey)
const storeKey = `expansion-${graphKey}.json`
const hasItem = await storage.hasItem(storeKey)
const str = hasItem ? (await storage.getItem(storeKey))! : "{}"
return JSON.parse(str) as Record<string, boolean>
}
@ -12,26 +13,29 @@ export async function readRootExpansionState(
key: string,
readKeys?: Set<string>,
) {
readKeys?.add(`_${key}`)
key = `expansion-_${key}.json`
if (await storage.hasItem(key)) {
return JSON.parse((await storage.getItem(key))!) as boolean
const graphKey = await getGraphKey(key)
readKeys?.add(`_${graphKey}`)
const storeKey = `expansion-_${graphKey}.json`
if (await storage.hasItem(storeKey)) {
return JSON.parse((await storage.getItem(storeKey))!) as boolean
} else {
return false
}
}
export async function writeRootExpansionState(key: string, value: boolean) {
key = `expansion-_${key}.json`
await storage.setItem(key, `${value}`)
const graphKey = await getGraphKey(key)
const storeKey = `expansion-_${graphKey}.json`
await storage.setItem(storeKey, `${value}`)
}
export async function writeExpansionState(
key: string,
value: Record<string, boolean>,
) {
key = `expansion-${key}.json`
await storage.setItem(key, JSON.stringify(value))
const graphKey = await getGraphKey(key)
const storeKey = `expansion-${graphKey}.json`
await storage.setItem(storeKey, JSON.stringify(value))
}
export async function allExpansionKeys() {
@ -47,3 +51,8 @@ export async function removeExpansionState(key: string) {
key = `expansion-${key}.json`
await storage.removeItem(key)
}
async function getGraphKey(key: string) {
const graph = await logseq.App.getCurrentGraph()
return `${key}-${graph?.name ?? ""}`
}

View File

@ -68,14 +68,11 @@ async function main() {
if (mutation?.target == null) return
const target = mutation.target as HTMLElement
if (
target.classList?.contains("bd") ||
target.classList?.contains("favorites")
) {
if (target.classList?.contains("nav-content-item-inner")) {
await processFavorites()
}
})
const favoritesEl = parent.document.querySelector("#left-sidebar .favorites")
const favoritesEl = await waitForEl("#left-sidebar .favorites", 300)
if (favoritesEl != null) {
favoritesObserver.observe(favoritesEl, { childList: true, subtree: true })
}