forked from zhongyidian/food-add-edu
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
import { appConfig } from "@/app.config";
|
|
import { useSpotlightStore } from "@/stores";
|
|
import type {
|
|
SpotlightStep,
|
|
StartSpotlightOptions,
|
|
} from "@/stores/types/spotlight.types";
|
|
|
|
type DefineSpotlightStep = Omit<SpotlightStep, "target"> & {
|
|
target?: SpotlightStep["target"];
|
|
};
|
|
|
|
export interface DefineSpotlightOptions {
|
|
id: string;
|
|
steps: DefineSpotlightStep[];
|
|
showOnce?: boolean;
|
|
initialStep?: number;
|
|
}
|
|
|
|
export interface SpotlightController {
|
|
id: string;
|
|
steps: SpotlightStep[];
|
|
start: (options?: Pick<StartSpotlightOptions, "initialStep">) => void;
|
|
reset: () => void;
|
|
}
|
|
|
|
function normalizeSteps(steps: DefineSpotlightStep[]): SpotlightStep[] {
|
|
return steps.map((step) => ({
|
|
...step,
|
|
target: step.target ?? { dataGuide: step.id },
|
|
}));
|
|
}
|
|
|
|
export function defineSpotlight({
|
|
id,
|
|
steps,
|
|
showOnce = true,
|
|
initialStep = 0,
|
|
}: DefineSpotlightOptions): SpotlightController {
|
|
const normalizedSteps = normalizeSteps(steps);
|
|
|
|
return {
|
|
id,
|
|
steps: normalizedSteps,
|
|
start: (options) => {
|
|
useSpotlightStore.getState().startSpotlight({
|
|
courseId: appConfig.course.id,
|
|
spotlightId: id,
|
|
showOnce,
|
|
steps: normalizedSteps,
|
|
initialStep: options?.initialStep ?? initialStep,
|
|
});
|
|
},
|
|
reset: () => {
|
|
useSpotlightStore.getState().resetSpotlight(appConfig.course.id, id);
|
|
},
|
|
};
|
|
}
|