@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/dist/index.mjs CHANGED
@@ -306,6 +306,7 @@ function inertTour(tours, theme, labels) {
306
306
  var StepAction = /* @__PURE__ */ ((StepAction2) => {
307
307
  StepAction2["Click"] = "click";
308
308
  StepAction2["Type"] = "type";
309
+ StepAction2["Upload"] = "upload";
309
310
  StepAction2["None"] = "none";
310
311
  return StepAction2;
311
312
  })(StepAction || {});
@@ -378,23 +379,92 @@ function useRectTracking(phase, refreshRect) {
378
379
  }, [phase, refreshRect]);
379
380
  }
380
381
 
382
+ // src/enums/WritePolicy.ts
383
+ var WritePolicy = /* @__PURE__ */ ((WritePolicy2) => {
384
+ WritePolicy2["None"] = "none";
385
+ WritePolicy2["PlainSave"] = "plainSave";
386
+ WritePolicy2["RecipeWrites"] = "recipeWrites";
387
+ return WritePolicy2;
388
+ })(WritePolicy || {});
389
+
381
390
  // src/dom/forbidden.ts
382
- var WRITE_CONTROL = /^(save|submit|approve|reject|delete|reset|pay|send)\b/i;
391
+ var WRITE_CONTROL = /^(save|submit|approve|reject|delete|reset|pay|send|publish)\b/i;
383
392
  var PLAIN_SAVE = /^save$/i;
384
- function isForbiddenClick(name, allowSave) {
393
+ var RECIPE_WRITE = /^(save|submit|send|publish)( changes| draft)?$/i;
394
+ var DENIED_VERB = /\b(delete|remove|reset|pay|approve|reject|refund)\b/i;
395
+ function isForbiddenWrite(name, policy) {
385
396
  const clean = normalise(name);
386
397
  if (!WRITE_CONTROL.test(clean)) return false;
387
- return !(allowSave && PLAIN_SAVE.test(clean));
398
+ if (policy === "recipeWrites" /* RecipeWrites */) return DENIED_VERB.test(clean) || !RECIPE_WRITE.test(clean);
399
+ return !(policy === "plainSave" /* PlainSave */ && PLAIN_SAVE.test(clean));
400
+ }
401
+ function isForbiddenClick(name, allowSave) {
402
+ return isForbiddenWrite(name, allowSave ? "plainSave" /* PlainSave */ : "none" /* None */);
403
+ }
404
+ function writePolicyOf(tour, appAllowsWrites) {
405
+ if (!appAllowsWrites) return "none" /* None */;
406
+ if (tour.recipe === true && tour.allowWrites === true) return "recipeWrites" /* RecipeWrites */;
407
+ return tour.allowSave === true ? "plainSave" /* PlainSave */ : "none" /* None */;
388
408
  }
389
409
 
410
+ // src/dom/upload.ts
411
+ var FILE_INPUT = 'input[type="file"]';
412
+ var FALLBACK_NAME = "upload";
413
+ function fileInputOf(node) {
414
+ if (node.matches(FILE_INPUT)) return node;
415
+ return node.querySelector(FILE_INPUT) ?? node.parentElement?.querySelector(FILE_INPUT) ?? null;
416
+ }
417
+ function fileNameOf(url) {
418
+ const path = url.split(/[?#]/)[0] ?? "";
419
+ const last = path.split("/").pop() ?? "";
420
+ return last !== "" ? decodeURIComponent(last) : FALLBACK_NAME;
421
+ }
422
+ function fileListOf(file) {
423
+ if (typeof DataTransfer === "function") {
424
+ const transfer = new DataTransfer();
425
+ transfer.items.add(file);
426
+ return transfer.files;
427
+ }
428
+ const list = { 0: file, length: 1, item: (index) => index === 0 ? file : null };
429
+ return list;
430
+ }
431
+ async function fetchBlob(url, fetcher) {
432
+ try {
433
+ const response = await fetcher(url);
434
+ return response.ok ? await response.blob() : null;
435
+ } catch {
436
+ return null;
437
+ }
438
+ }
439
+ async function uploadFromUrl(input, url, fetcher) {
440
+ const blob = await fetchBlob(url, fetcher);
441
+ if (blob === null) return false;
442
+ const file = new File([blob], fileNameOf(url), { type: blob.type });
443
+ Object.defineProperty(input, "files", { value: fileListOf(file), configurable: true });
444
+ input.dispatchEvent(new Event("input", { bubbles: true }));
445
+ input.dispatchEvent(new Event("change", { bubbles: true }));
446
+ return true;
447
+ }
448
+ var browserFetch = (url) => fetch(url);
449
+
390
450
  // src/engine/runStep.ts
391
451
  var CANCELLED = { kind: "cancelled" /* Cancelled */ };
452
+ var MISSING = { kind: "missing" /* Missing */ };
392
453
  function ready(node, clicked) {
393
454
  return { kind: "ready" /* Ready */, node, clicked };
394
455
  }
456
+ async function upload(context, node) {
457
+ const input = fileInputOf(node);
458
+ if (input === null) return MISSING;
459
+ await context.moveCursor(centreOf(node));
460
+ if (context.isCancelled()) return CANCELLED;
461
+ const done = await uploadFromUrl(input, substitute(context.step.value ?? "", context.vars), context.fetchFile);
462
+ return done ? ready(node, false) : MISSING;
463
+ }
395
464
  async function perform(context, node) {
396
465
  const { step, mode } = context;
397
466
  const acts = mode !== "guide" /* Guide */;
467
+ if (step.action === "upload" /* Upload */) return acts ? upload(context, node) : ready(node, false);
398
468
  if (step.action === "type" /* Type */) {
399
469
  if (acts) await context.moveCursor(centreOf(node));
400
470
  await typeValue(node, substitute(step.value ?? "", context.vars), context.timing.typeMs);
@@ -402,7 +472,7 @@ async function perform(context, node) {
402
472
  }
403
473
  if (step.action !== "click" /* Click */ || !acts) return ready(node, false);
404
474
  const name = accessibleName(node);
405
- if (!isToggleControl(node) && isForbiddenClick(name, context.allowSave)) return { kind: "refused" /* Refused */, node, name };
475
+ if (!isToggleControl(node) && isForbiddenWrite(name, context.writePolicy)) return { kind: "refused" /* Refused */, node, name };
406
476
  const point = centreOf(node);
407
477
  await context.moveCursor(point);
408
478
  if (context.isCancelled()) return CANCELLED;
@@ -444,13 +514,15 @@ function useStepRunner(options) {
444
514
  }
445
515
  let cancelled = false;
446
516
  setView(LOADING_VIEW);
447
- const allowSave = latest.allowWrites && tourOf(latest.tours, active)?.allowSave === true;
517
+ const tour = tourOf(latest.tours, active);
518
+ const writePolicy = tour === null ? "none" /* None */ : writePolicyOf(tour, latest.allowWrites);
448
519
  void runStep({
449
520
  step,
450
521
  mode: active.mode,
451
522
  vars: latest.vars,
452
523
  timing: latest.timing,
453
- allowSave,
524
+ writePolicy,
525
+ fetchFile: browserFetch,
454
526
  navigate: latest.navigate,
455
527
  getPathname: latest.getPathname,
456
528
  isCancelled: () => cancelled,
@@ -1004,6 +1076,7 @@ function stepErrors(tourId, index, step) {
1004
1076
  if (!hasTarget(step.target)) errors.push(`${where}: missing target`);
1005
1077
  if (typeof step.text !== "string" || step.text.trim() === "") errors.push(`${where}: missing text`);
1006
1078
  if (step.action === "type" /* Type */ && (step.value ?? "") === "") errors.push(`${where}: type step needs a value`);
1079
+ if (step.action === "upload" /* Upload */ && (step.value ?? "").trim() === "") errors.push(`${where}: upload step needs a url value`);
1007
1080
  return errors;
1008
1081
  }
1009
1082
  function validateTours(tours) {
@@ -1015,11 +1088,12 @@ function validateTours(tours) {
1015
1088
  seen.add(id);
1016
1089
  const steps = Array.isArray(tour.steps) ? tour.steps : [];
1017
1090
  if (steps.length === 0) errors.push(`tour ${id}: has no steps`);
1091
+ if (tour.allowWrites === true && tour.recipe !== true) errors.push(`tour ${id}: allowWrites needs recipe: true`);
1018
1092
  steps.forEach((step, stepIndex) => errors.push(...stepErrors(id, stepIndex, step)));
1019
1093
  });
1020
1094
  return { valid: errors.length === 0, errors };
1021
1095
  }
1022
1096
 
1023
- export { DEFAULT_TIMING, FirstRunOffer, StepAction, StepPhase, TargetRole, TourEventType, TourMode, TourPicker, TourProvider, defaultTourLabels, findTarget, fontStack, formatLabel, isForbiddenClick, useTour, validateTours };
1097
+ export { DEFAULT_TIMING, FirstRunOffer, StepAction, StepPhase, TargetRole, TourEventType, TourMode, TourPicker, TourProvider, WritePolicy, defaultTourLabels, findTarget, fontStack, formatLabel, isForbiddenClick, isForbiddenWrite, useTour, validateTours, writePolicyOf };
1024
1098
  //# sourceMappingURL=index.mjs.map
1025
1099
  //# sourceMappingURL=index.mjs.map