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", "name": "logseq-plugin-favorite-tree",
"version": "1.1.0", "version": "1.1.1",
"main": "dist/index.html", "main": "dist/index.html",
"logseq": { "logseq": {
"id": "_sethyuan-logseq-favorite-tree", "id": "_sethyuan-logseq-favorite-tree",

View File

@ -1,10 +1,11 @@
const storage = logseq.Assets.makeSandboxStorage() const storage = logseq.Assets.makeSandboxStorage()
export async function readExpansionState(key: string, readKeys?: Set<string>) { export async function readExpansionState(key: string, readKeys?: Set<string>) {
readKeys?.add(key) const graphKey = await getGraphKey(key)
key = `expansion-${key}.json` readKeys?.add(graphKey)
const hasItem = await storage.hasItem(key) const storeKey = `expansion-${graphKey}.json`
const str = hasItem ? (await storage.getItem(key))! : "{}" const hasItem = await storage.hasItem(storeKey)
const str = hasItem ? (await storage.getItem(storeKey))! : "{}"
return JSON.parse(str) as Record<string, boolean> return JSON.parse(str) as Record<string, boolean>
} }
@ -12,26 +13,29 @@ export async function readRootExpansionState(
key: string, key: string,
readKeys?: Set<string>, readKeys?: Set<string>,
) { ) {
readKeys?.add(`_${key}`) const graphKey = await getGraphKey(key)
key = `expansion-_${key}.json` readKeys?.add(`_${graphKey}`)
if (await storage.hasItem(key)) { const storeKey = `expansion-_${graphKey}.json`
return JSON.parse((await storage.getItem(key))!) as boolean if (await storage.hasItem(storeKey)) {
return JSON.parse((await storage.getItem(storeKey))!) as boolean
} else { } else {
return false return false
} }
} }
export async function writeRootExpansionState(key: string, value: boolean) { export async function writeRootExpansionState(key: string, value: boolean) {
key = `expansion-_${key}.json` const graphKey = await getGraphKey(key)
await storage.setItem(key, `${value}`) const storeKey = `expansion-_${graphKey}.json`
await storage.setItem(storeKey, `${value}`)
} }
export async function writeExpansionState( export async function writeExpansionState(
key: string, key: string,
value: Record<string, boolean>, value: Record<string, boolean>,
) { ) {
key = `expansion-${key}.json` const graphKey = await getGraphKey(key)
await storage.setItem(key, JSON.stringify(value)) const storeKey = `expansion-${graphKey}.json`
await storage.setItem(storeKey, JSON.stringify(value))
} }
export async function allExpansionKeys() { export async function allExpansionKeys() {
@ -47,3 +51,8 @@ export async function removeExpansionState(key: string) {
key = `expansion-${key}.json` key = `expansion-${key}.json`
await storage.removeItem(key) 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 if (mutation?.target == null) return
const target = mutation.target as HTMLElement const target = mutation.target as HTMLElement
if ( if (target.classList?.contains("nav-content-item-inner")) {
target.classList?.contains("bd") ||
target.classList?.contains("favorites")
) {
await processFavorites() await processFavorites()
} }
}) })
const favoritesEl = parent.document.querySelector("#left-sidebar .favorites") const favoritesEl = await waitForEl("#left-sidebar .favorites", 300)
if (favoritesEl != null) { if (favoritesEl != null) {
favoritesObserver.observe(favoritesEl, { childList: true, subtree: true }) favoritesObserver.observe(favoritesEl, { childList: true, subtree: true })
} }