2021-11-30 09:54:14 -05:00
|
|
|
import classNames from 'classnames'
|
2022-03-21 10:47:01 -04:00
|
|
|
import { memo, useCallback, useEffect, useState, useMemo } from 'react'
|
2021-11-30 09:54:14 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-10-21 06:31:51 -04:00
|
|
|
import { useIdeContext } from '../../../shared/context/ide-context'
|
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
|
|
|
import { getJSON } from '../../../infrastructure/fetch-json'
|
|
|
|
import { useCompileContext } from '../../../shared/context/compile-context'
|
2021-11-30 09:54:14 -05:00
|
|
|
import { useLayoutContext } from '../../../shared/context/layout-context'
|
2021-10-21 06:31:51 -04:00
|
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
|
|
|
import { Button, OverlayTrigger, Tooltip } from 'react-bootstrap'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import useIsMounted from '../../../shared/hooks/use-is-mounted'
|
|
|
|
import useAbortController from '../../../shared/hooks/use-abort-controller'
|
2021-11-30 09:54:14 -05:00
|
|
|
import useDetachState from '../../../shared/hooks/use-detach-state'
|
|
|
|
import useDetachAction from '../../../shared/hooks/use-detach-action'
|
2021-12-14 08:24:32 -05:00
|
|
|
import localStorage from '../../../infrastructure/local-storage'
|
2022-03-21 10:47:01 -04:00
|
|
|
import { useFileTreeData } from '../../../shared/context/file-tree-data-context'
|
2021-11-30 09:54:14 -05:00
|
|
|
|
|
|
|
function GoToCodeButton({
|
|
|
|
position,
|
|
|
|
syncToCode,
|
|
|
|
syncToCodeInFlight,
|
|
|
|
isDetachLayout,
|
2022-03-21 10:47:01 -04:00
|
|
|
hasSingleSelectedDoc,
|
2021-11-30 09:54:14 -05:00
|
|
|
}) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const tooltipPlacement = isDetachLayout ? 'bottom' : 'right'
|
|
|
|
const buttonClasses = classNames('synctex-control', {
|
|
|
|
'detach-synctex-control': !!isDetachLayout,
|
|
|
|
})
|
|
|
|
|
2022-01-11 09:59:31 -05:00
|
|
|
let buttonIcon = null
|
|
|
|
if (syncToCodeInFlight) {
|
2022-01-19 06:56:57 -05:00
|
|
|
buttonIcon = <Icon type="refresh" spin className="synctex-spin-icon" />
|
2022-01-11 09:59:31 -05:00
|
|
|
} else if (!isDetachLayout) {
|
2022-01-19 06:56:57 -05:00
|
|
|
buttonIcon = <Icon type="arrow-left" className="synctex-control-icon" />
|
2022-01-11 09:59:31 -05:00
|
|
|
}
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
return (
|
|
|
|
<OverlayTrigger
|
|
|
|
placement={tooltipPlacement}
|
|
|
|
overlay={
|
|
|
|
<Tooltip id="sync-to-code-tooltip">
|
|
|
|
{t('go_to_pdf_location_in_code')}
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
bsStyle="default"
|
|
|
|
bsSize="xs"
|
|
|
|
onClick={() => syncToCode(position, 72)}
|
2022-03-21 10:47:01 -04:00
|
|
|
disabled={syncToCodeInFlight || !hasSingleSelectedDoc}
|
2021-11-30 09:54:14 -05:00
|
|
|
className={buttonClasses}
|
|
|
|
aria-label={t('go_to_pdf_location_in_code')}
|
|
|
|
>
|
2022-01-11 09:59:31 -05:00
|
|
|
{buttonIcon}
|
2021-11-30 09:54:14 -05:00
|
|
|
{isDetachLayout ? <span> {t('show_in_code')}</span> : ''}
|
|
|
|
</Button>
|
|
|
|
</OverlayTrigger>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function GoToPdfButton({
|
|
|
|
cursorPosition,
|
|
|
|
syncToPdf,
|
|
|
|
syncToPdfInFlight,
|
|
|
|
isDetachLayout,
|
2022-03-21 10:47:01 -04:00
|
|
|
hasSingleSelectedDoc,
|
2021-11-30 09:54:14 -05:00
|
|
|
}) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const tooltipPlacement = isDetachLayout ? 'bottom' : 'right'
|
|
|
|
const buttonClasses = classNames('synctex-control', {
|
|
|
|
'detach-synctex-control': !!isDetachLayout,
|
|
|
|
})
|
|
|
|
|
2022-01-11 09:59:31 -05:00
|
|
|
let buttonIcon = null
|
|
|
|
if (syncToPdfInFlight) {
|
2022-01-19 06:56:57 -05:00
|
|
|
buttonIcon = <Icon type="refresh" spin className="synctex-spin-icon" />
|
2022-01-11 09:59:31 -05:00
|
|
|
} else if (!isDetachLayout) {
|
2022-01-19 06:56:57 -05:00
|
|
|
buttonIcon = <Icon type="arrow-right" className="synctex-control-icon" />
|
2022-01-11 09:59:31 -05:00
|
|
|
}
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
return (
|
|
|
|
<OverlayTrigger
|
|
|
|
placement={tooltipPlacement}
|
|
|
|
overlay={
|
|
|
|
<Tooltip id="sync-to-pdf-tooltip">
|
|
|
|
{t('go_to_code_location_in_pdf')}
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
bsStyle="default"
|
|
|
|
bsSize="xs"
|
|
|
|
onClick={() => syncToPdf(cursorPosition)}
|
2022-03-21 10:47:01 -04:00
|
|
|
disabled={syncToPdfInFlight || !cursorPosition || !hasSingleSelectedDoc}
|
2021-11-30 09:54:14 -05:00
|
|
|
className={buttonClasses}
|
|
|
|
aria-label={t('go_to_code_location_in_pdf')}
|
|
|
|
>
|
2022-01-11 09:59:31 -05:00
|
|
|
{buttonIcon}
|
2021-11-30 09:54:14 -05:00
|
|
|
{isDetachLayout ? <span> {t('show_in_pdf')}</span> : ''}
|
|
|
|
</Button>
|
|
|
|
</OverlayTrigger>
|
|
|
|
)
|
|
|
|
}
|
2021-10-21 06:31:51 -04:00
|
|
|
|
|
|
|
function PdfSynctexControls() {
|
|
|
|
const ide = useIdeContext()
|
|
|
|
|
|
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
const { detachRole } = useLayoutContext()
|
|
|
|
|
2022-03-15 09:54:58 -04:00
|
|
|
const {
|
|
|
|
clsiServerId,
|
|
|
|
pdfUrl,
|
|
|
|
pdfViewer,
|
|
|
|
position,
|
|
|
|
setShowLogs,
|
|
|
|
setHighlights,
|
|
|
|
} = useCompileContext()
|
2021-10-21 06:31:51 -04:00
|
|
|
|
2022-03-21 10:47:01 -04:00
|
|
|
const { selectedEntities } = useFileTreeData()
|
|
|
|
|
2021-12-14 08:24:32 -05:00
|
|
|
const [cursorPosition, setCursorPosition] = useState(() => {
|
|
|
|
const position = localStorage.getItem(
|
|
|
|
`doc.position.${ide.editorManager.getCurrentDocId()}`
|
|
|
|
)
|
|
|
|
return position ? position.cursorPosition : null
|
|
|
|
})
|
2021-10-21 06:31:51 -04:00
|
|
|
|
|
|
|
const isMounted = useIsMounted()
|
|
|
|
|
|
|
|
const { signal } = useAbortController()
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
// for detacher editor tab, which cannot access pdfUrl in a scope value in
|
|
|
|
// detached state
|
|
|
|
const [pdfExists, setPdfExists] = useDetachState(
|
|
|
|
'pdf-exists',
|
|
|
|
!!pdfUrl,
|
|
|
|
'detached',
|
|
|
|
'detacher'
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setPdfExists(!!pdfUrl)
|
|
|
|
}, [pdfUrl, setPdfExists])
|
|
|
|
|
2021-10-21 06:31:51 -04:00
|
|
|
useEffect(() => {
|
|
|
|
const listener = event => setCursorPosition(event.detail)
|
|
|
|
window.addEventListener('cursor:editor:update', listener)
|
|
|
|
return () => window.removeEventListener('cursor:editor:update', listener)
|
|
|
|
}, [ide])
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
const [syncToPdfInFlight, setSyncToPdfInFlight] = useDetachState(
|
|
|
|
'sync-to-pdf-inflight',
|
|
|
|
false,
|
|
|
|
'detached',
|
|
|
|
'detacher'
|
|
|
|
)
|
2021-10-21 06:31:51 -04:00
|
|
|
const [syncToCodeInFlight, setSyncToCodeInFlight] = useState(false)
|
|
|
|
|
|
|
|
const [, setSynctexError] = useScopeValue('sync_tex_error')
|
|
|
|
|
|
|
|
const getCurrentFilePath = useCallback(() => {
|
|
|
|
const docId = ide.editorManager.getCurrentDocId()
|
|
|
|
const doc = ide.fileTreeManager.findEntityById(docId)
|
|
|
|
|
|
|
|
let path = ide.fileTreeManager.getEntityPath(doc)
|
|
|
|
|
|
|
|
// If the root file is folder/main.tex, then synctex sees the path as folder/./main.tex
|
|
|
|
const rootDocDirname = ide.fileTreeManager.getRootDocDirname()
|
|
|
|
|
|
|
|
if (rootDocDirname) {
|
|
|
|
path = path.replace(RegExp(`^${rootDocDirname}`), `${rootDocDirname}/.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}, [ide])
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
const _goToCodeLine = useCallback(
|
|
|
|
(file, line) => {
|
|
|
|
if (file) {
|
|
|
|
const doc = ide.fileTreeManager.findEntityByPath(file)
|
2021-10-21 06:31:51 -04:00
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
ide.editorManager.openDoc(doc, {
|
|
|
|
gotoLine: line,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setSynctexError(true)
|
|
|
|
|
|
|
|
window.setTimeout(() => {
|
|
|
|
if (isMounted.current) {
|
|
|
|
setSynctexError(false)
|
|
|
|
}
|
|
|
|
}, 4000)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[ide, isMounted, setSynctexError]
|
|
|
|
)
|
|
|
|
|
|
|
|
const goToCodeLine = useDetachAction(
|
|
|
|
'go-to-code-line',
|
|
|
|
_goToCodeLine,
|
|
|
|
'detached',
|
|
|
|
'detacher'
|
|
|
|
)
|
|
|
|
|
|
|
|
const _goToPdfLocation = useCallback(
|
|
|
|
params => {
|
|
|
|
setSyncToPdfInFlight(true)
|
2021-10-21 06:31:51 -04:00
|
|
|
|
|
|
|
if (clsiServerId) {
|
2021-12-02 10:18:28 -05:00
|
|
|
params += `&clsiserverid=${clsiServerId}`
|
2021-10-21 06:31:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getJSON(`/project/${projectId}/sync/code?${params}`, { signal })
|
|
|
|
.then(data => {
|
2022-03-15 09:54:58 -04:00
|
|
|
setShowLogs(false)
|
2021-10-21 06:31:51 -04:00
|
|
|
setHighlights(data.pdf)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error)
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
if (isMounted.current) {
|
|
|
|
setSyncToPdfInFlight(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[
|
|
|
|
clsiServerId,
|
2021-11-30 09:54:14 -05:00
|
|
|
isMounted,
|
2021-10-21 06:31:51 -04:00
|
|
|
projectId,
|
2022-03-15 09:54:58 -04:00
|
|
|
setShowLogs,
|
2021-10-21 06:31:51 -04:00
|
|
|
setHighlights,
|
2021-11-30 09:54:14 -05:00
|
|
|
setSyncToPdfInFlight,
|
2021-10-21 06:31:51 -04:00
|
|
|
signal,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
const goToPdfLocation = useDetachAction(
|
|
|
|
'go-to-pdf-location',
|
|
|
|
_goToPdfLocation,
|
|
|
|
'detacher',
|
|
|
|
'detached'
|
|
|
|
)
|
|
|
|
|
|
|
|
const syncToPdf = useCallback(
|
|
|
|
cursorPosition => {
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
file: getCurrentFilePath(),
|
|
|
|
line: cursorPosition.row + 1,
|
|
|
|
column: cursorPosition.column,
|
2021-12-02 10:18:28 -05:00
|
|
|
}).toString()
|
2021-11-30 09:54:14 -05:00
|
|
|
|
|
|
|
goToPdfLocation(params)
|
|
|
|
},
|
|
|
|
[getCurrentFilePath, goToPdfLocation]
|
|
|
|
)
|
|
|
|
|
2021-10-21 06:31:51 -04:00
|
|
|
const syncToCode = useCallback(
|
|
|
|
(position, visualOffset = 0) => {
|
2021-11-30 09:54:14 -05:00
|
|
|
setSyncToCodeInFlight(true)
|
2021-10-21 06:31:51 -04:00
|
|
|
// FIXME: this actually works better if it's halfway across the
|
|
|
|
// page (or the visible part of the page). Synctex doesn't
|
|
|
|
// always find the right place in the file when the point is at
|
|
|
|
// the edge of the page, it sometimes returns the start of the
|
|
|
|
// next paragraph instead.
|
|
|
|
const h = position.offset.left
|
|
|
|
|
|
|
|
// Compute the vertical position to pass to synctex, which
|
|
|
|
// works with coordinates increasing from the top of the page
|
|
|
|
// down. This matches the browser's DOM coordinate of the
|
|
|
|
// click point, but the pdf position is measured from the
|
|
|
|
// bottom of the page so we need to invert it.
|
|
|
|
let v = 0
|
|
|
|
if (position.pageSize?.height) {
|
|
|
|
v += position.pageSize.height - position.offset.top // measure from pdf point (inverted)
|
|
|
|
} else {
|
|
|
|
v += position.offset.top // measure from html click position
|
|
|
|
}
|
|
|
|
v += visualOffset
|
|
|
|
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
page: position.page + 1,
|
|
|
|
h: h.toFixed(2),
|
|
|
|
v: v.toFixed(2),
|
|
|
|
})
|
|
|
|
|
|
|
|
if (clsiServerId) {
|
|
|
|
params.set('clsiserverid', clsiServerId)
|
|
|
|
}
|
|
|
|
|
|
|
|
getJSON(`/project/${projectId}/sync/pdf?${params}`, { signal })
|
|
|
|
.then(data => {
|
|
|
|
const [{ file, line }] = data.code
|
2021-11-30 09:54:14 -05:00
|
|
|
goToCodeLine(file, line)
|
2021-10-21 06:31:51 -04:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error)
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
if (isMounted.current) {
|
|
|
|
setSyncToCodeInFlight(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2021-11-30 09:54:14 -05:00
|
|
|
[
|
|
|
|
clsiServerId,
|
|
|
|
projectId,
|
|
|
|
signal,
|
|
|
|
isMounted,
|
|
|
|
setSyncToCodeInFlight,
|
|
|
|
goToCodeLine,
|
|
|
|
]
|
2021-10-21 06:31:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const listener = event => syncToCode(event.detail)
|
|
|
|
window.addEventListener('synctex:sync-to-position', listener)
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('synctex:sync-to-position', listener)
|
|
|
|
}
|
|
|
|
}, [syncToCode])
|
|
|
|
|
2022-03-21 10:47:01 -04:00
|
|
|
const hasSingleSelectedDoc = useMemo(() => {
|
|
|
|
if (selectedEntities.length !== 1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedEntities[0].type !== 'doc') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, [selectedEntities])
|
|
|
|
|
2022-02-14 09:20:06 -05:00
|
|
|
if (!position) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
if (!pdfExists || pdfViewer === 'native') {
|
2021-10-21 06:31:51 -04:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
if (detachRole === 'detacher') {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<GoToPdfButton
|
|
|
|
cursorPosition={cursorPosition}
|
|
|
|
syncToPdf={syncToPdf}
|
|
|
|
syncToPdfInFlight={syncToPdfInFlight}
|
|
|
|
isDetachLayout
|
2022-03-21 10:47:01 -04:00
|
|
|
hasSingleSelectedDoc={hasSingleSelectedDoc}
|
2021-11-30 09:54:14 -05:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
} else if (detachRole === 'detached') {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<GoToCodeButton
|
|
|
|
position={position}
|
|
|
|
syncToCode={syncToCode}
|
|
|
|
syncToCodeInFlight={syncToCodeInFlight}
|
|
|
|
isDetachLayout
|
2022-03-21 10:47:01 -04:00
|
|
|
hasSingleSelectedDoc={hasSingleSelectedDoc}
|
2021-11-30 09:54:14 -05:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<GoToPdfButton
|
|
|
|
cursorPosition={cursorPosition}
|
|
|
|
syncToPdf={syncToPdf}
|
|
|
|
syncToPdfInFlight={syncToPdfInFlight}
|
2022-03-21 10:47:01 -04:00
|
|
|
hasSingleSelectedDoc={hasSingleSelectedDoc}
|
2021-11-30 09:54:14 -05:00
|
|
|
/>
|
|
|
|
|
|
|
|
<GoToCodeButton
|
|
|
|
position={position}
|
|
|
|
syncToCode={syncToCode}
|
|
|
|
syncToCodeInFlight={syncToCodeInFlight}
|
2022-03-21 10:47:01 -04:00
|
|
|
hasSingleSelectedDoc={hasSingleSelectedDoc}
|
2021-11-30 09:54:14 -05:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2021-10-21 06:31:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default memo(PdfSynctexControls)
|
2021-11-30 09:54:14 -05:00
|
|
|
|
|
|
|
GoToCodeButton.propTypes = {
|
|
|
|
isDetachLayout: PropTypes.bool,
|
|
|
|
position: PropTypes.object.isRequired,
|
|
|
|
syncToCode: PropTypes.func.isRequired,
|
|
|
|
syncToCodeInFlight: PropTypes.bool.isRequired,
|
2022-03-21 10:47:01 -04:00
|
|
|
hasSingleSelectedDoc: PropTypes.bool.isRequired,
|
2021-11-30 09:54:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
GoToPdfButton.propTypes = {
|
|
|
|
cursorPosition: PropTypes.object,
|
|
|
|
isDetachLayout: PropTypes.bool,
|
|
|
|
syncToPdf: PropTypes.func.isRequired,
|
|
|
|
syncToPdfInFlight: PropTypes.bool.isRequired,
|
2022-03-21 10:47:01 -04:00
|
|
|
hasSingleSelectedDoc: PropTypes.bool.isRequired,
|
2021-11-30 09:54:14 -05:00
|
|
|
}
|