summaryrefslogtreecommitdiff
path: root/packages/frontend/src/utility/tour.ts
blob: b14486e953571064ad622a894847def731a14cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { computed, ref, shallowRef, watch, defineAsyncComponent } from 'vue';
import * as os from '@/os.js';

type TourStep = {
	title: string;
	description: string;
	element: HTMLElement;
};

export function startTour(steps: TourStep[]) {
	return new Promise<void>((resolve) => {
		const currentStepIndex = ref(0);
		const titleRef = ref(steps[0].title);
		const descriptionRef = ref(steps[0].description);
		const anchorElementRef = shallowRef<HTMLElement>(steps[0].element);

		watch(currentStepIndex, (newIndex) => {
			const step = steps[newIndex];
			titleRef.value = step.title;
			descriptionRef.value = step.description;
			anchorElementRef.value = step.element;
		});

		const { dispose } = os.popup(defineAsyncComponent(() => import('@/components/MkSpot.vue')), {
			title: titleRef,
			description: descriptionRef,
			anchorElement: anchorElementRef,
			hasPrev: computed(() => currentStepIndex.value > 0),
			hasNext: computed(() => currentStepIndex.value < steps.length - 1),
		}, {
			next: () => {
				if (currentStepIndex.value >= steps.length - 1) {
					dispose();
					resolve();
					return;
				}
				currentStepIndex.value++;
			},
			prev: () => {
				currentStepIndex.value--;
			},
		});
	});
}