2021-06-23 05:37:08 -04:00
|
|
|
import { useState, useRef } from 'react'
|
2020-09-28 06:51:15 -04:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import classNames from 'classnames'
|
2020-11-05 05:22:05 -05:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2020-12-02 05:03:03 -05:00
|
|
|
import { OverlayTrigger, Tooltip } from 'react-bootstrap'
|
2020-11-05 05:22:05 -05:00
|
|
|
import useExpandCollapse from '../../../shared/hooks/use-expand-collapse'
|
2020-12-02 05:03:03 -05:00
|
|
|
import useResizeObserver from '../../../shared/hooks/use-resize-observer'
|
2020-09-29 07:08:49 -04:00
|
|
|
import Icon from '../../../shared/components/icon'
|
2020-09-28 06:51:15 -04:00
|
|
|
|
2020-11-26 04:58:42 -05:00
|
|
|
function PreviewLogsPaneEntry({
|
|
|
|
headerTitle,
|
2021-01-21 07:21:22 -05:00
|
|
|
headerIcon,
|
2020-11-26 04:58:42 -05:00
|
|
|
rawContent,
|
2021-01-05 05:56:38 -05:00
|
|
|
logType,
|
2020-11-26 04:58:42 -05:00
|
|
|
formattedContent,
|
2020-11-05 05:22:05 -05:00
|
|
|
extraInfoURL,
|
|
|
|
level,
|
2020-11-26 04:58:42 -05:00
|
|
|
sourceLocation,
|
|
|
|
showSourceLocationLink = true,
|
2020-11-16 05:01:01 -05:00
|
|
|
showCloseButton = false,
|
2020-11-26 04:58:42 -05:00
|
|
|
entryAriaLabel = null,
|
2020-12-02 05:03:03 -05:00
|
|
|
customClass,
|
2020-11-26 04:58:42 -05:00
|
|
|
onSourceLocationClick,
|
2021-04-27 03:52:58 -04:00
|
|
|
onClose,
|
2020-11-05 05:22:05 -05:00
|
|
|
}) {
|
2020-12-02 05:03:03 -05:00
|
|
|
const logEntryClasses = classNames('log-entry', customClass)
|
|
|
|
|
|
|
|
function handleLogEntryLinkClick(e) {
|
|
|
|
e.preventDefault()
|
2020-11-26 04:58:42 -05:00
|
|
|
onSourceLocationClick(sourceLocation)
|
2020-11-05 05:22:05 -05:00
|
|
|
}
|
2020-11-26 04:58:42 -05:00
|
|
|
|
2020-09-28 06:51:15 -04:00
|
|
|
return (
|
2020-12-02 05:03:03 -05:00
|
|
|
<div className={logEntryClasses} aria-label={entryAriaLabel}>
|
2020-11-05 05:22:05 -05:00
|
|
|
<PreviewLogEntryHeader
|
|
|
|
level={level}
|
2020-11-26 04:58:42 -05:00
|
|
|
sourceLocation={sourceLocation}
|
|
|
|
headerTitle={headerTitle}
|
2021-01-21 07:21:22 -05:00
|
|
|
headerIcon={headerIcon}
|
2021-01-05 05:56:38 -05:00
|
|
|
logType={logType}
|
2020-11-26 04:58:42 -05:00
|
|
|
showSourceLocationLink={showSourceLocationLink}
|
|
|
|
onSourceLocationClick={handleLogEntryLinkClick}
|
2020-11-16 05:01:01 -05:00
|
|
|
showCloseButton={showCloseButton}
|
|
|
|
onClose={onClose}
|
2020-11-05 05:22:05 -05:00
|
|
|
/>
|
2020-11-26 04:58:42 -05:00
|
|
|
{rawContent || formattedContent ? (
|
2020-11-05 05:22:05 -05:00
|
|
|
<PreviewLogEntryContent
|
2020-11-26 04:58:42 -05:00
|
|
|
rawContent={rawContent}
|
|
|
|
formattedContent={formattedContent}
|
2020-11-05 05:22:05 -05:00
|
|
|
extraInfoURL={extraInfoURL}
|
|
|
|
/>
|
2020-09-28 06:51:15 -04:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-05 05:22:05 -05:00
|
|
|
function PreviewLogEntryHeader({
|
2020-11-26 04:58:42 -05:00
|
|
|
sourceLocation,
|
2020-11-05 05:22:05 -05:00
|
|
|
level,
|
2020-11-26 04:58:42 -05:00
|
|
|
headerTitle,
|
2021-01-21 07:21:22 -05:00
|
|
|
headerIcon,
|
2021-01-05 05:56:38 -05:00
|
|
|
logType,
|
2020-11-26 04:58:42 -05:00
|
|
|
showSourceLocationLink = true,
|
2020-11-16 05:01:01 -05:00
|
|
|
showCloseButton = false,
|
2020-11-26 04:58:42 -05:00
|
|
|
onSourceLocationClick,
|
2021-04-27 03:52:58 -04:00
|
|
|
onClose,
|
2020-11-05 05:22:05 -05:00
|
|
|
}) {
|
|
|
|
const { t } = useTranslation()
|
2020-12-02 05:03:03 -05:00
|
|
|
const logLocationSpanRef = useRef()
|
2021-05-25 05:57:34 -04:00
|
|
|
const [locationSpanOverflown, setLocationSpanOverflown] = useState(false)
|
2020-12-02 05:03:03 -05:00
|
|
|
|
|
|
|
useResizeObserver(
|
|
|
|
logLocationSpanRef,
|
2021-05-25 05:57:34 -04:00
|
|
|
locationSpanOverflown,
|
|
|
|
checkLocationSpanOverflow
|
2020-12-02 05:03:03 -05:00
|
|
|
)
|
|
|
|
|
2020-11-26 04:58:42 -05:00
|
|
|
const file = sourceLocation ? sourceLocation.file : null
|
|
|
|
const line = sourceLocation ? sourceLocation.line : null
|
2020-11-05 05:22:05 -05:00
|
|
|
const logEntryHeaderClasses = classNames('log-entry-header', {
|
|
|
|
'log-entry-header-error': level === 'error',
|
|
|
|
'log-entry-header-warning': level === 'warning',
|
2020-11-16 05:15:29 -05:00
|
|
|
'log-entry-header-typesetting': level === 'typesetting',
|
2021-01-14 10:16:54 -05:00
|
|
|
'log-entry-header-raw': level === 'raw',
|
2021-04-27 03:52:58 -04:00
|
|
|
'log-entry-header-success': level === 'success',
|
2020-11-05 05:22:05 -05:00
|
|
|
})
|
2020-12-02 05:03:03 -05:00
|
|
|
const logEntryLocationBtnClasses = classNames('log-entry-header-link', {
|
|
|
|
'log-entry-header-link-error': level === 'error',
|
|
|
|
'log-entry-header-link-warning': level === 'warning',
|
|
|
|
'log-entry-header-link-typesetting': level === 'typesetting',
|
2021-01-14 10:16:54 -05:00
|
|
|
'log-entry-header-link-raw': level === 'raw',
|
2021-04-27 03:52:58 -04:00
|
|
|
'log-entry-header-link-success': level === 'success',
|
2020-12-02 05:03:03 -05:00
|
|
|
})
|
2020-11-16 05:01:01 -05:00
|
|
|
const headerLogLocationTitle = t('navigate_log_source', {
|
2021-04-27 03:52:58 -04:00
|
|
|
location: file + (line ? `, ${line}` : ''),
|
2020-11-05 05:22:05 -05:00
|
|
|
})
|
2020-11-16 05:01:01 -05:00
|
|
|
|
2021-05-25 05:57:34 -04:00
|
|
|
function checkLocationSpanOverflow(observedElement) {
|
2020-12-02 05:03:03 -05:00
|
|
|
const spanEl = observedElement.target
|
2021-05-25 05:57:34 -04:00
|
|
|
const isOverflowing = spanEl.scrollWidth > spanEl.clientWidth
|
|
|
|
setLocationSpanOverflown(isOverflowing)
|
2020-12-02 05:03:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const locationLinkText =
|
|
|
|
showSourceLocationLink && file ? `${file}${line ? `, ${line}` : ''}` : null
|
|
|
|
|
|
|
|
// Because we want an ellipsis on the left-hand side (e.g. "...longfilename.tex"), the
|
|
|
|
// `log-entry-header-link-location` class has text laid out from right-to-left using the CSS
|
|
|
|
// rule `direction: rtl;`.
|
|
|
|
// This works most of the times, except when the first character of the filename is considered
|
|
|
|
// a punctuation mark, like `/` (e.g. `/foo/bar/baz.sty`). In this case, because of
|
|
|
|
// right-to-left writing rules, the punctuation mark is moved to the right-side of the string,
|
|
|
|
// resulting in `...bar/baz.sty/` instead of `...bar/baz.sty`.
|
|
|
|
// To avoid this edge-case, we wrap the `logLocationLinkText` in two directional formatting
|
|
|
|
// characters:
|
|
|
|
// * \u202A LEFT-TO-RIGHT EMBEDDING Treat the following text as embedded left-to-right.
|
|
|
|
// * \u202C POP DIRECTIONAL FORMATTING End the scope of the last LRE, RLE, RLO, or LRO.
|
|
|
|
// This essentially tells the browser that, althought the text is laid out from right-to-left,
|
|
|
|
// the wrapped portion of text should follow left-to-right writing rules.
|
|
|
|
const locationLink = locationLinkText ? (
|
|
|
|
<button
|
|
|
|
className={logEntryLocationBtnClasses}
|
|
|
|
type="button"
|
|
|
|
aria-label={headerLogLocationTitle}
|
|
|
|
onClick={onSourceLocationClick}
|
|
|
|
>
|
|
|
|
<Icon type="chain" />
|
|
|
|
|
|
|
|
<span ref={logLocationSpanRef} className="log-entry-header-link-location">
|
|
|
|
{`\u202A${locationLinkText}\u202C`}
|
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
) : null
|
|
|
|
|
2021-05-25 05:57:34 -04:00
|
|
|
const locationTooltip =
|
|
|
|
locationSpanOverflown && locationLinkText ? (
|
|
|
|
<Tooltip id={locationLinkText} className="log-location-tooltip">
|
|
|
|
{locationLinkText}
|
|
|
|
</Tooltip>
|
|
|
|
) : null
|
2020-12-02 05:03:03 -05:00
|
|
|
|
2021-01-05 05:56:38 -05:00
|
|
|
var headerTitleText = logType ? `${logType} ${headerTitle}` : headerTitle
|
|
|
|
|
2020-11-05 05:22:05 -05:00
|
|
|
return (
|
|
|
|
<header className={logEntryHeaderClasses}>
|
2021-01-21 07:21:22 -05:00
|
|
|
{headerIcon ? (
|
|
|
|
<div className="log-entry-header-icon-container">{headerIcon}</div>
|
|
|
|
) : null}
|
2021-01-05 05:56:38 -05:00
|
|
|
<h3 className="log-entry-header-title">{headerTitleText}</h3>
|
2021-05-25 05:57:34 -04:00
|
|
|
{locationTooltip ? (
|
2020-12-02 05:03:03 -05:00
|
|
|
<OverlayTrigger placement="left" overlay={locationTooltip}>
|
|
|
|
{locationLink}
|
|
|
|
</OverlayTrigger>
|
|
|
|
) : (
|
|
|
|
locationLink
|
|
|
|
)}
|
2020-11-26 04:58:42 -05:00
|
|
|
{showCloseButton ? (
|
2020-11-16 05:01:01 -05:00
|
|
|
<button
|
|
|
|
className="btn-inline-link log-entry-header-link"
|
|
|
|
type="button"
|
|
|
|
aria-label={t('dismiss_error_popup')}
|
|
|
|
onClick={onClose}
|
|
|
|
>
|
|
|
|
<span aria-hidden="true">×</span>
|
|
|
|
</button>
|
|
|
|
) : null}
|
2020-11-05 05:22:05 -05:00
|
|
|
</header>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function PreviewLogEntryContent({
|
2020-11-26 04:58:42 -05:00
|
|
|
rawContent,
|
|
|
|
formattedContent,
|
2021-04-27 03:52:58 -04:00
|
|
|
extraInfoURL,
|
2020-11-05 05:22:05 -05:00
|
|
|
}) {
|
2020-12-02 05:03:03 -05:00
|
|
|
const {
|
|
|
|
isExpanded,
|
|
|
|
needsExpandCollapse,
|
|
|
|
expandableProps,
|
2021-04-27 03:52:58 -04:00
|
|
|
toggleProps,
|
2020-12-02 05:03:03 -05:00
|
|
|
} = useExpandCollapse({
|
2021-04-27 03:52:58 -04:00
|
|
|
collapsedSize: 150,
|
2020-11-05 05:22:05 -05:00
|
|
|
})
|
2020-12-02 05:03:03 -05:00
|
|
|
|
2020-11-05 05:22:05 -05:00
|
|
|
const buttonContainerClasses = classNames(
|
|
|
|
'log-entry-content-button-container',
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
'log-entry-content-button-container-collapsed': !isExpanded,
|
2020-11-05 05:22:05 -05:00
|
|
|
}
|
|
|
|
)
|
2020-12-02 05:03:03 -05:00
|
|
|
|
2020-11-05 05:22:05 -05:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="log-entry-content">
|
2020-11-26 04:58:42 -05:00
|
|
|
{rawContent ? (
|
2020-12-02 05:03:03 -05:00
|
|
|
<div className="log-entry-content-raw-container">
|
|
|
|
<div {...expandableProps}>
|
|
|
|
<pre className="log-entry-content-raw">{rawContent.trim()}</pre>
|
2020-11-26 04:58:42 -05:00
|
|
|
</div>
|
2020-12-02 05:03:03 -05:00
|
|
|
{needsExpandCollapse ? (
|
|
|
|
<div className={buttonContainerClasses}>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="btn btn-xs btn-default log-entry-btn-expand-collapse"
|
|
|
|
{...toggleProps}
|
|
|
|
>
|
|
|
|
{isExpanded ? (
|
|
|
|
<>
|
|
|
|
<Icon type="angle-up" /> {t('collapse')}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Icon type="angle-down" /> {t('expand')}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2020-11-05 05:22:05 -05:00
|
|
|
</div>
|
|
|
|
) : null}
|
2020-11-26 04:58:42 -05:00
|
|
|
{formattedContent ? (
|
|
|
|
<div className="log-entry-formatted-content">{formattedContent}</div>
|
|
|
|
) : null}
|
2020-11-05 05:22:05 -05:00
|
|
|
{extraInfoURL ? (
|
2020-11-26 04:58:42 -05:00
|
|
|
<div className="log-entry-content-link">
|
|
|
|
<a href={extraInfoURL} target="_blank">
|
2020-11-05 05:22:05 -05:00
|
|
|
{t('log_hint_extra_info')}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
PreviewLogEntryHeader.propTypes = {
|
2020-11-26 04:58:42 -05:00
|
|
|
sourceLocation: PropTypes.shape({
|
|
|
|
file: PropTypes.string,
|
|
|
|
// `line should be either a number or null (i.e. not required), but currently sometimes we get
|
|
|
|
// an empty string (from BibTeX errors), which is why we're using `any` here. We should revert
|
|
|
|
// to PropTypes.number (not required) once we fix that.
|
|
|
|
line: PropTypes.any,
|
2021-04-27 03:52:58 -04:00
|
|
|
column: PropTypes.any,
|
2020-11-26 04:58:42 -05:00
|
|
|
}),
|
2020-11-05 05:22:05 -05:00
|
|
|
level: PropTypes.string.isRequired,
|
2020-11-26 04:58:42 -05:00
|
|
|
headerTitle: PropTypes.string,
|
2021-01-21 07:21:22 -05:00
|
|
|
headerIcon: PropTypes.element,
|
2021-01-05 05:56:38 -05:00
|
|
|
logType: PropTypes.string,
|
2020-11-26 04:58:42 -05:00
|
|
|
showSourceLocationLink: PropTypes.bool,
|
2020-11-16 05:01:01 -05:00
|
|
|
showCloseButton: PropTypes.bool,
|
2020-11-26 04:58:42 -05:00
|
|
|
onSourceLocationClick: PropTypes.func,
|
2021-04-27 03:52:58 -04:00
|
|
|
onClose: PropTypes.func,
|
2020-11-05 05:22:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
PreviewLogEntryContent.propTypes = {
|
2020-11-26 04:58:42 -05:00
|
|
|
rawContent: PropTypes.string,
|
|
|
|
formattedContent: PropTypes.node,
|
2021-04-27 03:52:58 -04:00
|
|
|
extraInfoURL: PropTypes.string,
|
2020-11-05 05:22:05 -05:00
|
|
|
}
|
|
|
|
|
2020-11-26 04:58:42 -05:00
|
|
|
PreviewLogsPaneEntry.propTypes = {
|
|
|
|
sourceLocation: PreviewLogEntryHeader.propTypes.sourceLocation,
|
|
|
|
headerTitle: PropTypes.string,
|
2021-01-21 07:21:22 -05:00
|
|
|
headerIcon: PropTypes.element,
|
2020-11-26 04:58:42 -05:00
|
|
|
rawContent: PropTypes.string,
|
2021-01-05 05:56:38 -05:00
|
|
|
logType: PropTypes.string,
|
2020-11-26 04:58:42 -05:00
|
|
|
formattedContent: PropTypes.node,
|
2020-11-05 05:22:05 -05:00
|
|
|
extraInfoURL: PropTypes.string,
|
2021-01-14 10:16:54 -05:00
|
|
|
level: PropTypes.oneOf(['error', 'warning', 'typesetting', 'raw', 'success'])
|
|
|
|
.isRequired,
|
2020-12-02 05:03:03 -05:00
|
|
|
customClass: PropTypes.string,
|
2020-11-26 04:58:42 -05:00
|
|
|
showSourceLocationLink: PropTypes.bool,
|
2020-11-16 05:01:01 -05:00
|
|
|
showCloseButton: PropTypes.bool,
|
2020-11-26 04:58:42 -05:00
|
|
|
entryAriaLabel: PropTypes.string,
|
|
|
|
onSourceLocationClick: PropTypes.func,
|
2021-04-27 03:52:58 -04:00
|
|
|
onClose: PropTypes.func,
|
2020-09-28 06:51:15 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 04:58:42 -05:00
|
|
|
export default PreviewLogsPaneEntry
|