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