preview-logs-pane-entry.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import React, { useState, useRef } from 'react'
  2. import PropTypes from 'prop-types'
  3. import classNames from 'classnames'
  4. import { useTranslation } from 'react-i18next'
  5. import { OverlayTrigger, Tooltip } from 'react-bootstrap'
  6. import useExpandCollapse from '../../../shared/hooks/use-expand-collapse'
  7. import useResizeObserver from '../../../shared/hooks/use-resize-observer'
  8. import Icon from '../../../shared/components/icon'
  9. function PreviewLogsPaneEntry({
  10. headerTitle,
  11. rawContent,
  12. logType,
  13. formattedContent,
  14. extraInfoURL,
  15. level,
  16. sourceLocation,
  17. showSourceLocationLink = true,
  18. showCloseButton = false,
  19. entryAriaLabel = null,
  20. customClass,
  21. onSourceLocationClick,
  22. onClose
  23. }) {
  24. const logEntryClasses = classNames('log-entry', customClass)
  25. function handleLogEntryLinkClick(e) {
  26. e.preventDefault()
  27. onSourceLocationClick(sourceLocation)
  28. }
  29. return (
  30. <div className={logEntryClasses} aria-label={entryAriaLabel}>
  31. <PreviewLogEntryHeader
  32. level={level}
  33. sourceLocation={sourceLocation}
  34. headerTitle={headerTitle}
  35. logType={logType}
  36. showSourceLocationLink={showSourceLocationLink}
  37. onSourceLocationClick={handleLogEntryLinkClick}
  38. showCloseButton={showCloseButton}
  39. onClose={onClose}
  40. />
  41. {rawContent || formattedContent ? (
  42. <PreviewLogEntryContent
  43. rawContent={rawContent}
  44. formattedContent={formattedContent}
  45. extraInfoURL={extraInfoURL}
  46. />
  47. ) : null}
  48. </div>
  49. )
  50. }
  51. function PreviewLogEntryHeader({
  52. sourceLocation,
  53. level,
  54. headerTitle,
  55. logType,
  56. showSourceLocationLink = true,
  57. showCloseButton = false,
  58. onSourceLocationClick,
  59. onClose
  60. }) {
  61. const { t } = useTranslation()
  62. const logLocationSpanRef = useRef()
  63. const [showLocationTooltip, setShowLocationTooltip] = useState(false)
  64. useResizeObserver(
  65. logLocationSpanRef,
  66. showLocationTooltip,
  67. setTooltipForLogLocationLinkIfNeeded
  68. )
  69. const file = sourceLocation ? sourceLocation.file : null
  70. const line = sourceLocation ? sourceLocation.line : null
  71. const logEntryHeaderClasses = classNames('log-entry-header', {
  72. 'log-entry-header-error': level === 'error',
  73. 'log-entry-header-warning': level === 'warning',
  74. 'log-entry-header-typesetting': level === 'typesetting',
  75. 'log-entry-header-raw': level === 'raw'
  76. })
  77. const logEntryLocationBtnClasses = classNames('log-entry-header-link', {
  78. 'log-entry-header-link-error': level === 'error',
  79. 'log-entry-header-link-warning': level === 'warning',
  80. 'log-entry-header-link-typesetting': level === 'typesetting',
  81. 'log-entry-header-link-raw': level === 'raw'
  82. })
  83. const headerLogLocationTitle = t('navigate_log_source', {
  84. location: file + (line ? `, ${line}` : '')
  85. })
  86. function setTooltipForLogLocationLinkIfNeeded(observedElement) {
  87. const spanEl = observedElement.target
  88. const shouldShowTooltip = spanEl.scrollWidth > spanEl.clientWidth
  89. setShowLocationTooltip(shouldShowTooltip)
  90. }
  91. const locationLinkText =
  92. showSourceLocationLink && file ? `${file}${line ? `, ${line}` : ''}` : null
  93. // Because we want an ellipsis on the left-hand side (e.g. "...longfilename.tex"), the
  94. // `log-entry-header-link-location` class has text laid out from right-to-left using the CSS
  95. // rule `direction: rtl;`.
  96. // This works most of the times, except when the first character of the filename is considered
  97. // a punctuation mark, like `/` (e.g. `/foo/bar/baz.sty`). In this case, because of
  98. // right-to-left writing rules, the punctuation mark is moved to the right-side of the string,
  99. // resulting in `...bar/baz.sty/` instead of `...bar/baz.sty`.
  100. // To avoid this edge-case, we wrap the `logLocationLinkText` in two directional formatting
  101. // characters:
  102. // * \u202A LEFT-TO-RIGHT EMBEDDING Treat the following text as embedded left-to-right.
  103. // * \u202C POP DIRECTIONAL FORMATTING End the scope of the last LRE, RLE, RLO, or LRO.
  104. // This essentially tells the browser that, althought the text is laid out from right-to-left,
  105. // the wrapped portion of text should follow left-to-right writing rules.
  106. const locationLink = locationLinkText ? (
  107. <button
  108. className={logEntryLocationBtnClasses}
  109. type="button"
  110. aria-label={headerLogLocationTitle}
  111. onClick={onSourceLocationClick}
  112. >
  113. <Icon type="chain" />
  114. &nbsp;
  115. <span ref={logLocationSpanRef} className="log-entry-header-link-location">
  116. {`\u202A${locationLinkText}\u202C`}
  117. </span>
  118. </button>
  119. ) : null
  120. const locationTooltip = showLocationTooltip ? (
  121. <Tooltip id={locationLinkText} className="log-location-tooltip">
  122. {locationLinkText}
  123. </Tooltip>
  124. ) : null
  125. var headerTitleText = logType ? `${logType} ${headerTitle}` : headerTitle
  126. return (
  127. <header className={logEntryHeaderClasses}>
  128. <h3 className="log-entry-header-title">{headerTitleText}</h3>
  129. {showLocationTooltip ? (
  130. <OverlayTrigger placement="left" overlay={locationTooltip}>
  131. {locationLink}
  132. </OverlayTrigger>
  133. ) : (
  134. locationLink
  135. )}
  136. {showCloseButton ? (
  137. <button
  138. className="btn-inline-link log-entry-header-link"
  139. type="button"
  140. aria-label={t('dismiss_error_popup')}
  141. onClick={onClose}
  142. >
  143. <span aria-hidden="true">&times;</span>
  144. </button>
  145. ) : null}
  146. </header>
  147. )
  148. }
  149. function PreviewLogEntryContent({
  150. rawContent,
  151. formattedContent,
  152. extraInfoURL
  153. }) {
  154. const {
  155. isExpanded,
  156. needsExpandCollapse,
  157. expandableProps,
  158. toggleProps
  159. } = useExpandCollapse({
  160. collapsedSize: 150
  161. })
  162. const buttonContainerClasses = classNames(
  163. 'log-entry-content-button-container',
  164. {
  165. 'log-entry-content-button-container-collapsed': !isExpanded
  166. }
  167. )
  168. const { t } = useTranslation()
  169. return (
  170. <div className="log-entry-content">
  171. {rawContent ? (
  172. <div className="log-entry-content-raw-container">
  173. <div {...expandableProps}>
  174. <pre className="log-entry-content-raw">{rawContent.trim()}</pre>
  175. </div>
  176. {needsExpandCollapse ? (
  177. <div className={buttonContainerClasses}>
  178. <button
  179. type="button"
  180. className="btn btn-xs btn-default log-entry-btn-expand-collapse"
  181. {...toggleProps}
  182. >
  183. {isExpanded ? (
  184. <>
  185. <Icon type="angle-up" /> {t('collapse')}
  186. </>
  187. ) : (
  188. <>
  189. <Icon type="angle-down" /> {t('expand')}
  190. </>
  191. )}
  192. </button>
  193. </div>
  194. ) : null}
  195. </div>
  196. ) : null}
  197. {formattedContent ? (
  198. <div className="log-entry-formatted-content">{formattedContent}</div>
  199. ) : null}
  200. {extraInfoURL ? (
  201. <div className="log-entry-content-link">
  202. <a href={extraInfoURL} target="_blank">
  203. {t('log_hint_extra_info')}
  204. </a>
  205. </div>
  206. ) : null}
  207. </div>
  208. )
  209. }
  210. PreviewLogEntryHeader.propTypes = {
  211. sourceLocation: PropTypes.shape({
  212. file: PropTypes.string,
  213. // `line should be either a number or null (i.e. not required), but currently sometimes we get
  214. // an empty string (from BibTeX errors), which is why we're using `any` here. We should revert
  215. // to PropTypes.number (not required) once we fix that.
  216. line: PropTypes.any,
  217. column: PropTypes.any
  218. }),
  219. level: PropTypes.string.isRequired,
  220. headerTitle: PropTypes.string,
  221. logType: PropTypes.string,
  222. showSourceLocationLink: PropTypes.bool,
  223. showCloseButton: PropTypes.bool,
  224. onSourceLocationClick: PropTypes.func,
  225. onClose: PropTypes.func
  226. }
  227. PreviewLogEntryContent.propTypes = {
  228. rawContent: PropTypes.string,
  229. formattedContent: PropTypes.node,
  230. extraInfoURL: PropTypes.string
  231. }
  232. PreviewLogsPaneEntry.propTypes = {
  233. sourceLocation: PreviewLogEntryHeader.propTypes.sourceLocation,
  234. headerTitle: PropTypes.string,
  235. rawContent: PropTypes.string,
  236. logType: PropTypes.string,
  237. formattedContent: PropTypes.node,
  238. extraInfoURL: PropTypes.string,
  239. level: PropTypes.oneOf(['error', 'warning', 'typesetting', 'raw']).isRequired,
  240. customClass: PropTypes.string,
  241. showSourceLocationLink: PropTypes.bool,
  242. showCloseButton: PropTypes.bool,
  243. entryAriaLabel: PropTypes.string,
  244. onSourceLocationClick: PropTypes.func,
  245. onClose: PropTypes.func
  246. }
  247. export default PreviewLogsPaneEntry