6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
156 lines
4.6 KiB
TypeScript
156 lines
4.6 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
|
interface UseCodeViewerFeaturesOptions {
|
|
/** Reference to the content container for scroll-to-match functionality */
|
|
contentRef?: React.RefObject<HTMLDivElement | null>
|
|
/** Initial wrap text state (ignored if externalWrapText is provided) */
|
|
initialWrapText?: boolean
|
|
/** External wrap text state (e.g., from Zustand store) */
|
|
externalWrapText?: boolean
|
|
/** External setter for wrap text (required if externalWrapText is provided) */
|
|
onWrapTextChange?: (wrap: boolean) => void
|
|
/** Callback when escape is pressed (optional, for custom handling) */
|
|
onEscape?: () => void
|
|
}
|
|
|
|
interface UseCodeViewerFeaturesReturn {
|
|
wrapText: boolean
|
|
setWrapText: (wrap: boolean) => void
|
|
toggleWrapText: () => void
|
|
|
|
isSearchActive: boolean
|
|
searchQuery: string
|
|
setSearchQuery: (query: string) => void
|
|
matchCount: number
|
|
currentMatchIndex: number
|
|
activateSearch: () => void
|
|
closeSearch: () => void
|
|
goToNextMatch: () => void
|
|
goToPreviousMatch: () => void
|
|
handleMatchCountChange: (count: number) => void
|
|
searchInputRef: React.RefObject<HTMLInputElement | null>
|
|
}
|
|
|
|
/**
|
|
* Reusable hook for Code.Viewer features: search and wrap text functionality.
|
|
* Supports both internal state and external state (e.g., from Zustand) for wrapText.
|
|
*/
|
|
export function useCodeViewerFeatures(
|
|
options: UseCodeViewerFeaturesOptions = {}
|
|
): UseCodeViewerFeaturesReturn {
|
|
const {
|
|
contentRef,
|
|
initialWrapText = true,
|
|
externalWrapText,
|
|
onWrapTextChange,
|
|
onEscape,
|
|
} = options
|
|
|
|
// Use external state if provided, otherwise use internal state
|
|
const [internalWrapText, setInternalWrapText] = useState(initialWrapText)
|
|
const wrapText = externalWrapText !== undefined ? externalWrapText : internalWrapText
|
|
const setWrapText = onWrapTextChange ?? setInternalWrapText
|
|
|
|
const [isSearchActive, setIsSearchActive] = useState(false)
|
|
const [searchQuery, setSearchQuery] = useState('')
|
|
const [matchCount, setMatchCount] = useState(0)
|
|
const [currentMatchIndex, setCurrentMatchIndex] = useState(0)
|
|
const searchInputRef = useRef<HTMLInputElement>(null)
|
|
|
|
const toggleWrapText = useCallback(() => {
|
|
setWrapText(!wrapText)
|
|
}, [wrapText, setWrapText])
|
|
|
|
const activateSearch = useCallback(() => {
|
|
setIsSearchActive(true)
|
|
setTimeout(() => {
|
|
searchInputRef.current?.focus()
|
|
}, 0)
|
|
}, [])
|
|
|
|
const closeSearch = useCallback(() => {
|
|
setIsSearchActive(false)
|
|
setSearchQuery('')
|
|
setMatchCount(0)
|
|
setCurrentMatchIndex(0)
|
|
}, [])
|
|
|
|
const goToNextMatch = useCallback(() => {
|
|
if (matchCount === 0) return
|
|
setCurrentMatchIndex((prev) => (prev + 1) % matchCount)
|
|
}, [matchCount])
|
|
|
|
const goToPreviousMatch = useCallback(() => {
|
|
if (matchCount === 0) return
|
|
setCurrentMatchIndex((prev) => (prev - 1 + matchCount) % matchCount)
|
|
}, [matchCount])
|
|
|
|
const handleMatchCountChange = useCallback((count: number) => {
|
|
setMatchCount(count)
|
|
setCurrentMatchIndex(0)
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === 'Escape' && isSearchActive) {
|
|
e.preventDefault()
|
|
closeSearch()
|
|
onEscape?.()
|
|
}
|
|
}
|
|
|
|
window.addEventListener('keydown', handleKeyDown)
|
|
return () => window.removeEventListener('keydown', handleKeyDown)
|
|
}, [isSearchActive, closeSearch, onEscape])
|
|
|
|
useEffect(() => {
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
if (!isSearchActive) return
|
|
|
|
const isSearchInputFocused = document.activeElement === searchInputRef.current
|
|
|
|
if (e.key === 'Enter' && isSearchInputFocused && matchCount > 0) {
|
|
e.preventDefault()
|
|
if (e.shiftKey) {
|
|
goToPreviousMatch()
|
|
} else {
|
|
goToNextMatch()
|
|
}
|
|
}
|
|
}
|
|
|
|
window.addEventListener('keydown', handleKeyDown)
|
|
return () => window.removeEventListener('keydown', handleKeyDown)
|
|
}, [isSearchActive, matchCount, goToNextMatch, goToPreviousMatch])
|
|
|
|
useEffect(() => {
|
|
if (!isSearchActive || matchCount === 0 || !contentRef?.current) return
|
|
|
|
const matchElements = contentRef.current.querySelectorAll('[data-search-match]')
|
|
const currentElement = matchElements[currentMatchIndex]
|
|
|
|
if (currentElement) {
|
|
currentElement.scrollIntoView({ block: 'center' })
|
|
}
|
|
}, [currentMatchIndex, isSearchActive, matchCount, contentRef])
|
|
|
|
return {
|
|
wrapText,
|
|
setWrapText,
|
|
toggleWrapText,
|
|
isSearchActive,
|
|
searchQuery,
|
|
setSearchQuery,
|
|
matchCount,
|
|
currentMatchIndex,
|
|
activateSearch,
|
|
closeSearch,
|
|
goToNextMatch,
|
|
goToPreviousMatch,
|
|
handleMatchCountChange,
|
|
searchInputRef,
|
|
}
|
|
}
|