Files
Mathias Jakobsen 77f208633b Merge pull request #36233 from overleaf/mj-command-palette-events
[web] Add events to command palette usage

GitOrigin-RevId: 1bb40c20f3228027ee4470dc84fa2983bb910cbf
2026-09-17 11:49:19 +00:00

45 lines
1.3 KiB
TypeScript

import { useEditorManagerContext } from '@/features/ide-react/context/editor-manager-context'
import { CommandPaletteSource } from '../types'
import { useMemo } from 'react'
import { useTranslation } from 'react-i18next'
const JUMP_REGEX = /^(\d+)(?:,(\d+))?$/
export const useJumpToLineCommandSource = (): CommandPaletteSource => {
const { jumpToLine } = useEditorManagerContext()
const { t } = useTranslation()
return useMemo(
() => ({
id: 'jump-to-line-source',
prefix: ':',
prefixRequired: true,
search: (query: string) => {
const match = query.match(JUMP_REGEX)
if (!match) {
return []
}
const line = parseInt(match[1], 10)
const column = match[2] ? parseInt(match[2], 10) : undefined
return [
{
onSelect: () => jumpToLine({ gotoLine: line, gotoColumn: column }),
title:
column != null
? t('jump_to_line_x_column_y', {
line,
column,
})
: t('jump_to_line_x', {
line,
}),
score: 1,
eventSegmentation: { source: 'jump-to-line' },
},
]
},
}),
[jumpToLine, t]
)
}