@dloizides/ui-tour 1.2.3 → 1.4.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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+
5
+ - `StepAction.Upload`: in Show and Auto modes the step fetches its `value` URL (`{var}` filled) and lands the file in the target's file input, then fires `input` and `change`. `validateTours` requires a URL value.
6
+ - Recipe tours: `recipe: true` with `allowWrites: true` may click a control named exactly Save, Save changes, Save draft, Submit, Send or Publish (when the provider passes `allowWrites`); a name holding delete, remove, reset, pay, approve, reject or refund is refused; Delete, Reset, Reject, Approve and Pay stay blocked. `allowWrites` without `recipe` is a `validateTours` / `expectValidTours` error.
7
+ - Publish is now a write control: no tour except a recipe tour clicks it.
8
+ - New exports: `WritePolicy`, `isForbiddenWrite`, `writePolicyOf`; `isForbiddenClick` keeps its signature.
9
+
3
10
  ## 1.2.3
4
11
 
5
12
  - `toTourTheme` dark accent is the first of primary 300, 400, 500, base that exists, then the text colour. Fixes kefi dark (toUiTheme primary has only 100/500/700) showing a cream Next button and ring instead of coral.
package/README.md CHANGED
@@ -6,7 +6,7 @@ In-app guided tours for React Native Web apps. Three modes:
6
6
  - **Show me**: a themed cursor moves to the target and clicks or types by itself, then waits for Next.
7
7
  - **Auto**: like Show me, and advances on its own every 2 s; Space pauses.
8
8
 
9
- Write controls (Save, Submit, Approve, Reject, Delete, Reset, Pay, Send) are never clicked. Only a tour flagged `allowSave`, in an app that passes `allowWrites`, may press a plain "Save".
9
+ Write controls (Save, Submit, Approve, Reject, Delete, Reset, Pay, Send, Publish) are never clicked. Only a tour flagged `allowSave`, in an app that passes `allowWrites`, may press a plain "Save"; only a `recipe` tour with `allowWrites` may press Save, Submit, Send and Publish.
10
10
 
11
11
  Web only. On native (`Platform.OS !== 'web'`) the provider renders its children and the tour API does nothing.
12
12
 
@@ -113,14 +113,18 @@ import { TOURS } from '../tours';
113
113
  |---|---|
114
114
  | `target` | A testID (`open-payees`) or `{ role, name, nth?, exact?, within? }` |
115
115
  | `text` | Translation key of the caption text; `{name}` placeholders come from `vars` |
116
- | `action` | `StepAction.Click`, `Type` (needs `value`) or `None` |
116
+ | `action` | `StepAction.Click`, `Type` (needs `value`), `Upload` (needs a URL `value`, `{var}` filled) or `None` |
117
117
  | `route?` | Navigates there first; `{name}` placeholders come from `vars` (`/t/{tenantSlug}/register`) |
118
118
  | `optional?` | Skipped when the target is absent |
119
119
  | `wait?` | Extra wait in ms before the step |
120
120
 
121
121
  ### Forbidden actions
122
122
 
123
- A click step whose target name starts with Save, Submit, Approve, Reject, Delete, Reset, Pay or Send is never performed. A plain "Save" is allowed only with `allowSave: true` on the tour and `allowWrites` on the provider. Switches, checkboxes and radios are settings, not writes.
123
+ A click step whose target name starts with Save, Submit, Approve, Reject, Delete, Reset, Pay, Send or Publish is never performed. A plain "Save" is allowed only with `allowSave: true` on the tour and `allowWrites` on the provider. A tour with `recipe: true` and `allowWrites: true` (it builds data from scratch on a throwaway tenant) may also press a control named exactly Save, Save changes, Save draft, Submit, Send or Publish when the provider passes `allowWrites` (a name such as "Save and delete" is refused); Delete, Reset, Reject, Approve and Pay stay blocked. `allowWrites` on a tour without `recipe` is a validation error.
124
+
125
+ ### Upload steps
126
+
127
+ In Show and Auto modes an `Upload` step fetches its `value` URL and puts the file in the target's file input (the target itself, one inside it, or one beside it), then fires `input` and `change` so the app's own upload flow runs. A failed fetch or no file input marks the step unavailable. In Guide mode the user picks the file and presses Next. Switches, checkboxes and radios are settings, not writes.
124
128
 
125
129
  ### How to test
126
130
 
package/dist/index.d.mts CHANGED
@@ -34,6 +34,7 @@ declare function formatLabel(template: string, ...params: Array<string | number>
34
34
  declare enum StepAction {
35
35
  Click = "click",
36
36
  Type = "type",
37
+ Upload = "upload",
37
38
  None = "none"
38
39
  }
39
40
 
@@ -88,6 +89,8 @@ interface Tour {
88
89
  category: string;
89
90
  persona?: string;
90
91
  allowSave?: boolean;
92
+ recipe?: boolean;
93
+ allowWrites?: boolean;
91
94
  steps: TourStep[];
92
95
  }
93
96
  interface TourEvent {
@@ -204,12 +207,22 @@ declare function validateTours(tours: readonly Tour[]): TourValidation;
204
207
  /** Finds the first visible element for a step target: a testID string or a role and accessible name. */
205
208
  declare function findTarget(target: StepTarget, vars?: TourVars): Element | null;
206
209
 
210
+ declare enum WritePolicy {
211
+ None = "none",
212
+ PlainSave = "plainSave",
213
+ RecipeWrites = "recipeWrites"
214
+ }
215
+
216
+ /** True when a control's accessible name marks it as a write action the tour may not click under `policy`. */
217
+ declare function isForbiddenWrite(name: string, policy: WritePolicy): boolean;
207
218
  /** True when a control's accessible name marks it as a write action the tour must never click. */
208
219
  declare function isForbiddenClick(name: string, allowSave: boolean): boolean;
220
+ /** The write policy of a tour; `appAllowsWrites` is the provider's `allowWrites`, without it nothing is written. */
221
+ declare function writePolicyOf(tour: Tour, appAllowsWrites: boolean): WritePolicy;
209
222
 
210
223
  declare const DEFAULT_TIMING: TourTiming;
211
224
 
212
225
  /** Font stack that always ends in a generic family, so a missing web font falls back to sans-serif. */
213
226
  declare function fontStack(family: string | null | undefined): string;
214
227
 
215
- export { type ActiveTour, DEFAULT_TIMING, FirstRunOffer, type FirstRunOfferProps, type Point, type RoleTarget, StepAction, StepPhase, type StepTarget, type StepView, type TargetRect, TargetRole, type Tour, type TourContextValue, type TourEvent, TourEventType, type TourLabels, TourMode, TourPicker, TourProvider, type TourProviderProps, type TourStep, type TourTheme, type TourTiming, type TourUser, type TourValidation, type TourVars, defaultTourLabels, findTarget, fontStack, formatLabel, isForbiddenClick, useTour, validateTours };
228
+ export { type ActiveTour, DEFAULT_TIMING, FirstRunOffer, type FirstRunOfferProps, type Point, type RoleTarget, StepAction, StepPhase, type StepTarget, type StepView, type TargetRect, TargetRole, type Tour, type TourContextValue, type TourEvent, TourEventType, type TourLabels, TourMode, TourPicker, TourProvider, type TourProviderProps, type TourStep, type TourTheme, type TourTiming, type TourUser, type TourValidation, type TourVars, WritePolicy, defaultTourLabels, findTarget, fontStack, formatLabel, isForbiddenClick, isForbiddenWrite, useTour, validateTours, writePolicyOf };
package/dist/index.d.ts CHANGED
@@ -34,6 +34,7 @@ declare function formatLabel(template: string, ...params: Array<string | number>
34
34
  declare enum StepAction {
35
35
  Click = "click",
36
36
  Type = "type",
37
+ Upload = "upload",
37
38
  None = "none"
38
39
  }
39
40
 
@@ -88,6 +89,8 @@ interface Tour {
88
89
  category: string;
89
90
  persona?: string;
90
91
  allowSave?: boolean;
92
+ recipe?: boolean;
93
+ allowWrites?: boolean;
91
94
  steps: TourStep[];
92
95
  }
93
96
  interface TourEvent {
@@ -204,12 +207,22 @@ declare function validateTours(tours: readonly Tour[]): TourValidation;
204
207
  /** Finds the first visible element for a step target: a testID string or a role and accessible name. */
205
208
  declare function findTarget(target: StepTarget, vars?: TourVars): Element | null;
206
209
 
210
+ declare enum WritePolicy {
211
+ None = "none",
212
+ PlainSave = "plainSave",
213
+ RecipeWrites = "recipeWrites"
214
+ }
215
+
216
+ /** True when a control's accessible name marks it as a write action the tour may not click under `policy`. */
217
+ declare function isForbiddenWrite(name: string, policy: WritePolicy): boolean;
207
218
  /** True when a control's accessible name marks it as a write action the tour must never click. */
208
219
  declare function isForbiddenClick(name: string, allowSave: boolean): boolean;
220
+ /** The write policy of a tour; `appAllowsWrites` is the provider's `allowWrites`, without it nothing is written. */
221
+ declare function writePolicyOf(tour: Tour, appAllowsWrites: boolean): WritePolicy;
209
222
 
210
223
  declare const DEFAULT_TIMING: TourTiming;
211
224
 
212
225
  /** Font stack that always ends in a generic family, so a missing web font falls back to sans-serif. */
213
226
  declare function fontStack(family: string | null | undefined): string;
214
227
 
215
- export { type ActiveTour, DEFAULT_TIMING, FirstRunOffer, type FirstRunOfferProps, type Point, type RoleTarget, StepAction, StepPhase, type StepTarget, type StepView, type TargetRect, TargetRole, type Tour, type TourContextValue, type TourEvent, TourEventType, type TourLabels, TourMode, TourPicker, TourProvider, type TourProviderProps, type TourStep, type TourTheme, type TourTiming, type TourUser, type TourValidation, type TourVars, defaultTourLabels, findTarget, fontStack, formatLabel, isForbiddenClick, useTour, validateTours };
228
+ export { type ActiveTour, DEFAULT_TIMING, FirstRunOffer, type FirstRunOfferProps, type Point, type RoleTarget, StepAction, StepPhase, type StepTarget, type StepView, type TargetRect, TargetRole, type Tour, type TourContextValue, type TourEvent, TourEventType, type TourLabels, TourMode, TourPicker, TourProvider, type TourProviderProps, type TourStep, type TourTheme, type TourTiming, type TourUser, type TourValidation, type TourVars, WritePolicy, defaultTourLabels, findTarget, fontStack, formatLabel, isForbiddenClick, isForbiddenWrite, useTour, validateTours, writePolicyOf };
package/dist/index.js CHANGED
@@ -308,6 +308,7 @@ function inertTour(tours, theme, labels) {
308
308
  var StepAction = /* @__PURE__ */ ((StepAction2) => {
309
309
  StepAction2["Click"] = "click";
310
310
  StepAction2["Type"] = "type";
311
+ StepAction2["Upload"] = "upload";
311
312
  StepAction2["None"] = "none";
312
313
  return StepAction2;
313
314
  })(StepAction || {});
@@ -380,23 +381,92 @@ function useRectTracking(phase, refreshRect) {
380
381
  }, [phase, refreshRect]);
381
382
  }
382
383
 
384
+ // src/enums/WritePolicy.ts
385
+ var WritePolicy = /* @__PURE__ */ ((WritePolicy2) => {
386
+ WritePolicy2["None"] = "none";
387
+ WritePolicy2["PlainSave"] = "plainSave";
388
+ WritePolicy2["RecipeWrites"] = "recipeWrites";
389
+ return WritePolicy2;
390
+ })(WritePolicy || {});
391
+
383
392
  // src/dom/forbidden.ts
384
- var WRITE_CONTROL = /^(save|submit|approve|reject|delete|reset|pay|send)\b/i;
393
+ var WRITE_CONTROL = /^(save|submit|approve|reject|delete|reset|pay|send|publish)\b/i;
385
394
  var PLAIN_SAVE = /^save$/i;
386
- function isForbiddenClick(name, allowSave) {
395
+ var RECIPE_WRITE = /^(save|submit|send|publish)( changes| draft)?$/i;
396
+ var DENIED_VERB = /\b(delete|remove|reset|pay|approve|reject|refund)\b/i;
397
+ function isForbiddenWrite(name, policy) {
387
398
  const clean = normalise(name);
388
399
  if (!WRITE_CONTROL.test(clean)) return false;
389
- return !(allowSave && PLAIN_SAVE.test(clean));
400
+ if (policy === "recipeWrites" /* RecipeWrites */) return DENIED_VERB.test(clean) || !RECIPE_WRITE.test(clean);
401
+ return !(policy === "plainSave" /* PlainSave */ && PLAIN_SAVE.test(clean));
402
+ }
403
+ function isForbiddenClick(name, allowSave) {
404
+ return isForbiddenWrite(name, allowSave ? "plainSave" /* PlainSave */ : "none" /* None */);
405
+ }
406
+ function writePolicyOf(tour, appAllowsWrites) {
407
+ if (!appAllowsWrites) return "none" /* None */;
408
+ if (tour.recipe === true && tour.allowWrites === true) return "recipeWrites" /* RecipeWrites */;
409
+ return tour.allowSave === true ? "plainSave" /* PlainSave */ : "none" /* None */;
390
410
  }
391
411
 
412
+ // src/dom/upload.ts
413
+ var FILE_INPUT = 'input[type="file"]';
414
+ var FALLBACK_NAME = "upload";
415
+ function fileInputOf(node) {
416
+ if (node.matches(FILE_INPUT)) return node;
417
+ return node.querySelector(FILE_INPUT) ?? node.parentElement?.querySelector(FILE_INPUT) ?? null;
418
+ }
419
+ function fileNameOf(url) {
420
+ const path = url.split(/[?#]/)[0] ?? "";
421
+ const last = path.split("/").pop() ?? "";
422
+ return last !== "" ? decodeURIComponent(last) : FALLBACK_NAME;
423
+ }
424
+ function fileListOf(file) {
425
+ if (typeof DataTransfer === "function") {
426
+ const transfer = new DataTransfer();
427
+ transfer.items.add(file);
428
+ return transfer.files;
429
+ }
430
+ const list = { 0: file, length: 1, item: (index) => index === 0 ? file : null };
431
+ return list;
432
+ }
433
+ async function fetchBlob(url, fetcher) {
434
+ try {
435
+ const response = await fetcher(url);
436
+ return response.ok ? await response.blob() : null;
437
+ } catch {
438
+ return null;
439
+ }
440
+ }
441
+ async function uploadFromUrl(input, url, fetcher) {
442
+ const blob = await fetchBlob(url, fetcher);
443
+ if (blob === null) return false;
444
+ const file = new File([blob], fileNameOf(url), { type: blob.type });
445
+ Object.defineProperty(input, "files", { value: fileListOf(file), configurable: true });
446
+ input.dispatchEvent(new Event("input", { bubbles: true }));
447
+ input.dispatchEvent(new Event("change", { bubbles: true }));
448
+ return true;
449
+ }
450
+ var browserFetch = (url) => fetch(url);
451
+
392
452
  // src/engine/runStep.ts
393
453
  var CANCELLED = { kind: "cancelled" /* Cancelled */ };
454
+ var MISSING = { kind: "missing" /* Missing */ };
394
455
  function ready(node, clicked) {
395
456
  return { kind: "ready" /* Ready */, node, clicked };
396
457
  }
458
+ async function upload(context, node) {
459
+ const input = fileInputOf(node);
460
+ if (input === null) return MISSING;
461
+ await context.moveCursor(centreOf(node));
462
+ if (context.isCancelled()) return CANCELLED;
463
+ const done = await uploadFromUrl(input, substitute(context.step.value ?? "", context.vars), context.fetchFile);
464
+ return done ? ready(node, false) : MISSING;
465
+ }
397
466
  async function perform(context, node) {
398
467
  const { step, mode } = context;
399
468
  const acts = mode !== "guide" /* Guide */;
469
+ if (step.action === "upload" /* Upload */) return acts ? upload(context, node) : ready(node, false);
400
470
  if (step.action === "type" /* Type */) {
401
471
  if (acts) await context.moveCursor(centreOf(node));
402
472
  await typeValue(node, substitute(step.value ?? "", context.vars), context.timing.typeMs);
@@ -404,7 +474,7 @@ async function perform(context, node) {
404
474
  }
405
475
  if (step.action !== "click" /* Click */ || !acts) return ready(node, false);
406
476
  const name = accessibleName(node);
407
- if (!isToggleControl(node) && isForbiddenClick(name, context.allowSave)) return { kind: "refused" /* Refused */, node, name };
477
+ if (!isToggleControl(node) && isForbiddenWrite(name, context.writePolicy)) return { kind: "refused" /* Refused */, node, name };
408
478
  const point = centreOf(node);
409
479
  await context.moveCursor(point);
410
480
  if (context.isCancelled()) return CANCELLED;
@@ -446,13 +516,15 @@ function useStepRunner(options) {
446
516
  }
447
517
  let cancelled = false;
448
518
  setView(LOADING_VIEW);
449
- const allowSave = latest.allowWrites && tourOf(latest.tours, active)?.allowSave === true;
519
+ const tour = tourOf(latest.tours, active);
520
+ const writePolicy = tour === null ? "none" /* None */ : writePolicyOf(tour, latest.allowWrites);
450
521
  void runStep({
451
522
  step,
452
523
  mode: active.mode,
453
524
  vars: latest.vars,
454
525
  timing: latest.timing,
455
- allowSave,
526
+ writePolicy,
527
+ fetchFile: browserFetch,
456
528
  navigate: latest.navigate,
457
529
  getPathname: latest.getPathname,
458
530
  isCancelled: () => cancelled,
@@ -1006,6 +1078,7 @@ function stepErrors(tourId, index, step) {
1006
1078
  if (!hasTarget(step.target)) errors.push(`${where}: missing target`);
1007
1079
  if (typeof step.text !== "string" || step.text.trim() === "") errors.push(`${where}: missing text`);
1008
1080
  if (step.action === "type" /* Type */ && (step.value ?? "") === "") errors.push(`${where}: type step needs a value`);
1081
+ if (step.action === "upload" /* Upload */ && (step.value ?? "").trim() === "") errors.push(`${where}: upload step needs a url value`);
1009
1082
  return errors;
1010
1083
  }
1011
1084
  function validateTours(tours) {
@@ -1017,6 +1090,7 @@ function validateTours(tours) {
1017
1090
  seen.add(id);
1018
1091
  const steps = Array.isArray(tour.steps) ? tour.steps : [];
1019
1092
  if (steps.length === 0) errors.push(`tour ${id}: has no steps`);
1093
+ if (tour.allowWrites === true && tour.recipe !== true) errors.push(`tour ${id}: allowWrites needs recipe: true`);
1020
1094
  steps.forEach((step, stepIndex) => errors.push(...stepErrors(id, stepIndex, step)));
1021
1095
  });
1022
1096
  return { valid: errors.length === 0, errors };
@@ -1031,12 +1105,15 @@ exports.TourEventType = TourEventType;
1031
1105
  exports.TourMode = TourMode;
1032
1106
  exports.TourPicker = TourPicker;
1033
1107
  exports.TourProvider = TourProvider;
1108
+ exports.WritePolicy = WritePolicy;
1034
1109
  exports.defaultTourLabels = defaultTourLabels;
1035
1110
  exports.findTarget = findTarget;
1036
1111
  exports.fontStack = fontStack;
1037
1112
  exports.formatLabel = formatLabel;
1038
1113
  exports.isForbiddenClick = isForbiddenClick;
1114
+ exports.isForbiddenWrite = isForbiddenWrite;
1039
1115
  exports.useTour = useTour;
1040
1116
  exports.validateTours = validateTours;
1117
+ exports.writePolicyOf = writePolicyOf;
1041
1118
  //# sourceMappingURL=index.js.map
1042
1119
  //# sourceMappingURL=index.js.map