@kolkrabbi/kol-component 0.257.0 → 0.259.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kolkrabbi/kol-component",
3
- "version": "0.257.0",
3
+ "version": "0.259.0",
4
4
  "description": "The core component library",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -368,15 +368,27 @@ export function PanViewport({ backdrop, children }) {
368
368
  * clamps, the anchor math, the wheel handling and the chip layout are the
369
369
  * source's — fxr is the reference for this set. */
370
370
 
371
- const ZOOM_MIN = 0.1
372
- const ZOOM_MAX = 8
371
+ /* The clamps are in REAL terms — screen px per frame px, `zoom × fit` (CanvasViewCommands, kol-fxr
372
+ * 2026-10-13). They were 0.1–8 relative to the fitted frame, so a 1080 frame fitted at ~0.58 capped
373
+ * at ~464% real and Affinity's 800% could not be reached. */
374
+ const ZOOM_MIN_REAL = 0.02
375
+ const ZOOM_MAX_REAL = 32
376
+ const FIT_MARGIN = 24 /* screen px around a rect fitted by `kol:zoom-fit` */
377
+
378
+ /* The fit scale: the frame's LAYOUT width (untransformed) over its virtual width. 1 without a frame. */
379
+ function fitOf(node) {
380
+ const w = node?.querySelector('[data-canvas-frame]')?.offsetWidth
381
+ return w ? w / CANVAS_VIRTUAL_W : 1
382
+ }
383
+
384
+ const clampZoom = (z, fit) => Math.min(ZOOM_MAX_REAL / fit, Math.max(ZOOM_MIN_REAL / fit, z))
373
385
 
374
386
  /* Anchor a zoom change at a screen point (sx, sy relative to the viewport
375
387
  * top-left) so the content under the cursor stays put. Transform is
376
388
  * `translate(x,y) scale(zoom)` with origin 0,0, so screen = p*zoom + pan;
377
389
  * inverting for a fixed p gives the new pan below. */
378
- function zoomAt(v, factor, sx, sy) {
379
- const z2 = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, v.zoom * factor))
390
+ function zoomAt(v, factor, sx, sy, fit = 1) {
391
+ const z2 = clampZoom(v.zoom * factor, fit)
380
392
  return {
381
393
  zoom: z2,
382
394
  x: sx - (sx - v.x) * (z2 / v.zoom),
@@ -428,14 +440,17 @@ function isTypingTarget(el) {
428
440
  * `translate() scale()` on the transform layer carries both, so a consumer's
429
441
  * screen→virtual math needs no zoom awareness. Pointer events on the
430
442
  * transform layer disable while Space is held so layer mousedowns don't fire
431
- * mid-pan. `f` toggles an fps chip beside the zoom readout.
443
+ * mid-pan. `f` toggles an fps chip beside the zoom readout. From outside: `kol:zoom-at`
444
+ * { clientX, clientY, factor } · `kol:zoom-to` { scale } (real, 1 = 100%) · `kol:zoom-fit` { to:
445
+ * 'frame' | 'width' | { x, y, w, h } } — window events. Zoom is clamped at 2%–3200% real. Touch:
446
+ * two fingers pinch-zoom + pan; a one-finger tap fires `onSpaceTap`.
432
447
  *
433
448
  * @param {ReactNode} backdrop placed behind the frame (grid / dark bg); the consumer owns its sizing — oversize it so panning never reveals an edge
434
449
  * @param {boolean} showRulers draw the virtual-px rulers (default true)
435
450
  * @param {{h: number[], v: number[]}} guides guide positions in VIRTUAL px
436
451
  * @param {Function} setGuides updater for `guides`; without both, no guides layer renders
437
452
  * @param {boolean} guidesInteractive allow grab + create (default true)
438
- * @param {Function} onSpaceTap fires on a Space press released without panning
453
+ * @param {Function} onSpaceTap fires on a Space press released without panning, or a one-finger touch tap
439
454
  * @param {ReactNode} children the letterboxed frame
440
455
  */
441
456
  export function PanZoomViewport({
@@ -455,6 +470,7 @@ export function PanZoomViewport({
455
470
  const [dragging, setDragging] = useState(false)
456
471
  const [view, setView] = useState({ zoom: 1, x: 0, y: 0 })
457
472
  const dragStart = useRef(null)
473
+ const [pinching, setPinching] = useState(false)
458
474
  const [showFps, setShowFps] = useState(false)
459
475
  const fps = useFps(showFps)
460
476
  /* Ref mirror so the keyup listener calls the latest callback without
@@ -482,11 +498,11 @@ export function PanZoomViewport({
482
498
  } else if (e.key === '=' || e.key === '+') {
483
499
  e.preventDefault()
484
500
  const r = containerRef.current?.getBoundingClientRect()
485
- setView((v) => zoomAt(v, 1.2, (r?.width ?? 0) / 2, (r?.height ?? 0) / 2))
501
+ setView((v) => zoomAt(v, 1.2, (r?.width ?? 0) / 2, (r?.height ?? 0) / 2, fitOf(containerRef.current)))
486
502
  } else if (e.key === '-') {
487
503
  e.preventDefault()
488
504
  const r = containerRef.current?.getBoundingClientRect()
489
- setView((v) => zoomAt(v, 1 / 1.2, (r?.width ?? 0) / 2, (r?.height ?? 0) / 2))
505
+ setView((v) => zoomAt(v, 1 / 1.2, (r?.width ?? 0) / 2, (r?.height ?? 0) / 2, fitOf(containerRef.current)))
490
506
  }
491
507
  }
492
508
  }
@@ -540,7 +556,7 @@ export function PanZoomViewport({
540
556
  const sx = e.clientX - rect.left
541
557
  const sy = e.clientY - rect.top
542
558
  if (e.ctrlKey || e.metaKey) {
543
- setView((v) => zoomAt(v, Math.exp(-e.deltaY * 0.0015), sx, sy))
559
+ setView((v) => zoomAt(v, Math.exp(-e.deltaY * 0.0015), sx, sy, fitOf(node)))
544
560
  } else {
545
561
  setView((v) => ({ ...v, x: v.x - e.deltaX, y: v.y - e.deltaY }))
546
562
  }
@@ -549,6 +565,59 @@ export function PanZoomViewport({
549
565
  return () => node.removeEventListener('wheel', onWheel)
550
566
  }, [])
551
567
 
568
+ /* Touch (ViewportTouchGestures, kol-fxr 2026-10-14): two fingers pinch-zoom at their midpoint and
569
+ * the midpoint's travel pans, in one gesture; the transform layer goes deaf while they are down so
570
+ * a consumer's tool never sees the second finger. One finger stays the consumer's, except a TAP
571
+ * (up within TAP_MS and TAP_PX, no second finger joined) fires `onSpaceTap`. Down is caught in the
572
+ * capture phase on the container; move/up on window, so a finger leaving the frame still ends. */
573
+ useEffect(() => {
574
+ const node = containerRef.current
575
+ if (!node) return
576
+ const TAP_MS = 350, TAP_PX = 12
577
+ const pts = new Map()
578
+ let pinch = null, tap = null
579
+ const pair = () => {
580
+ const [a, b] = [...pts.values()]
581
+ return { mx: (a.x + b.x) / 2, my: (a.y + b.y) / 2, d: Math.hypot(a.x - b.x, a.y - b.y) || 1 }
582
+ }
583
+ const onDown = (e) => {
584
+ if (e.pointerType !== 'touch') return
585
+ pts.set(e.pointerId, { x: e.clientX, y: e.clientY })
586
+ if (pts.size === 1) tap = { id: e.pointerId, t: e.timeStamp, x: e.clientX, y: e.clientY }
587
+ else if (pts.size === 2) { tap = null; pinch = pair(); setPinching(true) }
588
+ }
589
+ const onMove = (e) => {
590
+ if (!pts.has(e.pointerId)) return
591
+ pts.set(e.pointerId, { x: e.clientX, y: e.clientY })
592
+ if (tap && Math.hypot(e.clientX - tap.x, e.clientY - tap.y) > TAP_PX) tap = null
593
+ if (!pinch || pts.size < 2) return
594
+ const prev = pinch, next = pair()
595
+ pinch = next
596
+ const r = node.getBoundingClientRect()
597
+ const fit = fitOf(node)
598
+ setView((v) => {
599
+ const z = zoomAt(v, next.d / prev.d, prev.mx - r.left, prev.my - r.top, fit)
600
+ return { ...z, x: z.x + next.mx - prev.mx, y: z.y + next.my - prev.my }
601
+ })
602
+ }
603
+ const onEnd = (e) => {
604
+ if (!pts.delete(e.pointerId)) return
605
+ if (tap?.id === e.pointerId && e.type === 'pointerup' && e.timeStamp - tap.t < TAP_MS) spaceTapRef.current?.()
606
+ if (tap?.id === e.pointerId) tap = null
607
+ if (pinch && pts.size < 2) { pinch = null; setPinching(false) }
608
+ }
609
+ node.addEventListener('pointerdown', onDown, true)
610
+ window.addEventListener('pointermove', onMove)
611
+ window.addEventListener('pointerup', onEnd)
612
+ window.addEventListener('pointercancel', onEnd)
613
+ return () => {
614
+ node.removeEventListener('pointerdown', onDown, true)
615
+ window.removeEventListener('pointermove', onMove)
616
+ window.removeEventListener('pointerup', onEnd)
617
+ window.removeEventListener('pointercancel', onEnd)
618
+ }
619
+ }, [])
620
+
552
621
  /* A consumer's zoom TOOL announces clicks as `kol:zoom-at` (client coords +
553
622
  * factor) rather than reaching into this state — anchored zoom at the
554
623
  * pointer. Same idiom as `kol:guide-drag-start` below. */
@@ -558,10 +627,53 @@ export function PanZoomViewport({
558
627
  if (!node) return
559
628
  const { clientX, clientY, factor } = e.detail
560
629
  const rect = node.getBoundingClientRect()
561
- setView((v) => zoomAt(v, factor, clientX - rect.left, clientY - rect.top))
630
+ setView((v) => zoomAt(v, factor, clientX - rect.left, clientY - rect.top, fitOf(node)))
631
+ }
632
+ /* `kol:zoom-to` { scale } — a REAL scale (1 = 100%: one screen px per frame px), about the
633
+ * viewport centre, so a consumer's 100–800% steps need not measure the fit. */
634
+ const onZoomTo = (e) => {
635
+ const node = containerRef.current
636
+ if (!node) return
637
+ const fit = fitOf(node)
638
+ const r = node.getBoundingClientRect()
639
+ setView((v) => zoomAt(v, e.detail.scale / (v.zoom * fit), r.width / 2, r.height / 2, fit))
640
+ }
641
+ /* `kol:zoom-fit` { to } — `'frame'` (default) is ⌘0's opening view; `'width'` fills the
642
+ * viewport with the frame's width, centred vertically; `{ x, y, w, h }` in frame (virtual) px
643
+ * fills it with that rect, FIT_MARGIN around it, centred. */
644
+ const onZoomFit = (e) => {
645
+ const node = containerRef.current
646
+ const frame = node?.querySelector('[data-canvas-frame]')
647
+ const to = e.detail?.to ?? 'frame'
648
+ if (!frame || to === 'frame') { setView({ zoom: 1, x: 0, y: 0 }); return }
649
+ const fit = fitOf(node)
650
+ const cr = node.getBoundingClientRect()
651
+ const fr = frame.getBoundingClientRect()
652
+ setView((v) => {
653
+ /* the frame's rect at rest (zoom 1, no pan), in viewport px */
654
+ const fx = (fr.left - cr.left - v.x) / v.zoom
655
+ const fy = (fr.top - cr.top - v.y) / v.zoom
656
+ const rect = to === 'width'
657
+ ? { x: fx, y: fy, w: frame.offsetWidth, h: frame.offsetHeight }
658
+ : { x: fx + to.x * fit, y: fy + to.y * fit, w: to.w * fit, h: to.h * fit }
659
+ const zoom = clampZoom(to === 'width'
660
+ ? cr.width / rect.w
661
+ : Math.min((cr.width - 2 * FIT_MARGIN) / rect.w, (cr.height - 2 * FIT_MARGIN) / rect.h), fit)
662
+ return {
663
+ zoom,
664
+ x: cr.width / 2 - (rect.x + rect.w / 2) * zoom,
665
+ y: cr.height / 2 - (rect.y + rect.h / 2) * zoom,
666
+ }
667
+ })
562
668
  }
563
669
  window.addEventListener('kol:zoom-at', onZoomEvt)
564
- return () => window.removeEventListener('kol:zoom-at', onZoomEvt)
670
+ window.addEventListener('kol:zoom-to', onZoomTo)
671
+ window.addEventListener('kol:zoom-fit', onZoomFit)
672
+ return () => {
673
+ window.removeEventListener('kol:zoom-at', onZoomEvt)
674
+ window.removeEventListener('kol:zoom-to', onZoomTo)
675
+ window.removeEventListener('kol:zoom-fit', onZoomFit)
676
+ }
565
677
  }, [])
566
678
 
567
679
  /* `f` toggles the fps chip (measured only while shown). Guarded against
@@ -597,7 +709,7 @@ export function PanZoomViewport({
597
709
  <div
598
710
  ref={containerRef}
599
711
  className="relative w-full h-full overflow-hidden select-none"
600
- style={cursor ? { cursor } : undefined}
712
+ style={{ cursor, touchAction: 'none' }}
601
713
  onMouseDown={onMouseDown}
602
714
  >
603
715
  {/* No transition on the transform: editing chrome (selection
@@ -610,7 +722,7 @@ export function PanZoomViewport({
610
722
  style={{
611
723
  transform: `translate(${view.x}px, ${view.y}px) scale(${view.zoom})`,
612
724
  transformOrigin: '0 0',
613
- pointerEvents: spaceHeld ? 'none' : 'auto',
725
+ pointerEvents: spaceHeld || pinching ? 'none' : 'auto',
614
726
  }}
615
727
  >
616
728
  {backdrop}