tutor-ai 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +206 -2
- package/assets/tutor-ai.png +0 -0
- package/dist/index.cjs +1148 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +241 -0
- package/dist/index.d.ts +241 -0
- package/dist/index.mjs +1119 -0
- package/dist/index.mjs.map +1 -0
- package/dist/styles.css +266 -0
- package/package.json +77 -4
- package/scripts/copy-css.mjs +12 -0
- package/scripts/install-skills.mjs +192 -0
- package/scripts/uninstall-skills.mjs +127 -0
- package/skills/tutor-ai/SKILL.md +160 -0
- package/skills/tutor-ai/references/api.md +146 -0
- package/skills/tutor-ai-authoring/SKILL.md +149 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.tsx
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
DEFAULT_LABELS: () => DEFAULT_LABELS,
|
|
24
|
+
TourResumePill: () => TourResumePill,
|
|
25
|
+
TutorProvider: () => TutorProvider,
|
|
26
|
+
TutorialGate: () => TutorialGate,
|
|
27
|
+
TutorialOverlay: () => TutorialOverlay,
|
|
28
|
+
createLocalStorage: () => createLocalStorage,
|
|
29
|
+
holdTour: () => holdTour,
|
|
30
|
+
isTourHeld: () => isTourHeld,
|
|
31
|
+
noopStorage: () => noopStorage,
|
|
32
|
+
releaseTour: () => releaseTour,
|
|
33
|
+
resetHold: () => resetHold,
|
|
34
|
+
useTutor: () => useTutor
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/TutorProvider.tsx
|
|
39
|
+
var import_react = require("react");
|
|
40
|
+
|
|
41
|
+
// src/hold.ts
|
|
42
|
+
var held = false;
|
|
43
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
44
|
+
function emit() {
|
|
45
|
+
for (const listener of listeners) listener();
|
|
46
|
+
}
|
|
47
|
+
function holdTour() {
|
|
48
|
+
if (held) return;
|
|
49
|
+
held = true;
|
|
50
|
+
emit();
|
|
51
|
+
}
|
|
52
|
+
function releaseTour() {
|
|
53
|
+
if (!held) return;
|
|
54
|
+
held = false;
|
|
55
|
+
emit();
|
|
56
|
+
}
|
|
57
|
+
function isTourHeld() {
|
|
58
|
+
return held;
|
|
59
|
+
}
|
|
60
|
+
function subscribeHold(listener) {
|
|
61
|
+
listeners.add(listener);
|
|
62
|
+
return () => {
|
|
63
|
+
listeners.delete(listener);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function resetHold() {
|
|
67
|
+
held = false;
|
|
68
|
+
emit();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/storage.ts
|
|
72
|
+
function createLocalStorage() {
|
|
73
|
+
return {
|
|
74
|
+
getItem(key) {
|
|
75
|
+
try {
|
|
76
|
+
if (typeof window === "undefined" || !window.localStorage) return null;
|
|
77
|
+
return window.localStorage.getItem(key);
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
setItem(key, value) {
|
|
83
|
+
try {
|
|
84
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
85
|
+
window.localStorage.setItem(key, value);
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
removeItem(key) {
|
|
91
|
+
try {
|
|
92
|
+
if (typeof window !== "undefined" && window.localStorage) {
|
|
93
|
+
window.localStorage.removeItem(key);
|
|
94
|
+
}
|
|
95
|
+
} catch {
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
var noopStorage = {
|
|
101
|
+
getItem: () => null,
|
|
102
|
+
setItem: () => void 0,
|
|
103
|
+
removeItem: () => void 0
|
|
104
|
+
};
|
|
105
|
+
function parseProgress(raw) {
|
|
106
|
+
if (!raw) return {};
|
|
107
|
+
try {
|
|
108
|
+
const parsed = JSON.parse(raw);
|
|
109
|
+
if (parsed && typeof parsed === "object") return parsed;
|
|
110
|
+
} catch {
|
|
111
|
+
}
|
|
112
|
+
return { done: true };
|
|
113
|
+
}
|
|
114
|
+
function serializeProgress(progress) {
|
|
115
|
+
return JSON.stringify({ ...progress, updatedAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
116
|
+
}
|
|
117
|
+
function mergeResumable(current, next) {
|
|
118
|
+
const rest = current.filter((item) => item.tourId !== next.tourId);
|
|
119
|
+
return [...rest, next];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/types.ts
|
|
123
|
+
var DEFAULT_LABELS = {
|
|
124
|
+
gateTitle: "Welcome",
|
|
125
|
+
gateSubtitle: "Choose a guided tour to get started.",
|
|
126
|
+
skip: "Skip",
|
|
127
|
+
next: "Next",
|
|
128
|
+
prev: "Back",
|
|
129
|
+
finish: "Finish",
|
|
130
|
+
progress: "Step {current} of {total}",
|
|
131
|
+
missingTarget: "We could not find this step\u2019s element, so it was skipped.",
|
|
132
|
+
topicsTitle: "Explore by topic",
|
|
133
|
+
topicFilterPlaceholder: "Filter topics\u2026",
|
|
134
|
+
resume: "Resume tour",
|
|
135
|
+
resumePillTitle: "You have an unfinished tour"
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// src/utils.ts
|
|
139
|
+
var EMPTY_CONTEXT = {
|
|
140
|
+
capabilities: [],
|
|
141
|
+
roles: [],
|
|
142
|
+
meta: {}
|
|
143
|
+
};
|
|
144
|
+
async function resolveContext(input) {
|
|
145
|
+
if (!input) return EMPTY_CONTEXT;
|
|
146
|
+
if (typeof input === "function") {
|
|
147
|
+
return { ...EMPTY_CONTEXT, ...await input() };
|
|
148
|
+
}
|
|
149
|
+
return { ...EMPTY_CONTEXT, ...input };
|
|
150
|
+
}
|
|
151
|
+
function flattenTour(config) {
|
|
152
|
+
return config.chapters.flatMap(
|
|
153
|
+
(chapter) => chapter.steps.map((step) => ({ step, chapter }))
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
function isStepDone(entry, config, ctx) {
|
|
157
|
+
const predicate = entry.step.isStepDone ?? config.isStepDone;
|
|
158
|
+
return predicate ? predicate(entry.step.id, ctx) === true : false;
|
|
159
|
+
}
|
|
160
|
+
function visibleEntries(config, ctx, topic) {
|
|
161
|
+
return flattenTour(config).filter((entry) => {
|
|
162
|
+
if (entry.chapter.when && !entry.chapter.when(ctx)) return false;
|
|
163
|
+
if (entry.step.when && !entry.step.when(ctx)) return false;
|
|
164
|
+
if (isStepDone(entry, config, ctx)) return false;
|
|
165
|
+
if (topic?.stepIds && !topic.stepIds.includes(entry.step.id)) return false;
|
|
166
|
+
return true;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function storageKeyFor(config) {
|
|
170
|
+
if (!config.storageKey) return `tutor-ai:${config.id}`;
|
|
171
|
+
return typeof config.storageKey === "function" ? config.storageKey(config.id) : config.storageKey;
|
|
172
|
+
}
|
|
173
|
+
function collectTopics(tours) {
|
|
174
|
+
const seen = /* @__PURE__ */ new Set();
|
|
175
|
+
const result = [];
|
|
176
|
+
for (const tour of tours) {
|
|
177
|
+
for (const topic of tour.topics ?? []) {
|
|
178
|
+
if (seen.has(topic.id)) continue;
|
|
179
|
+
seen.add(topic.id);
|
|
180
|
+
result.push(topic);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
function matchTopics(topics, query) {
|
|
186
|
+
const q = (query ?? "").trim().toLowerCase();
|
|
187
|
+
if (!q) return topics;
|
|
188
|
+
return topics.filter(
|
|
189
|
+
(topic) => [topic.label, topic.description ?? "", ...topic.keywords ?? []].join(" ").toLowerCase().includes(q)
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
function progressText(template, current, total) {
|
|
193
|
+
return template.split("{current}").join(String(current)).split("{total}").join(String(total));
|
|
194
|
+
}
|
|
195
|
+
function isElementVisible(element) {
|
|
196
|
+
if (!(element instanceof HTMLElement) && !(element instanceof SVGElement)) {
|
|
197
|
+
return !!(element.getClientRects().length > 0);
|
|
198
|
+
}
|
|
199
|
+
const rect = element.getBoundingClientRect();
|
|
200
|
+
if (rect.width <= 0 || rect.height <= 0) return false;
|
|
201
|
+
const style = typeof getComputedStyle === "function" ? getComputedStyle(element) : null;
|
|
202
|
+
if (!style) return true;
|
|
203
|
+
return style.visibility !== "hidden" && style.display !== "none" && Number(style.opacity) > 0;
|
|
204
|
+
}
|
|
205
|
+
function querySelectorSafe(selector) {
|
|
206
|
+
if (typeof document === "undefined" || !selector) return null;
|
|
207
|
+
try {
|
|
208
|
+
return document.querySelector(selector);
|
|
209
|
+
} catch {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function resolveTarget(step, ctx) {
|
|
214
|
+
const fallback = () => {
|
|
215
|
+
if (!step.target) return null;
|
|
216
|
+
if (typeof step.target === "function") {
|
|
217
|
+
try {
|
|
218
|
+
return step.target(ctx) ?? null;
|
|
219
|
+
} catch {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return querySelectorSafe(step.target);
|
|
224
|
+
};
|
|
225
|
+
if (!step.targetWhen) return fallback();
|
|
226
|
+
const conditional = querySelectorSafe(step.targetWhen.selector);
|
|
227
|
+
const conditionalVisible = !!conditional && isElementVisible(conditional);
|
|
228
|
+
if (step.targetWhen.when === "visible") {
|
|
229
|
+
return conditionalVisible ? conditional : fallback();
|
|
230
|
+
}
|
|
231
|
+
return conditionalVisible ? conditional : fallback();
|
|
232
|
+
}
|
|
233
|
+
function isVisibleElement(element) {
|
|
234
|
+
return !!element && isElementVisible(element);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// src/TutorProvider.tsx
|
|
238
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
239
|
+
var TutorContext = (0, import_react.createContext)(null);
|
|
240
|
+
var defaultStore = createLocalStorage();
|
|
241
|
+
function useTutor() {
|
|
242
|
+
const api = (0, import_react.useContext)(TutorContext);
|
|
243
|
+
if (!api) throw new Error("useTutor must be used inside <TutorProvider>");
|
|
244
|
+
return api;
|
|
245
|
+
}
|
|
246
|
+
function TutorProvider({
|
|
247
|
+
config,
|
|
248
|
+
children,
|
|
249
|
+
context,
|
|
250
|
+
labels: labelsProp
|
|
251
|
+
}) {
|
|
252
|
+
const inputTours = (0, import_react.useMemo)(
|
|
253
|
+
() => Array.isArray(config) ? config : [config],
|
|
254
|
+
[config]
|
|
255
|
+
);
|
|
256
|
+
const toursRef = (0, import_react.useRef)(inputTours);
|
|
257
|
+
toursRef.current = inputTours;
|
|
258
|
+
const tourKey = inputTours.map((tour) => tour.id).join("\0");
|
|
259
|
+
const contextRef = (0, import_react.useRef)(context);
|
|
260
|
+
contextRef.current = context;
|
|
261
|
+
const labelsOverrideRef = (0, import_react.useRef)(labelsProp);
|
|
262
|
+
labelsOverrideRef.current = labelsProp;
|
|
263
|
+
const [state, setState] = (0, import_react.useState)("idle");
|
|
264
|
+
const [activeTourId, setActiveTourId] = (0, import_react.useState)(null);
|
|
265
|
+
const [entries, setEntries] = (0, import_react.useState)([]);
|
|
266
|
+
const [index, setIndex] = (0, import_react.useState)(-1);
|
|
267
|
+
const [ctx, setCtx] = (0, import_react.useState)(EMPTY_CONTEXT);
|
|
268
|
+
const [doneIds, setDoneIds] = (0, import_react.useState)([]);
|
|
269
|
+
const [completedTours, setCompletedTours] = (0, import_react.useState)([]);
|
|
270
|
+
const [resumableTours, setResumableTours] = (0, import_react.useState)([]);
|
|
271
|
+
const [activeTopic, setActiveTopic] = (0, import_react.useState)(null);
|
|
272
|
+
const activeTourRef = (0, import_react.useRef)(null);
|
|
273
|
+
const entriesRef = (0, import_react.useRef)([]);
|
|
274
|
+
const indexRef = (0, import_react.useRef)(-1);
|
|
275
|
+
const contextValueRef = (0, import_react.useRef)(EMPTY_CONTEXT);
|
|
276
|
+
const topicRef = (0, import_react.useRef)(null);
|
|
277
|
+
const resumableRef = (0, import_react.useRef)([]);
|
|
278
|
+
const completedRef = (0, import_react.useRef)([]);
|
|
279
|
+
completedRef.current = completedTours;
|
|
280
|
+
const lockRef = (0, import_react.useRef)(Promise.resolve());
|
|
281
|
+
const enqueue = (0, import_react.useCallback)((fn) => {
|
|
282
|
+
const run = lockRef.current.then(fn, fn);
|
|
283
|
+
lockRef.current = run.catch(() => void 0);
|
|
284
|
+
return run.catch(() => void 0);
|
|
285
|
+
}, []);
|
|
286
|
+
const findTour = (0, import_react.useCallback)(
|
|
287
|
+
(id) => toursRef.current.find((tour) => tour.id === id) ?? toursRef.current[0] ?? null,
|
|
288
|
+
[]
|
|
289
|
+
);
|
|
290
|
+
const getStore = (0, import_react.useCallback)(
|
|
291
|
+
(tour) => tour.storage ?? defaultStore,
|
|
292
|
+
[]
|
|
293
|
+
);
|
|
294
|
+
const writeProgress = (0, import_react.useCallback)(
|
|
295
|
+
(tour, progress) => {
|
|
296
|
+
try {
|
|
297
|
+
void Promise.resolve(
|
|
298
|
+
getStore(tour).setItem(
|
|
299
|
+
storageKeyFor(tour),
|
|
300
|
+
serializeProgress(progress)
|
|
301
|
+
)
|
|
302
|
+
).catch(() => void 0);
|
|
303
|
+
} catch {
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
[getStore]
|
|
307
|
+
);
|
|
308
|
+
const clearProgress = (0, import_react.useCallback)(
|
|
309
|
+
(tour) => {
|
|
310
|
+
try {
|
|
311
|
+
void Promise.resolve(
|
|
312
|
+
getStore(tour).removeItem(storageKeyFor(tour))
|
|
313
|
+
).catch(() => void 0);
|
|
314
|
+
} catch {
|
|
315
|
+
}
|
|
316
|
+
},
|
|
317
|
+
[getStore]
|
|
318
|
+
);
|
|
319
|
+
const finishInternal = (0, import_react.useCallback)(async () => {
|
|
320
|
+
const tour = findTour(activeTourRef.current);
|
|
321
|
+
const old = entriesRef.current[indexRef.current]?.step;
|
|
322
|
+
if (old?.onExit) {
|
|
323
|
+
try {
|
|
324
|
+
await old.onExit(contextValueRef.current);
|
|
325
|
+
} catch {
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (tour) {
|
|
329
|
+
writeProgress(tour, { done: true });
|
|
330
|
+
setCompletedTours((prev2) => Array.from(/* @__PURE__ */ new Set([...prev2, tour.id])));
|
|
331
|
+
setResumableTours((prev2) => {
|
|
332
|
+
const next2 = prev2.filter((item) => item.tourId !== tour.id);
|
|
333
|
+
resumableRef.current = next2;
|
|
334
|
+
return next2;
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
activeTourRef.current = null;
|
|
338
|
+
entriesRef.current = [];
|
|
339
|
+
indexRef.current = -1;
|
|
340
|
+
topicRef.current = null;
|
|
341
|
+
setActiveTourId(null);
|
|
342
|
+
setEntries([]);
|
|
343
|
+
setIndex(-1);
|
|
344
|
+
setActiveTopic(null);
|
|
345
|
+
setState("finished");
|
|
346
|
+
}, [findTour, writeProgress]);
|
|
347
|
+
const goToIndexInternal = (0, import_react.useCallback)(
|
|
348
|
+
async (targetIndex, tourIdOverride) => {
|
|
349
|
+
const tour = findTour(tourIdOverride ?? activeTourRef.current);
|
|
350
|
+
if (!tour) return;
|
|
351
|
+
const resolved = await resolveContext(contextRef.current ?? tour.context);
|
|
352
|
+
const nextEntries = visibleEntries(tour, resolved, topicRef.current);
|
|
353
|
+
if (targetIndex < 0 || targetIndex >= nextEntries.length) {
|
|
354
|
+
await finishInternal();
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const old = entriesRef.current[indexRef.current]?.step;
|
|
358
|
+
const nextEntry = nextEntries[targetIndex];
|
|
359
|
+
if (old && old.id !== nextEntry.step.id && old.onExit) {
|
|
360
|
+
try {
|
|
361
|
+
await old.onExit(resolved);
|
|
362
|
+
} catch {
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
activeTourRef.current = tour.id;
|
|
366
|
+
entriesRef.current = nextEntries;
|
|
367
|
+
indexRef.current = targetIndex;
|
|
368
|
+
contextValueRef.current = resolved;
|
|
369
|
+
topicRef.current = topicRef.current;
|
|
370
|
+
setActiveTourId(tour.id);
|
|
371
|
+
setEntries(nextEntries);
|
|
372
|
+
setIndex(targetIndex);
|
|
373
|
+
setCtx(resolved);
|
|
374
|
+
setState("running");
|
|
375
|
+
setDoneIds(
|
|
376
|
+
flattenTour(tour).filter((entry) => isStepDone(entry, tour, resolved)).map((entry) => entry.step.id)
|
|
377
|
+
);
|
|
378
|
+
writeProgress(tour, { stepId: nextEntry.step.id });
|
|
379
|
+
setResumableTours((prev2) => {
|
|
380
|
+
const next2 = mergeResumable(prev2, {
|
|
381
|
+
tourId: tour.id,
|
|
382
|
+
stepId: nextEntry.step.id
|
|
383
|
+
});
|
|
384
|
+
resumableRef.current = next2;
|
|
385
|
+
return next2;
|
|
386
|
+
});
|
|
387
|
+
if (nextEntry.step.onEnter) {
|
|
388
|
+
try {
|
|
389
|
+
await nextEntry.step.onEnter(resolved);
|
|
390
|
+
} catch {
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
[findTour, finishInternal, writeProgress]
|
|
395
|
+
);
|
|
396
|
+
const startInternal = (0, import_react.useCallback)(
|
|
397
|
+
async (tourId, stepId) => {
|
|
398
|
+
if (isTourHeld()) return;
|
|
399
|
+
const tour = findTour(tourId ?? activeTourRef.current);
|
|
400
|
+
if (!tour) return;
|
|
401
|
+
activeTourRef.current = tour.id;
|
|
402
|
+
let target = 0;
|
|
403
|
+
if (stepId) {
|
|
404
|
+
const resolved = await resolveContext(
|
|
405
|
+
contextRef.current ?? tour.context
|
|
406
|
+
);
|
|
407
|
+
const found = visibleEntries(tour, resolved, topicRef.current).map((entry) => entry.step.id).indexOf(stepId);
|
|
408
|
+
if (found >= 0) target = found;
|
|
409
|
+
}
|
|
410
|
+
await goToIndexInternal(target, tour.id);
|
|
411
|
+
},
|
|
412
|
+
[findTour, goToIndexInternal]
|
|
413
|
+
);
|
|
414
|
+
const nextInternal = (0, import_react.useCallback)(async () => {
|
|
415
|
+
if (isTourHeld() || !activeTourRef.current) return;
|
|
416
|
+
await goToIndexInternal(indexRef.current + 1);
|
|
417
|
+
}, [goToIndexInternal]);
|
|
418
|
+
const prevInternal = (0, import_react.useCallback)(async () => {
|
|
419
|
+
if (isTourHeld() || indexRef.current <= 0) return;
|
|
420
|
+
await goToIndexInternal(indexRef.current - 1);
|
|
421
|
+
}, [goToIndexInternal]);
|
|
422
|
+
const goToInternal = (0, import_react.useCallback)(
|
|
423
|
+
async (stepId) => {
|
|
424
|
+
if (isTourHeld()) return;
|
|
425
|
+
const found = entriesRef.current.map((entry) => entry.step.id).indexOf(stepId);
|
|
426
|
+
if (found >= 0) await goToIndexInternal(found);
|
|
427
|
+
},
|
|
428
|
+
[goToIndexInternal]
|
|
429
|
+
);
|
|
430
|
+
const stopInternal = (0, import_react.useCallback)(async () => {
|
|
431
|
+
const old = entriesRef.current[indexRef.current]?.step;
|
|
432
|
+
if (old?.onExit) {
|
|
433
|
+
try {
|
|
434
|
+
await old.onExit(contextValueRef.current);
|
|
435
|
+
} catch {
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
activeTourRef.current = null;
|
|
439
|
+
entriesRef.current = [];
|
|
440
|
+
indexRef.current = -1;
|
|
441
|
+
topicRef.current = null;
|
|
442
|
+
setActiveTourId(null);
|
|
443
|
+
setEntries([]);
|
|
444
|
+
setIndex(-1);
|
|
445
|
+
setActiveTopic(null);
|
|
446
|
+
setState("idle");
|
|
447
|
+
}, []);
|
|
448
|
+
const resumeInternal = (0, import_react.useCallback)(async () => {
|
|
449
|
+
const list = resumableRef.current;
|
|
450
|
+
const progress = list.find((item) => item.tourId === activeTourRef.current) ?? list[0];
|
|
451
|
+
if (!progress) return;
|
|
452
|
+
await startInternal(progress.tourId, progress.stepId);
|
|
453
|
+
}, [startInternal]);
|
|
454
|
+
const startTopicInternal = (0, import_react.useCallback)(
|
|
455
|
+
async (topicId) => {
|
|
456
|
+
const topic = collectTopics(toursRef.current).find(
|
|
457
|
+
(item) => item.id === topicId
|
|
458
|
+
);
|
|
459
|
+
if (!topic) return;
|
|
460
|
+
topicRef.current = topic;
|
|
461
|
+
setActiveTopic(topic);
|
|
462
|
+
await startInternal(topic.tourId, topic.stepIds?.[0]);
|
|
463
|
+
},
|
|
464
|
+
[startInternal]
|
|
465
|
+
);
|
|
466
|
+
const resetProgressInternal = (0, import_react.useCallback)(
|
|
467
|
+
async (tourId) => {
|
|
468
|
+
const targets = tourId ? toursRef.current.filter((tour) => tour.id === tourId) : toursRef.current;
|
|
469
|
+
const targetIds = targets.map((tour) => tour.id);
|
|
470
|
+
targets.forEach(clearProgress);
|
|
471
|
+
setCompletedTours(
|
|
472
|
+
(prev2) => prev2.filter((id) => !targetIds.includes(id))
|
|
473
|
+
);
|
|
474
|
+
setResumableTours((prev2) => {
|
|
475
|
+
const next2 = prev2.filter((item) => !targetIds.includes(item.tourId));
|
|
476
|
+
resumableRef.current = next2;
|
|
477
|
+
return next2;
|
|
478
|
+
});
|
|
479
|
+
if (activeTourRef.current && targetIds.includes(activeTourRef.current)) {
|
|
480
|
+
activeTourRef.current = null;
|
|
481
|
+
entriesRef.current = [];
|
|
482
|
+
indexRef.current = -1;
|
|
483
|
+
topicRef.current = null;
|
|
484
|
+
setActiveTourId(null);
|
|
485
|
+
setEntries([]);
|
|
486
|
+
setIndex(-1);
|
|
487
|
+
setActiveTopic(null);
|
|
488
|
+
}
|
|
489
|
+
const stillDone = completedRef.current.filter(
|
|
490
|
+
(id) => !targetIds.includes(id)
|
|
491
|
+
);
|
|
492
|
+
const nextTour = toursRef.current.find((tour) => !stillDone.includes(tour.id));
|
|
493
|
+
setState(nextTour ? "pending" : "idle");
|
|
494
|
+
},
|
|
495
|
+
[clearProgress]
|
|
496
|
+
);
|
|
497
|
+
const start = (0, import_react.useCallback)(
|
|
498
|
+
(tourId, stepId) => enqueue(() => startInternal(tourId, stepId)),
|
|
499
|
+
[enqueue, startInternal]
|
|
500
|
+
);
|
|
501
|
+
const startTour = start;
|
|
502
|
+
const startTopic = (0, import_react.useCallback)(
|
|
503
|
+
(topicId) => enqueue(() => startTopicInternal(topicId)),
|
|
504
|
+
[enqueue, startTopicInternal]
|
|
505
|
+
);
|
|
506
|
+
const next = (0, import_react.useCallback)(
|
|
507
|
+
() => enqueue(nextInternal),
|
|
508
|
+
[enqueue, nextInternal]
|
|
509
|
+
);
|
|
510
|
+
const prev = (0, import_react.useCallback)(
|
|
511
|
+
() => enqueue(prevInternal),
|
|
512
|
+
[enqueue, prevInternal]
|
|
513
|
+
);
|
|
514
|
+
const goTo = (0, import_react.useCallback)(
|
|
515
|
+
(stepId) => enqueue(() => goToInternal(stepId)),
|
|
516
|
+
[enqueue, goToInternal]
|
|
517
|
+
);
|
|
518
|
+
const finish = (0, import_react.useCallback)(
|
|
519
|
+
() => enqueue(finishInternal),
|
|
520
|
+
[enqueue, finishInternal]
|
|
521
|
+
);
|
|
522
|
+
const skip = (0, import_react.useCallback)(
|
|
523
|
+
() => enqueue(stopInternal),
|
|
524
|
+
[enqueue, stopInternal]
|
|
525
|
+
);
|
|
526
|
+
const stop = skip;
|
|
527
|
+
const resume = (0, import_react.useCallback)(
|
|
528
|
+
() => enqueue(resumeInternal),
|
|
529
|
+
[enqueue, resumeInternal]
|
|
530
|
+
);
|
|
531
|
+
const resetProgress = (0, import_react.useCallback)(
|
|
532
|
+
(tourId) => enqueue(() => resetProgressInternal(tourId)),
|
|
533
|
+
[enqueue, resetProgressInternal]
|
|
534
|
+
);
|
|
535
|
+
const openGate = (0, import_react.useCallback)(() => {
|
|
536
|
+
if (isTourHeld()) return;
|
|
537
|
+
setState((prevState) => prevState === "running" ? prevState : "pending");
|
|
538
|
+
}, []);
|
|
539
|
+
const hold = (0, import_react.useCallback)(() => holdTour(), []);
|
|
540
|
+
const release = (0, import_react.useCallback)(() => releaseTour(), []);
|
|
541
|
+
const held2 = (0, import_react.useSyncExternalStore)(subscribeHold, isTourHeld, () => false);
|
|
542
|
+
const filterTopics = (0, import_react.useCallback)(
|
|
543
|
+
(query) => matchTopics(collectTopics(toursRef.current), query),
|
|
544
|
+
[]
|
|
545
|
+
);
|
|
546
|
+
const resolveTopicSteps = (0, import_react.useCallback)((topicId) => {
|
|
547
|
+
const topic = collectTopics(toursRef.current).find(
|
|
548
|
+
(item) => item.id === topicId
|
|
549
|
+
);
|
|
550
|
+
if (!topic) return [];
|
|
551
|
+
const tour = toursRef.current.find((item) => item.id === topic.tourId);
|
|
552
|
+
if (!tour) return [];
|
|
553
|
+
const all = flattenTour(tour).map((entry) => entry.step);
|
|
554
|
+
if (!topic.stepIds) return all;
|
|
555
|
+
return topic.stepIds.map((id) => all.find((step) => step.id === id)).filter((step) => !!step);
|
|
556
|
+
}, []);
|
|
557
|
+
(0, import_react.useEffect)(() => {
|
|
558
|
+
let cancelled = false;
|
|
559
|
+
void (async () => {
|
|
560
|
+
const done = [];
|
|
561
|
+
const resumable = [];
|
|
562
|
+
for (const tour of toursRef.current) {
|
|
563
|
+
let raw = null;
|
|
564
|
+
try {
|
|
565
|
+
raw = await getStore(tour).getItem(storageKeyFor(tour));
|
|
566
|
+
} catch {
|
|
567
|
+
raw = null;
|
|
568
|
+
}
|
|
569
|
+
const progress = parseProgress(raw);
|
|
570
|
+
if (progress.done) done.push(tour.id);
|
|
571
|
+
else if (progress.stepId) {
|
|
572
|
+
resumable.push({ tourId: tour.id, stepId: progress.stepId });
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (cancelled) return;
|
|
576
|
+
setCompletedTours(done);
|
|
577
|
+
resumableRef.current = resumable;
|
|
578
|
+
setResumableTours(resumable);
|
|
579
|
+
const firstIncomplete = toursRef.current.find(
|
|
580
|
+
(tour) => !done.includes(tour.id)
|
|
581
|
+
);
|
|
582
|
+
setState(firstIncomplete ? "pending" : "idle");
|
|
583
|
+
})();
|
|
584
|
+
return () => {
|
|
585
|
+
cancelled = true;
|
|
586
|
+
};
|
|
587
|
+
}, [tourKey, getStore]);
|
|
588
|
+
(0, import_react.useEffect)(() => {
|
|
589
|
+
if (state !== "running" || held2 || typeof document === "undefined") return;
|
|
590
|
+
const onKeyDown = (event) => {
|
|
591
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
592
|
+
const target = event.target;
|
|
593
|
+
if (target && (target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName))) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
if (event.key === "ArrowRight" || event.key === "Enter") {
|
|
597
|
+
event.preventDefault();
|
|
598
|
+
void next();
|
|
599
|
+
} else if (event.key === "ArrowLeft") {
|
|
600
|
+
event.preventDefault();
|
|
601
|
+
void prev();
|
|
602
|
+
} else if (event.key === "Escape") {
|
|
603
|
+
event.preventDefault();
|
|
604
|
+
void skip();
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
document.addEventListener("keydown", onKeyDown);
|
|
608
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
609
|
+
}, [state, held2, next, prev, skip]);
|
|
610
|
+
const baseTour = findTour(activeTourId) ?? inputTours[0] ?? null;
|
|
611
|
+
const labels = (0, import_react.useMemo)(
|
|
612
|
+
() => ({
|
|
613
|
+
...DEFAULT_LABELS,
|
|
614
|
+
...baseTour?.labels ?? {},
|
|
615
|
+
...labelsOverrideRef.current ?? {}
|
|
616
|
+
}),
|
|
617
|
+
[baseTour]
|
|
618
|
+
);
|
|
619
|
+
const topics = (0, import_react.useMemo)(() => collectTopics(inputTours), [inputTours]);
|
|
620
|
+
const currentIndex = index;
|
|
621
|
+
const total = entries.length;
|
|
622
|
+
const currentStep = entries[index]?.step ?? null;
|
|
623
|
+
const api = (0, import_react.useMemo)(
|
|
624
|
+
() => ({
|
|
625
|
+
state,
|
|
626
|
+
tours: inputTours,
|
|
627
|
+
topics,
|
|
628
|
+
config: baseTour,
|
|
629
|
+
activeTourId,
|
|
630
|
+
currentStep,
|
|
631
|
+
currentChapter: entries[index]?.chapter ?? null,
|
|
632
|
+
currentIndex,
|
|
633
|
+
currentStepIndex: currentIndex,
|
|
634
|
+
total,
|
|
635
|
+
totalSteps: total,
|
|
636
|
+
doneStepIds: doneIds,
|
|
637
|
+
completedTours,
|
|
638
|
+
resumableTours,
|
|
639
|
+
isRunning: state === "running",
|
|
640
|
+
isHeld: held2,
|
|
641
|
+
context: ctx,
|
|
642
|
+
labels,
|
|
643
|
+
start,
|
|
644
|
+
startTour,
|
|
645
|
+
startTopic,
|
|
646
|
+
openGate,
|
|
647
|
+
resume,
|
|
648
|
+
next,
|
|
649
|
+
prev,
|
|
650
|
+
goTo,
|
|
651
|
+
finish,
|
|
652
|
+
skip,
|
|
653
|
+
stop,
|
|
654
|
+
hold,
|
|
655
|
+
release,
|
|
656
|
+
resetProgress,
|
|
657
|
+
filterTopics,
|
|
658
|
+
resolveTopicSteps
|
|
659
|
+
}),
|
|
660
|
+
[
|
|
661
|
+
state,
|
|
662
|
+
inputTours,
|
|
663
|
+
topics,
|
|
664
|
+
baseTour,
|
|
665
|
+
activeTourId,
|
|
666
|
+
currentStep,
|
|
667
|
+
currentIndex,
|
|
668
|
+
total,
|
|
669
|
+
doneIds,
|
|
670
|
+
completedTours,
|
|
671
|
+
resumableTours,
|
|
672
|
+
held2,
|
|
673
|
+
ctx,
|
|
674
|
+
labels,
|
|
675
|
+
start,
|
|
676
|
+
startTour,
|
|
677
|
+
startTopic,
|
|
678
|
+
openGate,
|
|
679
|
+
resume,
|
|
680
|
+
next,
|
|
681
|
+
prev,
|
|
682
|
+
goTo,
|
|
683
|
+
finish,
|
|
684
|
+
skip,
|
|
685
|
+
stop,
|
|
686
|
+
hold,
|
|
687
|
+
release,
|
|
688
|
+
resetProgress,
|
|
689
|
+
filterTopics,
|
|
690
|
+
resolveTopicSteps,
|
|
691
|
+
entries,
|
|
692
|
+
index
|
|
693
|
+
]
|
|
694
|
+
);
|
|
695
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TutorContext.Provider, { value: api, children });
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// src/TutorialGate.tsx
|
|
699
|
+
var import_react2 = require("react");
|
|
700
|
+
var import_react_dom = require("react-dom");
|
|
701
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
702
|
+
function TutorialGate({ className }) {
|
|
703
|
+
const tutor = useTutor();
|
|
704
|
+
const [query, setQuery] = (0, import_react2.useState)("");
|
|
705
|
+
if (typeof document === "undefined") return null;
|
|
706
|
+
if (tutor.state !== "pending") return null;
|
|
707
|
+
const tour = tutor.config;
|
|
708
|
+
const options = tour?.gate ?? tutor.tours.map((item) => ({
|
|
709
|
+
id: item.id,
|
|
710
|
+
label: item.title ?? item.id,
|
|
711
|
+
description: item.description,
|
|
712
|
+
tourId: item.id
|
|
713
|
+
}));
|
|
714
|
+
const topics = tutor.filterTopics(query);
|
|
715
|
+
if (tour?.renderGate) {
|
|
716
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: tour.renderGate({
|
|
717
|
+
options,
|
|
718
|
+
topics,
|
|
719
|
+
labels: tutor.labels,
|
|
720
|
+
tour,
|
|
721
|
+
start: tutor.startTour,
|
|
722
|
+
startTopic: tutor.startTopic,
|
|
723
|
+
skip: tutor.skip,
|
|
724
|
+
filterTopics: tutor.filterTopics
|
|
725
|
+
}) });
|
|
726
|
+
}
|
|
727
|
+
return (0, import_react_dom.createPortal)(
|
|
728
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
729
|
+
"div",
|
|
730
|
+
{
|
|
731
|
+
className: `tutorai-gate${className ? ` ${className}` : ""}`,
|
|
732
|
+
role: "dialog",
|
|
733
|
+
"aria-modal": "true",
|
|
734
|
+
"aria-labelledby": "tutorai-gate-title",
|
|
735
|
+
"data-test": "tutor-gate",
|
|
736
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "tutorai-gate-panel", children: [
|
|
737
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h2", { className: "tutorai-gate-title", id: "tutorai-gate-title", children: tutor.labels.gateTitle }),
|
|
738
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "tutorai-gate-subtitle", children: tutor.labels.gateSubtitle }),
|
|
739
|
+
options.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "tutorai-gate-options", "data-test": "tutor-gate-options", children: options.map((option) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
740
|
+
"button",
|
|
741
|
+
{
|
|
742
|
+
type: "button",
|
|
743
|
+
className: "tutorai-button tutorai-primary tutorai-gate-option",
|
|
744
|
+
"data-test": `tutor-gate-option-${option.id}`,
|
|
745
|
+
onClick: () => void tutor.startTour(option.tourId ?? option.id),
|
|
746
|
+
children: [
|
|
747
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: option.label }),
|
|
748
|
+
option.description && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("small", { className: "tutorai-gate-option-description", children: option.description })
|
|
749
|
+
]
|
|
750
|
+
},
|
|
751
|
+
option.id
|
|
752
|
+
)) }),
|
|
753
|
+
topics.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "tutorai-topics", "data-test": "tutor-topics", children: [
|
|
754
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h3", { className: "tutorai-topics-title", children: tutor.labels.topicsTitle }),
|
|
755
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
756
|
+
"input",
|
|
757
|
+
{
|
|
758
|
+
className: "tutorai-topic-filter",
|
|
759
|
+
type: "search",
|
|
760
|
+
value: query,
|
|
761
|
+
placeholder: tutor.labels.topicFilterPlaceholder,
|
|
762
|
+
"aria-label": tutor.labels.topicFilterPlaceholder,
|
|
763
|
+
"data-test": "tutor-topic-filter",
|
|
764
|
+
onChange: (event) => setQuery(event.target.value)
|
|
765
|
+
}
|
|
766
|
+
),
|
|
767
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "tutorai-topic-list", children: topics.map((topic) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
768
|
+
"button",
|
|
769
|
+
{
|
|
770
|
+
type: "button",
|
|
771
|
+
className: "tutorai-topic-button",
|
|
772
|
+
"data-test": `tutor-topic-${topic.id}`,
|
|
773
|
+
onClick: () => void tutor.startTopic(topic.id),
|
|
774
|
+
children: [
|
|
775
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: topic.label }),
|
|
776
|
+
topic.description && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("small", { children: topic.description })
|
|
777
|
+
]
|
|
778
|
+
},
|
|
779
|
+
topic.id
|
|
780
|
+
)) })
|
|
781
|
+
] }),
|
|
782
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
783
|
+
"button",
|
|
784
|
+
{
|
|
785
|
+
type: "button",
|
|
786
|
+
className: "tutorai-button tutorai-gate-skip",
|
|
787
|
+
"data-test": "tutor-gate-skip",
|
|
788
|
+
onClick: () => void tutor.skip(),
|
|
789
|
+
children: tutor.labels.skip
|
|
790
|
+
}
|
|
791
|
+
)
|
|
792
|
+
] })
|
|
793
|
+
}
|
|
794
|
+
),
|
|
795
|
+
document.body
|
|
796
|
+
);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
// src/TutorialOverlay.tsx
|
|
800
|
+
var import_react3 = require("react");
|
|
801
|
+
var import_react_dom2 = require("react-dom");
|
|
802
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
803
|
+
function sameRect(a, b) {
|
|
804
|
+
if (!a || !b) return a === b;
|
|
805
|
+
return Math.abs(a.top - b.top) < 1 && Math.abs(a.left - b.left) < 1 && Math.abs(a.width - b.width) < 1 && Math.abs(a.height - b.height) < 1;
|
|
806
|
+
}
|
|
807
|
+
function TutorialOverlay({
|
|
808
|
+
labels: labelsProp,
|
|
809
|
+
className,
|
|
810
|
+
zIndex = 1e4,
|
|
811
|
+
showProgress
|
|
812
|
+
}) {
|
|
813
|
+
const tutor = useTutor();
|
|
814
|
+
const {
|
|
815
|
+
currentStep,
|
|
816
|
+
currentChapter,
|
|
817
|
+
currentIndex,
|
|
818
|
+
total,
|
|
819
|
+
isHeld,
|
|
820
|
+
state,
|
|
821
|
+
labels: contextLabels,
|
|
822
|
+
config,
|
|
823
|
+
context
|
|
824
|
+
} = tutor;
|
|
825
|
+
const labels = (0, import_react3.useMemo)(
|
|
826
|
+
() => ({ ...contextLabels, ...labelsProp }),
|
|
827
|
+
[contextLabels, labelsProp]
|
|
828
|
+
);
|
|
829
|
+
const [target, setTarget] = (0, import_react3.useState)(null);
|
|
830
|
+
const [hole, setHole] = (0, import_react3.useState)(null);
|
|
831
|
+
const [cardSize, setCardSize] = (0, import_react3.useState)({ width: 340, height: 190 });
|
|
832
|
+
const [missing, setMissing] = (0, import_react3.useState)(false);
|
|
833
|
+
const cardRef = (0, import_react3.useRef)(null);
|
|
834
|
+
const updateRef = (0, import_react3.useRef)(() => void 0);
|
|
835
|
+
const lastTargetRef = (0, import_react3.useRef)(null);
|
|
836
|
+
const scrolledRef = (0, import_react3.useRef)(null);
|
|
837
|
+
const advancedRef = (0, import_react3.useRef)(false);
|
|
838
|
+
const lastHoleRef = (0, import_react3.useRef)(null);
|
|
839
|
+
const stepId = currentStep?.id;
|
|
840
|
+
const placement = currentStep?.placement ?? "bottom";
|
|
841
|
+
(0, import_react3.useEffect)(() => {
|
|
842
|
+
advancedRef.current = false;
|
|
843
|
+
lastTargetRef.current = null;
|
|
844
|
+
scrolledRef.current = null;
|
|
845
|
+
lastHoleRef.current = null;
|
|
846
|
+
setTarget(null);
|
|
847
|
+
setHole(null);
|
|
848
|
+
setMissing(false);
|
|
849
|
+
if (!currentStep || state !== "running" || isHeld || typeof document === "undefined") {
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
const waitMs = currentStep.waitForTarget ?? config?.defaultWaitForTarget ?? 1500;
|
|
853
|
+
const startedAt = Date.now();
|
|
854
|
+
let raf = 0;
|
|
855
|
+
let cancelled = false;
|
|
856
|
+
const update = () => {
|
|
857
|
+
if (cancelled) return;
|
|
858
|
+
const chosen = resolveTarget(currentStep, context);
|
|
859
|
+
const usable = chosen && isVisibleElement(chosen) ? chosen : null;
|
|
860
|
+
if (!usable) {
|
|
861
|
+
if (currentStep.advanceWhenHidden && lastTargetRef.current && !advancedRef.current) {
|
|
862
|
+
advancedRef.current = true;
|
|
863
|
+
lastTargetRef.current = null;
|
|
864
|
+
void tutor.next();
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
if (Date.now() - startedAt >= waitMs) setMissing(true);
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
setMissing(false);
|
|
871
|
+
setTarget((previous) => previous === usable ? previous : usable);
|
|
872
|
+
lastTargetRef.current = usable;
|
|
873
|
+
const rect = usable.getBoundingClientRect();
|
|
874
|
+
const padding = currentStep.padding ?? 8;
|
|
875
|
+
const nextHole = {
|
|
876
|
+
top: rect.top - padding,
|
|
877
|
+
left: rect.left - padding,
|
|
878
|
+
width: rect.width + padding * 2,
|
|
879
|
+
height: rect.height + padding * 2
|
|
880
|
+
};
|
|
881
|
+
if (!sameRect(lastHoleRef.current, nextHole)) {
|
|
882
|
+
lastHoleRef.current = nextHole;
|
|
883
|
+
setHole(nextHole);
|
|
884
|
+
}
|
|
885
|
+
const shouldScroll = currentStep.scrollIntoView ?? config?.scrollIntoView ?? true;
|
|
886
|
+
if (shouldScroll && scrolledRef.current !== usable) {
|
|
887
|
+
scrolledRef.current = usable;
|
|
888
|
+
usable.scrollIntoView({ block: "center", behavior: "smooth" });
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
updateRef.current = update;
|
|
892
|
+
update();
|
|
893
|
+
const interval = window.setInterval(update, 100);
|
|
894
|
+
const schedule = () => {
|
|
895
|
+
if (raf) return;
|
|
896
|
+
raf = window.requestAnimationFrame(() => {
|
|
897
|
+
raf = 0;
|
|
898
|
+
updateRef.current();
|
|
899
|
+
});
|
|
900
|
+
};
|
|
901
|
+
window.addEventListener("scroll", schedule, true);
|
|
902
|
+
window.addEventListener("resize", schedule);
|
|
903
|
+
return () => {
|
|
904
|
+
cancelled = true;
|
|
905
|
+
window.clearInterval(interval);
|
|
906
|
+
window.removeEventListener("scroll", schedule, true);
|
|
907
|
+
window.removeEventListener("resize", schedule);
|
|
908
|
+
if (raf) window.cancelAnimationFrame(raf);
|
|
909
|
+
};
|
|
910
|
+
}, [stepId, state, isHeld, context, config?.defaultWaitForTarget, config?.scrollIntoView]);
|
|
911
|
+
(0, import_react3.useEffect)(() => {
|
|
912
|
+
if (!missing) return;
|
|
913
|
+
const timer = window.setTimeout(() => {
|
|
914
|
+
void tutor.next();
|
|
915
|
+
}, 1e3);
|
|
916
|
+
return () => window.clearTimeout(timer);
|
|
917
|
+
}, [missing, tutor.next]);
|
|
918
|
+
(0, import_react3.useEffect)(() => {
|
|
919
|
+
if (!cardRef.current) return;
|
|
920
|
+
const rect = cardRef.current.getBoundingClientRect();
|
|
921
|
+
if (rect.width > 0 && rect.height > 0) {
|
|
922
|
+
setCardSize({ width: rect.width, height: rect.height });
|
|
923
|
+
}
|
|
924
|
+
}, [stepId, hole, missing]);
|
|
925
|
+
const cardPosition = (0, import_react3.useMemo)(() => {
|
|
926
|
+
const viewportWidth = typeof window === "undefined" ? 1024 : window.innerWidth;
|
|
927
|
+
const viewportHeight = typeof window === "undefined" ? 768 : window.innerHeight;
|
|
928
|
+
const gap = 12;
|
|
929
|
+
const margin = 12;
|
|
930
|
+
const width = cardSize.width;
|
|
931
|
+
const height = cardSize.height;
|
|
932
|
+
if (!hole) {
|
|
933
|
+
return {
|
|
934
|
+
left: Math.max(margin, (viewportWidth - width) / 2),
|
|
935
|
+
top: Math.max(margin, (viewportHeight - height) / 2)
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
const centerX = hole.left + hole.width / 2;
|
|
939
|
+
const centerY = hole.top + hole.height / 2;
|
|
940
|
+
let left = 0;
|
|
941
|
+
let top = 0;
|
|
942
|
+
if (placement === "top") {
|
|
943
|
+
left = centerX - width / 2;
|
|
944
|
+
top = hole.top - height - gap;
|
|
945
|
+
if (top < margin) top = hole.top + hole.height + gap;
|
|
946
|
+
} else if (placement === "left") {
|
|
947
|
+
left = hole.left - width - gap;
|
|
948
|
+
top = centerY - height / 2;
|
|
949
|
+
if (left < margin) left = hole.left + hole.width + gap;
|
|
950
|
+
} else if (placement === "right") {
|
|
951
|
+
left = hole.left + hole.width + gap;
|
|
952
|
+
top = centerY - height / 2;
|
|
953
|
+
if (left + width > viewportWidth - margin) left = hole.left - width - gap;
|
|
954
|
+
} else {
|
|
955
|
+
left = centerX - width / 2;
|
|
956
|
+
top = hole.top + hole.height + gap;
|
|
957
|
+
if (top + height > viewportHeight - margin) top = hole.top - height - gap;
|
|
958
|
+
}
|
|
959
|
+
return {
|
|
960
|
+
left: Math.min(Math.max(margin, left), Math.max(margin, viewportWidth - width - margin)),
|
|
961
|
+
top: Math.min(Math.max(margin, top), Math.max(margin, viewportHeight - height - margin))
|
|
962
|
+
};
|
|
963
|
+
}, [hole, cardSize, placement]);
|
|
964
|
+
if (typeof document === "undefined") return null;
|
|
965
|
+
if (state !== "running" || !currentStep || isHeld) return null;
|
|
966
|
+
const progressVisible = showProgress ?? config?.showProgress ?? true;
|
|
967
|
+
const isLast = currentIndex === total - 1;
|
|
968
|
+
if (config?.renderCoachmark) {
|
|
969
|
+
return (0, import_react_dom2.createPortal)(
|
|
970
|
+
config.renderCoachmark({
|
|
971
|
+
step: currentStep,
|
|
972
|
+
chapter: currentChapter,
|
|
973
|
+
index: currentIndex,
|
|
974
|
+
total,
|
|
975
|
+
context,
|
|
976
|
+
labels,
|
|
977
|
+
isHeld,
|
|
978
|
+
target,
|
|
979
|
+
hole,
|
|
980
|
+
placement,
|
|
981
|
+
cardSize,
|
|
982
|
+
missing,
|
|
983
|
+
next: tutor.next,
|
|
984
|
+
prev: tutor.prev,
|
|
985
|
+
skip: tutor.skip,
|
|
986
|
+
finish: tutor.finish
|
|
987
|
+
}),
|
|
988
|
+
document.body
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
return (0, import_react_dom2.createPortal)(
|
|
992
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
993
|
+
"div",
|
|
994
|
+
{
|
|
995
|
+
className: `tutorai-overlay${className ? ` ${className}` : ""}`,
|
|
996
|
+
style: { zIndex },
|
|
997
|
+
"data-test": "tutor-overlay",
|
|
998
|
+
children: [
|
|
999
|
+
hole && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1000
|
+
"div",
|
|
1001
|
+
{
|
|
1002
|
+
className: "tutorai-spotlight",
|
|
1003
|
+
"data-test": "tutor-spotlight",
|
|
1004
|
+
"aria-hidden": "true",
|
|
1005
|
+
style: {
|
|
1006
|
+
top: hole.top,
|
|
1007
|
+
left: hole.left,
|
|
1008
|
+
width: hole.width,
|
|
1009
|
+
height: hole.height
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
),
|
|
1013
|
+
progressVisible && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "tutorai-progress", "data-test": "tutor-progress", role: "status", children: progressText(labels.progress, currentIndex + 1, total) }),
|
|
1014
|
+
missing ? /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1015
|
+
"div",
|
|
1016
|
+
{
|
|
1017
|
+
className: "tutorai-notice",
|
|
1018
|
+
role: "status",
|
|
1019
|
+
"data-test": "tutor-missing-target",
|
|
1020
|
+
style: { left: cardPosition.left, top: cardPosition.top },
|
|
1021
|
+
children: [
|
|
1022
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: labels.missingTarget }),
|
|
1023
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1024
|
+
"button",
|
|
1025
|
+
{
|
|
1026
|
+
type: "button",
|
|
1027
|
+
className: "tutorai-button tutorai-primary",
|
|
1028
|
+
onClick: () => void tutor.next(),
|
|
1029
|
+
children: labels.next
|
|
1030
|
+
}
|
|
1031
|
+
)
|
|
1032
|
+
]
|
|
1033
|
+
}
|
|
1034
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1035
|
+
"div",
|
|
1036
|
+
{
|
|
1037
|
+
className: "tutorai-card",
|
|
1038
|
+
ref: cardRef,
|
|
1039
|
+
role: "dialog",
|
|
1040
|
+
"aria-label": currentStep.title,
|
|
1041
|
+
"data-test": "tutor-card",
|
|
1042
|
+
style: { left: cardPosition.left, top: cardPosition.top },
|
|
1043
|
+
children: [
|
|
1044
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "tutorai-card-header", children: [
|
|
1045
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("h3", { className: "tutorai-card-title", children: currentStep.title }),
|
|
1046
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1047
|
+
"button",
|
|
1048
|
+
{
|
|
1049
|
+
type: "button",
|
|
1050
|
+
className: "tutorai-icon-button",
|
|
1051
|
+
"aria-label": labels.skip,
|
|
1052
|
+
"data-test": "tutor-close",
|
|
1053
|
+
onClick: () => void tutor.skip(),
|
|
1054
|
+
children: "\xD7"
|
|
1055
|
+
}
|
|
1056
|
+
)
|
|
1057
|
+
] }),
|
|
1058
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "tutorai-card-content", children: currentStep.content }),
|
|
1059
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "tutorai-card-actions", children: [
|
|
1060
|
+
currentIndex > 0 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1061
|
+
"button",
|
|
1062
|
+
{
|
|
1063
|
+
type: "button",
|
|
1064
|
+
className: "tutorai-button",
|
|
1065
|
+
"data-test": "tutor-prev",
|
|
1066
|
+
onClick: () => void tutor.prev(),
|
|
1067
|
+
children: labels.prev
|
|
1068
|
+
}
|
|
1069
|
+
),
|
|
1070
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1071
|
+
"button",
|
|
1072
|
+
{
|
|
1073
|
+
type: "button",
|
|
1074
|
+
className: "tutorai-button tutorai-primary",
|
|
1075
|
+
"data-test": "tutor-next",
|
|
1076
|
+
onClick: () => void (isLast ? tutor.finish() : tutor.next()),
|
|
1077
|
+
children: isLast ? labels.finish : labels.next
|
|
1078
|
+
}
|
|
1079
|
+
)
|
|
1080
|
+
] })
|
|
1081
|
+
]
|
|
1082
|
+
}
|
|
1083
|
+
)
|
|
1084
|
+
]
|
|
1085
|
+
}
|
|
1086
|
+
),
|
|
1087
|
+
document.body
|
|
1088
|
+
);
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
// src/TourResumePill.tsx
|
|
1092
|
+
var import_react4 = require("react");
|
|
1093
|
+
var import_react_dom3 = require("react-dom");
|
|
1094
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
1095
|
+
function TourResumePill({
|
|
1096
|
+
labels: labelsProp,
|
|
1097
|
+
className
|
|
1098
|
+
}) {
|
|
1099
|
+
const tutor = useTutor();
|
|
1100
|
+
const labels = (0, import_react4.useMemo)(
|
|
1101
|
+
() => ({ ...tutor.labels, ...labelsProp }),
|
|
1102
|
+
[tutor.labels, labelsProp]
|
|
1103
|
+
);
|
|
1104
|
+
const progress = tutor.resumableTours[0];
|
|
1105
|
+
const enabled = tutor.config?.showResumePill ?? true;
|
|
1106
|
+
if (typeof document === "undefined") return null;
|
|
1107
|
+
if (!enabled || !progress) return null;
|
|
1108
|
+
if (tutor.state === "running" || tutor.state === "pending") return null;
|
|
1109
|
+
if (tutor.config?.renderResumePill) {
|
|
1110
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: tutor.config.renderResumePill({
|
|
1111
|
+
labels,
|
|
1112
|
+
progress,
|
|
1113
|
+
resume: tutor.resume,
|
|
1114
|
+
className
|
|
1115
|
+
}) });
|
|
1116
|
+
}
|
|
1117
|
+
return (0, import_react_dom3.createPortal)(
|
|
1118
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
1119
|
+
"button",
|
|
1120
|
+
{
|
|
1121
|
+
type: "button",
|
|
1122
|
+
className: `tutorai-resume-pill${className ? ` ${className}` : ""}`,
|
|
1123
|
+
title: labels.resumePillTitle,
|
|
1124
|
+
"aria-label": labels.resumePillTitle,
|
|
1125
|
+
"data-test": "tutor-resume-pill",
|
|
1126
|
+
onClick: () => void tutor.resume(),
|
|
1127
|
+
children: labels.resume
|
|
1128
|
+
}
|
|
1129
|
+
),
|
|
1130
|
+
document.body
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1134
|
+
0 && (module.exports = {
|
|
1135
|
+
DEFAULT_LABELS,
|
|
1136
|
+
TourResumePill,
|
|
1137
|
+
TutorProvider,
|
|
1138
|
+
TutorialGate,
|
|
1139
|
+
TutorialOverlay,
|
|
1140
|
+
createLocalStorage,
|
|
1141
|
+
holdTour,
|
|
1142
|
+
isTourHeld,
|
|
1143
|
+
noopStorage,
|
|
1144
|
+
releaseTour,
|
|
1145
|
+
resetHold,
|
|
1146
|
+
useTutor
|
|
1147
|
+
});
|
|
1148
|
+
//# sourceMappingURL=index.cjs.map
|