2021-11-30 09:54:14 -05:00
|
|
|
import classNames from 'classnames'
|
2022-04-08 05:07:13 -04:00
|
|
|
import { memo, useCallback, useEffect, useState, useRef } 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'
|
2022-03-31 07:22:36 -04:00
|
|
|
import { useDetachCompileContext as useCompileContext } from '../../../shared/context/detach-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'
|
2022-05-18 09:46:10 -04:00
|
|
|
import { Button } from 'react-bootstrap'
|
|
|
|
import Tooltip from '../../../shared/components/tooltip'
|
2021-10-21 06:31:51 -04:00
|
|
|
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'
|
2022-04-08 05:07:13 -04:00
|
|
|
import useScopeEventListener from '../../../shared/hooks/use-scope-event-listener'
|
2021-11-30 09:54:14 -05:00
|
|
|
|
|
|
|
function GoToCodeButton({
|
|
|
|
position,
|
|
|
|
syncToCode,
|
|
|
|
syncToCodeInFlight,
|
|
|
|
isDetachLayout,
|
|
|
|
}) {
|
|
|
|
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 (
|
2022-05-18 09:46:10 -04:00
|
|
|
<Tooltip
|
|
|
|
id="sync-to-code"
|
|
|
|
description={t('go_to_pdf_location_in_code')}
|
|
|
|
overlayProps={{ placement: tooltipPlacement }}
|
2021-11-30 09:54:14 -05:00
|
|
|
>
|
|
|
|
<Button
|
|
|
|
bsStyle="default"
|
|
|
|
bsSize="xs"
|
|
|
|
onClick={() => syncToCode(position, 72)}
|
2022-04-12 06:44:20 -04:00
|
|
|
disabled={syncToCodeInFlight}
|
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>
|
2022-05-18 09:46:10 -04:00
|
|
|
</Tooltip>
|
2021-11-30 09:54:14 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
2022-05-18 09:46:10 -04:00
|
|
|
<Tooltip
|
|
|
|
id="sync-to-pdf"
|
|
|
|
description={t('go_to_code_location_in_pdf')}
|
|
|
|
overlayProps={{ placement: tooltipPlacement }}
|
2021-11-30 09:54:14 -05:00
|
|
|
>
|
|
|
|
<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>
|
2022-05-18 09:46:10 -04:00
|
|
|
</Tooltip>
|
2021-11-30 09:54:14 -05:00
|
|
|
)
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const listener = event => setCursorPosition(event.detail)
|
|
|
|
window.addEventListener('cursor:editor:update', listener)
|
|
|
|
return () => window.removeEventListener('cursor:editor:update', listener)
|
|
|
|
}, [ide])
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const [syncToPdfInFlight, setSyncToPdfInFlight] = useState(false)
|
|
|
|
const [syncToCodeInFlight, setSyncToCodeInFlight] = useDetachState(
|
|
|
|
'sync-to-code-inflight',
|
2021-11-30 09:54:14 -05:00
|
|
|
false,
|
2022-03-31 07:22:36 -04:00
|
|
|
'detacher',
|
|
|
|
'detached'
|
2021-11-30 09:54:14 -05:00
|
|
|
)
|
2021-10-21 06:31:51 -04:00
|
|
|
|
|
|
|
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])
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const goToCodeLine = useCallback(
|
2021-11-30 09:54:14 -05:00
|
|
|
(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]
|
|
|
|
)
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const goToPdfLocation = useCallback(
|
2021-11-30 09:54:14 -05:00
|
|
|
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 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]
|
|
|
|
)
|
|
|
|
|
2022-04-08 05:07:13 -04:00
|
|
|
const cursorPositionRef = useRef(cursorPosition)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
cursorPositionRef.current = cursorPosition
|
|
|
|
}, [cursorPosition])
|
|
|
|
|
|
|
|
const handleSyncToPdf = useCallback(() => {
|
|
|
|
syncToPdf(cursorPositionRef.current)
|
|
|
|
}, [syncToPdf])
|
|
|
|
|
|
|
|
useScopeEventListener('cursor:editor:syncToPdf', handleSyncToPdf)
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const _syncToCode = useCallback(
|
2021-10-21 06:31:51 -04:00
|
|
|
(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
|
|
|
)
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const syncToCode = useDetachAction(
|
|
|
|
'sync-to-code',
|
|
|
|
_syncToCode,
|
|
|
|
'detached',
|
|
|
|
'detacher'
|
|
|
|
)
|
|
|
|
|
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-31 07:22:36 -04:00
|
|
|
const [hasSingleSelectedDoc, setHasSingleSelectedDoc] = useDetachState(
|
|
|
|
'has-single-selected-doc',
|
|
|
|
false,
|
|
|
|
'detacher',
|
|
|
|
'detached'
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-21 10:47:01 -04:00
|
|
|
if (selectedEntities.length !== 1) {
|
2022-03-31 07:22:36 -04:00
|
|
|
setHasSingleSelectedDoc(false)
|
|
|
|
return
|
2022-03-21 10:47:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedEntities[0].type !== 'doc') {
|
2022-03-31 07:22:36 -04:00
|
|
|
setHasSingleSelectedDoc(false)
|
|
|
|
return
|
2022-03-21 10:47:01 -04:00
|
|
|
}
|
2022-03-31 07:22:36 -04:00
|
|
|
|
|
|
|
setHasSingleSelectedDoc(true)
|
|
|
|
}, [selectedEntities, setHasSingleSelectedDoc])
|
2022-03-21 10:47:01 -04:00
|
|
|
|
2022-02-14 09:20:06 -05:00
|
|
|
if (!position) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
if (!pdfUrl || 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
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
} 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}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|