Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions example/html-tooltip/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,22 @@ <h4>Section Six</h4>
</div>
<script type="text/javascript" src="../../dist/intro.js"></script>
<script type="text/javascript">
function onBeforeChange () {
const currentStep = this.getCurrentStep();

// اگر رسیدیم به استپ سوم (index = 2)
if (currentStep >= 2) {
return Promise.resolve(false); // نرو به استپ بعد
}

return new Promise((resolve) => {
setTimeout(() => resolve(true), 5000);
});
}
function startIntro(){
var intro = introJs.tour();
intro.onBeforeChange(onBeforeChange)

intro.setOptions({
hideNext: true,
language: 'de_DE',
Expand Down
61 changes: 10 additions & 51 deletions src/packages/tour/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,17 @@ export type TourStep = {
* @api private
*/
export async function nextStep(tour: Tour) {
tour.incrementCurrentStep();

const currentStep = tour.getCurrentStep();

if (currentStep === undefined) {
return false;
}

const nextStep = tour.getStep(currentStep);
let continueStep: boolean | undefined = true;

continueStep = await tour
.callback("beforeChange")
?.call(
tour,
nextStep && (nextStep.element as HTMLElement),
tour.getCurrentStep(),
tour.getDirection()
);

// if `onBeforeChange` returned `false`, stop displaying the element
if (continueStep === false) {
tour.decrementCurrentStep();
return false;
}
let currentStep = tour.getCurrentStep();
const nextStepIndex = currentStep !== undefined ? currentStep + 1 : 0;

if (tour.isEnd()) {
// check if any callback is defined
await tour.callback("complete")?.call(tour, tour.getCurrentStep(), "end");
await tour.exit();

return false;
}

await tour.setCurrentStep(nextStepIndex);
const nextStep = tour.getStep(nextStepIndex);
await showElement(tour, nextStep);

return true;
Expand All @@ -80,39 +57,21 @@ export async function nextStep(tour: Tour) {
* @api private
*/
export async function previousStep(tour: Tour) {
let currentStep = tour.getCurrentStep();
const currentStep = tour.getCurrentStep();

if (currentStep === undefined || currentStep <= 0) {
return false;
}

tour.decrementCurrentStep();
// update the current step after decrementing
currentStep = tour.getCurrentStep();
const prevStepIndex = currentStep - 1;
const prevStep = tour.getStep(prevStepIndex);

if (currentStep === undefined) {
if (!prevStep) {
return false;
}

const nextStep = tour.getStep(currentStep);
let continueStep: boolean | undefined = true;

continueStep = await tour
.callback("beforeChange")
?.call(
tour,
nextStep && (nextStep.element as HTMLElement),
tour.getCurrentStep(),
tour.getDirection()
);

// if `onBeforeChange` returned `false`, stop displaying the element
if (continueStep === false) {
tour.incrementCurrentStep();
return false;
}

await showElement(tour, nextStep);
await tour.setCurrentStep(prevStepIndex);
await showElement(tour, prevStep);

return true;
}
Expand Down
28 changes: 21 additions & 7 deletions src/packages/tour/tour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,32 @@ export class Tour implements Package<TourOptions> {
* Set the current step of the tour and the direction of the tour
* @param step
*/
setCurrentStep(step: number): this {
async setCurrentStep(step: number): Promise<boolean> {
const currentStep = this._currentStepSignal.val;
const targetStep = this.getStep(step);
if (!targetStep) return false;

const direction =
currentStep === undefined || step >= currentStep ? "forward" : "backward";

const continueStep = await this.callback("beforeChange")?.call(
this,
targetStep.element as HTMLElement,
currentStep,
direction
);

if (
this._currentStepSignal.val === undefined ||
step >= this._currentStepSignal.val
direction === "forward" &&
continueStep === false &&
currentStep !== undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be continueStep === false || currentStep !== undefined?

Consider this case, what if there is a step (currentStep !== undefined) but continueStep is false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No — it should remain continueStep === false && currentStep !== undefined because we only want to block the step change when the callback explicitly returns false, and there is an existing step to stay on.

) {
this._direction = "forward";
} else {
this._direction = "backward";
return false;
}

this._direction = direction;
this._currentStepSignal.val = step;
return this;
return true;
}

/**
Expand Down
Loading