@wcstack/router 4.11.2 → 4.12.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.esm.js CHANGED
@@ -78,7 +78,7 @@ function setConfig(partialConfig) {
78
78
  }
79
79
 
80
80
  var name = "@wcstack/router";
81
- var version = "4.11.2";
81
+ var version = "4.12.0";
82
82
  var pkg = {
83
83
  name: name,
84
84
  version: version};
@@ -247,6 +247,20 @@ function queryIncludes(current, required) {
247
247
  function queryEquals(a, b) {
248
248
  return queryIncludes(a, b) && queryIncludes(b, a);
249
249
  }
250
+ /**
251
+ * 2 つのクエリが、名指したキーについてだけ同じ値を持つか(重複キーは同じ値の組、順序は問わない。どちらにも
252
+ * 無いキーは等しい)。`<wcs-link query-keys>` の判定 — ページもクエリにある一覧の「すべて」のタブ
253
+ * (`to="/notices?" query-keys="cat"`)が、どのページでも現在のタブになるように(docs/seam-apps.md、homeroom の取り込み)。
254
+ */
255
+ function queryKeysEqual(a, b, keys) {
256
+ const left = new URLSearchParams(a);
257
+ const right = new URLSearchParams(b);
258
+ return keys.every((key) => {
259
+ const x = left.getAll(key).sort();
260
+ const y = right.getAll(key).sort();
261
+ return x.length === y.length && x.every((value, i) => value === y[i]);
262
+ });
263
+ }
250
264
 
251
265
  /**
252
266
  * guard 判定関数に渡す第 3 引数を組み立てる。
@@ -4101,7 +4115,7 @@ class Router extends HTMLElement {
4101
4115
  const MIRRORED_ATTRIBUTES = ['title', 'rel', 'target', 'download', 'hreflang', 'lang', 'dir'];
4102
4116
  class Link extends HTMLElement {
4103
4117
  static get observedAttributes() {
4104
- return ['to', 'match', ...MIRRORED_ATTRIBUTES];
4118
+ return ['to', 'match', 'query-keys', ...MIRRORED_ATTRIBUTES];
4105
4119
  }
4106
4120
  _childNodeArray = [];
4107
4121
  _uuid = getUUID();
@@ -4336,7 +4350,7 @@ class Link extends HTMLElement {
4336
4350
  this._router = null;
4337
4351
  }
4338
4352
  attributeChangedCallback(name, oldValue, newValue) {
4339
- if (name === 'match' && oldValue !== newValue) {
4353
+ if ((name === 'match' || name === 'query-keys') && oldValue !== newValue) {
4340
4354
  // anchor 生成前(upgrade の属性の通知 — まだ router の下に居ないことがある)は接続時の判定に任せる
4341
4355
  if (this._anchorElement) {
4342
4356
  this._updateActiveState();
@@ -4377,12 +4391,18 @@ class Link extends HTMLElement {
4377
4391
  // `?` で終わる `to`(`to="/notices?"`、`to="?"` — 明示した空のクエリ)もクエリのある `to` で、そのページは
4378
4392
  // クエリの無いページだけ(href からは `?` を落とす)。クエリの無い `to` と同じに pathname だけで比べていたので、
4379
4393
  // 「すべて」のタブが `/notices?cat=event` でも現在のページで、現在のリンクが 2 つになった(docs/seam-apps.md、homeroom)
4394
+ //
4395
+ // `query-keys` はクエリのうち比べるキーを名指す。そのページそのもの("page")は今までどおりクエリ全体が等しいとき、
4396
+ // 名指したキーだけが等しければ "true"(どちらにも無いキーは等しい)。クエリの無い `to` もキーで比べる。ページも
4397
+ // クエリにある一覧では、`to="/notices?"` の「すべて」のタブは 1 ページ目でしか現在にならず、`match="prefix"` なら
4398
+ // どの区分でも現在だった — `query-keys="cat"` ならどのページでも「すべて」だけ(docs/seam-apps.md、homeroom の取り込み)
4380
4399
  const current = new URL(window.location.href);
4381
4400
  const currentPath = this._normalizePathname(current.pathname);
4382
4401
  const { pathname, search } = splitUrlTarget(this._path);
4383
4402
  const wanted = effectiveSearch(search);
4403
+ const keys = (this.getAttribute('query-keys') ?? '').split(/[\s,]+/).filter((key) => key !== '');
4384
4404
  const queryIncluded = queryIncludes(current.search, wanted);
4385
- const queryEqual = search === '' || queryEquals(current.search, wanted);
4405
+ const queryEqual = (search === '' && keys.length === 0) || queryEquals(current.search, wanted);
4386
4406
  const linkPath = this._normalizePathname(this._isInternalTarget(this._path)
4387
4407
  ? (pathname === ""
4388
4408
  ? window.location.pathname
@@ -4393,9 +4413,12 @@ class Link extends HTMLElement {
4393
4413
  // aria-current は "true"
4394
4414
  // 行き先の無いリンク(`to` が空)はどこでも active ではない(空のパスは "/" に正規化される)
4395
4415
  const samePath = currentPath === linkPath;
4416
+ const prefix = this.getAttribute('match') === 'prefix';
4417
+ const below = prefix && currentPath.startsWith(linkPath + '/');
4396
4418
  const exact = this._path !== '' && samePath && queryEqual;
4397
- const within = !exact && this._path !== '' && queryIncluded && this.getAttribute('match') === 'prefix'
4398
- && (samePath || currentPath.startsWith(linkPath + '/'));
4419
+ const within = !exact && this._path !== '' && (samePath || below) && (keys.length > 0
4420
+ ? queryKeysEqual(current.search, wanted, keys)
4421
+ : prefix && queryIncluded);
4399
4422
  if (this._anchorElement) {
4400
4423
  if (exact || within) {
4401
4424
  this._anchorElement.classList.add('active');