-
-
Couldn't load subscription status.
- Fork 2.5k
Scroll to selected chapter #18497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Scroll to selected chapter #18497
Changes from 3 commits
c050eba
d64afa8
31cad91
c5dc912
f08d656
bb93703
65253ed
9645e81
ef37f19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,7 +166,7 @@ export const gameLinksListener = (select: ChapterSelect) => (vnode: VNode) => | |
| export function view(ctrl: StudyCtrl): VNode { | ||
| const canContribute = ctrl.members.canContribute(), | ||
| current = ctrl.currentChapter(); | ||
| function update(vnode: VNode) { | ||
| function update(vnode: VNode, pageJustLoaded: boolean) { | ||
| const newCount = ctrl.chapters.list.size(), | ||
| vData = vnode.data!.li!, | ||
| el = vnode.elm as HTMLElement; | ||
|
|
@@ -177,6 +177,7 @@ export function view(ctrl: StudyCtrl): VNode { | |
| scrollToInnerSelector(el, '.active'); | ||
| } | ||
| vData.count = newCount; | ||
| vData.pageJustLoaded = pageJustLoaded; | ||
| if (canContribute && newCount > 1 && !vData.sortable) { | ||
| site.asset.loadEsm<typeof Sortable>('sortable.esm', { npm: true }).then(s => { | ||
| vData.sortable = s.create(el, { | ||
|
|
@@ -204,14 +205,33 @@ export function view(ctrl: StudyCtrl): VNode { | |
| } else ctrl.setChapter(id); | ||
| }); | ||
| vnode.data!.li = {}; | ||
| update(vnode); | ||
| update(vnode, true); | ||
| }, | ||
| postpatch(old, vnode) { | ||
| const scrollTop = (old.elm as HTMLElement).scrollTop; | ||
| const activeChapter = (v: VNode) => { | ||
| const kids = (v.children as any[]) ?? []; | ||
| const activeVnode = kids.find(c => c?.data?.class?.active); | ||
| return activeVnode?.elm as HTMLElement | undefined; | ||
| }; | ||
|
|
||
| const el = vnode.elm as HTMLElement; | ||
| const pageJustLoaded = old.data?.li?.pageJustLoaded; | ||
| const prevScrollTop = (old.elm as HTMLElement).scrollTop; | ||
| const prevActiveChapterId = activeChapter(old)?.dataset.id; | ||
|
|
||
| vnode.data!.li = old.data!.li; | ||
| update(vnode); | ||
| if (old.children?.length === vnode.children?.length) | ||
| (vnode.elm as HTMLElement).scrollTop = scrollTop; | ||
| update(vnode, false); | ||
| if (old.children?.length !== vnode.children?.length) return; | ||
| const target = activeChapter(vnode); | ||
| el.scrollTop = prevScrollTop; | ||
|
|
||
| if (!target?.dataset.id || (target.dataset.id === prevActiveChapterId && !pageJustLoaded)) return; | ||
| const t = target.getBoundingClientRect(); | ||
| const c = el.getBoundingClientRect(); | ||
| const dyTop = t.top - c.top; | ||
| const dyBottom = t.bottom - c.bottom; | ||
| if (dyTop < 0) el.scrollTop += dyTop; | ||
| else if (dyBottom > 0) el.scrollTop += dyBottom; | ||
|
||
| }, | ||
| destroy: vnode => { | ||
| const sortable: Sortable = vnode.data!.li!.sortable; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the source of truth is
ctrl.currentChapter(), not the DOM. The DOM is for presentation only, not storage/retrieval of information.