1- import { computed , App , InjectionKey , inject , ref , ComputedRef , Ref } from 'vue'
1+ import { computed , App , InjectionKey , inject , ref , ComputedRef , Ref , watch } from 'vue'
22import { and , Fn , not , whenever } from '@vueuse/core'
33import { Router , RouteRecordRaw } from 'vue-router'
44import { clickCurrent , clickElements } from '../logic'
55import { isInputing , magicKeys } from '../state'
66import { rawRoutes } from '../routes'
7+ // @ts -expect-error
8+ import serverState from '/@server-ref/state'
79
810declare module 'vue-router' {
911 interface RouteMeta {
@@ -31,6 +33,7 @@ export interface NavigateControls {
3133 paused : Ref < boolean >
3234 hasNext : ComputedRef < boolean >
3335 hasPrev : ComputedRef < boolean >
36+ go ( page : number ) : void
3437 install ( app : App ) : void
3538}
3639
@@ -51,14 +54,34 @@ export function createNavigateControls(router: Router) {
5154 const hasPrev = computed ( ( ) => currentPage . value > 0 )
5255 const nextRoute = computed ( ( ) => routes . find ( i => i . path === `${ Math . min ( routes . length - 1 , currentPage . value + 1 ) } ` ) )
5356
57+ router . isReady ( ) . then ( ( ) => {
58+ watch ( serverState ,
59+ ( ) => {
60+ if ( + serverState . value . page !== + currentPage . value )
61+ router . replace ( getPath ( serverState . value . page ) )
62+ clickCurrent . value = serverState . value . tab || 0
63+ } ,
64+ { deep : true } ,
65+ )
66+ } )
67+
68+ function updateState ( ) {
69+ if ( isPresenter . value ) {
70+ serverState . value . page = + currentPage . value
71+ serverState . value . tab = clickCurrent . value
72+ }
73+ }
74+
75+ watch ( clickCurrent , updateState )
76+
5477 function next ( ) {
5578 if ( clickElements . value . length <= clickCurrent . value )
5679 nextSlide ( )
5780 else
5881 clickCurrent . value += 1
5982 }
6083
61- function prev ( ) {
84+ async function prev ( ) {
6285 if ( clickCurrent . value <= 0 )
6386 prevSlide ( )
6487 else
@@ -69,18 +92,21 @@ export function createNavigateControls(router: Router) {
6992 return isPresenter . value ? `/presenter/${ no } ` : `/${ no } `
7093 }
7194
72- function nextSlide ( ) {
73- clickCurrent . value = 0
74- clickElements . value = [ ]
95+ async function nextSlide ( ) {
7596 const next = Math . min ( routes . length - 1 , currentPage . value + 1 )
76- router . push ( getPath ( next ) )
97+ go ( next )
7798 }
7899
79- function prevSlide ( ) {
100+ async function prevSlide ( ) {
101+ const next = Math . max ( 0 , currentPage . value - 1 )
102+ go ( next )
103+ }
104+
105+ async function go ( page : number ) {
80106 clickCurrent . value = 0
81107 clickElements . value = [ ]
82- const next = Math . max ( 0 , currentPage . value - 1 )
83- router . push ( getPath ( next ) )
108+ await router . push ( getPath ( page ) )
109+ updateState ( )
84110 }
85111
86112 const shortcutEnabled = and ( not ( paused ) , not ( isInputing ) )
@@ -105,6 +131,7 @@ export function createNavigateControls(router: Router) {
105131 routes,
106132 isPresenter,
107133 currentPage,
134+ go,
108135 install ( app : App ) {
109136 app . provide ( NavigateControlsInjection , navigateControls )
110137 } ,
0 commit comments