Merge pull request #8982 from benediktwerner/fix-study-next-chapter

Fix study next chapter button
forum-mod-deletion-message-dialog
Thibault Duplessis 2021-05-25 10:05:13 +02:00 committed by GitHub
commit b58df256b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 48 deletions

View File

@ -87,15 +87,8 @@ export const bind = (ctrl: AnalyseCtrl) => {
keyToMousedown('g', '.study__buttons .glyphs');
// navigation for next and prev chapters
const study = ctrl.study;
['n', 'p'].forEach(key =>
kbd.bind(key, () => {
const chapters = study.chapters.list();
const i = chapters.findIndex(ch => ch.id === study.vm.chapterId);
const chapter = chapters[i + (key === 'n' ? 1 : -1)];
if (chapter) study.setChapter(chapter.id);
})
);
kbd.bind('p', ctrl.study.goToPrevChapter);
kbd.bind('n', ctrl.study.goToNextChapter);
}
};

View File

@ -85,9 +85,7 @@ export default class GamebookPlayCtrl {
this.retry();
break;
case 'end': {
const s = this.root.study!,
c = s.nextChapter();
if (c) s.setChapter(c.id);
this.root.study!.goToNextChapter();
break;
}
default:

View File

@ -92,15 +92,14 @@ function renderFeedback(ctrl: GamebookPlayCtrl, state: State) {
}
function renderEnd(ctrl: GamebookPlayCtrl) {
const study = ctrl.root.study!,
nextChapter = study.nextChapter();
const study = ctrl.root.study!;
return h('div.feedback.end', [
nextChapter
study.nextChapter()
? h(
'a.next.text',
{
attrs: dataIcon('G'),
hook: bind('click', () => study.setChapter(nextChapter.id)),
hook: bind('click', study.goToNextChapter),
},
'Next chapter'
)

View File

@ -60,7 +60,10 @@ export interface StudyCtrl {
currentNode(): Tree.Node;
practice?: StudyPracticeCtrl;
gamebookPlay(): GamebookPlayCtrl | undefined;
prevChapter(): StudyChapterMeta | undefined;
nextChapter(): StudyChapterMeta | undefined;
goToPrevChapter(): void;
goToNextChapter(): void;
mutateCgConfig(config: Required<Pick<CgConfig, 'drawable'>>): void;
isUpdatedRecently(): boolean;
setGamebookOverride(o: GamebookOverride): void;

View File

@ -44,5 +44,4 @@ export interface StudyPracticeCtrl {
isWhite(): boolean;
analysisUrl: Prop<string>;
autoNext: StoredBooleanProp;
goToNext(): void;
}

View File

@ -60,7 +60,7 @@ export default function (root: AnalyseCtrl, studyData: StudyData, data: StudyPra
function onVictory(): void {
saveNbMoves();
lichess.sound.play('practiceSuccess');
if (studyData.chapter.practice && autoNext()) setTimeout(goToNext, 1000);
if (studyData.chapter.practice && autoNext()) setTimeout(getStudy().goToNextChapter, 1000);
}
function saveNbMoves(): void {
@ -72,11 +72,6 @@ export default function (root: AnalyseCtrl, studyData: StudyData, data: StudyPra
}
}
function goToNext() {
const next = getStudy().nextChapter();
if (next) getStudy().setChapter(next.id);
}
function onFailure(): void {
root.node.fail = true;
lichess.sound.play('practiceFailure');
@ -104,6 +99,5 @@ export default function (root: AnalyseCtrl, studyData: StudyData, data: StudyPra
isWhite: root.bottomIsWhite,
analysisUrl,
autoNext,
goToNext,
};
}

View File

@ -70,7 +70,7 @@ export function underboard(ctrl: StudyCtrl): MaybeVNodes {
'a.feedback.win',
ctrl.nextChapter()
? {
hook: bind('click', p.goToNext),
hook: bind('click', ctrl.goToNextChapter),
}
: {
attrs: { href: '/practice' },

View File

@ -368,6 +368,31 @@ export default function (
const likeToggler = debounce(() => send('like', { liked: data.liked }), 1000);
function setChapter(id: string, force?: boolean) {
const alreadySet = id === vm.chapterId && !force;
if (relay?.tourShow.active) {
relay.tourShow.disable();
if (alreadySet) redraw();
}
if (alreadySet) return;
if (!vm.mode.sticky || !makeChange('setChapter', id)) {
vm.mode.sticky = false;
if (!vm.behind) vm.behind = 1;
vm.chapterId = id;
xhrReload();
}
vm.loading = true;
vm.nextChapterId = id;
vm.justSetChapterId = id;
redraw();
}
const [prevChapter, nextChapter] = [-1, +1].map(delta => (): StudyChapterMeta | undefined => {
const chs = chapters.list();
const i = chs.findIndex(ch => ch.id === vm.chapterId);
return i < 0 ? undefined : chs[i + delta];
});
const socketHandlers: Handlers = {
path(d) {
const position = d.p,
@ -642,24 +667,7 @@ export default function (
})
);
},
setChapter(id, force) {
const alreadySet = id === vm.chapterId && !force;
if (relay?.tourShow.active) {
relay.tourShow.disable();
if (alreadySet) redraw();
}
if (alreadySet) return;
if (!vm.mode.sticky || !makeChange('setChapter', id)) {
vm.mode.sticky = false;
if (!vm.behind) vm.behind = 1;
vm.chapterId = id;
xhrReload();
}
vm.loading = true;
vm.nextChapterId = id;
vm.justSetChapterId = id;
redraw();
},
setChapter,
toggleSticky() {
vm.mode.sticky = !vm.mode.sticky && data.features.sticky;
xhrReload();
@ -675,11 +683,15 @@ export default function (
currentNode,
practice,
gamebookPlay: () => gamebookPlay,
nextChapter(): StudyChapterMeta | undefined {
const chapters = data.chapters,
currentId = currentChapter().id;
for (const i in chapters) if (chapters[i].id === currentId) return chapters[parseInt(i) + 1];
return undefined;
prevChapter,
nextChapter,
goToPrevChapter() {
const chapter = prevChapter();
if (chapter) setChapter(chapter.id);
},
goToNextChapter() {
const chapter = nextChapter();
if (chapter) setChapter(chapter.id);
},
setGamebookOverride(o) {
vm.gamebookOverride = o;