Compare commits
6 Commits
abacus-rea
...
abacus-rea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16ccaf2c8b | ||
|
|
23ae1b0c6f | ||
|
|
e852afddc5 | ||
|
|
645140648a | ||
|
|
be7d4c4713 | ||
|
|
88c0baaad9 |
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useSuspenseQuery } from '@tanstack/react-query'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { css } from '../../../../../styled-system/css'
|
||||
|
||||
interface CalendarPreviewProps {
|
||||
@@ -26,15 +26,45 @@ async function fetchTypstPreview(month: number, year: number, format: string): P
|
||||
}
|
||||
|
||||
export function CalendarPreview({ month, year, format, previewSvg }: CalendarPreviewProps) {
|
||||
// Use React Query with Suspense to fetch Typst-generated preview
|
||||
const { data: typstPreviewSvg } = useSuspenseQuery({
|
||||
// Use React Query to fetch Typst-generated preview (client-side only)
|
||||
const { data: typstPreviewSvg, isLoading } = useQuery({
|
||||
queryKey: ['calendar-typst-preview', month, year, format],
|
||||
queryFn: () => fetchTypstPreview(month, year, format),
|
||||
enabled: typeof window !== 'undefined' && format === 'monthly', // Only run on client and for monthly format
|
||||
})
|
||||
|
||||
// Use generated PDF SVG if available, otherwise use Typst live preview
|
||||
const displaySvg = previewSvg || typstPreviewSvg
|
||||
|
||||
// Show loading state while fetching preview
|
||||
if (isLoading || (!displaySvg && format === 'monthly')) {
|
||||
return (
|
||||
<div
|
||||
data-component="calendar-preview"
|
||||
className={css({
|
||||
bg: 'gray.800',
|
||||
borderRadius: '12px',
|
||||
padding: '2rem',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
minHeight: '600px',
|
||||
})}
|
||||
>
|
||||
<p
|
||||
className={css({
|
||||
fontSize: '1.25rem',
|
||||
color: 'gray.400',
|
||||
textAlign: 'center',
|
||||
})}
|
||||
>
|
||||
Loading preview...
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (!displaySvg) {
|
||||
return (
|
||||
<div
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useState, Suspense } from 'react'
|
||||
import { useState } from 'react'
|
||||
import { css } from '../../../../styled-system/css'
|
||||
import { useAbacusConfig } from '@soroban/abacus-react'
|
||||
import { PageWithNav } from '@/components/PageWithNav'
|
||||
@@ -128,35 +128,7 @@ export default function CalendarCreatorPage() {
|
||||
/>
|
||||
|
||||
{/* Preview */}
|
||||
<Suspense
|
||||
fallback={
|
||||
<div
|
||||
data-component="calendar-preview"
|
||||
className={css({
|
||||
bg: 'gray.800',
|
||||
borderRadius: '12px',
|
||||
padding: '2rem',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
minHeight: '600px',
|
||||
})}
|
||||
>
|
||||
<p
|
||||
className={css({
|
||||
fontSize: '1.25rem',
|
||||
color: 'gray.400',
|
||||
textAlign: 'center',
|
||||
})}
|
||||
>
|
||||
Loading preview...
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<CalendarPreview month={month} year={year} format={format} previewSvg={previewSvg} />
|
||||
</Suspense>
|
||||
<CalendarPreview month={month} year={year} format={format} previewSvg={previewSvg} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -11,7 +11,13 @@
|
||||
"Bash(git commit:*)",
|
||||
"Bash(npm run build:*)",
|
||||
"Bash(git reset:*)",
|
||||
"Bash(cat:*)"
|
||||
"Bash(cat:*)",
|
||||
"Bash(pnpm --filter @soroban/abacus-react build:*)",
|
||||
"Bash(git show:*)",
|
||||
"Bash(pnpm build:*)",
|
||||
"Bash(pnpm --filter @soroban/web build:*)",
|
||||
"Bash(pnpm tsc:*)",
|
||||
"Bash(AbacusReact.tsx)"
|
||||
]
|
||||
},
|
||||
"enableAllProjectMcpServers": true,
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
## [2.8.2](https://github.com/antialias/soroban-abacus-flashcards/compare/abacus-react-v2.8.1...abacus-react-v2.8.2) (2025-11-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **abacus-react:** add data-testid attributes back to beads for testing ([23ae1b0](https://github.com/antialias/soroban-abacus-flashcards/commit/23ae1b0c6f878daf79a993992d43ad80a89fa790))
|
||||
|
||||
## [2.8.1](https://github.com/antialias/soroban-abacus-flashcards/compare/abacus-react-v2.8.0...abacus-react-v2.8.1) (2025-11-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **abacus-react:** fix animations by preventing component remounting ([be7d4c4](https://github.com/antialias/soroban-abacus-flashcards/commit/be7d4c471327534a95c4c75372680c629b5f12c2))
|
||||
* **abacus-react:** restore original AbacusReact measurements and positioning ([88c0baa](https://github.com/antialias/soroban-abacus-flashcards/commit/88c0baaad9b83b60ab8cdcad92070cc049d61cc7))
|
||||
|
||||
# [2.8.0](https://github.com/antialias/soroban-abacus-flashcards/compare/abacus-react-v2.7.1...abacus-react-v2.8.0) (2025-11-04)
|
||||
|
||||
|
||||
|
||||
@@ -146,6 +146,62 @@ import { AbacusStatic } from '@soroban/abacus-react/static';
|
||||
- `@soroban/abacus-react` - Full package (client components with hooks/animations)
|
||||
- `@soroban/abacus-react/static` - Server-compatible components only (no client code)
|
||||
|
||||
**Guaranteed Visual Consistency:**
|
||||
|
||||
Both `AbacusStatic` and `AbacusReact` share the same underlying layout engine. **Same props = same exact SVG output.** This ensures:
|
||||
- Static previews match interactive versions pixel-perfect
|
||||
- Server-rendered abaci look identical to client-rendered ones
|
||||
- PDF generation produces accurate representations
|
||||
- No visual discrepancies between environments
|
||||
|
||||
**Architecture: How We Guarantee Consistency**
|
||||
|
||||
The package uses a shared rendering architecture with dependency injection:
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────┐
|
||||
│ Shared Utilities (AbacusUtils.ts) │
|
||||
│ • calculateStandardDimensions() - Single │
|
||||
│ source of truth for all layout dimensions│
|
||||
│ • calculateBeadPosition() - Exact bead │
|
||||
│ positioning using shared formulas │
|
||||
└────────────┬────────────────────────────────┘
|
||||
│
|
||||
├──────────────────────────────────┐
|
||||
↓ ↓
|
||||
┌─────────────────┐ ┌─────────────────┐
|
||||
│ AbacusStatic │ │ AbacusReact │
|
||||
│ (Server/Static) │ │ (Interactive) │
|
||||
└────────┬────────┘ └────────┬────────┘
|
||||
│ │
|
||||
└────────────┬───────────────────┘
|
||||
↓
|
||||
┌────────────────────────┐
|
||||
│ AbacusSVGRenderer │
|
||||
│ • Pure SVG structure │
|
||||
│ • Dependency injection │
|
||||
│ • Bead component prop │
|
||||
└────────────────────────┘
|
||||
↓
|
||||
┌───────────────┴───────────────┐
|
||||
↓ ↓
|
||||
┌──────────────┐ ┌──────────────────┐
|
||||
│ AbacusStatic │ │ AbacusAnimated │
|
||||
│ Bead │ │ Bead │
|
||||
│ (Simple SVG) │ │ (react-spring) │
|
||||
└──────────────┘ └──────────────────┘
|
||||
```
|
||||
|
||||
**Key Components:**
|
||||
|
||||
1. **`calculateStandardDimensions()`** - Returns complete layout dimensions (bar position, bead sizes, gaps, etc.)
|
||||
2. **`calculateBeadPosition()`** - Calculates exact x,y coordinates for any bead
|
||||
3. **`AbacusSVGRenderer`** - Shared SVG rendering component that accepts a bead component via dependency injection
|
||||
4. **`AbacusStaticBead`** - Simple SVG shapes for static display (no hooks, RSC-compatible)
|
||||
5. **`AbacusAnimatedBead`** - Client component with react-spring animations and gesture handling
|
||||
|
||||
This architecture eliminates code duplication (~560 lines removed in the refactor) while guaranteeing pixel-perfect consistency.
|
||||
|
||||
**When to use `AbacusStatic` vs `AbacusReact`:**
|
||||
|
||||
| Feature | AbacusStatic | AbacusReact |
|
||||
@@ -156,8 +212,9 @@ import { AbacusStatic } from '@soroban/abacus-react/static';
|
||||
| Animations | ❌ No | ✅ Smooth transitions |
|
||||
| Sound effects | ❌ No | ✅ Optional sounds |
|
||||
| 3D effects | ❌ No | ✅ Yes |
|
||||
| **Visual output** | **✅ Identical** | **✅ Identical** |
|
||||
| Bundle size | 📦 Minimal | 📦 Full-featured |
|
||||
| Use cases | Preview cards, thumbnails, static pages | Interactive tutorials, games, tools |
|
||||
| Use cases | Preview cards, thumbnails, static pages, PDFs | Interactive tutorials, games, tools |
|
||||
|
||||
```tsx
|
||||
// Example: Server Component with static abacus cards
|
||||
@@ -648,6 +705,63 @@ const state2 = numberToAbacusState(123);
|
||||
const isEqual = areStatesEqual(state1, state2); // true
|
||||
```
|
||||
|
||||
### calculateStandardDimensions
|
||||
|
||||
**⚡ Core Architecture Function** - Calculate complete layout dimensions for consistent rendering.
|
||||
|
||||
This is the **single source of truth** for all layout dimensions, used internally by both `AbacusStatic` and `AbacusReact` to guarantee pixel-perfect consistency.
|
||||
|
||||
```tsx
|
||||
import { calculateStandardDimensions } from '@soroban/abacus-react';
|
||||
|
||||
const dimensions = calculateStandardDimensions({
|
||||
columns: 3,
|
||||
scaleFactor: 1.5,
|
||||
showNumbers: true,
|
||||
columnLabels: ['ones', 'tens', 'hundreds']
|
||||
});
|
||||
|
||||
// Returns complete layout info:
|
||||
// {
|
||||
// width, height, // SVG canvas size
|
||||
// beadSize, // 12 * scaleFactor (standard bead size)
|
||||
// rodSpacing, // 25 * scaleFactor (column spacing)
|
||||
// rodWidth, // 3 * scaleFactor
|
||||
// barThickness, // 2 * scaleFactor
|
||||
// barY, // Reckoning bar Y position (30 * scaleFactor + labels)
|
||||
// heavenY, earthY, // Inactive bead rest positions
|
||||
// activeGap, // 1 * scaleFactor (gap to bar when active)
|
||||
// inactiveGap, // 8 * scaleFactor (gap between active/inactive)
|
||||
// adjacentSpacing, // 0.5 * scaleFactor (spacing between adjacent beads)
|
||||
// padding, labelHeight, numbersHeight, totalColumns
|
||||
// }
|
||||
```
|
||||
|
||||
**Why this matters:** Same input parameters = same exact layout dimensions = pixel-perfect visual consistency across static and interactive displays.
|
||||
|
||||
### calculateBeadPosition
|
||||
|
||||
**⚡ Core Architecture Function** - Calculate exact x,y coordinates for any bead.
|
||||
|
||||
Used internally by `AbacusSVGRenderer` to position all beads consistently in both static and interactive modes.
|
||||
|
||||
```tsx
|
||||
import { calculateBeadPosition, calculateStandardDimensions } from '@soroban/abacus-react';
|
||||
|
||||
const dimensions = calculateStandardDimensions({ columns: 3, scaleFactor: 1 });
|
||||
const bead = {
|
||||
type: 'heaven',
|
||||
active: true,
|
||||
position: 0,
|
||||
placeValue: 1 // tens column
|
||||
};
|
||||
|
||||
const position = calculateBeadPosition(bead, dimensions);
|
||||
// Returns: { x: 25, y: 29 } // exact pixel coordinates
|
||||
```
|
||||
|
||||
Useful for custom rendering or positioning tooltips/overlays relative to specific beads.
|
||||
|
||||
## Educational Use Cases
|
||||
|
||||
### Interactive Math Lessons
|
||||
@@ -725,6 +839,8 @@ import {
|
||||
calculateBeadDiffFromValues,
|
||||
validateAbacusValue,
|
||||
areStatesEqual,
|
||||
calculateStandardDimensions, // NEW: Shared layout calculator
|
||||
calculateBeadPosition, // NEW: Bead position calculator
|
||||
|
||||
// Theme Presets
|
||||
ABACUS_THEMES,
|
||||
@@ -740,7 +856,9 @@ import {
|
||||
BeadState,
|
||||
BeadDiffResult,
|
||||
BeadDiffOutput,
|
||||
AbacusThemeName
|
||||
AbacusThemeName,
|
||||
AbacusLayoutDimensions, // NEW: Complete layout dimensions type
|
||||
BeadPositionConfig // NEW: Bead config for position calculation
|
||||
} from '@soroban/abacus-react';
|
||||
|
||||
// All interfaces fully typed for excellent developer experience
|
||||
|
||||
353
packages/abacus-react/src/AbacusAnimatedBead.tsx
Normal file
353
packages/abacus-react/src/AbacusAnimatedBead.tsx
Normal file
@@ -0,0 +1,353 @@
|
||||
'use client'
|
||||
|
||||
/**
|
||||
* AbacusAnimatedBead - Interactive bead component for AbacusReact (Core Architecture)
|
||||
*
|
||||
* This is the **client-side bead component** injected into AbacusSVGRenderer by AbacusReact.
|
||||
* It provides animations and interactivity while the parent renderer handles positioning.
|
||||
*
|
||||
* ## Architecture Role:
|
||||
* - Injected into `AbacusSVGRenderer` via dependency injection (BeadComponent prop)
|
||||
* - Receives x,y position from `calculateBeadPosition()` (already calculated)
|
||||
* - Adds animations and interactions on top of the shared layout
|
||||
* - Used ONLY by AbacusReact (requires "use client")
|
||||
*
|
||||
* ## Features:
|
||||
* - ✅ React Spring animations for smooth position changes
|
||||
* - ✅ Drag gesture handling with @use-gesture/react
|
||||
* - ✅ Direction indicators for tutorials (pulsing arrows)
|
||||
* - ✅ 3D effects and gradients
|
||||
* - ✅ Click and hover interactions
|
||||
*
|
||||
* ## Comparison:
|
||||
* - `AbacusStaticBead` - Simple SVG shapes (no animations, RSC-compatible)
|
||||
* - `AbacusAnimatedBead` - This component (animations, gestures, client-only)
|
||||
*
|
||||
* Both receive the same position from `calculateBeadPosition()`, ensuring visual consistency.
|
||||
*/
|
||||
|
||||
import React, { useCallback, useRef } from 'react'
|
||||
import { useSpring, animated, to } from '@react-spring/web'
|
||||
import { useDrag } from '@use-gesture/react'
|
||||
import type { BeadComponentProps } from './AbacusSVGRenderer'
|
||||
import type { BeadConfig } from './AbacusReact'
|
||||
|
||||
interface AnimatedBeadProps extends BeadComponentProps {
|
||||
// Animation controls
|
||||
enableAnimation: boolean
|
||||
physicsConfig: any
|
||||
|
||||
// Gesture handling
|
||||
enableGestures: boolean
|
||||
onGestureToggle?: (bead: BeadConfig, direction: 'activate' | 'deactivate') => void
|
||||
|
||||
// Direction indicators (for tutorials)
|
||||
showDirectionIndicator?: boolean
|
||||
direction?: 'activate' | 'deactivate'
|
||||
isCurrentStep?: boolean
|
||||
|
||||
// 3D effects
|
||||
enhanced3d?: 'none' | 'subtle' | 'realistic' | 'delightful'
|
||||
columnIndex?: number
|
||||
}
|
||||
|
||||
export function AbacusAnimatedBead({
|
||||
bead,
|
||||
x,
|
||||
y,
|
||||
size,
|
||||
shape,
|
||||
color,
|
||||
hideInactiveBeads,
|
||||
customStyle,
|
||||
onClick,
|
||||
onMouseEnter,
|
||||
onMouseLeave,
|
||||
onRef,
|
||||
enableAnimation,
|
||||
physicsConfig,
|
||||
enableGestures,
|
||||
onGestureToggle,
|
||||
showDirectionIndicator,
|
||||
direction,
|
||||
isCurrentStep,
|
||||
enhanced3d = 'none',
|
||||
columnIndex,
|
||||
}: AnimatedBeadProps) {
|
||||
// x, y are already calculated by AbacusSVGRenderer
|
||||
|
||||
// Spring animation for position
|
||||
const [{ springX, springY }, api] = useSpring(() => ({
|
||||
springX: x,
|
||||
springY: y,
|
||||
config: physicsConfig,
|
||||
}))
|
||||
|
||||
// Arrow pulse animation for direction indicators
|
||||
const [{ arrowPulse }, arrowApi] = useSpring(() => ({
|
||||
arrowPulse: 1,
|
||||
config: enableAnimation ? { tension: 200, friction: 10 } : { duration: 0 },
|
||||
}))
|
||||
|
||||
const gestureStateRef = useRef({
|
||||
isDragging: false,
|
||||
lastDirection: null as 'activate' | 'deactivate' | null,
|
||||
startY: 0,
|
||||
threshold: size * 0.3,
|
||||
hasGestureTriggered: false,
|
||||
})
|
||||
|
||||
// Calculate gesture direction based on bead type
|
||||
const getGestureDirection = useCallback(
|
||||
(deltaY: number) => {
|
||||
const movement = Math.abs(deltaY)
|
||||
if (movement < gestureStateRef.current.threshold) return null
|
||||
|
||||
if (bead.type === 'heaven') {
|
||||
return deltaY > 0 ? 'activate' : 'deactivate'
|
||||
} else {
|
||||
return deltaY < 0 ? 'activate' : 'deactivate'
|
||||
}
|
||||
},
|
||||
[bead.type, size]
|
||||
)
|
||||
|
||||
// Gesture handler
|
||||
const bind = enableGestures
|
||||
? useDrag(
|
||||
({ event, movement: [, deltaY], first, active }) => {
|
||||
if (first) {
|
||||
event?.preventDefault()
|
||||
gestureStateRef.current.isDragging = true
|
||||
gestureStateRef.current.lastDirection = null
|
||||
gestureStateRef.current.hasGestureTriggered = false
|
||||
return
|
||||
}
|
||||
|
||||
if (!active || !gestureStateRef.current.isDragging) {
|
||||
if (!active) {
|
||||
gestureStateRef.current.isDragging = false
|
||||
gestureStateRef.current.lastDirection = null
|
||||
setTimeout(() => {
|
||||
gestureStateRef.current.hasGestureTriggered = false
|
||||
}, 100)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const currentDirection = getGestureDirection(deltaY)
|
||||
|
||||
if (
|
||||
currentDirection &&
|
||||
currentDirection !== gestureStateRef.current.lastDirection
|
||||
) {
|
||||
gestureStateRef.current.lastDirection = currentDirection
|
||||
gestureStateRef.current.hasGestureTriggered = true
|
||||
onGestureToggle?.(bead, currentDirection)
|
||||
}
|
||||
},
|
||||
{
|
||||
enabled: enableGestures,
|
||||
preventDefault: true,
|
||||
}
|
||||
)
|
||||
: () => ({})
|
||||
|
||||
// Update spring animation when position changes
|
||||
React.useEffect(() => {
|
||||
if (enableAnimation) {
|
||||
api.start({ springX: x, springY: y, config: physicsConfig })
|
||||
} else {
|
||||
api.set({ springX: x, springY: y })
|
||||
}
|
||||
}, [x, y, enableAnimation, api, physicsConfig])
|
||||
|
||||
// Pulse animation for direction indicators
|
||||
React.useEffect(() => {
|
||||
if (showDirectionIndicator && direction && isCurrentStep) {
|
||||
const startPulse = () => {
|
||||
arrowApi.start({
|
||||
from: { arrowPulse: 1 },
|
||||
to: async (next) => {
|
||||
await next({ arrowPulse: 1.3 })
|
||||
await next({ arrowPulse: 1 })
|
||||
},
|
||||
loop: true,
|
||||
})
|
||||
}
|
||||
|
||||
const timeoutId = setTimeout(startPulse, 200)
|
||||
return () => {
|
||||
clearTimeout(timeoutId)
|
||||
arrowApi.stop()
|
||||
}
|
||||
} else {
|
||||
arrowApi.set({ arrowPulse: 1 })
|
||||
}
|
||||
}, [showDirectionIndicator, direction, isCurrentStep, arrowApi])
|
||||
|
||||
// Render bead shape
|
||||
const renderShape = () => {
|
||||
const halfSize = size / 2
|
||||
|
||||
// Determine fill - use gradient for realistic mode, otherwise use color
|
||||
let fillValue = customStyle?.fill || color
|
||||
if (enhanced3d === 'realistic' && columnIndex !== undefined) {
|
||||
if (bead.type === 'heaven') {
|
||||
fillValue = `url(#bead-gradient-${columnIndex}-heaven)`
|
||||
} else {
|
||||
fillValue = `url(#bead-gradient-${columnIndex}-earth-${bead.position})`
|
||||
}
|
||||
}
|
||||
|
||||
const opacity = bead.active ? (customStyle?.opacity ?? 1) : 0.3
|
||||
const stroke = customStyle?.stroke || '#000'
|
||||
const strokeWidth = customStyle?.strokeWidth || 0.5
|
||||
|
||||
switch (shape) {
|
||||
case 'diamond':
|
||||
return (
|
||||
<polygon
|
||||
points={`${size * 0.7},0 ${size * 1.4},${halfSize} ${size * 0.7},${size} 0,${halfSize}`}
|
||||
fill={fillValue}
|
||||
stroke={stroke}
|
||||
strokeWidth={strokeWidth}
|
||||
opacity={opacity}
|
||||
/>
|
||||
)
|
||||
case 'square':
|
||||
return (
|
||||
<rect
|
||||
width={size}
|
||||
height={size}
|
||||
fill={fillValue}
|
||||
stroke={stroke}
|
||||
strokeWidth={strokeWidth}
|
||||
rx="1"
|
||||
opacity={opacity}
|
||||
/>
|
||||
)
|
||||
case 'circle':
|
||||
default:
|
||||
return (
|
||||
<circle
|
||||
cx={halfSize}
|
||||
cy={halfSize}
|
||||
r={halfSize}
|
||||
fill={fillValue}
|
||||
stroke={stroke}
|
||||
strokeWidth={strokeWidth}
|
||||
opacity={opacity}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate offsets for shape positioning
|
||||
const getXOffset = () => {
|
||||
return shape === 'diamond' ? size * 0.7 : size / 2
|
||||
}
|
||||
|
||||
const getYOffset = () => {
|
||||
return size / 2
|
||||
}
|
||||
|
||||
// Use animated.g if animations enabled, otherwise regular g
|
||||
const GElement = enableAnimation ? animated.g : 'g'
|
||||
const DirectionIndicatorG =
|
||||
enableAnimation && showDirectionIndicator && direction ? animated.g : 'g'
|
||||
|
||||
// Build style object
|
||||
const beadStyle: any = enableAnimation
|
||||
? {
|
||||
transform: to(
|
||||
[springX, springY],
|
||||
(sx, sy) => `translate(${sx - getXOffset()}px, ${sy - getYOffset()}px)`
|
||||
),
|
||||
cursor: enableGestures ? 'grab' : onClick ? 'pointer' : 'default',
|
||||
touchAction: 'none' as const,
|
||||
transition: 'opacity 0.2s ease-in-out',
|
||||
}
|
||||
: {
|
||||
transform: `translate(${x - getXOffset()}px, ${y - getYOffset()}px)`,
|
||||
cursor: enableGestures ? 'grab' : onClick ? 'pointer' : 'default',
|
||||
touchAction: 'none' as const,
|
||||
transition: 'opacity 0.2s ease-in-out',
|
||||
}
|
||||
|
||||
// Don't render inactive beads if hideInactiveBeads is true
|
||||
if (!bead.active && hideInactiveBeads) {
|
||||
return null
|
||||
}
|
||||
|
||||
const handleClick = (event: React.MouseEvent) => {
|
||||
// Prevent click if gesture was triggered
|
||||
if (gestureStateRef.current.hasGestureTriggered) {
|
||||
event.preventDefault()
|
||||
return
|
||||
}
|
||||
onClick?.(bead, event)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<GElement
|
||||
className={`abacus-bead ${bead.active ? 'active' : 'inactive'} ${hideInactiveBeads && !bead.active ? 'hidden-inactive' : ''}`}
|
||||
data-testid={`bead-place-${bead.placeValue}-${bead.type}${bead.type === 'earth' ? `-pos-${bead.position}` : ''}`}
|
||||
style={beadStyle}
|
||||
{...bind()}
|
||||
onClick={handleClick}
|
||||
onMouseEnter={(e) => onMouseEnter?.(bead, e as any)}
|
||||
onMouseLeave={(e) => onMouseLeave?.(bead, e as any)}
|
||||
ref={(el) => onRef?.(bead, el as any)}
|
||||
>
|
||||
{renderShape()}
|
||||
</GElement>
|
||||
|
||||
{/* Direction indicator for tutorials */}
|
||||
{showDirectionIndicator && direction && (
|
||||
<DirectionIndicatorG
|
||||
className="direction-indicator"
|
||||
style={
|
||||
(enableAnimation
|
||||
? {
|
||||
transform: to(
|
||||
[springX, springY, arrowPulse],
|
||||
(sx, sy, pulse) => {
|
||||
const centerX = shape === 'diamond' ? size * 0.7 : size / 2
|
||||
const centerY = size / 2
|
||||
return `translate(${sx - centerX}px, ${sy - centerY}px) scale(${pulse})`
|
||||
}
|
||||
),
|
||||
pointerEvents: 'none' as const,
|
||||
}
|
||||
: {
|
||||
transform: `translate(${x - (shape === 'diamond' ? size * 0.7 : size / 2)}px, ${y - size / 2}px) scale(1)`,
|
||||
pointerEvents: 'none' as const,
|
||||
}) as any
|
||||
}
|
||||
>
|
||||
<circle
|
||||
cx={shape === 'diamond' ? size * 0.7 : size / 2}
|
||||
cy={size / 2}
|
||||
r={size * 0.8}
|
||||
fill="rgba(255, 165, 0, 0.3)"
|
||||
stroke="orange"
|
||||
strokeWidth="2"
|
||||
/>
|
||||
<text
|
||||
x={shape === 'diamond' ? size * 0.7 : size / 2}
|
||||
y={size / 2}
|
||||
textAnchor="middle"
|
||||
dy=".35em"
|
||||
fontSize={size * 0.8}
|
||||
fill="orange"
|
||||
fontWeight="bold"
|
||||
>
|
||||
{direction === 'activate' ? '↓' : '↑'}
|
||||
</text>
|
||||
</DirectionIndicatorG>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
383
packages/abacus-react/src/AbacusSVGRenderer.tsx
Normal file
383
packages/abacus-react/src/AbacusSVGRenderer.tsx
Normal file
@@ -0,0 +1,383 @@
|
||||
/**
|
||||
* AbacusSVGRenderer - Shared SVG rendering component (Core Architecture)
|
||||
*
|
||||
* This is the **single SVG renderer** used by both AbacusStatic and AbacusReact to guarantee
|
||||
* pixel-perfect visual consistency. It implements dependency injection to support different
|
||||
* bead components while maintaining identical layout.
|
||||
*
|
||||
* ## Architecture Role:
|
||||
* ```
|
||||
* AbacusStatic + AbacusReact
|
||||
* ↓
|
||||
* calculateStandardDimensions() ← Single source for all layout dimensions
|
||||
* ↓
|
||||
* AbacusSVGRenderer ← This component (shared structure)
|
||||
* ↓
|
||||
* calculateBeadPosition() ← Exact positioning for every bead
|
||||
* ↓
|
||||
* BeadComponent (injected) ← AbacusStaticBead OR AbacusAnimatedBead
|
||||
* ```
|
||||
*
|
||||
* ## Key Features:
|
||||
* - ✅ No "use client" directive - works in React Server Components
|
||||
* - ✅ No hooks or state - pure rendering from props
|
||||
* - ✅ Dependency injection for bead components
|
||||
* - ✅ Supports 3D gradients, background glows, overlays (via props)
|
||||
* - ✅ Same props → same dimensions → same positions → same layout
|
||||
*
|
||||
* ## Why This Matters:
|
||||
* Before this architecture, AbacusStatic and AbacusReact had ~700 lines of duplicate
|
||||
* SVG rendering code with separate dimension calculations. This led to layout inconsistencies.
|
||||
* Now they share this single renderer, eliminating duplication and guaranteeing consistency.
|
||||
*/
|
||||
|
||||
import React from 'react'
|
||||
import type { AbacusLayoutDimensions } from './AbacusUtils'
|
||||
import type { BeadConfig, AbacusCustomStyles, ValidPlaceValues } from './AbacusReact'
|
||||
import { numberToAbacusState, calculateBeadPosition, type AbacusState } from './AbacusUtils'
|
||||
|
||||
/**
|
||||
* Props that bead components must accept
|
||||
*/
|
||||
export interface BeadComponentProps {
|
||||
bead: BeadConfig
|
||||
x: number
|
||||
y: number
|
||||
size: number
|
||||
shape: 'circle' | 'diamond' | 'square'
|
||||
color: string
|
||||
hideInactiveBeads: boolean
|
||||
customStyle?: {
|
||||
fill?: string
|
||||
stroke?: string
|
||||
strokeWidth?: number
|
||||
opacity?: number
|
||||
}
|
||||
onClick?: (bead: BeadConfig, event?: React.MouseEvent) => void
|
||||
onMouseEnter?: (bead: BeadConfig, event?: React.MouseEvent) => void
|
||||
onMouseLeave?: (bead: BeadConfig, event?: React.MouseEvent) => void
|
||||
onRef?: (bead: BeadConfig, element: SVGElement | null) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Props for the SVG renderer
|
||||
*/
|
||||
export interface AbacusSVGRendererProps {
|
||||
// Core data
|
||||
value: number | bigint
|
||||
columns: number
|
||||
state: AbacusState
|
||||
beadConfigs: BeadConfig[][] // Array of columns, each containing beads
|
||||
|
||||
// Layout
|
||||
dimensions: AbacusLayoutDimensions
|
||||
scaleFactor?: number
|
||||
|
||||
// Appearance
|
||||
beadShape: 'circle' | 'diamond' | 'square'
|
||||
colorScheme: string
|
||||
colorPalette: string
|
||||
hideInactiveBeads: boolean
|
||||
frameVisible: boolean
|
||||
showNumbers: boolean
|
||||
customStyles?: AbacusCustomStyles
|
||||
interactive?: boolean // Enable interactive CSS styles
|
||||
|
||||
// Tutorial features
|
||||
highlightColumns?: number[]
|
||||
columnLabels?: string[]
|
||||
|
||||
// 3D Enhancement (optional - only used by AbacusReact)
|
||||
defsContent?: React.ReactNode // Custom defs content (gradients, patterns, etc.)
|
||||
|
||||
// Additional content (overlays, etc.)
|
||||
children?: React.ReactNode // Rendered at the end of the SVG
|
||||
|
||||
// Dependency injection
|
||||
BeadComponent: React.ComponentType<any> // Accept any bead component (base props + extra props)
|
||||
getBeadColor: (bead: BeadConfig, totalColumns: number, colorScheme: string, colorPalette: string) => string
|
||||
|
||||
// Event handlers (optional, passed through to beads)
|
||||
onBeadClick?: (bead: BeadConfig, event?: React.MouseEvent) => void
|
||||
onBeadMouseEnter?: (bead: BeadConfig, event?: React.MouseEvent) => void
|
||||
onBeadMouseLeave?: (bead: BeadConfig, event?: React.MouseEvent) => void
|
||||
onBeadRef?: (bead: BeadConfig, element: SVGElement | null) => void
|
||||
|
||||
// Extra props calculator (for animations, gestures, etc.)
|
||||
// This function is called for each bead to get extra props
|
||||
calculateExtraBeadProps?: (bead: BeadConfig, baseProps: BeadComponentProps) => Record<string, any>
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure SVG renderer for abacus
|
||||
* Uses dependency injection to support both static and animated beads
|
||||
*/
|
||||
export function AbacusSVGRenderer({
|
||||
value,
|
||||
columns,
|
||||
state,
|
||||
beadConfigs,
|
||||
dimensions,
|
||||
scaleFactor = 1,
|
||||
beadShape,
|
||||
colorScheme,
|
||||
colorPalette,
|
||||
hideInactiveBeads,
|
||||
frameVisible,
|
||||
showNumbers,
|
||||
customStyles,
|
||||
interactive = false,
|
||||
highlightColumns = [],
|
||||
columnLabels = [],
|
||||
defsContent,
|
||||
children,
|
||||
BeadComponent,
|
||||
getBeadColor,
|
||||
onBeadClick,
|
||||
onBeadMouseEnter,
|
||||
onBeadMouseLeave,
|
||||
onBeadRef,
|
||||
calculateExtraBeadProps,
|
||||
}: AbacusSVGRendererProps) {
|
||||
const { width, height, rodSpacing, barY, beadSize, barThickness, labelHeight, numbersHeight } = dimensions
|
||||
|
||||
return (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width={width * scaleFactor}
|
||||
height={height * scaleFactor}
|
||||
viewBox={`0 0 ${width} ${height}`}
|
||||
className={`abacus-svg ${hideInactiveBeads ? 'hide-inactive-mode' : ''} ${interactive ? 'interactive' : ''}`}
|
||||
style={{ overflow: 'visible', display: 'block' }}
|
||||
>
|
||||
<defs>
|
||||
<style>{`
|
||||
/* CSS-based opacity system for hidden inactive beads */
|
||||
.abacus-bead {
|
||||
transition: opacity 0.2s ease-in-out;
|
||||
}
|
||||
|
||||
/* Hidden inactive beads are invisible by default */
|
||||
.hide-inactive-mode .abacus-bead.hidden-inactive {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
/* Interactive abacus: When hovering over the abacus, hidden inactive beads become semi-transparent */
|
||||
.abacus-svg.hide-inactive-mode.interactive:hover .abacus-bead.hidden-inactive {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
/* Interactive abacus: When hovering over a specific hidden inactive bead, it becomes fully visible */
|
||||
.hide-inactive-mode.interactive .abacus-bead.hidden-inactive:hover {
|
||||
opacity: 1 !important;
|
||||
}
|
||||
|
||||
/* Non-interactive abacus: Hidden inactive beads always stay at opacity 0 */
|
||||
.abacus-svg.hide-inactive-mode:not(.interactive) .abacus-bead.hidden-inactive {
|
||||
opacity: 0 !important;
|
||||
}
|
||||
`}</style>
|
||||
|
||||
{/* Custom defs content (for 3D gradients, patterns, etc.) */}
|
||||
{defsContent}
|
||||
</defs>
|
||||
|
||||
{/* Background glow effects - rendered behind everything */}
|
||||
{Array.from({ length: columns }, (_, colIndex) => {
|
||||
const placeValue = columns - 1 - colIndex
|
||||
const columnStyles = customStyles?.columns?.[colIndex]
|
||||
const backgroundGlow = columnStyles?.backgroundGlow
|
||||
|
||||
if (!backgroundGlow) return null
|
||||
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2
|
||||
const glowWidth = rodSpacing + (backgroundGlow.spread || 0)
|
||||
const glowHeight = height + (backgroundGlow.spread || 0)
|
||||
|
||||
return (
|
||||
<rect
|
||||
key={`background-glow-pv${placeValue}`}
|
||||
x={x - glowWidth / 2}
|
||||
y={-(backgroundGlow.spread || 0) / 2}
|
||||
width={glowWidth}
|
||||
height={glowHeight}
|
||||
fill={backgroundGlow.fill || 'rgba(59, 130, 246, 0.2)'}
|
||||
filter={backgroundGlow.blur ? `blur(${backgroundGlow.blur}px)` : 'none'}
|
||||
opacity={backgroundGlow.opacity ?? 0.6}
|
||||
rx={8}
|
||||
style={{ pointerEvents: 'none' }}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Column highlights */}
|
||||
{highlightColumns.map((colIndex) => {
|
||||
if (colIndex < 0 || colIndex >= columns) return null
|
||||
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2
|
||||
const highlightWidth = rodSpacing * 0.9
|
||||
const highlightHeight = height - labelHeight - numbersHeight
|
||||
|
||||
return (
|
||||
<rect
|
||||
key={`column-highlight-${colIndex}`}
|
||||
x={x - highlightWidth / 2}
|
||||
y={labelHeight}
|
||||
width={highlightWidth}
|
||||
height={highlightHeight}
|
||||
fill="rgba(59, 130, 246, 0.15)"
|
||||
stroke="rgba(59, 130, 246, 0.4)"
|
||||
strokeWidth={2}
|
||||
rx={6}
|
||||
style={{ pointerEvents: 'none' }}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Column labels */}
|
||||
{columnLabels.map((label, colIndex) => {
|
||||
if (!label || colIndex >= columns) return null
|
||||
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2
|
||||
|
||||
return (
|
||||
<text
|
||||
key={`column-label-${colIndex}`}
|
||||
x={x}
|
||||
y={labelHeight / 2 + 5}
|
||||
textAnchor="middle"
|
||||
fontSize="14"
|
||||
fontWeight="600"
|
||||
fill="rgba(0, 0, 0, 0.7)"
|
||||
style={{ pointerEvents: 'none', userSelect: 'none' }}
|
||||
>
|
||||
{label}
|
||||
</text>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Rods (column posts) */}
|
||||
{frameVisible && beadConfigs.map((_, colIndex) => {
|
||||
const placeValue = columns - 1 - colIndex
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2
|
||||
|
||||
// Apply custom column post styling (column-specific overrides global)
|
||||
const columnStyles = customStyles?.columns?.[colIndex]
|
||||
const globalColumnPosts = customStyles?.columnPosts
|
||||
const rodStyle = {
|
||||
fill: columnStyles?.columnPost?.fill || globalColumnPosts?.fill || 'rgb(0, 0, 0, 0.1)',
|
||||
stroke: columnStyles?.columnPost?.stroke || globalColumnPosts?.stroke || 'none',
|
||||
strokeWidth: columnStyles?.columnPost?.strokeWidth ?? globalColumnPosts?.strokeWidth ?? 0,
|
||||
opacity: columnStyles?.columnPost?.opacity ?? globalColumnPosts?.opacity ?? 1,
|
||||
}
|
||||
|
||||
return (
|
||||
<rect
|
||||
key={`rod-pv${placeValue}`}
|
||||
x={x - dimensions.rodWidth / 2}
|
||||
y={labelHeight}
|
||||
width={dimensions.rodWidth}
|
||||
height={height - labelHeight - numbersHeight}
|
||||
fill={rodStyle.fill}
|
||||
stroke={rodStyle.stroke}
|
||||
strokeWidth={rodStyle.strokeWidth}
|
||||
opacity={rodStyle.opacity}
|
||||
className="column-post"
|
||||
/>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Reckoning bar */}
|
||||
{frameVisible && (
|
||||
<rect
|
||||
x={0}
|
||||
y={barY}
|
||||
width={columns * rodSpacing}
|
||||
height={barThickness}
|
||||
fill={customStyles?.reckoningBar?.fill || 'rgb(0, 0, 0, 0.15)'}
|
||||
stroke={customStyles?.reckoningBar?.stroke || 'rgba(0, 0, 0, 0.3)'}
|
||||
strokeWidth={customStyles?.reckoningBar?.strokeWidth || 2}
|
||||
opacity={customStyles?.reckoningBar?.opacity ?? 1}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Beads - delegated to injected component */}
|
||||
{beadConfigs.map((columnBeads, colIndex) => {
|
||||
const placeValue = columns - 1 - colIndex
|
||||
// Get column state for inactive earth bead positioning
|
||||
const columnState = state[placeValue] || { heavenActive: false, earthActive: 0 }
|
||||
|
||||
return (
|
||||
<g key={`column-${colIndex}`}>
|
||||
{columnBeads.map((bead, beadIndex) => {
|
||||
// Calculate position using shared utility with column state for accurate positioning
|
||||
const position = calculateBeadPosition(bead, dimensions, { earthActive: columnState.earthActive })
|
||||
const color = getBeadColor(bead, columns, colorScheme, colorPalette)
|
||||
|
||||
// Get custom style for this specific bead
|
||||
const customStyle =
|
||||
bead.type === 'heaven'
|
||||
? customStyles?.heavenBeads
|
||||
: customStyles?.earthBeads
|
||||
|
||||
// Build base props
|
||||
const baseProps: BeadComponentProps = {
|
||||
bead,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
size: beadSize,
|
||||
shape: beadShape,
|
||||
color,
|
||||
hideInactiveBeads,
|
||||
customStyle,
|
||||
onClick: onBeadClick,
|
||||
onMouseEnter: onBeadMouseEnter,
|
||||
onMouseLeave: onBeadMouseLeave,
|
||||
onRef: onBeadRef,
|
||||
}
|
||||
|
||||
// Calculate extra props if provided (for animations, etc.)
|
||||
const extraProps = calculateExtraBeadProps?.(bead, baseProps) || {}
|
||||
|
||||
return (
|
||||
<BeadComponent
|
||||
key={`bead-pv${bead.placeValue}-${bead.type}-${bead.position}`}
|
||||
{...baseProps}
|
||||
{...extraProps}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</g>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Column numbers */}
|
||||
{showNumbers && beadConfigs.map((_, colIndex) => {
|
||||
const placeValue = columns - 1 - colIndex
|
||||
const columnState = state[placeValue] || { heavenActive: false, earthActive: 0 }
|
||||
const digit = (columnState.heavenActive ? 5 : 0) + columnState.earthActive
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2
|
||||
|
||||
return (
|
||||
<text
|
||||
key={`number-${colIndex}`}
|
||||
x={x}
|
||||
y={height - numbersHeight / 2 + 5}
|
||||
textAnchor="middle"
|
||||
fontSize={customStyles?.numerals?.fontSize || '16px'}
|
||||
fontWeight={customStyles?.numerals?.fontWeight || '600'}
|
||||
fill={customStyles?.numerals?.color || 'rgba(0, 0, 0, 0.8)'}
|
||||
style={{ pointerEvents: 'none', userSelect: 'none' }}
|
||||
>
|
||||
{digit}
|
||||
</text>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Additional content (overlays, numbers, etc.) */}
|
||||
{children}
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export default AbacusSVGRenderer
|
||||
@@ -7,20 +7,39 @@ import { ABACUS_THEMES } from './AbacusThemes'
|
||||
*
|
||||
* ## Key Features:
|
||||
* - ✅ Works in React Server Components (no "use client")
|
||||
* - ✅ Shares core utilities with AbacusReact (numberToAbacusState, color logic)
|
||||
* - ✅ **Identical layout to AbacusReact** - same props = same exact SVG output
|
||||
* - ✅ No animations, hooks, or client-side JavaScript
|
||||
* - ✅ Lightweight rendering for static displays
|
||||
*
|
||||
* ## Shared Code (No Duplication!):
|
||||
* - Uses `numberToAbacusState()` from AbacusUtils
|
||||
* - Uses same color scheme logic as AbacusReact
|
||||
* - Uses same bead positioning concepts
|
||||
* - Accepts same `customStyles` prop structure
|
||||
* ## Shared Architecture (Zero Duplication!):
|
||||
* Both AbacusStatic and AbacusReact use the **exact same rendering pipeline**:
|
||||
*
|
||||
* ```
|
||||
* calculateStandardDimensions() → AbacusSVGRenderer → calculateBeadPosition()
|
||||
* ↓
|
||||
* ┌───────────────────┴───────────────────┐
|
||||
* ↓ ↓
|
||||
* AbacusStaticBead AbacusAnimatedBead
|
||||
* (Simple SVG) (react-spring)
|
||||
* ```
|
||||
*
|
||||
* - `calculateStandardDimensions()` - Single source of truth for layout (beadSize, gaps, bar position, etc.)
|
||||
* - `AbacusSVGRenderer` - Shared SVG structure with dependency injection for bead components
|
||||
* - `calculateBeadPosition()` - Exact positioning formulas used by both variants
|
||||
* - `AbacusStaticBead` - RSC-compatible simple SVG shapes (this component)
|
||||
* - `AbacusAnimatedBead` - Client component with animations (AbacusReact)
|
||||
*
|
||||
* ## Visual Consistency Guarantee:
|
||||
* Both AbacusStatic and AbacusReact produce **pixel-perfect identical output** for the same props.
|
||||
* This ensures previews match interactive versions, PDFs match web displays, etc.
|
||||
*
|
||||
* **Architecture benefit:** ~560 lines of duplicate code eliminated. Same props = same dimensions = same positions = same layout.
|
||||
*
|
||||
* ## When to Use:
|
||||
* - React Server Components (Next.js App Router)
|
||||
* - Static site generation
|
||||
* - Non-interactive previews
|
||||
* - PDF generation
|
||||
* - Server-side rendering without hydration
|
||||
*/
|
||||
const meta = {
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
/**
|
||||
* AbacusStatic - Server Component compatible static abacus
|
||||
*
|
||||
* Shares core logic with AbacusReact but uses static rendering without hooks/animations.
|
||||
* Reuses: numberToAbacusState, getBeadColor logic, positioning calculations
|
||||
* Different: No hooks, no animations, no interactions, simplified rendering
|
||||
* Shares layout and rendering with AbacusReact through dependency injection.
|
||||
* Uses standard dimensions to ensure same props = same exact visual output.
|
||||
* Reuses: AbacusSVGRenderer for structure, shared dimension/position calculators
|
||||
* Different: No hooks, no animations, no interactions, simplified bead rendering
|
||||
*/
|
||||
|
||||
import { numberToAbacusState, calculateAbacusDimensions } from './AbacusUtils'
|
||||
import { numberToAbacusState, calculateStandardDimensions } from './AbacusUtils'
|
||||
import { AbacusSVGRenderer } from './AbacusSVGRenderer'
|
||||
import { AbacusStaticBead } from './AbacusStaticBead'
|
||||
import type {
|
||||
AbacusCustomStyles,
|
||||
@@ -30,7 +32,7 @@ export interface AbacusStaticConfig {
|
||||
columnLabels?: string[]
|
||||
}
|
||||
|
||||
// Shared color logic from AbacusReact (simplified for static use)
|
||||
// Shared color logic (matches AbacusReact)
|
||||
function getBeadColor(
|
||||
bead: BeadConfig,
|
||||
totalColumns: number,
|
||||
@@ -87,37 +89,6 @@ function getBeadColor(
|
||||
return '#3b82f6'
|
||||
}
|
||||
|
||||
// Calculate bead positions (simplified from AbacusReact)
|
||||
function calculateBeadPosition(
|
||||
bead: BeadConfig,
|
||||
dimensions: { beadSize: number; rodSpacing: number; heavenY: number; earthY: number; barY: number; totalColumns: number }
|
||||
): { x: number; y: number } {
|
||||
const { beadSize, rodSpacing, heavenY, earthY, barY, totalColumns } = dimensions
|
||||
|
||||
// X position based on place value (rightmost = ones place)
|
||||
const columnIndex = totalColumns - 1 - bead.placeValue
|
||||
const x = columnIndex * rodSpacing + rodSpacing / 2
|
||||
|
||||
// Y position based on bead type and active state
|
||||
if (bead.type === 'heaven') {
|
||||
// Heaven bead: if active, near bar; if inactive, at top
|
||||
const y = bead.active ? barY - beadSize - 5 : heavenY
|
||||
return { x, y }
|
||||
} else {
|
||||
// Earth bead: if active, stack up from bar; if inactive, at bottom
|
||||
const earthSpacing = beadSize + 4
|
||||
if (bead.active) {
|
||||
// Active earth beads stack upward from the bar
|
||||
const y = barY + beadSize / 2 + 10 + bead.position * earthSpacing
|
||||
return { x, y }
|
||||
} else {
|
||||
// Inactive earth beads rest at the bottom
|
||||
const y = earthY + (bead.position - 2) * earthSpacing
|
||||
return { x, y }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AbacusStatic - Pure static abacus component (Server Component compatible)
|
||||
*/
|
||||
@@ -175,196 +146,38 @@ export function AbacusStatic({
|
||||
beadConfigs.push(beads)
|
||||
}
|
||||
|
||||
// Calculate dimensions using shared utility
|
||||
const { width, height } = calculateAbacusDimensions({
|
||||
// Calculate standard dimensions (same as AbacusReact!)
|
||||
const dimensions = calculateStandardDimensions({
|
||||
columns: effectiveColumns,
|
||||
scaleFactor,
|
||||
showNumbers: !!showNumbers,
|
||||
columnLabels,
|
||||
})
|
||||
|
||||
// Layout constants (must match calculateAbacusDimensions)
|
||||
const beadSize = 20
|
||||
const rodSpacing = 40
|
||||
const heavenHeight = 60
|
||||
const earthHeight = 120
|
||||
const barHeight = 10
|
||||
const padding = 20
|
||||
const numberHeightCalc = showNumbers ? 30 : 0
|
||||
const labelHeight = columnLabels.length > 0 ? 30 : 0
|
||||
|
||||
const dimensions = {
|
||||
width,
|
||||
height,
|
||||
beadSize,
|
||||
rodSpacing,
|
||||
heavenY: padding + labelHeight + heavenHeight / 3,
|
||||
earthY: padding + labelHeight + heavenHeight + barHeight + earthHeight * 0.7,
|
||||
barY: padding + labelHeight + heavenHeight,
|
||||
padding,
|
||||
totalColumns: effectiveColumns,
|
||||
}
|
||||
|
||||
// Compact mode hides frame
|
||||
const effectiveFrameVisible = compact ? false : frameVisible
|
||||
|
||||
// Use shared renderer with static bead component
|
||||
return (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width={width * scaleFactor}
|
||||
height={height * scaleFactor}
|
||||
viewBox={`0 0 ${width} ${height}`}
|
||||
className={`abacus-svg ${hideInactiveBeads ? 'hide-inactive-mode' : ''}`}
|
||||
style={{ overflow: 'visible', display: 'block' }}
|
||||
>
|
||||
<defs>
|
||||
<style>{`
|
||||
.abacus-bead {
|
||||
transition: opacity 0.2s ease-in-out;
|
||||
}
|
||||
.hide-inactive-mode .abacus-bead.hidden-inactive {
|
||||
opacity: 0 !important;
|
||||
}
|
||||
`}</style>
|
||||
</defs>
|
||||
|
||||
{/* Column highlights */}
|
||||
{highlightColumns.map((colIndex) => {
|
||||
if (colIndex < 0 || colIndex >= effectiveColumns) return null
|
||||
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2 + padding
|
||||
const highlightWidth = rodSpacing * 0.9
|
||||
const highlightHeight = height - padding * 2 - numberHeightCalc - labelHeight
|
||||
|
||||
return (
|
||||
<rect
|
||||
key={`column-highlight-${colIndex}`}
|
||||
x={x - highlightWidth / 2}
|
||||
y={padding + labelHeight}
|
||||
width={highlightWidth}
|
||||
height={highlightHeight}
|
||||
fill="rgba(59, 130, 246, 0.15)"
|
||||
stroke="rgba(59, 130, 246, 0.4)"
|
||||
strokeWidth={2}
|
||||
rx={6}
|
||||
style={{ pointerEvents: 'none' }}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Column labels */}
|
||||
{columnLabels.map((label, colIndex) => {
|
||||
if (!label || colIndex >= effectiveColumns) return null
|
||||
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2 + padding
|
||||
|
||||
return (
|
||||
<text
|
||||
key={`column-label-${colIndex}`}
|
||||
x={x}
|
||||
y={padding + 15}
|
||||
textAnchor="middle"
|
||||
fontSize="14"
|
||||
fontWeight="600"
|
||||
fill="rgba(0, 0, 0, 0.7)"
|
||||
style={{ pointerEvents: 'none', userSelect: 'none' }}
|
||||
>
|
||||
{label}
|
||||
</text>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Rods (column posts) */}
|
||||
{effectiveFrameVisible && beadConfigs.map((_, colIndex) => {
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2 + padding
|
||||
|
||||
return (
|
||||
<rect
|
||||
key={`rod-${colIndex}`}
|
||||
x={x - 3}
|
||||
y={padding + labelHeight}
|
||||
width={6}
|
||||
height={heavenHeight + earthHeight + barHeight}
|
||||
fill={customStyles?.columnPosts?.fill || 'rgb(0, 0, 0, 0.1)'}
|
||||
stroke={customStyles?.columnPosts?.stroke || 'rgba(0, 0, 0, 0.2)'}
|
||||
strokeWidth={customStyles?.columnPosts?.strokeWidth || 1}
|
||||
opacity={customStyles?.columnPosts?.opacity ?? 1}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Reckoning bar */}
|
||||
{effectiveFrameVisible && (
|
||||
<rect
|
||||
x={padding}
|
||||
y={dimensions.barY}
|
||||
width={effectiveColumns * rodSpacing}
|
||||
height={barHeight}
|
||||
fill={customStyles?.reckoningBar?.fill || 'rgb(0, 0, 0, 0.15)'}
|
||||
stroke={customStyles?.reckoningBar?.stroke || 'rgba(0, 0, 0, 0.3)'}
|
||||
strokeWidth={customStyles?.reckoningBar?.strokeWidth || 2}
|
||||
opacity={customStyles?.reckoningBar?.opacity ?? 1}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Beads */}
|
||||
{beadConfigs.map((columnBeads, colIndex) => {
|
||||
const placeValue = effectiveColumns - 1 - colIndex
|
||||
|
||||
return (
|
||||
<g key={`column-${colIndex}`}>
|
||||
{columnBeads.map((bead, beadIndex) => {
|
||||
const position = calculateBeadPosition(bead, dimensions)
|
||||
|
||||
// Adjust X for padding
|
||||
position.x += padding
|
||||
|
||||
const color = getBeadColor(bead, effectiveColumns, colorScheme, colorPalette)
|
||||
|
||||
return (
|
||||
<AbacusStaticBead
|
||||
key={`bead-${colIndex}-${beadIndex}`}
|
||||
bead={bead}
|
||||
x={position.x}
|
||||
y={position.y}
|
||||
size={beadSize}
|
||||
shape={beadShape}
|
||||
color={color}
|
||||
hideInactiveBeads={hideInactiveBeads}
|
||||
customStyle={
|
||||
bead.type === 'heaven'
|
||||
? customStyles?.heavenBeads
|
||||
: customStyles?.earthBeads
|
||||
}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</g>
|
||||
)
|
||||
})}
|
||||
|
||||
{/* Column numbers */}
|
||||
{showNumbers && beadConfigs.map((_, colIndex) => {
|
||||
const placeValue = effectiveColumns - 1 - colIndex
|
||||
const columnState = state[placeValue] || { heavenActive: false, earthActive: 0 }
|
||||
const digit = (columnState.heavenActive ? 5 : 0) + columnState.earthActive
|
||||
const x = colIndex * rodSpacing + rodSpacing / 2 + padding
|
||||
|
||||
return (
|
||||
<text
|
||||
key={`number-${colIndex}`}
|
||||
x={x}
|
||||
y={height - padding + 5}
|
||||
textAnchor="middle"
|
||||
fontSize={customStyles?.numerals?.fontSize || 16}
|
||||
fontWeight={customStyles?.numerals?.fontWeight || '600'}
|
||||
fill={customStyles?.numerals?.color || 'rgba(0, 0, 0, 0.8)'}
|
||||
style={{ pointerEvents: 'none', userSelect: 'none' }}
|
||||
>
|
||||
{digit}
|
||||
</text>
|
||||
)
|
||||
})}
|
||||
</svg>
|
||||
<AbacusSVGRenderer
|
||||
value={value}
|
||||
columns={effectiveColumns}
|
||||
state={state}
|
||||
beadConfigs={beadConfigs}
|
||||
dimensions={dimensions}
|
||||
scaleFactor={scaleFactor}
|
||||
beadShape={beadShape}
|
||||
colorScheme={colorScheme}
|
||||
colorPalette={colorPalette}
|
||||
hideInactiveBeads={hideInactiveBeads}
|
||||
frameVisible={effectiveFrameVisible}
|
||||
showNumbers={!!showNumbers}
|
||||
customStyles={customStyles}
|
||||
highlightColumns={highlightColumns}
|
||||
columnLabels={columnLabels}
|
||||
BeadComponent={AbacusStaticBead}
|
||||
getBeadColor={getBeadColor}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ export function AbacusStaticBead({
|
||||
return (
|
||||
<g
|
||||
className={`abacus-bead ${bead.active ? 'active' : 'inactive'} ${hideInactiveBeads && !bead.active ? 'hidden-inactive' : ''}`}
|
||||
data-testid={`bead-place-${bead.placeValue}-${bead.type}${bead.type === 'earth' ? `-pos-${bead.position}` : ''}`}
|
||||
transform={transform}
|
||||
style={{ transition: 'opacity 0.2s ease-in-out' }}
|
||||
>
|
||||
|
||||
@@ -358,13 +358,114 @@ function getPlaceName(place: number): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the natural dimensions of an abacus SVG
|
||||
* This uses the same logic as AbacusStatic to ensure consistency
|
||||
* Complete layout dimensions for abacus rendering
|
||||
* Used by both static and dynamic rendering to ensure identical layouts
|
||||
*/
|
||||
export interface AbacusLayoutDimensions {
|
||||
// SVG canvas size
|
||||
width: number
|
||||
height: number
|
||||
|
||||
// Bead and spacing
|
||||
beadSize: number
|
||||
rodSpacing: number // Same as columnSpacing
|
||||
rodWidth: number
|
||||
barThickness: number
|
||||
|
||||
// Gaps and positioning
|
||||
heavenEarthGap: number // Gap between heaven and earth sections (where bar sits)
|
||||
activeGap: number // Gap between active beads and reckoning bar
|
||||
inactiveGap: number // Gap between inactive beads and active beads/bar
|
||||
adjacentSpacing: number // Minimal spacing for adjacent beads of same type
|
||||
|
||||
// Key Y positions (absolute coordinates)
|
||||
barY: number // Y position of reckoning bar
|
||||
heavenY: number // Y position where inactive heaven beads rest
|
||||
earthY: number // Y position where inactive earth beads rest
|
||||
|
||||
// Padding and extras
|
||||
padding: number
|
||||
labelHeight: number
|
||||
numbersHeight: number
|
||||
|
||||
// Derived values
|
||||
totalColumns: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate standard layout dimensions for abacus rendering
|
||||
* This ensures both static and dynamic rendering use identical geometry
|
||||
* Same props = same exact visual output
|
||||
*
|
||||
* @param columns - Number of columns in the abacus
|
||||
* @param scaleFactor - Size multiplier (default: 1)
|
||||
* @param showNumbers - Whether numbers are shown below columns
|
||||
* @param columnLabels - Array of column labels (if any)
|
||||
* @returns Object with width and height in pixels (at scale=1)
|
||||
* @returns Complete layout dimensions object
|
||||
*/
|
||||
export function calculateStandardDimensions({
|
||||
columns,
|
||||
scaleFactor = 1,
|
||||
showNumbers = false,
|
||||
columnLabels = [],
|
||||
}: {
|
||||
columns: number
|
||||
scaleFactor?: number
|
||||
showNumbers?: boolean
|
||||
columnLabels?: string[]
|
||||
}): AbacusLayoutDimensions {
|
||||
// Standard dimensions - used by both AbacusStatic and AbacusReact
|
||||
const rodWidth = 3 * scaleFactor
|
||||
const beadSize = 12 * scaleFactor
|
||||
const adjacentSpacing = 0.5 * scaleFactor
|
||||
const columnSpacing = 25 * scaleFactor
|
||||
const heavenEarthGap = 30 * scaleFactor
|
||||
const barThickness = 2 * scaleFactor
|
||||
|
||||
// Positioning gaps
|
||||
const activeGap = 1 * scaleFactor
|
||||
const inactiveGap = 8 * scaleFactor
|
||||
|
||||
// Calculate total dimensions
|
||||
const totalWidth = columns * columnSpacing
|
||||
const baseHeight = heavenEarthGap + 5 * (beadSize + 4 * scaleFactor) + 10 * scaleFactor
|
||||
|
||||
// Extra spacing
|
||||
const numbersSpace = showNumbers ? 40 * scaleFactor : 0
|
||||
const labelSpace = columnLabels.length > 0 ? 30 * scaleFactor : 0
|
||||
const padding = 0 // No padding - keeps layout clean
|
||||
|
||||
const totalHeight = baseHeight + numbersSpace + labelSpace
|
||||
|
||||
// Key Y positions - bar is at heavenEarthGap from top
|
||||
const barY = heavenEarthGap + labelSpace
|
||||
const heavenY = labelSpace + activeGap // Top area for inactive heaven beads
|
||||
const earthY = barY + barThickness + (4 * beadSize) + activeGap + inactiveGap // Bottom area for inactive earth
|
||||
|
||||
return {
|
||||
width: totalWidth,
|
||||
height: totalHeight,
|
||||
beadSize,
|
||||
rodSpacing: columnSpacing,
|
||||
rodWidth,
|
||||
barThickness,
|
||||
heavenEarthGap,
|
||||
activeGap,
|
||||
inactiveGap,
|
||||
adjacentSpacing,
|
||||
barY,
|
||||
heavenY,
|
||||
earthY,
|
||||
padding,
|
||||
labelHeight: labelSpace,
|
||||
numbersHeight: numbersSpace,
|
||||
totalColumns: columns,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use calculateStandardDimensions instead for full layout info
|
||||
* This function only returns width/height for backward compatibility
|
||||
*/
|
||||
export function calculateAbacusDimensions({
|
||||
columns,
|
||||
@@ -375,18 +476,87 @@ export function calculateAbacusDimensions({
|
||||
showNumbers?: boolean
|
||||
columnLabels?: string[]
|
||||
}): { width: number; height: number } {
|
||||
// Constants matching AbacusStatic
|
||||
const beadSize = 20
|
||||
const rodSpacing = 40
|
||||
const heavenHeight = 60
|
||||
const earthHeight = 120
|
||||
const barHeight = 10
|
||||
const padding = 20
|
||||
const numberHeightCalc = showNumbers ? 30 : 0
|
||||
const labelHeight = columnLabels.length > 0 ? 30 : 0
|
||||
|
||||
const width = columns * rodSpacing + padding * 2
|
||||
const height = heavenHeight + earthHeight + barHeight + padding * 2 + numberHeightCalc + labelHeight
|
||||
|
||||
return { width, height }
|
||||
// Redirect to new function for backward compatibility
|
||||
const dims = calculateStandardDimensions({ columns, scaleFactor: 1, showNumbers, columnLabels })
|
||||
return { width: dims.width, height: dims.height }
|
||||
}
|
||||
|
||||
/**
|
||||
* Simplified bead config for position calculation
|
||||
* (Compatible with BeadConfig from AbacusReact)
|
||||
*/
|
||||
export interface BeadPositionConfig {
|
||||
type: 'heaven' | 'earth'
|
||||
active: boolean
|
||||
position: number // 0 for heaven, 0-3 for earth
|
||||
placeValue: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Column state needed for earth bead positioning
|
||||
* (Required to calculate inactive earth bead positions correctly)
|
||||
*/
|
||||
export interface ColumnStateForPositioning {
|
||||
earthActive: number // Number of active earth beads (0-4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the x,y position for a bead based on standard layout dimensions
|
||||
* This ensures both static and dynamic rendering position beads identically
|
||||
* Uses exact Typst formulas from the original implementation
|
||||
*
|
||||
* @param bead - Bead configuration
|
||||
* @param dimensions - Layout dimensions from calculateStandardDimensions
|
||||
* @param columnState - Optional column state (required for inactive earth beads)
|
||||
* @returns Object with x and y coordinates
|
||||
*/
|
||||
export function calculateBeadPosition(
|
||||
bead: BeadPositionConfig,
|
||||
dimensions: AbacusLayoutDimensions,
|
||||
columnState?: ColumnStateForPositioning
|
||||
): { x: number; y: number } {
|
||||
const { beadSize, rodSpacing, heavenEarthGap, barThickness, activeGap, inactiveGap, adjacentSpacing, totalColumns } = dimensions
|
||||
|
||||
// X position based on place value (rightmost = ones place)
|
||||
const columnIndex = totalColumns - 1 - bead.placeValue
|
||||
const x = columnIndex * rodSpacing + rodSpacing / 2
|
||||
|
||||
// Y position based on bead type and active state
|
||||
// These formulas match the original Typst implementation exactly
|
||||
if (bead.type === 'heaven') {
|
||||
if (bead.active) {
|
||||
// Active heaven bead: positioned close to reckoning bar (Typst line 175)
|
||||
const y = heavenEarthGap - beadSize / 2 - activeGap
|
||||
return { x, y }
|
||||
} else {
|
||||
// Inactive heaven bead: positioned away from reckoning bar (Typst line 178)
|
||||
const y = heavenEarthGap - inactiveGap - beadSize / 2
|
||||
return { x, y }
|
||||
}
|
||||
} else {
|
||||
// Earth bead positioning (Typst lines 249-261)
|
||||
const earthActive = columnState?.earthActive ?? 0
|
||||
|
||||
if (bead.active) {
|
||||
// Active beads: positioned near reckoning bar, adjacent beads touch (Typst line 251)
|
||||
const y = heavenEarthGap + barThickness + activeGap + beadSize / 2 +
|
||||
bead.position * (beadSize + adjacentSpacing)
|
||||
return { x, y }
|
||||
} else {
|
||||
// Inactive beads: positioned after active beads + gap (Typst lines 254-261)
|
||||
let y: number
|
||||
if (earthActive > 0) {
|
||||
// Position after the last active bead + gap, then adjacent inactive beads touch (Typst line 256)
|
||||
y = heavenEarthGap + barThickness + activeGap + beadSize / 2 +
|
||||
(earthActive - 1) * (beadSize + adjacentSpacing) +
|
||||
beadSize / 2 + inactiveGap + beadSize / 2 +
|
||||
(bead.position - earthActive) * (beadSize + adjacentSpacing)
|
||||
} else {
|
||||
// No active beads: position after reckoning bar + gap, adjacent inactive beads touch (Typst line 259)
|
||||
y = heavenEarthGap + barThickness + inactiveGap + beadSize / 2 +
|
||||
bead.position * (beadSize + adjacentSpacing)
|
||||
}
|
||||
return { x, y }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,8 @@ export {
|
||||
validateAbacusValue,
|
||||
areStatesEqual,
|
||||
calculateAbacusDimensions,
|
||||
calculateStandardDimensions, // NEW: Shared layout calculator
|
||||
calculateBeadPosition, // NEW: Bead position calculator
|
||||
} from "./AbacusUtils";
|
||||
export type {
|
||||
BeadState,
|
||||
@@ -55,6 +57,8 @@ export type {
|
||||
BeadDiffResult,
|
||||
BeadDiffOutput,
|
||||
PlaceValueBasedBead,
|
||||
AbacusLayoutDimensions, // NEW: Complete layout dimensions type
|
||||
BeadPositionConfig, // NEW: Bead config for position calculation
|
||||
} from "./AbacusUtils";
|
||||
|
||||
export { useAbacusDiff, useAbacusState } from "./AbacusHooks";
|
||||
|
||||
Reference in New Issue
Block a user