@wcstack/router 4.31.0 → 4.33.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.d.ts CHANGED
@@ -255,6 +255,8 @@ interface IRouter extends IRouteChildContainer {
255
255
  navigate(path: string): Promise<void>;
256
256
  /** navigateUrl(push)の対になる replace 遷移(§4.2) */
257
257
  replace(path: string): Promise<void>;
258
+ /** guard のリダイレクト(replace)。すでに通したナビゲーションの続きなので、leaveHold は止めない */
259
+ guardRedirect(path: string): Promise<void>;
258
260
  /**
259
261
  * 初期化(最初のルート適用と navigate リスナの登録)が終わると決着する。guard のリダイレクトは
260
262
  * これを待つ — 初期化中の遷移はリスナに捕まらず、文書ごとの読み込みになるため
@@ -344,6 +346,14 @@ declare class Router extends HTMLElement implements IRouter {
344
346
  private _leaveConfirmed;
345
347
  /** ページを離れるナビゲーションが確認された: beforeunload で二度聞かない(次のタスクで戻す) */
346
348
  private _unloadConfirmed;
349
+ /** leaveHold が止めているナビゲーション(leaving はその path)。止めていなければ null */
350
+ private _held;
351
+ /** proceed() が始めたナビゲーション: 次の commit まで止めない(その遷移の guard のリダイレクトも) */
352
+ private _proceeding;
353
+ /** 取り消せなかった戻る / 進むを、止めた項目へ戻している: その項目の key(戻る遷移は描き直さない) */
354
+ private _revertingTo;
355
+ /** guardRedirect() が replace() を呼んでいる間(_performNavigation が同期の頭で読む) */
356
+ private _redirecting;
347
357
  private _listeningUnload;
348
358
  private _connectedCallbackPromise;
349
359
  private _resolveConnectedCallback;
@@ -387,6 +397,27 @@ declare class Router extends HTMLElement implements IRouter {
387
397
  set basename(value: string | null | undefined);
388
398
  get leaveConfirm(): string | null;
389
399
  set leaveConfirm(value: string | boolean | null | undefined);
400
+ get leaveHold(): boolean;
401
+ set leaveHold(value: boolean | null | undefined);
402
+ /** leaveHold が止めているナビゲーションの行き先(navigate() が受ける形)。止めていなければ null */
403
+ get leaving(): string | null;
404
+ private _setHeld;
405
+ /**
406
+ * 止めるか: leaveHold が真で、最初の表示の後、proceed() の遷移でなく、別のルートへ出るもの。今の URL と同じ
407
+ * パスへのもの(クエリだけ)は止めない — 前のナビゲーションの guard を待つ間は、URL は進み、ルートはまだ前の
408
+ * もの(全数テストの [link(/g), hold, query])。
409
+ */
410
+ private _shouldHold;
411
+ /** leaving に出す形: basename の下のパス + クエリ + フラグメント(navigate() にそのまま渡せる) */
412
+ private _leavingPath;
413
+ /**
414
+ * 止めたナビゲーションを行う(leaving への「行く」)。同じ種類で: リンク・navigate() は push、replace() /
415
+ * replaceUrl は replace、戻る / 進むはその項目へ辿り直す。leaveHold のままでも止めない(その遷移の guard の
416
+ * リダイレクトも)。止めていなければ何もしない。
417
+ */
418
+ proceed(): Promise<void>;
419
+ /** 止めたナビゲーションを捨てる(leaving への「留まる」)。URL も画面も今のまま */
420
+ stay(): void;
390
421
  private _onBeforeUnload;
391
422
  /** beforeunload は leaveConfirm がある間だけ聞く(リスナーがあるだけで bfcache を外すブラウザがある) */
392
423
  private _syncBeforeUnload;
@@ -471,6 +502,12 @@ declare class Router extends HTMLElement implements IRouter {
471
502
  * フォールバックでは `history.replaceState` + applyRoute + 通知。
472
503
  */
473
504
  replace(path: string): Promise<void>;
505
+ /**
506
+ * guard のリダイレクト(showRouteContent)。replace() と同じだが、すでに通したナビゲーションの続きなので
507
+ * leaveHold は止めない — 止めると URL だけが guard のルートに進み、画面は前のまま残る。replace() を通すのは、
508
+ * それを差し替えたテストがそのまま効くように。
509
+ */
510
+ guardRedirect(path: string): Promise<void>;
474
511
  private _performNavigation;
475
512
  /**
476
513
  * basename 配下の URL かどうかを判定する。
package/dist/index.esm.js CHANGED
@@ -78,7 +78,7 @@ function setConfig(partialConfig) {
78
78
  }
79
79
 
80
80
  var name = "@wcstack/router";
81
- var version = "4.31.0";
81
+ var version = "4.33.0";
82
82
  var pkg = {
83
83
  name: name,
84
84
  version: version};
@@ -2534,7 +2534,7 @@ async function runGuardPhase(routerNode, matchResult, superseded = () => false)
2534
2534
  redirectCounts.set(routerNode, count);
2535
2535
  routerNode.connectedCallbackPromise
2536
2536
  .catch(() => { })
2537
- .then(() => routerNode.replace(e.fallbackPath))
2537
+ .then(() => routerNode.guardRedirect(e.fallbackPath))
2538
2538
  .catch((err) => {
2539
2539
  if (!isAbortError(err))
2540
2540
  console.error('Fallback navigation failed:', err);
@@ -2886,6 +2886,14 @@ function reflectAttribute(el, name, value) {
2886
2886
  const initial = initialAttribute(el, name);
2887
2887
  writeAttribute(el, name, value === undefined ? initial : value === null ? null : String(value));
2888
2888
  }
2889
+ /**
2890
+ * A boolean attribute, present while `value` is truthy (`null` and `false` remove it);
2891
+ * `undefined` restores the attribute the element started with.
2892
+ */
2893
+ function reflectBooleanAttribute(el, name, value) {
2894
+ const initial = initialAttribute(el, name);
2895
+ writeAttribute(el, name, value === undefined ? initial : value ? "" : null);
2896
+ }
2889
2897
 
2890
2898
  /**
2891
2899
  * SSR モード判定。@wcstack/server の renderToString がレンダリング中の document
@@ -3033,6 +3041,9 @@ class Router extends HTMLElement {
3033
3041
  // guard 関数がオブジェクトを返したナビゲーションのロード済みデータ(loader)。
3034
3042
  // output-only。データ無しのナビゲーションでは null に戻る
3035
3043
  { name: "data", event: "wcs-router:data-changed", semantics: "state" },
3044
+ // leaveHold が止めているナビゲーションの行き先(navigate() が受ける形 — basename の下のパスとクエリと
3045
+ // フラグメント)。止めていなければ null。output-only。ページはこれで自分のダイアログを出す(§5.28)
3046
+ { name: "leaving", event: "wcs-router:leaving-changed", semantics: "state" },
3036
3047
  ],
3037
3048
  // `navigateUrl` は observable output であると同時に settable な書き込み面でもある
3038
3049
  // (setter が navigate() を起動し、完了後に自分で null へ戻す)。properties にだけ
@@ -3044,10 +3055,14 @@ class Router extends HTMLElement {
3044
3055
  { name: "navigateUrl" },
3045
3056
  { name: "replaceUrl" },
3046
3057
  { name: "leaveConfirm", attribute: "leave-confirm" },
3058
+ { name: "leaveHold", attribute: "leave-hold" },
3047
3059
  ],
3048
3060
  commands: [
3049
3061
  { name: "navigate", async: true },
3050
3062
  { name: "replace", async: true },
3063
+ // leaving への答え: 止めたナビゲーションをそのまま行う / 捨てる
3064
+ { name: "proceed", async: true },
3065
+ { name: "stay" },
3051
3066
  ],
3052
3067
  };
3053
3068
  _outlet = null;
@@ -3085,6 +3100,14 @@ class Router extends HTMLElement {
3085
3100
  _leaveConfirmed = false;
3086
3101
  /** ページを離れるナビゲーションが確認された: beforeunload で二度聞かない(次のタスクで戻す) */
3087
3102
  _unloadConfirmed = false;
3103
+ /** leaveHold が止めているナビゲーション(leaving はその path)。止めていなければ null */
3104
+ _held = null;
3105
+ /** proceed() が始めたナビゲーション: 次の commit まで止めない(その遷移の guard のリダイレクトも) */
3106
+ _proceeding = false;
3107
+ /** 取り消せなかった戻る / 進むを、止めた項目へ戻している: その項目の key(戻る遷移は描き直さない) */
3108
+ _revertingTo = null;
3109
+ /** guardRedirect() が replace() を呼んでいる間(_performNavigation が同期の頭で読む) */
3110
+ _redirecting = false;
3088
3111
  _listeningUnload = false;
3089
3112
  _connectedCallbackPromise;
3090
3113
  _resolveConnectedCallback = null;
@@ -3209,6 +3232,67 @@ class Router extends HTMLElement {
3209
3232
  reflectAttribute(this, "leave-confirm", value === true ? DEFAULT_LEAVE_CONFIRM : value === false ? null : value);
3210
3233
  this._syncBeforeUnload();
3211
3234
  }
3235
+ // `leaveHold` は input(属性は leave-hold、真偽値)。真の間、別のルートへのナビゲーション — リンク、戻る /
3236
+ // 進む、navigate() / navigateUrl — を確定させずに止め、行き先を `leaving` に出す。ページは自分のダイアログを
3237
+ // 出し、`proceed()`(行く)か `stay()`(留まる)で答える。leaveConfirm(ブラウザの confirm())の、ページが
3238
+ // 答える形(React の useBlocker に当たる。docs/seam-apps.md の street-reports、v4-direction §5.28)。クエリだけの
3239
+ // ナビゲーション・最初の表示・proceed() の遷移(その guard のリダイレクトも)は止めない。ページを離れる
3240
+ // ナビゲーション(basename の外、読み直し、閉じる)は待てないので leaveConfirm / beforeunload のまま。
3241
+ get leaveHold() {
3242
+ return this.hasAttribute("leave-hold");
3243
+ }
3244
+ set leaveHold(value) {
3245
+ reflectBooleanAttribute(this, "leave-hold", value);
3246
+ }
3247
+ /** leaveHold が止めているナビゲーションの行き先(navigate() が受ける形)。止めていなければ null */
3248
+ get leaving() {
3249
+ return this._held?.path ?? null;
3250
+ }
3251
+ _setHeld(held) {
3252
+ if ((this._held?.path ?? null) === (held?.path ?? null) && this._held?.traverseKey === held?.traverseKey) {
3253
+ this._held = held;
3254
+ return;
3255
+ }
3256
+ this._held = held;
3257
+ this.dispatchEvent(new CustomEvent("wcs-router:leaving-changed", { detail: this.leaving, bubbles: true }));
3258
+ }
3259
+ /**
3260
+ * 止めるか: leaveHold が真で、最初の表示の後、proceed() の遷移でなく、別のルートへ出るもの。今の URL と同じ
3261
+ * パスへのもの(クエリだけ)は止めない — 前のナビゲーションの guard を待つ間は、URL は進み、ルートはまだ前の
3262
+ * もの(全数テストの [link(/g), hold, query])。
3263
+ */
3264
+ _shouldHold(fullPath) {
3265
+ const normalized = this._normalizePathname(fullPath);
3266
+ return this.leaveHold && this._hasCommitted && !this._proceeding &&
3267
+ normalized !== this._normalizePathname(window.location.pathname) &&
3268
+ !this.isSameMatch(sliceBasename(normalized, this._basename));
3269
+ }
3270
+ /** leaving に出す形: basename の下のパス + クエリ + フラグメント(navigate() にそのまま渡せる) */
3271
+ _leavingPath(fullPath, search, hash) {
3272
+ return sliceBasename(this._normalizePathname(fullPath), this._basename) + search + hash;
3273
+ }
3274
+ /**
3275
+ * 止めたナビゲーションを行う(leaving への「行く」)。同じ種類で: リンク・navigate() は push、replace() /
3276
+ * replaceUrl は replace、戻る / 進むはその項目へ辿り直す。leaveHold のままでも止めない(その遷移の guard の
3277
+ * リダイレクトも)。止めていなければ何もしない。
3278
+ */
3279
+ async proceed() {
3280
+ const held = this._held;
3281
+ if (held === null)
3282
+ return;
3283
+ this._setHeld(null);
3284
+ this._proceeding = true;
3285
+ const navigation = getNavigation();
3286
+ if (held.traverseKey !== undefined && navigation?.traverseTo) {
3287
+ await navigation.traverseTo(held.traverseKey)?.finished;
3288
+ return;
3289
+ }
3290
+ await this._performNavigation(held.path, held.history === "replace");
3291
+ }
3292
+ /** 止めたナビゲーションを捨てる(leaving への「留まる」)。URL も画面も今のまま */
3293
+ stay() {
3294
+ this._setHeld(null);
3295
+ }
3212
3296
  _onBeforeUnload = (e) => {
3213
3297
  if (this.leaveConfirm !== null && !this._unloadConfirmed)
3214
3298
  e.preventDefault();
@@ -3395,6 +3479,10 @@ class Router extends HTMLElement {
3395
3479
  this._path = commit.path;
3396
3480
  this._hasCommitted = true;
3397
3481
  this._leaveConfirmed = false;
3482
+ this._proceeding = false;
3483
+ // 別のルートへ移った(止めずに通った戻る / 進む、leaveHold を外した後のリンク): 止めていた行き先は古い
3484
+ if (pathChanged && this._held !== null)
3485
+ this._setHeld(null);
3398
3486
  // --- その後で発火(順序規範: data → params → route-name → search → path) ---
3399
3487
  if (dataChanged) {
3400
3488
  this.dispatchEvent(new CustomEvent("wcs-router:data-changed", {
@@ -3504,7 +3592,23 @@ class Router extends HTMLElement {
3504
3592
  async replace(path) {
3505
3593
  await this._performNavigation(path, true);
3506
3594
  }
3595
+ /**
3596
+ * guard のリダイレクト(showRouteContent)。replace() と同じだが、すでに通したナビゲーションの続きなので
3597
+ * leaveHold は止めない — 止めると URL だけが guard のルートに進み、画面は前のまま残る。replace() を通すのは、
3598
+ * それを差し替えたテストがそのまま効くように。
3599
+ */
3600
+ guardRedirect(path) {
3601
+ this._redirecting = true;
3602
+ try {
3603
+ return this.replace(path);
3604
+ }
3605
+ finally {
3606
+ this._redirecting = false;
3607
+ }
3608
+ }
3507
3609
  async _performNavigation(path, replace) {
3610
+ // (read before the first await: guardRedirect() sets it around its synchronous call)
3611
+ const redirect = this._redirecting;
3508
3612
  // クエリ / ハッシュ込みターゲットの受理(docs/router-state-contract-design.md §4.1)。
3509
3613
  // normalizePathname / basename 結合は pathname にのみ適用し、search / hash は
3510
3614
  // 再結合して URL に渡す。pathname 空("?k=v" / "?" / "#x")は現在の pathname を
@@ -3514,10 +3618,14 @@ class Router extends HTMLElement {
3514
3618
  ? window.location.pathname
3515
3619
  : this._joinInternalPath(this._basename, target.pathname);
3516
3620
  const url = fullPath + effectiveSearch(target.search) + target.hash;
3517
- if (this.leaveConfirm !== null) {
3518
- // 1 マイクロタスク待ってから確かめる: 保存のハンドラが leaveConfirm を外すバインディングと
3621
+ if (this.leaveConfirm !== null || this.leaveHold) {
3622
+ // 1 マイクロタスク待ってから確かめる: 保存のハンドラが leaveConfirm / leaveHold を外すバインディングと
3519
3623
  // navigateUrl を同じ drain で書いたとき、どちらが先に適用されても外れた後の値を見る
3520
3624
  await Promise.resolve();
3625
+ if (!redirect && this._shouldHold(fullPath)) {
3626
+ this._setHeld({ path: this._leavingPath(fullPath, effectiveSearch(target.search), target.hash), history: replace ? "replace" : "push" });
3627
+ return;
3628
+ }
3521
3629
  if (!this._confirmLeave(fullPath))
3522
3630
  return;
3523
3631
  }
@@ -3630,6 +3738,41 @@ class Router extends HTMLElement {
3630
3738
  this._confirmUnload(navEvent);
3631
3739
  return;
3632
3740
  }
3741
+ // 取り消せなかった戻る / 進むを止めた項目へ戻す遷移(下): 画面は変わっていないので描き直さない
3742
+ if (this._revertingTo !== null && navEvent.destination.key === this._revertingTo) {
3743
+ this._revertingTo = null;
3744
+ navEvent.intercept({ handler: async () => { }, scroll: "manual", focusReset: "manual" });
3745
+ return;
3746
+ }
3747
+ // leaveHold: 別のルートへ出るナビゲーションを止め、行き先を leaving に出す。自分の navigate() / replace()
3748
+ // からのものは止めるかを確かめ済み
3749
+ if (!this._leaveChecked && this._shouldHold(fullPath)) {
3750
+ const traverseKey = navEvent.navigationType === "traverse" ? navEvent.destination.key : undefined;
3751
+ const held = {
3752
+ path: this._leavingPath(fullPath, url.search, url.hash),
3753
+ history: navEvent.navigationType === "replace" ? "replace" : "push",
3754
+ ...(traverseKey !== undefined ? { traverseKey } : {}),
3755
+ };
3756
+ if (navEvent.cancelable !== false) {
3757
+ navEvent.preventDefault?.();
3758
+ this._setHeld(held);
3759
+ return;
3760
+ }
3761
+ // 取り消せない戻る / 進む: URL は先に変わる。描かずに受け、止めた項目へ戻る(作者が手で書く history.go(1))
3762
+ const fromKey = getNavigation()?.currentEntry?.key;
3763
+ if (traverseKey !== undefined && fromKey !== undefined) {
3764
+ this._setHeld(held);
3765
+ navEvent.intercept({
3766
+ handler: async () => {
3767
+ this._revertingTo = fromKey;
3768
+ setTimeout(() => void getNavigation()?.traverseTo?.(fromKey)?.finished?.catch(() => { }), 0);
3769
+ },
3770
+ scroll: "manual",
3771
+ focusReset: "manual",
3772
+ });
3773
+ return;
3774
+ }
3775
+ }
3633
3776
  // 別のルートへ出る前の確認(leaveConfirm)。取り消せないナビゲーション(一部の戻る / 進む)では
3634
3777
  // 聞けない。自分の navigate() / replace() からのものは確認済み
3635
3778
  if (!this._leaveChecked && navEvent.cancelable !== false && !this._confirmLeave(fullPath)) {