mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 03:14:11 +00:00
Compare commits
5
Commits
ae6cdc88da
...
8446eaba94
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8446eaba94 | ||
|
|
7c264ee447 | ||
|
|
127bd033b3 | ||
|
|
e691acc45b | ||
|
|
380c0854f8 |
@@ -16,6 +16,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
||||
|
||||
### Changed
|
||||
|
||||
- **viewer**: Refine the 3D turntable viewer bottom bar into a unified single row with a centered cyclic ruler slider, fixed center indicator, degree tooltip on drag, and ±15° step buttons
|
||||
|
||||
## [0.5.1] - 2026-10-06
|
||||
|
||||
### Added
|
||||
|
||||
@@ -2,8 +2,8 @@ import { FileTypeDefinition } from '../types'
|
||||
import ModelViewer from './model-viewer'
|
||||
|
||||
export function secondToDegree(second: number): number {
|
||||
const frameIndex = Math.min(Math.max(0, Math.floor(second * 6)), 23)
|
||||
return frameIndex * 15
|
||||
const frameIndex = Math.min(Math.max(0, Math.round(second * 6)), 23)
|
||||
return (frameIndex * 15) % 360
|
||||
}
|
||||
|
||||
export const modelTypeDefinition: FileTypeDefinition = {
|
||||
|
||||
@@ -44,41 +44,41 @@ export const ModelControlBar: React.FC<ModelControlBarProps> = ({
|
||||
document.body.removeChild(a)
|
||||
}
|
||||
|
||||
const handleStepDegree = (delta: number) => {
|
||||
const next = (((currentDegree + delta) % 360) + 360) % 360
|
||||
onSeekDegree(next)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'transition-all duration-300 ease-in-out z-20 text-foreground',
|
||||
isFullScreen
|
||||
? 'absolute bottom-0 left-0 right-0 px-4 py-3 bg-card/90 backdrop-blur-md border-t border-border'
|
||||
: 'relative w-full bg-card border-t border-border px-4 py-3 z-10 transition-colors duration-200',
|
||||
? 'absolute bottom-0 left-0 right-0 px-4 py-2.5 bg-card/90 backdrop-blur-md border-t border-border'
|
||||
: 'relative w-full bg-card border-t border-border px-4 py-2.5 z-10 transition-colors duration-200',
|
||||
)}
|
||||
>
|
||||
{/* Top: Ruler Slider across full width */}
|
||||
<div className="mb-3 px-1">
|
||||
<RulerSlider currentDegree={currentDegree} onChange={onSeekDegree} />
|
||||
</div>
|
||||
|
||||
{/* Bottom: Lower Controls Row */}
|
||||
<div className="flex items-center justify-between text-foreground">
|
||||
{/* Unified Single Row Controls */}
|
||||
<div className="flex items-center justify-between text-foreground gap-4">
|
||||
{/* Left Side: Play/Pause, Degree Readout, Reset Rotation */}
|
||||
<div className="flex items-center gap-4">
|
||||
<div className="flex items-center gap-3 justify-start min-w-0 flex-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={togglePlay}
|
||||
aria-label={isPlaying ? 'Pause' : 'Play'}
|
||||
className="hover:text-primary transition-colors"
|
||||
className="hover:text-primary transition-colors shrink-0"
|
||||
data-testid="play-toggle"
|
||||
data-playing={isPlaying}
|
||||
>
|
||||
{isPlaying ? (
|
||||
<Pause className="w-6 h-6 fill-current" />
|
||||
<Pause className="w-5 h-5 fill-current" />
|
||||
) : (
|
||||
<Play className="w-6 h-6 fill-current" />
|
||||
<Play className="w-5 h-5 fill-current" />
|
||||
)}
|
||||
</button>
|
||||
|
||||
<div
|
||||
className="flex items-center gap-1 text-sm font-medium tabular-nums select-none min-w-[90px]"
|
||||
className="flex items-center text-sm font-medium tabular-nums select-none shrink-0"
|
||||
data-testid="degree-readout"
|
||||
>
|
||||
<span className="text-muted-foreground font-mono">{currentDegree}° / 360°</span>
|
||||
@@ -89,16 +89,47 @@ export const ModelControlBar: React.FC<ModelControlBarProps> = ({
|
||||
onClick={onResetRotation}
|
||||
title="Reset to 0°"
|
||||
aria-label="Reset rotation to 0°"
|
||||
className="p-1.5 rounded-lg hover:bg-muted text-muted-foreground hover:text-foreground transition-colors"
|
||||
className="p-1.5 rounded-lg hover:bg-muted text-muted-foreground hover:text-foreground transition-colors shrink-0"
|
||||
>
|
||||
<RotateCcw className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Center: RulerSlider with minus & plus buttons */}
|
||||
<div className="flex items-center gap-1.5 w-full max-w-[320px] justify-center mx-auto shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleStepDegree(-15)}
|
||||
title="Decrease 15°"
|
||||
aria-label="Decrease 15°"
|
||||
className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground transition-colors shrink-0"
|
||||
data-testid="ruler-step-minus"
|
||||
>
|
||||
<Minus className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
|
||||
<RulerSlider
|
||||
currentDegree={currentDegree}
|
||||
onChange={onSeekDegree}
|
||||
className="flex-1 min-w-[140px]"
|
||||
/>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleStepDegree(15)}
|
||||
title="Increase 15°"
|
||||
aria-label="Increase 15°"
|
||||
className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground transition-colors shrink-0"
|
||||
data-testid="ruler-step-plus"
|
||||
>
|
||||
<Plus className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Right Side: Zoom controls, Download, Fullscreen */}
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-2 justify-end min-w-0 flex-1">
|
||||
{/* Zoom controls */}
|
||||
<div className="flex items-center gap-1 bg-muted/60 rounded-lg p-0.5 border border-border/40">
|
||||
<div className="flex items-center gap-1 bg-muted/60 rounded-lg p-0.5 border border-border/40 shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onZoomChange(Math.max(0.2, zoom - 0.2))}
|
||||
@@ -132,7 +163,7 @@ export const ModelControlBar: React.FC<ModelControlBarProps> = ({
|
||||
onClick={handleDownload}
|
||||
title="Download asset"
|
||||
aria-label="Download asset"
|
||||
className="p-1.5 rounded-lg hover:bg-muted text-muted-foreground hover:text-foreground transition-colors"
|
||||
className="p-1.5 rounded-lg hover:bg-muted text-muted-foreground hover:text-foreground transition-colors shrink-0"
|
||||
>
|
||||
<Download className="w-4 h-4" />
|
||||
</button>
|
||||
@@ -144,7 +175,7 @@ export const ModelControlBar: React.FC<ModelControlBarProps> = ({
|
||||
onClick={toggleFullScreen}
|
||||
title={isFullScreen ? 'Exit fullscreen' : 'Fullscreen'}
|
||||
aria-label={isFullScreen ? 'Exit fullscreen' : 'Fullscreen'}
|
||||
className="p-1.5 rounded-lg hover:bg-muted text-muted-foreground hover:text-foreground transition-colors"
|
||||
className="p-1.5 rounded-lg hover:bg-muted text-muted-foreground hover:text-foreground transition-colors shrink-0"
|
||||
>
|
||||
{isFullScreen ? <Minimize className="w-4 h-4" /> : <Maximize className="w-4 h-4" />}
|
||||
</button>
|
||||
|
||||
@@ -104,6 +104,14 @@ describe('ModelViewer', () => {
|
||||
fireEvent.keyDown(window, { key: 'ArrowRight' })
|
||||
expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(1 / 6, 2))
|
||||
|
||||
// Press ArrowRight second time -> advance to frame 2 (30 deg = 2/6 sec)
|
||||
fireEvent.keyDown(window, { key: 'ArrowRight' })
|
||||
expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(2 / 6, 2))
|
||||
|
||||
// Press ArrowLeft -> back 1 frame (frame 1 = 1/6 sec)
|
||||
fireEvent.keyDown(window, { key: 'ArrowLeft' })
|
||||
expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(1 / 6, 2))
|
||||
|
||||
// Press ArrowLeft -> back 1 frame (frame 0 = 0 sec)
|
||||
fireEvent.keyDown(window, { key: 'ArrowLeft' })
|
||||
expect(onTimeUpdate).toHaveBeenCalledWith(0)
|
||||
@@ -171,4 +179,22 @@ describe('ModelViewer', () => {
|
||||
expect(getByTestId('test-sidebar')).toBeDefined()
|
||||
expect(getByTestId('model-viewer-surface')).toBeDefined()
|
||||
})
|
||||
|
||||
it('steps rotation by 15° forward and backward via plus and minus buttons', () => {
|
||||
const onTimeUpdate = vi.fn()
|
||||
const { getByTestId } = render(
|
||||
<ModelViewer file={mockAsset} startTime={0} onTimeUpdate={onTimeUpdate} />,
|
||||
)
|
||||
|
||||
const plusBtn = getByTestId('ruler-step-plus')
|
||||
const minusBtn = getByTestId('ruler-step-minus')
|
||||
|
||||
// Click plus button at 0° -> seeks to 15° (frame 1, 1/6s)
|
||||
fireEvent.click(plusBtn)
|
||||
expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(1 / 6, 2))
|
||||
|
||||
// Click minus button at 0° (or current) -> wraps backward
|
||||
fireEvent.click(minusBtn)
|
||||
expect(onTimeUpdate).toHaveBeenCalledWith(0)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -167,11 +167,11 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
togglePlay()
|
||||
} else if (e.key === 'ArrowLeft') {
|
||||
e.preventDefault()
|
||||
const currentFrame = Math.floor(currentTime * FPS)
|
||||
const currentFrame = Math.round(currentTime * FPS)
|
||||
seekToFrame(currentFrame - 1)
|
||||
} else if (e.key === 'ArrowRight') {
|
||||
e.preventDefault()
|
||||
const currentFrame = Math.floor(currentTime * FPS)
|
||||
const currentFrame = Math.round(currentTime * FPS)
|
||||
seekToFrame(currentFrame + 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,54 +8,134 @@ describe('RulerSlider', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
it('renders slider and shows current degree text', () => {
|
||||
|
||||
it('renders slider with fixed center indicator and aria values', () => {
|
||||
const onChange = vi.fn()
|
||||
const { getByRole, getAllByText } = render(
|
||||
const { getByRole, getByTestId, queryByTestId } = render(
|
||||
<RulerSlider currentDegree={90} onChange={onChange} />,
|
||||
)
|
||||
|
||||
const slider = getByRole('slider')
|
||||
expect(slider).toBeDefined()
|
||||
expect(slider.getAttribute('aria-valuenow')).toBe('90')
|
||||
expect(getAllByText('90°').length).toBeGreaterThan(0)
|
||||
expect(slider.getAttribute('aria-valuetext')).toBe('90°')
|
||||
|
||||
// Fixed center indicator exists
|
||||
expect(getByTestId('ruler-center-indicator')).toBeDefined()
|
||||
|
||||
// Tooltip should not be rendered while not dragging
|
||||
expect(queryByTestId('ruler-slider-tooltip')).toBeNull()
|
||||
})
|
||||
|
||||
it('handles keyboard navigation with 15 degree snapping', () => {
|
||||
it('renders ticks at 15° intervals, longer at 60° intervals, and no numeric tick labels', () => {
|
||||
const onChange = vi.fn()
|
||||
const { container, getAllByTestId } = render(
|
||||
<RulerSlider currentDegree={0} onChange={onChange} />,
|
||||
)
|
||||
|
||||
// Major ticks at multiples of 60°
|
||||
const ticks0 = getAllByTestId('tick-0')
|
||||
expect(ticks0.length).toBeGreaterThan(0)
|
||||
expect(ticks0[0]?.getAttribute('data-major')).toBe('true')
|
||||
|
||||
const ticks60 = getAllByTestId('tick-60')
|
||||
expect(ticks60.length).toBeGreaterThan(0)
|
||||
expect(ticks60[0]?.getAttribute('data-major')).toBe('true')
|
||||
|
||||
// Regular tick at 15° is not major
|
||||
const ticks15 = getAllByTestId('tick-15')
|
||||
expect(ticks15.length).toBeGreaterThan(0)
|
||||
expect(ticks15[0]?.getAttribute('data-major')).toBe('false')
|
||||
|
||||
// No numeric degree text inside the slider track
|
||||
expect(container.textContent).toBe('')
|
||||
})
|
||||
|
||||
it('shows tooltip displaying degree while dragging and hides on pointer up', () => {
|
||||
const onChange = vi.fn()
|
||||
const { getByRole, getByTestId, queryByTestId } = render(
|
||||
<RulerSlider currentDegree={45} onChange={onChange} />,
|
||||
)
|
||||
|
||||
const slider = getByRole('slider')
|
||||
slider.setPointerCapture = vi.fn()
|
||||
slider.releasePointerCapture = vi.fn()
|
||||
|
||||
// Initially no tooltip
|
||||
expect(queryByTestId('ruler-slider-tooltip')).toBeNull()
|
||||
|
||||
// Start dragging
|
||||
fireEvent.pointerDown(slider, { clientX: 100, button: 0, pointerId: 1 })
|
||||
const tooltip = getByTestId('ruler-slider-tooltip')
|
||||
expect(tooltip).toBeDefined()
|
||||
expect(tooltip.textContent).toBe('45°')
|
||||
|
||||
// Release drag
|
||||
fireEvent.pointerUp(slider, { clientX: 100, button: 0, pointerId: 1 })
|
||||
expect(queryByTestId('ruler-slider-tooltip')).toBeNull()
|
||||
})
|
||||
|
||||
it('handles keyboard navigation with 15 degree stepping and wrapping', () => {
|
||||
const onChange = vi.fn()
|
||||
const { getByRole } = render(<RulerSlider currentDegree={45} onChange={onChange} />)
|
||||
|
||||
const slider = getByRole('slider')
|
||||
|
||||
// 45 + 15 = 60
|
||||
fireEvent.keyDown(slider, { key: 'ArrowRight' })
|
||||
expect(onChange).toHaveBeenCalledWith(60)
|
||||
|
||||
// 45 - 15 = 30
|
||||
fireEvent.keyDown(slider, { key: 'ArrowLeft' })
|
||||
expect(onChange).toHaveBeenCalledWith(30)
|
||||
|
||||
// Home -> 0
|
||||
fireEvent.keyDown(slider, { key: 'Home' })
|
||||
expect(onChange).toHaveBeenCalledWith(0)
|
||||
|
||||
// End -> 345
|
||||
fireEvent.keyDown(slider, { key: 'End' })
|
||||
expect(onChange).toHaveBeenCalledWith(360)
|
||||
expect(onChange).toHaveBeenCalledWith(345)
|
||||
})
|
||||
|
||||
it('snaps pointer events to nearest 15 degrees', () => {
|
||||
it('wraps around 0° and 360° on keyboard navigation', () => {
|
||||
const onChange = vi.fn()
|
||||
const { getByRole, rerender } = render(<RulerSlider currentDegree={0} onChange={onChange} />)
|
||||
|
||||
const slider = getByRole('slider')
|
||||
|
||||
// 0 - 15 wraps to 345
|
||||
fireEvent.keyDown(slider, { key: 'ArrowLeft' })
|
||||
expect(onChange).toHaveBeenCalledWith(345)
|
||||
|
||||
// 345 + 15 wraps to 0
|
||||
rerender(<RulerSlider currentDegree={345} onChange={onChange} />)
|
||||
fireEvent.keyDown(slider, { key: 'ArrowRight' })
|
||||
expect(onChange).toHaveBeenCalledWith(0)
|
||||
})
|
||||
|
||||
it('handles pointer drag interaction with 15 degree snapping and wrapping', () => {
|
||||
const onChange = vi.fn()
|
||||
const { getByRole } = render(<RulerSlider currentDegree={0} onChange={onChange} />)
|
||||
|
||||
const slider = getByRole('slider')
|
||||
// Mock getBoundingClientRect
|
||||
slider.getBoundingClientRect = () =>
|
||||
({
|
||||
left: 0,
|
||||
width: 360,
|
||||
top: 0,
|
||||
height: 40,
|
||||
right: 360,
|
||||
bottom: 40,
|
||||
}) as DOMRect
|
||||
slider.setPointerCapture = vi.fn()
|
||||
slider.releasePointerCapture = vi.fn()
|
||||
|
||||
// Pointer down at x=50 -> ratio = 50/360 = 50 deg -> round(50/15)*15 = 45 deg
|
||||
fireEvent.pointerDown(slider, { clientX: 50, button: 0, pointerId: 1 })
|
||||
expect(onChange).toHaveBeenCalledWith(45)
|
||||
// Start drag at x = 100
|
||||
fireEvent.pointerDown(slider, { clientX: 100, button: 0, pointerId: 1 })
|
||||
expect(slider.setPointerCapture).toHaveBeenCalledWith(1)
|
||||
|
||||
// Drag left by 24px (15° forward: deltaX = -24 => deltaDeg = +15)
|
||||
fireEvent.pointerMove(slider, { clientX: 76, button: 0, pointerId: 1 })
|
||||
expect(onChange).toHaveBeenCalledWith(15)
|
||||
|
||||
// Drag right by 24px from origin (x=124 => deltaX = +24 => deltaDeg = -15 => wraps to 345)
|
||||
fireEvent.pointerMove(slider, { clientX: 124, button: 0, pointerId: 1 })
|
||||
expect(onChange).toHaveBeenCalledWith(345)
|
||||
|
||||
// Release pointer
|
||||
fireEvent.pointerUp(slider, { clientX: 124, button: 0, pointerId: 1 })
|
||||
expect(slider.releasePointerCapture).toHaveBeenCalledWith(1)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2,14 +2,15 @@ import React, { useCallback, useRef, useState } from 'react'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
|
||||
interface RulerSliderProps {
|
||||
currentDegree: number // 0 to 360
|
||||
currentDegree: number // 0 to 360 (or normalized modulo 360)
|
||||
onChange: (degree: number) => void
|
||||
className?: string
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
const TICKS = Array.from({ length: 25 }, (_, i) => i * 15)
|
||||
const MAJOR_TICKS = new Set([0, 90, 180, 270, 360])
|
||||
const PX_PER_DEGREE = 1.6 // 24px per 15° step
|
||||
const STEP = 15
|
||||
const BUFFER_DEGREE = 240 // Render buffer on each side of the active degree
|
||||
|
||||
export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
currentDegree,
|
||||
@@ -19,6 +20,15 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
}) => {
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const [isDragging, setIsDragging] = useState(false)
|
||||
const [dragDegree, setDragDegree] = useState<number | null>(null)
|
||||
const dragStartRef = useRef<{ clientX: number; startDegree: number }>({
|
||||
clientX: 0,
|
||||
startDegree: 0,
|
||||
})
|
||||
|
||||
const normalizedDegree = (((Math.round(currentDegree / STEP) * STEP) % 360) + 360) % 360
|
||||
const activeDegree = isDragging && dragDegree !== null ? dragDegree : normalizedDegree
|
||||
const displayedDegree = (((Math.round(activeDegree / STEP) * STEP) % 360) + 360) % 360
|
||||
|
||||
const handlePointerDown = useCallback(
|
||||
(e: React.PointerEvent<HTMLDivElement>) => {
|
||||
@@ -29,25 +39,25 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
const target = containerRef.current
|
||||
if (!target) return
|
||||
target.setPointerCapture(e.pointerId)
|
||||
setIsDragging(true)
|
||||
|
||||
const rect = target.getBoundingClientRect()
|
||||
const ratio = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width))
|
||||
const snapped = Math.min(360, Math.max(0, Math.round((ratio * 360) / 15) * 15))
|
||||
onChange(snapped)
|
||||
dragStartRef.current = {
|
||||
clientX: e.clientX,
|
||||
startDegree: currentDegree,
|
||||
}
|
||||
setIsDragging(true)
|
||||
setDragDegree(currentDegree)
|
||||
},
|
||||
[disabled, onChange],
|
||||
[disabled, currentDegree],
|
||||
)
|
||||
|
||||
const handlePointerMove = useCallback(
|
||||
(e: React.PointerEvent<HTMLDivElement>) => {
|
||||
if (!isDragging || disabled) return
|
||||
const target = containerRef.current
|
||||
if (!target) return
|
||||
|
||||
const rect = target.getBoundingClientRect()
|
||||
const ratio = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width))
|
||||
const snapped = Math.min(360, Math.max(0, Math.round((ratio * 360) / 15) * 15))
|
||||
const deltaX = e.clientX - dragStartRef.current.clientX
|
||||
// Dragging left (deltaX < 0) rotates turntable forward (angle increases)
|
||||
const nextRawDegree = dragStartRef.current.startDegree - deltaX / PX_PER_DEGREE
|
||||
const snapped = (((Math.round(nextRawDegree / STEP) * STEP) % 360) + 360) % 360
|
||||
setDragDegree(snapped)
|
||||
onChange(snapped)
|
||||
},
|
||||
[isDragging, disabled, onChange],
|
||||
@@ -57,13 +67,17 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
(e: React.PointerEvent<HTMLDivElement>) => {
|
||||
if (!isDragging) return
|
||||
setIsDragging(false)
|
||||
if (dragDegree !== null) {
|
||||
onChange(dragDegree)
|
||||
}
|
||||
setDragDegree(null)
|
||||
try {
|
||||
containerRef.current?.releasePointerCapture(e.pointerId)
|
||||
} catch {
|
||||
// pointer may have already been released
|
||||
}
|
||||
},
|
||||
[isDragging],
|
||||
[isDragging, dragDegree, onChange],
|
||||
)
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
@@ -71,23 +85,32 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
if (disabled) return
|
||||
if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') {
|
||||
e.preventDefault()
|
||||
onChange(Math.max(0, currentDegree - 15))
|
||||
e.stopPropagation()
|
||||
onChange((((normalizedDegree - STEP) % 360) + 360) % 360)
|
||||
} else if (e.key === 'ArrowRight' || e.key === 'ArrowUp') {
|
||||
e.preventDefault()
|
||||
onChange(Math.min(360, currentDegree + 15))
|
||||
e.stopPropagation()
|
||||
onChange((normalizedDegree + STEP) % 360)
|
||||
} else if (e.key === 'Home') {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
onChange(0)
|
||||
} else if (e.key === 'End') {
|
||||
e.preventDefault()
|
||||
onChange(360)
|
||||
e.stopPropagation()
|
||||
onChange(345)
|
||||
}
|
||||
},
|
||||
[disabled, currentDegree, onChange],
|
||||
[disabled, normalizedDegree, onChange],
|
||||
)
|
||||
|
||||
const clampedDegree = Math.min(360, Math.max(0, currentDegree))
|
||||
const percent = (clampedDegree / 360) * 100
|
||||
// Compute visible ticks dynamically around activeDegree for seamless cyclic looping
|
||||
const minTickIndex = Math.floor((activeDegree - BUFFER_DEGREE) / STEP)
|
||||
const maxTickIndex = Math.ceil((activeDegree + BUFFER_DEGREE) / STEP)
|
||||
const ticks: number[] = []
|
||||
for (let i = minTickIndex; i <= maxTickIndex; i++) {
|
||||
ticks.push(i * STEP)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -96,8 +119,8 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
aria-label="Turntable angle"
|
||||
aria-valuemin={0}
|
||||
aria-valuemax={360}
|
||||
aria-valuenow={clampedDegree}
|
||||
aria-valuetext={`${clampedDegree}°`}
|
||||
aria-valuenow={displayedDegree}
|
||||
aria-valuetext={`${displayedDegree}°`}
|
||||
tabIndex={disabled ? -1 : 0}
|
||||
onPointerDown={handlePointerDown}
|
||||
onPointerMove={handlePointerMove}
|
||||
@@ -105,58 +128,61 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
onPointerCancel={handlePointerUp}
|
||||
onKeyDown={handleKeyDown}
|
||||
className={cn(
|
||||
'relative select-none cursor-pointer flex flex-col justify-end h-11 px-2.5 touch-none group',
|
||||
'relative select-none cursor-ew-resize flex flex-col justify-end h-9 touch-none w-full min-w-[140px] group',
|
||||
disabled && 'opacity-50 cursor-not-allowed',
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{/* Major tick labels */}
|
||||
<div className="relative w-full h-3 mb-1 pointer-events-none">
|
||||
{Array.from(MAJOR_TICKS).map((deg) => (
|
||||
<span
|
||||
key={`label-${deg}`}
|
||||
className="absolute text-[10px] leading-none font-mono text-muted-foreground -translate-x-1/2"
|
||||
style={{ left: `${(deg / 360) * 100}%` }}
|
||||
>
|
||||
{deg}°
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
{/* Tooltip above center indicator (visible only when dragging) */}
|
||||
{isDragging && (
|
||||
<div
|
||||
data-testid="ruler-slider-tooltip"
|
||||
className="absolute -top-6 left-1/2 -translate-x-1/2 px-1.5 py-0.5 rounded bg-primary text-primary-foreground text-[10px] font-mono font-medium shadow-md whitespace-nowrap pointer-events-none z-30 animate-in fade-in zoom-in-95 duration-100"
|
||||
>
|
||||
{displayedDegree}°
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Track & Ticks */}
|
||||
<div className="relative w-full h-4 flex items-end">
|
||||
{/* Moving Track & Ticks with Edge Mask */}
|
||||
<div
|
||||
className="relative w-full h-5 flex items-end overflow-hidden"
|
||||
style={{
|
||||
maskImage:
|
||||
'linear-gradient(to right, transparent, black 24px, black calc(100% - 24px), transparent)',
|
||||
}}
|
||||
>
|
||||
{/* Baseline bar */}
|
||||
<div className="absolute inset-x-0 bottom-0 h-0.5 bg-muted-foreground/30 rounded-full" />
|
||||
<div className="absolute inset-x-0 bottom-0 h-0.5 bg-muted-foreground/20 rounded-full" />
|
||||
|
||||
{/* Cyclic Ticks */}
|
||||
{ticks.map((tickAngle) => {
|
||||
const offsetPx = (tickAngle - activeDegree) * PX_PER_DEGREE
|
||||
const deg = ((tickAngle % 360) + 360) % 360
|
||||
const isMajor = deg % 60 === 0
|
||||
|
||||
{/* Ticks */}
|
||||
{TICKS.map((deg) => {
|
||||
const isMajor = MAJOR_TICKS.has(deg)
|
||||
return (
|
||||
<div
|
||||
key={`tick-${deg}`}
|
||||
key={`tick-${tickAngle}`}
|
||||
data-testid={`tick-${deg}`}
|
||||
data-major={isMajor}
|
||||
className={cn(
|
||||
'absolute bottom-0 -translate-x-1/2 transition-colors',
|
||||
'absolute bottom-0 -translate-x-1/2 transition-colors pointer-events-none',
|
||||
isMajor
|
||||
? 'w-0.5 h-3.5 bg-foreground/70'
|
||||
: 'w-px h-2 bg-muted-foreground/40 group-hover:bg-muted-foreground/70',
|
||||
? 'w-0.5 h-4 bg-foreground/75'
|
||||
: 'w-px h-2 bg-muted-foreground/40 group-hover:bg-muted-foreground/60',
|
||||
)}
|
||||
style={{ left: `${(deg / 360) * 100}%` }}
|
||||
style={{ left: `calc(50% + ${offsetPx}px)` }}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* Current Position Thumb */}
|
||||
<div
|
||||
className="absolute bottom-0 -translate-x-1/2 flex flex-col items-center pointer-events-none transition-[left] duration-75 ease-out"
|
||||
style={{ left: `${percent}%` }}
|
||||
>
|
||||
{/* Degree Tooltip on drag/hover */}
|
||||
<div className="text-[10px] font-mono font-semibold px-1 py-0.5 rounded bg-primary text-primary-foreground mb-1 shadow-xs whitespace-nowrap">
|
||||
{clampedDegree}°
|
||||
</div>
|
||||
{/* Thumb needle */}
|
||||
<div className="w-1.5 h-5 bg-primary rounded-full ring-2 ring-background shadow-md" />
|
||||
</div>
|
||||
{/* Fixed Center Indicator (Pinned at 50% horizontally) */}
|
||||
<div
|
||||
data-testid="ruler-center-indicator"
|
||||
className="absolute bottom-0 left-1/2 -translate-x-1/2 flex flex-col items-center pointer-events-none z-20"
|
||||
>
|
||||
<div className="w-0.5 h-5 bg-primary rounded-full shadow-xs ring-1 ring-background/50" />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user