Merge pull request #27846 from overleaf/dp-pdf-preview-typescript

Convert several pdf-preview files to typescript

GitOrigin-RevId: afa7f7fce8fec2a36090a259783a586960ccb291
This commit is contained in:
David
2025-08-13 10:21:27 +01:00
committed by Copybot
parent dd42a3b4c7
commit 12fe3843be
7 changed files with 38 additions and 13 deletions

View File

@@ -2,7 +2,9 @@ import { useCallback, useEffect } from 'react'
import useEventListener from '../../../shared/hooks/use-event-listener'
import useDetachAction from '../../../shared/hooks/use-detach-action'
export const startCompileKeypress = event => {
export const startCompileKeypress = (
event: KeyboardEvent | React.KeyboardEvent<Element>
) => {
if (event.shiftKey || event.altKey) {
return false
}
@@ -30,9 +32,12 @@ export const startCompileKeypress = event => {
}
}
export default function useCompileTriggers(startCompile, setChangedAt) {
export default function useCompileTriggers(
startCompile: (...args: any[]) => void,
setChangedAt: (...args: any[]) => void
) {
const handleKeyDown = useCallback(
event => {
(event: KeyboardEvent) => {
if (startCompileKeypress(event)) {
event.preventDefault()
startCompile()

View File

@@ -1,4 +0,0 @@
const documentClassRe = /^[^%]*\\documentclass/
export const isMainFile = doc =>
doc.split('\n').some(line => documentClassRe.test(line))

View File

@@ -0,0 +1,4 @@
const documentClassRe = /^[^%]*\\documentclass/
export const isMainFile = (doc: string | undefined): boolean =>
!!doc && doc.split('\n').some(line => documentClassRe.test(line))

View File

@@ -1,6 +1,11 @@
import { PDFJS } from '@/features/pdf-preview/util/pdf-js'
import { PDFViewer } from 'pdfjs-dist/web/pdf_viewer.mjs'
import { HighlightData } from './types'
export function buildHighlightElement(highlight, viewer) {
export function buildHighlightElement(
highlight: HighlightData,
viewer: PDFViewer
) {
const { viewport, div } = viewer.getPageView(highlight.page - 1)
// page coordinates from synctex

View File

@@ -7,13 +7,16 @@ if (!hasTextEncoder) {
}
const isOpera =
Array.isArray(navigator.userAgentData?.brands) &&
navigator.userAgentData.brands.some(b => b.brand === 'Opera')
// userAgentData is experimental and not currently in the Navigator type
Array.isArray((navigator as any).userAgentData?.brands) &&
(navigator as any).userAgentData.brands.some(
(b: { brand: string }) => b.brand === 'Opera'
)
if (isOpera) {
debugConsole.warn('Browser cache is limited in Opera. Disabling pdf-caching.')
}
function isFlagEnabled(flag) {
function isFlagEnabled(flag: string): boolean {
if (!hasTextEncoder) return false
if (isOpera) return false
return getMeta('ol-splitTestVariants')?.[flag] === 'enabled'

View File

@@ -40,3 +40,11 @@ export type PdfFileDataList = {
other: PdfFileData[]
archive?: PdfFileArchiveData
}
export type HighlightData = {
page: number
h: number
v: number
width: number
height: number
}

View File

@@ -44,7 +44,11 @@ import {
PdfScrollPosition,
usePdfScrollPosition,
} from '@/shared/hooks/use-pdf-scroll-position'
import { LogEntry, PdfFileDataList } from '@/features/pdf-preview/util/types'
import {
HighlightData,
LogEntry,
PdfFileDataList,
} from '@/features/pdf-preview/util/types'
import { isSplitTestEnabled } from '@/utils/splitTestUtils'
import { captureException } from '@/infrastructure/error-reporter'
import OError from '@overleaf/o-error'
@@ -65,7 +69,7 @@ export type CompileContext = {
fileList?: PdfFileDataList
hasChanges: boolean
hasShortCompileTimeout: boolean
highlights?: Record<string, any>[]
highlights?: HighlightData[]
isProjectOwner: boolean
logEntries?: {
all: LogEntry[]