@aiquants/period-slider 0.10.1 → 0.10.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiquants/period-slider",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "description": "Customizable period slider and year/month/date selectors for React",
5
5
  "sideEffects": [
6
6
  "**/*.css"
@@ -1693,6 +1693,55 @@ const cancelPendingEmit = (slot: PendingEmitSlot): void => {
1693
1693
  slot.emit = null
1694
1694
  }
1695
1695
 
1696
+ /**
1697
+ * Value one rail displays while a pointer gesture owns it, and the two ways a gesture hands it over.
1698
+ * ポインター操作が持ち主である間、1 本のレールが表示する値と、操作がそれを引き渡す 2 つの口。
1699
+ */
1700
+ interface DragDisplaySlot {
1701
+ /** Selection the live gesture has drawn for this rail, null while the rail displays its owner's selection / 生きている操作がこのレールへ描いた選択 (レールが持ち主の選択を表示している間は null) */
1702
+ shown: [Date, Date] | null
1703
+ /** Draws the selection the gesture resolved for the position it is drawing / 操作が、描いている位置に対して解決した選択を描くメソッド */
1704
+ show: (low: Date, high: Date) => void
1705
+ /** Returns the rail to its owner / レールを持ち主へ返すメソッド */
1706
+ release: () => void
1707
+ }
1708
+
1709
+ /**
1710
+ * Holds one rail's drag-time displayed selection, mirrored in a ref the pointer handler reads at event time.
1711
+ * ドラッグ中に 1 本のレールが表示する選択を保持し、ポインターのハンドラが事象の時点で読む ref へ写すフック。
1712
+ *
1713
+ * The value is rendered, so it has to be state; the mirror is what makes the write decision correct. Two moves can
1714
+ * reach the handler before a commit, and comparing against the render on screen would skip the write exactly when the
1715
+ * pointer walks back onto the value that render still shows - leaving the text on the value the finger just left.
1716
+ * 表示される値であるためステートでなければならないが、書き込みの判定はこの写しが無いと成立しない。
1717
+ * 1 回のコミットの前に移動が 2 件届くことがあり、画面に出ているレンダーと比べると、ポインターが
1718
+ * そのレンダーの値へ戻る移動のときだけ書き込みを飛ばしてしまう (文字が指の去った値に取り残される)。
1719
+ *
1720
+ * @returns Selection currently drawn for the rail, together with the two transfers a gesture performs / レールへいま描かれている選択と、操作が行う 2 つの引き渡し
1721
+ */
1722
+ const useDragDisplaySlot = (): DragDisplaySlot => {
1723
+ const [shown, setShown] = useState<[Date, Date] | null>(null)
1724
+ const shownRef = useRef<[Date, Date] | null>(null)
1725
+
1726
+ const show = useCallback((low: Date, high: Date): void => {
1727
+ const drawn = shownRef.current
1728
+ // 表示値は離散であり、1 目盛りを跨がない移動では動かない。動いていない値を書くと 1 回の移動ごとに再描画が走る
1729
+ if (drawn !== null && drawn[0].getTime() === low.getTime() && drawn[1].getTime() === high.getTime()) return
1730
+ const next: [Date, Date] = [low, high]
1731
+ shownRef.current = next
1732
+ setShown(next)
1733
+ }, [])
1734
+
1735
+ const release = useCallback((): void => {
1736
+ // 一度も描かなかった操作 (動かさなかった押下) は返すものを持たない
1737
+ if (shownRef.current === null) return
1738
+ shownRef.current = null
1739
+ setShown(null)
1740
+ }, [])
1741
+
1742
+ return { shown, show, release }
1743
+ }
1744
+
1696
1745
  /**
1697
1746
  * Validates a BCP 47 language tag prop and fails fast on a tag Intl cannot use.
1698
1747
  * BCP 47 言語タグ Props を検証し、Intl が解釈できないタグで即座に失敗させる関数。
@@ -2313,9 +2362,20 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
2313
2362
  return [props.microValue, props.microValue]
2314
2363
  }, [props.mode, controlledMicroPointMs])
2315
2364
 
2316
- // null はノギス以外を意味する。ノギスでは resolveVernierMicroSelection が値の存在を保証済み。
2365
+ // ノギス 2 本のレールが、ポインター操作の生きている間に表示する値。平レールの draggingValues と同じ役割を
2366
+ // 「対になる値を 2 本のレールが持つ」ノギスへ与えたもの。掴んでいないレールは持ち主の値を表示し続ける
2367
+ const macroDragDisplay = useDragDisplaySlot()
2368
+ const microDragDisplay = useDragDisplaySlot()
2369
+
2370
+ // ノギスのミクロ選択のうち、利用側が保持しているもの。null はノギス以外を意味する
2371
+ // (ノギスでは resolveVernierMicroSelection が値の存在を保証済み)。
2317
2372
  // 時点形態も同じ幅ゼロタプルで表すため、以降の全計算は形態を意識しない
2318
- const vernierMicroSelection: [Date, Date] | null = props.mode !== "vernier" ? null : isMicroPointShape ? (controlledMicroPoint ?? uncontrolledMicroRange) : props.microRange !== undefined ? props.microRange : uncontrolledMicroRange
2373
+ const ownedMicroSelection: [Date, Date] | null = props.mode !== "vernier" ? null : isMicroPointShape ? (controlledMicroPoint ?? uncontrolledMicroRange) : props.microRange !== undefined ? props.microRange : uncontrolledMicroRange
2374
+
2375
+ // 表示されるミクロ選択。ポインター操作が生きている間は、その操作が描いている位置が名指す値が持ち主であり、
2376
+ // 指を離した瞬間に利用側の値へ返る。制御下で debounceMs を渡した利用側では、通知が届くまで利用側の値が動かないため、
2377
+ // この 1 段が無いと文字と ARIA だけが操作の間ずっと古い値に留まる
2378
+ const vernierMicroSelection: [Date, Date] | null = ownedMicroSelection === null ? null : (microDragDisplay.shown ?? ownedMicroSelection)
2319
2379
 
2320
2380
  const effectiveLowerDate: Date | undefined = props.mode === "range" ? (props.range?.[0] ?? props.defaultRange?.[0]) : props.mode === "vernier" ? vernierMicroSelection?.[0] : undefined
2321
2381
 
@@ -2378,8 +2438,6 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
2378
2438
  const formatMacroLabel = isVernierMode ? ((props as PeriodSliderVernierProps).formatMacroLabel ?? props.formatLabel) : props.formatLabel
2379
2439
  const formatMicroLabel = isVernierMode ? ((props as PeriodSliderVernierProps).formatMicroLabel ?? props.formatLabel) : props.formatLabel
2380
2440
 
2381
- const macroRangeStartMs = props.mode === "vernier" ? props.macroRange[0].getTime() : undefined
2382
- const macroRangeEndMs = props.mode === "vernier" ? props.macroRange[1].getTime() : undefined
2383
2441
  const microLowMs = effectiveLowerDate?.getTime()
2384
2442
  const microHighMs = effectiveUpperDate?.getTime()
2385
2443
 
@@ -2427,15 +2485,20 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
2427
2485
  // macroRange は定義域をはみ出してよい窓なので、レールに載る対はここで一度だけ定義域へ畳む。
2428
2486
  // 生の値のまま使うと aria-valuenow が自分の aria-valuemin/max の外を指し、
2429
2487
  // 平行移動はみ出した分を戻そうとして操作と逆向きに動く
2430
- const vernierMacroLowEnc = useMemo(() => {
2431
- if (props.mode !== "vernier") return vernierMacroMinEnc
2432
- return Math.max(vernierMacroMinEnc, Math.min(vernierMacroMaxEnc, encode(macroUnit, props.macroRange[0])))
2433
- }, [props.mode, macroRangeStartMs, vernierMacroMinEnc, vernierMacroMaxEnc, macroUnit])
2488
+ /**
2489
+ * Folds one macro window pair into the declared domain and returns it encoded.
2490
+ * マクロ窓の対 1 組を宣言済み定義域へ畳み、符号化して返す関数。
2491
+ *
2492
+ * @param range - Macro window pair to fold / 畳む対象のマクロ窓の対
2493
+ * @returns Encoded [start, end] pair inside the domain / 定義域の内側にある符号化済みの [開始, 終了] の対
2494
+ */
2495
+ const encodeMacroRangeIntoDomain = (range: [Date, Date]): [number, number] => [Math.max(vernierMacroMinEnc, Math.min(vernierMacroMaxEnc, encode(macroUnit, range[0]))), Math.max(vernierMacroMinEnc, Math.min(vernierMacroMaxEnc, encode(macroUnit, range[1])))]
2496
+
2497
+ // 利用側が保持しているマクロ窓 (ノギスでは必須 Props であり常に制御されている)。発火の重複判定はこれを基準にする
2498
+ const [ownedMacroLowEnc, ownedMacroHighEnc] = props.mode !== "vernier" ? [vernierMacroMinEnc, vernierMacroMaxEnc] : encodeMacroRangeIntoDomain(props.macroRange)
2434
2499
 
2435
- const vernierMacroHighEnc = useMemo(() => {
2436
- if (props.mode !== "vernier") return vernierMacroMaxEnc
2437
- return Math.max(vernierMacroMinEnc, Math.min(vernierMacroMaxEnc, encode(macroUnit, props.macroRange[1])))
2438
- }, [props.mode, macroRangeEndMs, vernierMacroMinEnc, vernierMacroMaxEnc, macroUnit])
2500
+ // 表示されるマクロ窓。ミクロ側と同じ規則で、ポインター操作が生きている間はその操作が持ち主
2501
+ const [vernierMacroLowEnc, vernierMacroHighEnc] = macroDragDisplay.shown === null ? [ownedMacroLowEnc, ownedMacroHighEnc] : encodeMacroRangeIntoDomain(macroDragDisplay.shown)
2439
2502
 
2440
2503
  // 位置と ARIA は正規化済みエンコード値、表示だけ生 prop という二重の真実を解消する。
2441
2504
  // 同じ値なら表示も一致することが構造的に保証される
@@ -2525,10 +2588,10 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
2525
2588
  const vernierMicroLowDate = useMemo(() => decode(microUnit, vernierMicroLowEnc), [microUnit, vernierMicroLowEnc])
2526
2589
  const vernierMicroHighDate = useMemo(() => decode(microUnit, vernierMicroHighEnc), [microUnit, vernierMicroHighEnc])
2527
2590
 
2528
- // 重複発火の判定基準。ミクロ側は利用側が保持している選択そのものを符号化する (レールへ載せた表示値を基準にすると、
2529
- // 窓の外へ出た選択を窓の中へ収め直す訂正が、比較の両辺が同値になって発火しなくなる)
2591
+ // 重複発火の判定基準。両レールとも利用側が保持している選択そのものを符号化する (ドラッグが描いている表示値や
2592
+ // レールへ載せた表示値を基準にすると、窓の外へ出た選択を窓の中へ収め直す訂正が、比較の両辺が同値になって発火しなくなる)
2530
2593
  const committedVernierScales: CommittedVernierScales | null =
2531
- !isVernierMode || vernierMicroSelection === null ? null : { macroUnit, macroLowEnc: vernierMacroLowEnc, macroHighEnc: vernierMacroHighEnc, microUnit, microLowEnc: encode(microUnit, vernierMicroSelection[0]), microHighEnc: encode(microUnit, vernierMicroSelection[1]) }
2594
+ !isVernierMode || ownedMicroSelection === null ? null : { macroUnit, macroLowEnc: ownedMacroLowEnc, macroHighEnc: ownedMacroHighEnc, microUnit, microLowEnc: encode(microUnit, ownedMicroSelection[0]), microHighEnc: encode(microUnit, ownedMicroSelection[1]) }
2532
2595
  const committedVernierRef = useRef(committedVernierScales)
2533
2596
 
2534
2597
  // 比率 (0.0 〜 1.0)。離散レールは当たり判定と同じ候補空間で描かないと、つまみがポインターから離れて飛ぶ
@@ -3128,6 +3191,9 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
3128
3191
  nextMacroHighDate = decode(macroUnit, nextHighEnc)
3129
3192
  }
3130
3193
 
3194
+ // 描かれているマクロ窓が名指す値。ラベル・ARIA・ミクロレールの座標系 (窓そのもの) はここから描かれる
3195
+ macroDragDisplay.show(nextMacroLowDate, nextMacroHighDate)
3196
+
3131
3197
  const macroKey = `${nextMacroLowDate.getTime()}_${nextMacroHighDate.getTime()}`
3132
3198
  if (macroKey !== lastEmittedMacroRangeRef.current) {
3133
3199
  lastEmittedMacroRangeRef.current = macroKey
@@ -3203,6 +3269,9 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
3203
3269
  nextMicroLowDate = decode(microUnit, emitLowEnc)
3204
3270
  nextMicroHighDate = decode(microUnit, emitHighEnc)
3205
3271
 
3272
+ // 窓が切り出し直したミクロ選択も同じ操作が描いている。発火の重複判定とは別の問いなので、同じ門を通さない
3273
+ microDragDisplay.show(nextMicroLowDate, nextMicroHighDate)
3274
+
3206
3275
  const microKey = `${nextMicroLowDate.getTime()}_${nextMicroHighDate.getTime()}`
3207
3276
  if (microKey !== lastEmittedMicroRangeRef.current) {
3208
3277
  lastEmittedMicroRangeRef.current = microKey
@@ -3233,6 +3302,9 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
3233
3302
  nextMicroHighDate = decode(microUnit, highEnc)
3234
3303
  }
3235
3304
 
3305
+ // 描かれているミクロ選択が名指す値
3306
+ microDragDisplay.show(nextMicroLowDate, nextMicroHighDate)
3307
+
3236
3308
  const microKey = `${nextMicroLowDate.getTime()}_${nextMicroHighDate.getTime()}`
3237
3309
  if (microKey !== lastEmittedMicroRangeRef.current) {
3238
3310
  lastEmittedMicroRangeRef.current = microKey
@@ -3319,6 +3391,10 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
3319
3391
  vernierDragContextRef.current = null
3320
3392
  activeVernierHandleRef.current = null
3321
3393
  setActiveVernierHandle(null)
3394
+ // 表示の持ち主を利用側へ返す。発火の掃き出しと同じ React の一括処理へ入るため、値をそのまま返す利用側では
3395
+ // 描画は 1 回で済み、値を畳む・拒む利用側では利用側の値が勝つ
3396
+ macroDragDisplay.release()
3397
+ microDragDisplay.release()
3322
3398
  // 予約したままドラッグを終えると、指を離した位置の値が発火されずに消える
3323
3399
  flushPendingEmit(macroEmitRef.current)
3324
3400
  flushPendingEmit(microEmitRef.current)
@@ -3649,7 +3725,7 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
3649
3725
  * @returns Nothing / 戻り値なし
3650
3726
  */
3651
3727
  const containMicroSelectionIntoMacroWindow = (nextMacroLow: Date, nextMacroHigh: Date): void => {
3652
- if (vernierMicroSelection === null) return
3728
+ if (ownedMicroSelection === null) return
3653
3729
  // 次のレンダーでミクロレール端はこの窓へ置き換わる。同じ解決器で収め直さないと aria-valuenow が aria-valuemax の外を指し続ける
3654
3730
  const [nextWindowStart, nextWindowEnd] = resolveMacroWindow(macroUnit, microUnit, [nextMacroLow, nextMacroHigh], minDate, maxDate)
3655
3731
  const nextMicroMinEnc = encode(microUnit, nextWindowStart)
@@ -3658,8 +3734,8 @@ const PeriodSliderComponent: React.FC<PeriodSliderProps> = (props) => {
3658
3734
  // 出発点は利用側が保持している選択そのもの。レールへ載せた表示値から出発すると、新しい窓が選択を
3659
3735
  // 完全に含む場合でも古い窓の端へ引き戻し、比較の両辺が同じ端になって訂正自体が発火しなくなる。
3660
3736
  // 動いていない選択を鳴らさない判定は notifyMicroSelection が全経路分まとめて持つ
3661
- const nextLowEnc = resolveMicroRailEnc(vernierMicroSelection[0], nextCandidates, nextMicroMinEnc, nextMicroMaxEnc)
3662
- const nextHighEnc = Math.max(nextLowEnc, resolveMicroRailEnc(vernierMicroSelection[1], nextCandidates, nextMicroMinEnc, nextMicroMaxEnc))
3737
+ const nextLowEnc = resolveMicroRailEnc(ownedMicroSelection[0], nextCandidates, nextMicroMinEnc, nextMicroMaxEnc)
3738
+ const nextHighEnc = Math.max(nextLowEnc, resolveMicroRailEnc(ownedMicroSelection[1], nextCandidates, nextMicroMinEnc, nextMicroMaxEnc))
3663
3739
  notifyMicroSelection(decode(microUnit, nextLowEnc), decode(microUnit, nextHighEnc), true)
3664
3740
  }
3665
3741