preview-log-entry-header.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import PropTypes from 'prop-types'
  2. import classNames from 'classnames'
  3. import { useState, useRef } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { OverlayTrigger, Tooltip } from 'react-bootstrap'
  6. import useResizeObserver from '../hooks/use-resize-observer'
  7. import Icon from '../../../shared/components/icon'
  8. function PreviewLogEntryHeader({
  9. sourceLocation,
  10. level,
  11. headerTitle,
  12. headerIcon,
  13. logType,
  14. showSourceLocationLink = true,
  15. showCloseButton = false,
  16. onSourceLocationClick,
  17. onClose,
  18. }) {
  19. const { t } = useTranslation()
  20. const logLocationSpanRef = useRef()
  21. const [locationSpanOverflown, setLocationSpanOverflown] = useState(false)
  22. useResizeObserver(
  23. logLocationSpanRef,
  24. locationSpanOverflown,
  25. checkLocationSpanOverflow
  26. )
  27. const file = sourceLocation ? sourceLocation.file : null
  28. const line = sourceLocation ? sourceLocation.line : null
  29. const logEntryHeaderClasses = classNames('log-entry-header', {
  30. 'log-entry-header-error': level === 'error',
  31. 'log-entry-header-warning': level === 'warning',
  32. 'log-entry-header-typesetting': level === 'typesetting',
  33. 'log-entry-header-raw': level === 'raw',
  34. 'log-entry-header-success': level === 'success',
  35. })
  36. const logEntryLocationBtnClasses = classNames('log-entry-header-link', {
  37. 'log-entry-header-link-error': level === 'error',
  38. 'log-entry-header-link-warning': level === 'warning',
  39. 'log-entry-header-link-typesetting': level === 'typesetting',
  40. 'log-entry-header-link-raw': level === 'raw',
  41. 'log-entry-header-link-success': level === 'success',
  42. })
  43. const headerLogLocationTitle = t('navigate_log_source', {
  44. location: file + (line ? `, ${line}` : ''),
  45. })
  46. function checkLocationSpanOverflow(observedElement) {
  47. const spanEl = observedElement.target
  48. const isOverflowing = spanEl.scrollWidth > spanEl.clientWidth
  49. setLocationSpanOverflown(isOverflowing)
  50. }
  51. const locationLinkText =
  52. showSourceLocationLink && file ? `${file}${line ? `, ${line}` : ''}` : null
  53. // Because we want an ellipsis on the left-hand side (e.g. "...longfilename.tex"), the
  54. // `log-entry-header-link-location` class has text laid out from right-to-left using the CSS
  55. // rule `direction: rtl;`.
  56. // This works most of the times, except when the first character of the filename is considered
  57. // a punctuation mark, like `/` (e.g. `/foo/bar/baz.sty`). In this case, because of
  58. // right-to-left writing rules, the punctuation mark is moved to the right-side of the string,
  59. // resulting in `...bar/baz.sty/` instead of `...bar/baz.sty`.
  60. // To avoid this edge-case, we wrap the `logLocationLinkText` in two directional formatting
  61. // characters:
  62. // * \u202A LEFT-TO-RIGHT EMBEDDING Treat the following text as embedded left-to-right.
  63. // * \u202C POP DIRECTIONAL FORMATTING End the scope of the last LRE, RLE, RLO, or LRO.
  64. // This essentially tells the browser that, althought the text is laid out from right-to-left,
  65. // the wrapped portion of text should follow left-to-right writing rules.
  66. const locationLink = locationLinkText ? (
  67. <button
  68. className={logEntryLocationBtnClasses}
  69. type="button"
  70. aria-label={headerLogLocationTitle}
  71. onClick={onSourceLocationClick}
  72. >
  73. <Icon type="chain" />
  74. &nbsp;
  75. <span ref={logLocationSpanRef} className="log-entry-header-link-location">
  76. {`\u202A${locationLinkText}\u202C`}
  77. </span>
  78. </button>
  79. ) : null
  80. const locationTooltip =
  81. locationSpanOverflown && locationLinkText ? (
  82. <Tooltip id={locationLinkText} className="log-location-tooltip">
  83. {locationLinkText}
  84. </Tooltip>
  85. ) : null
  86. const headerTitleText = logType ? `${logType} ${headerTitle}` : headerTitle
  87. return (
  88. <header className={logEntryHeaderClasses}>
  89. {headerIcon ? (
  90. <div className="log-entry-header-icon-container">{headerIcon}</div>
  91. ) : null}
  92. <h3 className="log-entry-header-title">{headerTitleText}</h3>
  93. {locationTooltip ? (
  94. <OverlayTrigger placement="left" overlay={locationTooltip}>
  95. {locationLink}
  96. </OverlayTrigger>
  97. ) : (
  98. locationLink
  99. )}
  100. {showCloseButton ? (
  101. <button
  102. className="btn-inline-link log-entry-header-link"
  103. type="button"
  104. aria-label={t('dismiss_error_popup')}
  105. onClick={onClose}
  106. >
  107. <span aria-hidden="true">&times;</span>
  108. </button>
  109. ) : null}
  110. </header>
  111. )
  112. }
  113. PreviewLogEntryHeader.propTypes = {
  114. sourceLocation: PropTypes.shape({
  115. file: PropTypes.string,
  116. // `line should be either a number or null (i.e. not required), but currently sometimes we get
  117. // an empty string (from BibTeX errors), which is why we're using `any` here. We should revert
  118. // to PropTypes.number (not required) once we fix that.
  119. line: PropTypes.any,
  120. column: PropTypes.any,
  121. }),
  122. level: PropTypes.string.isRequired,
  123. headerTitle: PropTypes.string,
  124. headerIcon: PropTypes.element,
  125. logType: PropTypes.string,
  126. showSourceLocationLink: PropTypes.bool,
  127. showCloseButton: PropTypes.bool,
  128. onSourceLocationClick: PropTypes.func,
  129. onClose: PropTypes.func,
  130. }
  131. export default PreviewLogEntryHeader