Skip to content

Commit 1f000ca

Browse files
authored
Merge pull request #74 from michaelpelletier/master
Move OnBefore to occur before step change
2 parents 1147e5a + cdeabf3 commit 1f000ca

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

src/components/VTour.vue

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ export interface IVTourProps {
2626
export interface IVTourData {
2727
currentStep: number;
2828
lastStep: number;
29+
nextStep: number;
2930
getCurrentStep: ComputedRef<ITourStep>;
3031
getLastStep: ComputedRef<ITourStep>;
32+
getNextStep: ComputedRef<ITourStep>;
3133
}
3234
3335
const _VTour: Ref<NanoPop | undefined> = ref(undefined);
3436
const _Tooltip: Ref<HTMLElement | undefined> = ref(undefined);
3537
const _CurrentStep: Reactive<IVTourData> = reactive({
3638
currentStep: 0,
3739
lastStep: 0,
40+
nextStep: 1,
3841
getCurrentStep: computed(() => props.steps[_CurrentStep.currentStep]),
3942
getLastStep: computed(() => props.steps[_CurrentStep.lastStep]),
43+
getNextStep: computed(() => props.steps[_CurrentStep.nextStep]),
4044
});
4145
const props = defineProps<IVTourProps>();
4246
const emit = defineEmits<{
@@ -65,7 +69,8 @@ function startTour(): void{
6569
if(props.saveToLocalStorage === 'step') _CurrentStep.currentStep = parseInt(localStorage.getItem('vjt-' + (props.name || 'default')) || '0');
6670
else _CurrentStep.currentStep = 0;
6771
68-
setTimeout(() => {
72+
setTimeout(async () => {
73+
await beforeStep(_CurrentStep.currentStep);
6974
if(!_VTour.value){
7075
_VTour.value = createPopper(document.querySelector(`${_CurrentStep.getCurrentStep.target}`) as HTMLElement, _Tooltip.value!, {
7176
position: _CurrentStep.getCurrentStep.placement || 'right',
@@ -87,21 +92,25 @@ function resetTour(restart: boolean): void{
8792
stopTour();
8893
_CurrentStep.currentStep = 0;
8994
_CurrentStep.lastStep = 0;
95+
_CurrentStep.nextStep = 1;
9096
localStorage.removeItem('vjt-' + (props.name || 'default'));
9197
if(restart) startTour();
9298
}
9399
94-
function nextStep(): void{
100+
async function nextStep() {
101+
await beforeStep(_CurrentStep.nextStep);
95102
_CurrentStep.lastStep = _CurrentStep.currentStep;
96103
_CurrentStep.currentStep++;
97104
if(_CurrentStep.currentStep > props.steps.length -1){
98105
endTour();
99106
return;
100107
}
108+
_CurrentStep.nextStep = _CurrentStep.currentStep++;
101109
updatePosition();
102110
}
103111
104-
function lastStep(): void{
112+
async function lastStep() {
113+
await beforeStep(_CurrentStep.lastStep);
105114
_CurrentStep.currentStep = _CurrentStep.lastStep;
106115
_CurrentStep.lastStep--;
107116
if(_CurrentStep.lastStep === -1){
@@ -111,6 +120,7 @@ function lastStep(): void{
111120
endTour();
112121
return;
113122
}
123+
_CurrentStep.nextStep = _CurrentStep.currentStep++;
114124
updatePosition();
115125
}
116126
@@ -121,14 +131,19 @@ function endTour(): void{
121131
}
122132
123133
function goToStep(step: number): void{
134+
beforeStep(step);
124135
_CurrentStep.currentStep = step;
125136
_CurrentStep.lastStep = step - 1;
126137
if(_CurrentStep.lastStep === -1) _CurrentStep.lastStep = 0;
138+
_CurrentStep.nextStep = step + 1;
127139
updatePosition();
128140
}
129141
142+
async function beforeStep(step: number): Promise<void> {
143+
await props.steps[step].onBefore?.();
144+
}
145+
130146
async function updatePosition(): Promise<void>{
131-
await _CurrentStep.getCurrentStep.onBefore?.();
132147
await new Promise<void>((resolve) => {
133148
updateHighlight();
134149
updateBackdrop();

0 commit comments

Comments
 (0)