Browse Source

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

Convert several pdf-preview files to typescript

GitOrigin-RevId: afa7f7fce8fec2a36090a259783a586960ccb291
David 1 year ago
parent
commit
95014d005a

+ 8 - 3
services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.js → services/web/frontend/js/features/pdf-preview/hooks/use-compile-triggers.ts

@@ -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()

+ 0 - 4
services/web/frontend/js/features/pdf-preview/util/editor-files.js

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

+ 4 - 0
services/web/frontend/js/features/pdf-preview/util/editor-files.ts

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

+ 6 - 1
services/web/frontend/js/features/pdf-preview/util/highlights.js → services/web/frontend/js/features/pdf-preview/util/highlights.ts

@@ -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

+ 6 - 3
services/web/frontend/js/features/pdf-preview/util/pdf-caching-flags.js → services/web/frontend/js/features/pdf-preview/util/pdf-caching-flags.ts

@@ -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'

+ 8 - 0
services/web/frontend/js/features/pdf-preview/util/types.ts

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

+ 6 - 2
services/web/frontend/js/shared/context/local-compile-context.tsx

@@ -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[]