@pisell/materials 1.10.32 → 1.10.34

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.
@@ -6,7 +6,23 @@ const require_utils = require("../utils.js");
6
6
  let react = require("react");
7
7
  //#region src/components/PisellSelectionFlow/hooks/useFlowNavigation.ts
8
8
  /** 导航、校验与提交门禁独立于模块加载生命周期。 */
9
- const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mountedRef, moduleStateRef, moduleRevisionRef, draftRevisionRef, flowGenerationRef, moduleHandlesRef, configurationErrorRef, compiledStepsRef, currentStepIndexRef, currentStepKeyRef, validatedCompletionRef, navigationRevisionRef, preparationRevisionRef, transitionPendingRef, completedRef, completionPromiseRef, isCompletingRef, draftRef, onCompleteRef, ensureModuleReady, commitModuleState, getFreshContext, onValidationFailed, setCurrentStepIndex, setCompletionError, setCompletionReason, setIsCompleting }) => {
9
+ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mountedRef, moduleStateRef, moduleRevisionRef, moduleInvalidationRef, draftRevisionRef, flowGenerationRef, moduleHandlesRef, configurationErrorRef, compiledStepsRef, currentStepIndexRef, currentStepKeyRef, validatedCompletionRef, navigationRevisionRef, preparationRevisionRef, transitionPendingRef, completedRef, completionPromiseRef, isCompletingRef, draftRef, onCompleteRef, ensureModuleReady, commitModuleState, getFreshContext, onValidationFailed, setCurrentStepIndex, setCompletionError, setCompletionReason, setIsCompleting }) => {
10
+ const navigationDirectionRef = (0, react.useRef)("forward");
11
+ const isStepSkippable = (0, react.useCallback)((stepIndex) => {
12
+ const step = compiledStepsRef.current[stepIndex];
13
+ const context = getFreshContext();
14
+ if (!(step === null || step === void 0 ? void 0 : step.modules.length) || !context) return false;
15
+ return step.modules.every((module) => {
16
+ var _moduleStateRef$curre;
17
+ if (((_moduleStateRef$curre = moduleStateRef.current[module.key]) === null || _moduleStateRef$curre === void 0 ? void 0 : _moduleStateRef$curre.status) !== "ready") return false;
18
+ try {
19
+ var _resolveFlowModuleAda, _resolveFlowModuleAda2;
20
+ return ((_resolveFlowModuleAda = require_compiler.resolveFlowModuleAdapter(registry, module.type)) === null || _resolveFlowModuleAda === void 0 || (_resolveFlowModuleAda2 = _resolveFlowModuleAda.isSkippable) === null || _resolveFlowModuleAda2 === void 0 ? void 0 : _resolveFlowModuleAda2.call(_resolveFlowModuleAda, context, module)) === true;
21
+ } catch (_unused) {
22
+ return false;
23
+ }
24
+ });
25
+ }, [getFreshContext, registry]);
10
26
  const validateModule = (0, react.useCallback)(function() {
11
27
  var _ref = require_asyncToGenerator._asyncToGenerator(function* (module) {
12
28
  const adapter = require_compiler.resolveFlowModuleAdapter(registry, module.type);
@@ -181,16 +197,16 @@ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mounted
181
197
  }
182
198
  }
183
199
  if (!targetModules.find(({ module }) => {
184
- var _moduleStateRef$curre;
185
- return ((_moduleStateRef$curre = moduleStateRef.current[module.key]) === null || _moduleStateRef$curre === void 0 ? void 0 : _moduleStateRef$curre.status) !== "ready";
200
+ var _moduleStateRef$curre2;
201
+ return ((_moduleStateRef$curre2 = moduleStateRef.current[module.key]) === null || _moduleStateRef$curre2 === void 0 ? void 0 : _moduleStateRef$curre2.status) !== "ready";
186
202
  })) return isCurrentPreparation() ? { prepared: true } : {
187
203
  prepared: false,
188
204
  cancelled: true
189
205
  };
190
206
  }
191
207
  const unresolved = targetModules.find(({ module }) => {
192
- var _moduleStateRef$curre2;
193
- return ((_moduleStateRef$curre2 = moduleStateRef.current[module.key]) === null || _moduleStateRef$curre2 === void 0 ? void 0 : _moduleStateRef$curre2.status) !== "ready";
208
+ var _moduleStateRef$curre3;
209
+ return ((_moduleStateRef$curre3 = moduleStateRef.current[module.key]) === null || _moduleStateRef$curre3 === void 0 ? void 0 : _moduleStateRef$curre3.status) !== "ready";
194
210
  });
195
211
  return unresolved ? {
196
212
  prepared: false,
@@ -258,19 +274,109 @@ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mounted
258
274
  onValidationFailed,
259
275
  registry
260
276
  ]);
277
+ const revalidateModuleBeforeComplete = (0, react.useCallback)(function() {
278
+ var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (module, expectedDraftRevision) {
279
+ const invalidation = moduleInvalidationRef.current.get(module.key);
280
+ const adapter = require_compiler.resolveFlowModuleAdapter(registry, module.type);
281
+ if (!(invalidation === null || invalidation === void 0 ? void 0 : invalidation.wasReady) || !(adapter === null || adapter === void 0 ? void 0 : adapter.canRevalidateBeforeComplete) || !adapter.validateAutoResolution) return false;
282
+ const moduleRevision = moduleRevisionRef.current.get(module.key) || 0;
283
+ const flowGeneration = flowGenerationRef.current;
284
+ const navigationRevision = navigationRevisionRef.current;
285
+ const compiledFingerprint = require_utils.getCompiledStepsFingerprint(compiledStepsRef.current);
286
+ const isCurrentRun = () => mountedRef.current && !configurationErrorRef.current && flowGenerationRef.current === flowGeneration && navigationRevisionRef.current === navigationRevision && (moduleRevisionRef.current.get(module.key) || 0) === moduleRevision && require_utils.getCompiledStepsFingerprint(compiledStepsRef.current) === compiledFingerprint;
287
+ const isCurrentSelection = () => isCurrentRun() && draftRevisionRef.current === expectedDraftRevision;
288
+ const requireReviewIfCurrent = () => {
289
+ if (isCurrentRun()) commitModuleState(module.key, {
290
+ status: "stale",
291
+ reason: createRuntimeIssue("selection-changed-validation")
292
+ });
293
+ };
294
+ try {
295
+ const initialContext = getFreshContext();
296
+ if (!initialContext || !isCurrentSelection() || !adapter.canRevalidateBeforeComplete(initialContext, invalidation.change, module)) return false;
297
+ if (!(yield ensureModuleReady(module))) return false;
298
+ if (!isCurrentSelection()) {
299
+ requireReviewIfCurrent();
300
+ return false;
301
+ }
302
+ const context = getFreshContext();
303
+ if (!context) return false;
304
+ const validationContext = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, context), {}, {
305
+ updateDraft: (update, meta) => {
306
+ if (isCurrentSelection()) context.updateDraft(update, meta);
307
+ },
308
+ setModuleState: (moduleKey, state) => {
309
+ if (isCurrentSelection()) context.setModuleState(moduleKey, state);
310
+ },
311
+ setModuleStatus: (moduleKey, status, message, error) => {
312
+ if (isCurrentSelection()) context.setModuleStatus(moduleKey, status, message, error);
313
+ },
314
+ setModuleData: (moduleKey, value) => {
315
+ if (isCurrentSelection()) context.setModuleData(moduleKey, value);
316
+ },
317
+ clearModuleData: (moduleKey) => {
318
+ if (isCurrentSelection()) context.clearModuleData(moduleKey);
319
+ }
320
+ });
321
+ const result = require_utils.normalizeFlowValidationResult(yield adapter.validateAutoResolution(validationContext, module));
322
+ if (!isCurrentSelection()) {
323
+ requireReviewIfCurrent();
324
+ return false;
325
+ }
326
+ if (!result.valid) {
327
+ commitModuleState(module.key, {
328
+ status: "error",
329
+ message: result.message,
330
+ reason: result.message ? void 0 : createRuntimeIssue("module-validation-failed")
331
+ });
332
+ return false;
333
+ }
334
+ commitModuleState(module.key, { status: "ready" });
335
+ return true;
336
+ } catch (error) {
337
+ if (isCurrentSelection()) commitModuleState(module.key, {
338
+ status: "error",
339
+ message: require_utils.getSelectionFlowErrorMessage(error),
340
+ error
341
+ });
342
+ else requireReviewIfCurrent();
343
+ return false;
344
+ }
345
+ });
346
+ return function(_x2, _x3) {
347
+ return _ref2.apply(this, arguments);
348
+ };
349
+ }(), [
350
+ commitModuleState,
351
+ createRuntimeIssue,
352
+ ensureModuleReady,
353
+ getFreshContext,
354
+ registry
355
+ ]);
261
356
  const validateFlowForCompletion = (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
262
357
  validatedCompletionRef.current = null;
263
358
  if (configurationErrorRef.current) return false;
264
359
  const currentIndex = currentStepIndexRef.current;
265
360
  const allSteps = compiledStepsRef.current;
266
- const compiledFingerprint = require_utils.getCompiledStepsFingerprint(allSteps);
267
- const revisionFingerprint = allSteps.flatMap((step) => step.modules).map((module) => `${module.key}:${moduleRevisionRef.current.get(module.key) || 0}`).join("|");
268
- const draftRevision = draftRevisionRef.current;
361
+ const preparationFingerprint = require_utils.getCompiledStepsFingerprint(allSteps);
362
+ const preparationGeneration = flowGenerationRef.current;
363
+ const preparationNavigation = navigationRevisionRef.current;
364
+ const preparationDraftRevision = draftRevisionRef.current;
365
+ const isCurrentPreparation = () => mountedRef.current && !configurationErrorRef.current && flowGenerationRef.current === preparationGeneration && navigationRevisionRef.current === preparationNavigation && currentStepIndexRef.current === currentIndex && require_utils.getCompiledStepsFingerprint(compiledStepsRef.current) === preparationFingerprint;
269
366
  for (const step of allSteps) {
270
367
  if (step.index >= currentIndex) break;
271
368
  for (const module of step.modules) {
272
- const state = moduleStateRef.current[module.key];
369
+ let state = moduleStateRef.current[module.key];
273
370
  if ((state === null || state === void 0 ? void 0 : state.status) !== "ready") {
371
+ const revalidationModuleRevision = moduleRevisionRef.current.get(module.key) || 0;
372
+ const revalidated = yield revalidateModuleBeforeComplete(module, preparationDraftRevision);
373
+ if (!isCurrentPreparation()) return false;
374
+ if (revalidated && draftRevisionRef.current === preparationDraftRevision) continue;
375
+ if (revalidated && (moduleRevisionRef.current.get(module.key) || 0) === revalidationModuleRevision) commitModuleState(module.key, {
376
+ status: "stale",
377
+ reason: createRuntimeIssue("selection-changed-validation")
378
+ });
379
+ state = moduleStateRef.current[module.key];
274
380
  currentStepIndexRef.current = step.index;
275
381
  currentStepKeyRef.current = step.key;
276
382
  setCurrentStepIndex(step.index);
@@ -282,6 +388,9 @@ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mounted
282
388
  }
283
389
  }
284
390
  }
391
+ const compiledFingerprint = require_utils.getCompiledStepsFingerprint(allSteps);
392
+ const revisionFingerprint = allSteps.flatMap((step) => step.modules).map((module) => `${module.key}:${moduleRevisionRef.current.get(module.key) || 0}`).join("|");
393
+ const draftRevision = draftRevisionRef.current;
285
394
  if (!(yield validateCurrentStep())) return false;
286
395
  if (configurationErrorRef.current) return false;
287
396
  const latestSteps = compiledStepsRef.current;
@@ -289,8 +398,8 @@ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mounted
289
398
  const latestRevisionFingerprint = latestSteps.flatMap((step) => step.modules).map((module) => `${module.key}:${moduleRevisionRef.current.get(module.key) || 0}`).join("|");
290
399
  if (compiledFingerprint !== latestCompiledFingerprint || revisionFingerprint !== latestRevisionFingerprint || draftRevision !== draftRevisionRef.current) {
291
400
  const targetStep = latestSteps.find((step) => step.modules.some((module) => {
292
- var _moduleStateRef$curre3;
293
- return ((_moduleStateRef$curre3 = moduleStateRef.current[module.key]) === null || _moduleStateRef$curre3 === void 0 ? void 0 : _moduleStateRef$curre3.status) !== "ready";
401
+ var _moduleStateRef$curre4;
402
+ return ((_moduleStateRef$curre4 = moduleStateRef.current[module.key]) === null || _moduleStateRef$curre4 === void 0 ? void 0 : _moduleStateRef$curre4.status) !== "ready";
294
403
  })) || latestSteps[0];
295
404
  const targetModule = targetStep === null || targetStep === void 0 ? void 0 : targetStep.modules[0];
296
405
  if (targetStep && targetModule) {
@@ -320,8 +429,10 @@ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mounted
320
429
  };
321
430
  return true;
322
431
  }), [
432
+ commitModuleState,
323
433
  createRuntimeIssue,
324
434
  onValidationFailed,
435
+ revalidateModuleBeforeComplete,
325
436
  validateCurrentStep
326
437
  ]);
327
438
  const complete = (0, react.useCallback)(() => {
@@ -329,6 +440,7 @@ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mounted
329
440
  if (completedRef.current) return Promise.resolve(true);
330
441
  if (completionPromiseRef.current) return completionPromiseRef.current;
331
442
  if (transitionPendingRef.current) return Promise.resolve(false);
443
+ navigationDirectionRef.current = "forward";
332
444
  transitionPendingRef.current = true;
333
445
  const navigationRevision = navigationRevisionRef.current + 1;
334
446
  navigationRevisionRef.current = navigationRevision;
@@ -383,6 +495,7 @@ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mounted
383
495
  const currentIndex = currentStepIndexRef.current;
384
496
  const allSteps = compiledStepsRef.current;
385
497
  if (!allSteps[currentIndex]) return false;
498
+ navigationDirectionRef.current = "forward";
386
499
  if (currentIndex >= allSteps.length - 1) return complete();
387
500
  const currentStepKey = allSteps[currentIndex].key;
388
501
  const navigationRevision = navigationRevisionRef.current + 1;
@@ -409,13 +522,18 @@ const useFlowNavigation = ({ registry, draftAdapter, createRuntimeIssue, mounted
409
522
  back: (0, react.useCallback)(() => {
410
523
  var _compiledStepsRef$cur3;
411
524
  if (!mountedRef.current || transitionPendingRef.current || currentStepIndexRef.current <= 0) return false;
525
+ let nextIndex = currentStepIndexRef.current - 1;
526
+ while (nextIndex >= 0 && isStepSkippable(nextIndex)) nextIndex -= 1;
527
+ if (nextIndex < 0) return false;
528
+ navigationDirectionRef.current = "backward";
412
529
  navigationRevisionRef.current += 1;
413
- const nextIndex = currentStepIndexRef.current - 1;
414
530
  currentStepIndexRef.current = nextIndex;
415
531
  currentStepKeyRef.current = (_compiledStepsRef$cur3 = compiledStepsRef.current[nextIndex]) === null || _compiledStepsRef$cur3 === void 0 ? void 0 : _compiledStepsRef$cur3.key;
416
532
  setCurrentStepIndex(nextIndex);
417
533
  return true;
418
- }, [])
534
+ }, [isStepSkippable]),
535
+ isStepSkippable,
536
+ navigationDirectionRef
419
537
  };
420
538
  };
421
539
  //#endregion
@@ -100,10 +100,19 @@ interface SelectionFlowModuleAdapter<TDraft, TDraftUpdate, TField extends string
100
100
  type: TModuleType;
101
101
  /** 离开所在步骤后仍需随 Draft 变化保持就绪的无界面门禁模块。 */
102
102
  initializeWhileInactive?: boolean;
103
+ /** Ready modules may omit their navigation stop; validation and beforeLeave still run. */
104
+ isSkippable?: (context: SelectionFlowContextValue<TDraft, TDraftUpdate, TField, TModuleType, TServices, THostContext>, module: CompiledSelectionFlowModuleConfig<TModuleType>) => boolean;
103
105
  provides?: readonly TField[];
104
106
  hardRequires?: readonly TField[];
105
107
  refreshOn?: readonly TField[];
106
108
  shouldRefresh?: (context: SelectionFlowContextValue<TDraft, TDraftUpdate, TField, TModuleType, TServices, THostContext>, change: SelectionFlowDraftChange<TDraft, TField, TModuleType>, module: CompiledSelectionFlowModuleConfig<TModuleType>) => boolean;
109
+ /**
110
+ * Explicitly permits completion to refresh a previously ready, invalidated
111
+ * module without revisiting its UI. The accumulated change starts at its last
112
+ * ready state. Completion still requires validateAutoResolution to succeed
113
+ * without any Draft revision changing; beforeLeave is not run here.
114
+ */
115
+ canRevalidateBeforeComplete?: (context: SelectionFlowContextValue<TDraft, TDraftUpdate, TField, TModuleType, TServices, THostContext>, change: SelectionFlowDraftChange<TDraft, TField, TModuleType>, module: CompiledSelectionFlowModuleConfig<TModuleType>) => boolean;
107
116
  isApplicable?: (context: SelectionFlowContextValue<TDraft, TDraftUpdate, TField, TModuleType, TServices, THostContext>, module: CompiledSelectionFlowModuleConfig<TModuleType>) => boolean;
108
117
  canInitialize?: (context: SelectionFlowContextValue<TDraft, TDraftUpdate, TField, TModuleType, TServices, THostContext>, module: CompiledSelectionFlowModuleConfig<TModuleType>) => boolean | Promise<boolean>;
109
118
  refresh?: (context: SelectionFlowContextValue<TDraft, TDraftUpdate, TField, TModuleType, TServices, THostContext>, module: CompiledSelectionFlowModuleConfig<TModuleType>) => void | Promise<void>;
@@ -164,6 +173,8 @@ interface PisellSelectionFlowController<TDraft, TDraftUpdate, TField extends str
164
173
  getState: () => SelectionFlowSnapshot<TDraft, TModuleType>;
165
174
  registerModuleHandle: (moduleKey: string, handle: FlowModuleHandle | null) => void;
166
175
  getModuleHandle: (moduleKey: string) => FlowModuleHandle | undefined;
176
+ /** True only when every module in the step is ready and explicitly permits skipping. */
177
+ isStepSkippable: (stepIndex: number) => boolean;
167
178
  }
168
179
  interface PisellSelectionFlowProps<TDraft, TDraftUpdate, TField extends string, TModuleType extends string, TServices = unknown, THostContext = unknown> {
169
180
  registry: SelectionFlowModuleRegistry<TDraft, TDraftUpdate, TField, TModuleType, TServices, THostContext>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pisell/materials",
3
- "version": "1.10.32",
3
+ "version": "1.10.34",
4
4
  "scripts": {
5
5
  "dev:tailwind-css": "tailwindcss -c ./tailwind.materials.config.cjs --postcss ./postcss.materials-tailwind.cjs -i ./src/tailwind-input.css -o ./src/pisell-materials.tw.css --watch",
6
6
  "build:tailwind-css:once": "tailwindcss -c ./tailwind.materials.config.cjs --postcss ./postcss.materials-tailwind.cjs -i ./src/tailwind-input.css -o ./src/pisell-materials.tw.css",