Skip to content
32 changes: 26 additions & 6 deletions ui/analyse/src/study/studyChapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, {
Expand Down Expand Up @@ -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;
Copy link
Collaborator

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.

};

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this reinventing scrollToInnerSelector that we see in use in this same file?

https://github.com/lichess-org/lila/blob/master/ui/analyse/src/study/studyChapters.ts#L177-L177

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ornicar Yeah I was approaching this the wrong way. Updated so that any adjustments needed for scrolling take place in view where it first happens.

},
destroy: vnode => {
const sortable: Sortable = vnode.data!.li!.sortable;
Expand Down
Loading