@kolkrabbi/kol-component 0.257.0 → 0.258.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.258.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,7 +440,9 @@ 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.
432
446
  *
433
447
  * @param {ReactNode} backdrop placed behind the frame (grid / dark bg); the consumer owns its sizing — oversize it so panning never reveals an edge
434
448
  * @param {boolean} showRulers draw the virtual-px rulers (default true)
@@ -482,11 +496,11 @@ export function PanZoomViewport({
482
496
  } else if (e.key === '=' || e.key === '+') {
483
497
  e.preventDefault()
484
498
  const r = containerRef.current?.getBoundingClientRect()
485
- setView((v) => zoomAt(v, 1.2, (r?.width ?? 0) / 2, (r?.height ?? 0) / 2))
499
+ setView((v) => zoomAt(v, 1.2, (r?.width ?? 0) / 2, (r?.height ?? 0) / 2, fitOf(containerRef.current)))
486
500
  } else if (e.key === '-') {
487
501
  e.preventDefault()
488
502
  const r = containerRef.current?.getBoundingClientRect()
489
- setView((v) => zoomAt(v, 1 / 1.2, (r?.width ?? 0) / 2, (r?.height ?? 0) / 2))
503
+ setView((v) => zoomAt(v, 1 / 1.2, (r?.width ?? 0) / 2, (r?.height ?? 0) / 2, fitOf(containerRef.current)))
490
504
  }
491
505
  }
492
506
  }
@@ -540,7 +554,7 @@ export function PanZoomViewport({
540
554
  const sx = e.clientX - rect.left
541
555
  const sy = e.clientY - rect.top
542
556
  if (e.ctrlKey || e.metaKey) {
543
- setView((v) => zoomAt(v, Math.exp(-e.deltaY * 0.0015), sx, sy))
557
+ setView((v) => zoomAt(v, Math.exp(-e.deltaY * 0.0015), sx, sy, fitOf(node)))
544
558
  } else {
545
559
  setView((v) => ({ ...v, x: v.x - e.deltaX, y: v.y - e.deltaY }))
546
560
  }
@@ -558,10 +572,53 @@ export function PanZoomViewport({
558
572
  if (!node) return
559
573
  const { clientX, clientY, factor } = e.detail
560
574
  const rect = node.getBoundingClientRect()
561
- setView((v) => zoomAt(v, factor, clientX - rect.left, clientY - rect.top))
575
+ setView((v) => zoomAt(v, factor, clientX - rect.left, clientY - rect.top, fitOf(node)))
576
+ }
577
+ /* `kol:zoom-to` { scale } — a REAL scale (1 = 100%: one screen px per frame px), about the
578
+ * viewport centre, so a consumer's 100–800% steps need not measure the fit. */
579
+ const onZoomTo = (e) => {
580
+ const node = containerRef.current
581
+ if (!node) return
582
+ const fit = fitOf(node)
583
+ const r = node.getBoundingClientRect()
584
+ setView((v) => zoomAt(v, e.detail.scale / (v.zoom * fit), r.width / 2, r.height / 2, fit))
585
+ }
586
+ /* `kol:zoom-fit` { to } — `'frame'` (default) is ⌘0's opening view; `'width'` fills the
587
+ * viewport with the frame's width, centred vertically; `{ x, y, w, h }` in frame (virtual) px
588
+ * fills it with that rect, FIT_MARGIN around it, centred. */
589
+ const onZoomFit = (e) => {
590
+ const node = containerRef.current
591
+ const frame = node?.querySelector('[data-canvas-frame]')
592
+ const to = e.detail?.to ?? 'frame'
593
+ if (!frame || to === 'frame') { setView({ zoom: 1, x: 0, y: 0 }); return }
594
+ const fit = fitOf(node)
595
+ const cr = node.getBoundingClientRect()
596
+ const fr = frame.getBoundingClientRect()
597
+ setView((v) => {
598
+ /* the frame's rect at rest (zoom 1, no pan), in viewport px */
599
+ const fx = (fr.left - cr.left - v.x) / v.zoom
600
+ const fy = (fr.top - cr.top - v.y) / v.zoom
601
+ const rect = to === 'width'
602
+ ? { x: fx, y: fy, w: frame.offsetWidth, h: frame.offsetHeight }
603
+ : { x: fx + to.x * fit, y: fy + to.y * fit, w: to.w * fit, h: to.h * fit }
604
+ const zoom = clampZoom(to === 'width'
605
+ ? cr.width / rect.w
606
+ : Math.min((cr.width - 2 * FIT_MARGIN) / rect.w, (cr.height - 2 * FIT_MARGIN) / rect.h), fit)
607
+ return {
608
+ zoom,
609
+ x: cr.width / 2 - (rect.x + rect.w / 2) * zoom,
610
+ y: cr.height / 2 - (rect.y + rect.h / 2) * zoom,
611
+ }
612
+ })
562
613
  }
563
614
  window.addEventListener('kol:zoom-at', onZoomEvt)
564
- return () => window.removeEventListener('kol:zoom-at', onZoomEvt)
615
+ window.addEventListener('kol:zoom-to', onZoomTo)
616
+ window.addEventListener('kol:zoom-fit', onZoomFit)
617
+ return () => {
618
+ window.removeEventListener('kol:zoom-at', onZoomEvt)
619
+ window.removeEventListener('kol:zoom-to', onZoomTo)
620
+ window.removeEventListener('kol:zoom-fit', onZoomFit)
621
+ }
565
622
  }, [])
566
623
 
567
624
  /* `f` toggles the fps chip (measured only while shown). Guarded against