Skip to content

Commit

Permalink
feat: store page offset in session
Browse files Browse the repository at this point in the history
  • Loading branch information
sethyuan committed Dec 26, 2023
1 parent b099b9f commit f38a8e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logseq-plugin-tocgen",
"version": "2.12.7",
"version": "2.13.0",
"main": "dist/index.html",
"logseq": {
"id": "_sethyuan-logseq-tocgen",
Expand Down
34 changes: 17 additions & 17 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let resizeObserver = null
// A map of all roots to observe to for a given slot.
const embedRoots = {}
let routeOff
let lastPageUUID
let lastPageID
let lastScrollTop = 0

const backtopScrollHandler = debounce((e) => {
Expand Down Expand Up @@ -348,21 +348,16 @@ async function main() {

routeOff = logseq.App.onRouteChanged(
async ({ template, parameters: { query } }) => {
if (lastPageUUID) {
if (lastScrollTop > 0) {
await logseq.Editor.upsertBlockProperty(
lastPageUUID,
"scroll-top",
lastScrollTop,
)
} else {
await logseq.Editor.removeBlockProperty(lastPageUUID, "scroll-top")
}
if (lastPageID) {
parent.sessionStorage.setItem(
`kef-toc-offset-${lastPageID}`,
lastScrollTop,
)
}

lastPageUUID = null
lastPageID = null
if (
template !== "/page/:name" ||
(template !== "/" && template !== "/page/:name") ||
(query.anchor &&
query.anchor.startsWith("block-content-") &&
query.anchor !== "block-content-editor")
Expand All @@ -374,12 +369,17 @@ async function main() {
mainContentContainer.scrollTop = 0
}, 100)
} else {
const currPage = await logseq.Editor.getCurrentPage()
if (currPage == null) return
const id =
template === "/"
? "journals"
: (await logseq.Editor.getCurrentPage())?.uuid
if (!id) return

lastPageUUID = currPage.uuid
lastPageID = id
setTimeout(() => {
gotoOffset(mainContentContainer, currPage.properties?.scrollTop ?? 0)
const offset =
parent.sessionStorage.getItem(`kef-toc-offset-${id}`) ?? 0
gotoOffset(mainContentContainer, offset)
}, 100)
}
},
Expand Down

0 comments on commit f38a8e2

Please sign in to comment.