@@ -7,21 +7,17 @@ import {
77
88import Flicking from "../Flicking" ;
99
10- const getIsReachStart = ( flicking : Flicking ) => flicking . index === 0 ;
11- const getIsReachEnd = ( flicking : Flicking ) =>
12- flicking . index === flicking . panelCount - 1 ;
10+ const getIsReachStart = ( flicking : Flicking ) => ! flicking . circular && flicking . index === 0 ;
11+
12+ const getIsReachEnd = ( flicking : Flicking ) => ! flicking . circular && flicking . index === flicking . panelCount - 1 ;
13+
1314
1415const getTotalPanelCount = ( flicking : Flicking ) => flicking . panelCount ;
1516const getCurrentPanelIndex = ( flicking : Flicking ) => flicking . index ;
1617
17- // Returns the progress percentage based on the current panel index.
18- const getProgressByPanelIndex = ( flicking : Flicking ) =>
19- flicking . panelCount > 1
20- ? ( flicking . index / ( flicking . panelCount - 1 ) ) * 100
21- : 100 ;
2218
2319// Returns the progress percentage based on the current scroll position.
24- const getProgressByScrollPos = ( flicking : Flicking ) => {
20+ const getProgress = ( flicking : Flicking ) => {
2521 const cam = flicking . camera ;
2622
2723 const progressRatio = ( cam . position - cam . range . min ) / ( cam . range . max - cam . range . min ) ;
@@ -31,37 +27,33 @@ const getProgressByScrollPos = (flicking: Flicking) => {
3127 return percent ;
3228} ;
3329
34- export type FlickingStateApi = ReactiveObject < {
35- isReachStart : boolean ;
36- isReachEnd : boolean ;
37- totalPanelCount : number ;
38- currentPanelIndex : number ;
39- indexProgress : number ;
40- scrollProgress : number ;
41- moveTo : ( i : number ) => Promise < void > | undefined ;
42- } > ;
30+ export type FlickingReactiveStateApi = ReactiveObject < FlickingReactiveState & FlickingReactiveMethod > ;
4331
4432export interface FlickingReactiveState {
4533 isReachStart : boolean ;
4634 isReachEnd : boolean ;
4735 totalPanelCount : number ;
4836 currentPanelIndex : number ;
49- indexProgress : number ;
50- scrollProgress : number ;
37+ progress : number ;
38+ }
39+
40+ export interface FlickingReactiveMethod {
41+ moveTo : ( i : number ) => Promise < void > ;
5142}
5243
53- const flickingStateApiAdapter : ReactiveSetupAdapter < FlickingStateApi , FlickingReactiveState , "moveTo" > = ( { onInit, onDestroy } ) => {
44+ const flickingStateApiAdapter : ReactiveSetupAdapter < FlickingReactiveStateApi , FlickingReactiveState , "moveTo" > = ( { onInit, onDestroy } ) => {
5445 let flicking : Flicking | null = null ;
5546
56- const moveTo = ( i : number ) => flicking ?. moveTo ( i ) ;
47+ const moveTo = ( i : number ) => {
48+ return flicking ? flicking . moveTo ( i ) : Promise . reject ( new Error ( "Flicking instance is not available" ) ) ;
49+ } ;
5750
5851 const reactiveObj = reactive ( {
5952 isReachStart : false ,
6053 isReachEnd : false ,
6154 totalPanelCount : 0 ,
6255 currentPanelIndex : 0 ,
63- indexProgress : 0 ,
64- scrollProgress : 0 ,
56+ progress : 0 ,
6557 moveTo
6658 } ) ;
6759
@@ -71,7 +63,6 @@ const flickingStateApiAdapter: ReactiveSetupAdapter<FlickingStateApi, FlickingRe
7163 reactiveObj . isReachStart = getIsReachStart ( flicking ) ;
7264 reactiveObj . isReachEnd = getIsReachEnd ( flicking ) ;
7365 reactiveObj . currentPanelIndex = getCurrentPanelIndex ( flicking ) ;
74- reactiveObj . indexProgress = getProgressByPanelIndex ( flicking ) ;
7566 } ;
7667
7768 const onPanelChange = ( ) => {
@@ -84,7 +75,7 @@ const flickingStateApiAdapter: ReactiveSetupAdapter<FlickingStateApi, FlickingRe
8475 const onMove = ( ) => {
8576 if ( flicking === null ) return ;
8677
87- reactiveObj . scrollProgress = getProgressByScrollPos ( flicking ) ;
78+ reactiveObj . progress = getProgress ( flicking ) ;
8879 } ;
8980
9081 onInit ( ( inst , data ) => {
@@ -94,8 +85,7 @@ const flickingStateApiAdapter: ReactiveSetupAdapter<FlickingStateApi, FlickingRe
9485 reactiveObj . isReachStart = getIsReachStart ( flicking ) ;
9586 reactiveObj . isReachEnd = getIsReachEnd ( flicking ) ;
9687 reactiveObj . currentPanelIndex = getCurrentPanelIndex ( flicking ) ;
97- reactiveObj . indexProgress = getProgressByPanelIndex ( flicking ) ;
98- reactiveObj . scrollProgress = getProgressByScrollPos ( flicking ) ;
88+ reactiveObj . progress = getProgress ( flicking ) ;
9989
10090 reactiveObj . totalPanelCount = getTotalPanelCount ( flicking ) ;
10191
0 commit comments