PdfController.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. import App from '../../../base'
  2. import HumanReadableLogs from '../../human-readable-logs/HumanReadableLogs'
  3. import BibLogParser from '../../log-parser/bib-log-parser'
  4. import PreviewPane from '../../../features/preview/components/preview-pane'
  5. import { react2angular } from 'react2angular'
  6. import { rootContext } from '../../../shared/context/root-context'
  7. import 'ace/ace'
  8. import getMeta from '../../../utils/meta'
  9. import { trackPdfDownload } from './PdfJsMetrics'
  10. const AUTO_COMPILE_MAX_WAIT = 5000
  11. // We add a 1 second debounce to sending user changes to server if they aren't
  12. // collaborating with anyone. This needs to be higher than that, and allow for
  13. // client to server latency, otherwise we compile before the op reaches the server
  14. // and then again on ack.
  15. const AUTO_COMPILE_DEBOUNCE = 2000
  16. App.filter('trusted', $sce => url => $sce.trustAsResourceUrl(url))
  17. App.controller(
  18. 'PdfController',
  19. function (
  20. $scope,
  21. $http,
  22. ide,
  23. $modal,
  24. synctex,
  25. eventTracking,
  26. localStorage,
  27. $q
  28. ) {
  29. let autoCompile = true
  30. // pdf.view = uncompiled | pdf | errors
  31. $scope.pdf.view = $scope.pdf.url ? 'pdf' : 'uncompiled'
  32. $scope.pdf.clearingCache = false
  33. $scope.shouldShowLogs = false
  34. $scope.logsUIVariant = window.logsUIVariant
  35. // view logic to check whether the files dropdown should "drop up" or "drop down"
  36. $scope.shouldDropUp = false
  37. // Exposed methods for React layout handling
  38. $scope.setPdfSplitLayout = function () {
  39. $scope.$applyAsync(() => $scope.switchToSideBySideLayout('editor'))
  40. }
  41. $scope.setPdfFullLayout = function () {
  42. $scope.$applyAsync(() => $scope.switchToFlatLayout('pdf'))
  43. }
  44. const logsContainerEl = document.querySelector('.pdf-logs')
  45. const filesDropdownEl =
  46. logsContainerEl && logsContainerEl.querySelector('.files-dropdown')
  47. // get the top coordinate of the files dropdown as a ratio (to the logs container height)
  48. // logs container supports scrollable content, so it's possible that ratio > 1.
  49. function getFilesDropdownTopCoordAsRatio() {
  50. if (filesDropdownEl == null || logsContainerEl == null) {
  51. return 0
  52. }
  53. return (
  54. filesDropdownEl.getBoundingClientRect().top /
  55. logsContainerEl.getBoundingClientRect().height
  56. )
  57. }
  58. $scope.$watch('shouldShowLogs', shouldShow => {
  59. if (shouldShow) {
  60. $scope.$applyAsync(() => {
  61. $scope.shouldDropUp = getFilesDropdownTopCoordAsRatio() > 0.65
  62. })
  63. }
  64. })
  65. $scope.trackLogHintsLearnMore = function () {
  66. eventTracking.sendMB('logs-hints-learn-more')
  67. }
  68. if (ace.require('ace/lib/useragent').isMac) {
  69. $scope.modifierKey = 'Cmd'
  70. } else {
  71. $scope.modifierKey = 'Ctrl'
  72. }
  73. // utility for making a query string from a hash, could use jquery $.param
  74. function createQueryString(args) {
  75. const qsArgs = []
  76. for (const k in args) {
  77. const v = args[k]
  78. qsArgs.push(`${k}=${v}`)
  79. }
  80. if (qsArgs.length) {
  81. return `?${qsArgs.join('&')}`
  82. } else {
  83. return ''
  84. }
  85. }
  86. $scope.$on('project:joined', () => {
  87. if (!autoCompile) {
  88. return
  89. }
  90. autoCompile = false
  91. $scope.recompile({ isAutoCompileOnLoad: true })
  92. $scope.hasPremiumCompile =
  93. $scope.project.features.compileGroup === 'priority'
  94. })
  95. $scope.$on('pdf:error:display', function () {
  96. $scope.pdf.view = 'errors'
  97. $scope.pdf.renderingError = true
  98. })
  99. let autoCompileInterval = null
  100. function autoCompileIfReady() {
  101. if (
  102. $scope.pdf.compiling ||
  103. !$scope.autocompile_enabled ||
  104. !$scope.pdf.uncompiled
  105. ) {
  106. return
  107. }
  108. // Only checking linting if syntaxValidation is on and visible to the user
  109. const autoCompileLintingError =
  110. ide.$scope.hasLintingError && ide.$scope.settings.syntaxValidation
  111. if ($scope.autoCompileLintingError !== autoCompileLintingError) {
  112. $scope.$apply(() => {
  113. $scope.autoCompileLintingError = autoCompileLintingError
  114. // We've likely been waiting a while until the user fixed the linting, but we
  115. // don't want to compile as soon as it is fixed, so reset the timeout.
  116. $scope.startedTryingAutoCompileAt = Date.now()
  117. $scope.docLastChangedAt = Date.now()
  118. })
  119. }
  120. if (autoCompileLintingError && $scope.stop_on_validation_error) {
  121. return
  122. }
  123. // If there's a longish compile, don't compile immediately after if user is still typing
  124. const startedTryingAt = Math.max(
  125. $scope.startedTryingAutoCompileAt,
  126. $scope.lastFinishedCompileAt || 0
  127. )
  128. const timeSinceStartedTrying = Date.now() - startedTryingAt
  129. const timeSinceLastChange = Date.now() - $scope.docLastChangedAt
  130. let shouldCompile = false
  131. if (timeSinceLastChange > AUTO_COMPILE_DEBOUNCE) {
  132. // Don't compile in the middle of the user typing
  133. shouldCompile = true
  134. } else if (timeSinceStartedTrying > AUTO_COMPILE_MAX_WAIT) {
  135. // Unless they type for a long time
  136. shouldCompile = true
  137. } else if (timeSinceStartedTrying < 0 || timeSinceLastChange < 0) {
  138. // If time is non-monotonic, assume that the user's system clock has been
  139. // changed and continue with compile
  140. shouldCompile = true
  141. }
  142. if (shouldCompile) {
  143. return triggerAutoCompile()
  144. }
  145. }
  146. function triggerAutoCompile() {
  147. $scope.recompile({ isAutoCompileOnChange: true })
  148. }
  149. function startTryingAutoCompile() {
  150. if (autoCompileInterval != null) {
  151. return
  152. }
  153. $scope.startedTryingAutoCompileAt = Date.now()
  154. autoCompileInterval = setInterval(autoCompileIfReady, 200)
  155. }
  156. function stopTryingAutoCompile() {
  157. clearInterval(autoCompileInterval)
  158. autoCompileInterval = null
  159. }
  160. $scope.changesToAutoCompile = false
  161. $scope.$watch('pdf.uncompiled', uncompiledChanges => {
  162. // don't autocompile if disabled or the pdf is not visible
  163. if (
  164. $scope.pdf.uncompiled &&
  165. $scope.autocompile_enabled &&
  166. !$scope.ui.pdfHidden
  167. ) {
  168. $scope.changesToAutoCompile = true
  169. startTryingAutoCompile()
  170. } else {
  171. $scope.changesToAutoCompile = false
  172. stopTryingAutoCompile()
  173. }
  174. })
  175. function recalculateUncompiledChanges() {
  176. if ($scope.docLastChangedAt == null) {
  177. $scope.pdf.uncompiled = false
  178. } else if (
  179. $scope.lastStartedCompileAt == null ||
  180. $scope.docLastChangedAt > $scope.lastStartedCompileAt
  181. ) {
  182. $scope.pdf.uncompiled = true
  183. } else {
  184. $scope.pdf.uncompiled = false
  185. }
  186. }
  187. function _updateDocLastChangedAt() {
  188. $scope.docLastChangedAt = Date.now()
  189. recalculateUncompiledChanges()
  190. }
  191. function onDocChanged() {
  192. _updateDocLastChangedAt()
  193. }
  194. function onDocSaved() {
  195. // We use the save as a trigger too, to account for the delay between the client
  196. // and server. Otherwise, we might have compiled after the user made
  197. // the change on the client, but before the server had it.
  198. _updateDocLastChangedAt()
  199. }
  200. function onCompilingStateChanged(compiling) {
  201. recalculateUncompiledChanges()
  202. }
  203. ide.$scope.$on('doc:changed', onDocChanged)
  204. ide.$scope.$on('doc:saved', onDocSaved)
  205. $scope.$watch('pdf.compiling', onCompilingStateChanged)
  206. $scope.autocompile_enabled =
  207. localStorage(`autocompile_enabled:${$scope.project_id}`) || false
  208. $scope.$watch('autocompile_enabled', (newValue, oldValue) => {
  209. if (newValue != null && oldValue !== newValue) {
  210. if (newValue === true) {
  211. $scope.autoCompileLintingError = false
  212. autoCompileIfReady()
  213. }
  214. localStorage(`autocompile_enabled:${$scope.project_id}`, newValue)
  215. }
  216. })
  217. // abort compile if syntax checks fail
  218. $scope.stop_on_validation_error = localStorage(
  219. `stop_on_validation_error:${$scope.project_id}`
  220. )
  221. if ($scope.stop_on_validation_error == null) {
  222. $scope.stop_on_validation_error = true
  223. }
  224. // turn on for all users by default
  225. $scope.$watch('stop_on_validation_error', (newValue, oldValue) => {
  226. if (newValue != null && oldValue !== newValue) {
  227. localStorage(`stop_on_validation_error:${$scope.project_id}`, newValue)
  228. }
  229. })
  230. $scope.draft = localStorage(`draft:${$scope.project_id}`) || false
  231. $scope.$watch('draft', (newValue, oldValue) => {
  232. if (newValue != null && oldValue !== newValue) {
  233. localStorage(`draft:${$scope.project_id}`, newValue)
  234. }
  235. })
  236. function sendCompileRequest(options) {
  237. if (options == null) {
  238. options = {}
  239. }
  240. const url = `/project/${$scope.project_id}/compile`
  241. const params = {}
  242. if (options.isAutoCompileOnLoad || options.isAutoCompileOnChange) {
  243. params.auto_compile = true
  244. }
  245. if (getMeta('ol-enablePdfCaching')) {
  246. params.enable_pdf_caching = true
  247. }
  248. if (window.location.search.includes('file_line_errors=true')) {
  249. params.file_line_errors = 'true'
  250. }
  251. // if the previous run was a check, clear the error logs
  252. if ($scope.check) {
  253. $scope.pdf.logEntries = {}
  254. }
  255. // keep track of whether this is a compile or check
  256. $scope.check = !!options.check
  257. if (options.check) {
  258. eventTracking.sendMB('syntax-check-request')
  259. }
  260. // send appropriate check type to clsi
  261. let checkType
  262. if ($scope.check) {
  263. checkType = 'validate' // validate only
  264. } else if (options.try) {
  265. checkType = 'silent' // allow use to try compile once
  266. } else if ($scope.stop_on_validation_error) {
  267. checkType = 'error' // try to compile
  268. } else {
  269. checkType = 'silent' // ignore errors
  270. }
  271. // FIXME: Temporarily disable syntax checking as it is causing
  272. // excessive support requests for projects migrated from v1
  273. // https://github.com/overleaf/sharelatex/issues/911
  274. if (checkType === 'error') {
  275. checkType = 'silent'
  276. }
  277. return $http.post(
  278. url,
  279. {
  280. rootDoc_id: options.rootDocOverride_id || null,
  281. draft: $scope.draft,
  282. check: checkType,
  283. // use incremental compile for all users but revert to a full
  284. // compile if there is a server error
  285. incrementalCompilesEnabled: !$scope.pdf.error,
  286. _csrf: window.csrfToken,
  287. },
  288. { params }
  289. )
  290. }
  291. function buildPdfDownloadUrl(pdfDownloadDomain, url) {
  292. if (pdfDownloadDomain) {
  293. return `${pdfDownloadDomain}${url}`
  294. } else {
  295. return url
  296. }
  297. }
  298. function noop() {}
  299. function parseCompileResponse(response, compileTimeClientE2E) {
  300. // keep last url
  301. const lastPdfUrl = $scope.pdf.url
  302. const { pdfDownloadDomain } = response
  303. // Reset everything
  304. $scope.pdf.error = false
  305. $scope.pdf.timedout = false
  306. $scope.pdf.failure = false
  307. $scope.pdf.url = null
  308. $scope.pdf.updateConsumedBandwidth = noop
  309. $scope.pdf.firstRenderDone = noop
  310. $scope.pdf.clsiMaintenance = false
  311. $scope.pdf.clsiUnavailable = false
  312. $scope.pdf.tooRecentlyCompiled = false
  313. $scope.pdf.renderingError = false
  314. $scope.pdf.projectTooLarge = false
  315. $scope.pdf.compileTerminated = false
  316. $scope.pdf.compileExited = false
  317. $scope.pdf.failedCheck = false
  318. $scope.pdf.compileInProgress = false
  319. $scope.pdf.autoCompileDisabled = false
  320. $scope.pdf.compileFailed = false
  321. // make a cache to look up files by name
  322. const fileByPath = {}
  323. if (response.outputFiles != null) {
  324. for (const file of response.outputFiles) {
  325. fileByPath[file.path] = file
  326. }
  327. }
  328. // prepare query string
  329. let qs = {}
  330. // add a query string parameter for the compile group
  331. if (response.compileGroup != null) {
  332. $scope.pdf.compileGroup = qs.compileGroup = response.compileGroup
  333. }
  334. // add a query string parameter for the clsi server id
  335. if (response.clsiServerId != null) {
  336. $scope.pdf.clsiServerId = qs.clsiserverid = response.clsiServerId
  337. }
  338. // TODO(das7pad): drop this hack once 2747f0d40af8729304 has landed in clsi
  339. if (response.status === 'success' && !fileByPath['output.pdf']) {
  340. response.status = 'failure'
  341. }
  342. if (response.status === 'success') {
  343. $scope.pdf.view = 'pdf'
  344. $scope.shouldShowLogs = false
  345. $scope.pdf.lastCompileTimestamp = Date.now()
  346. $scope.pdf.validation = {}
  347. $scope.pdf.url = buildPdfDownloadUrl(
  348. pdfDownloadDomain,
  349. fileByPath['output.pdf'].url
  350. )
  351. if (window.location.search.includes('verify_chunks=true')) {
  352. // Instruct the serviceWorker to verify composed ranges.
  353. qs.verify_chunks = 'true'
  354. }
  355. if (getMeta('ol-enablePdfCaching')) {
  356. // Tag traffic that uses the pdf caching logic.
  357. qs.enable_pdf_caching = 'true'
  358. }
  359. // convert the qs hash into a query string and append it
  360. $scope.pdf.url += createQueryString(qs)
  361. if (getMeta('ol-trackPdfDownload')) {
  362. const { firstRenderDone, updateConsumedBandwidth } = trackPdfDownload(
  363. response,
  364. compileTimeClientE2E
  365. )
  366. $scope.pdf.firstRenderDone = firstRenderDone
  367. $scope.pdf.updateConsumedBandwidth = updateConsumedBandwidth
  368. }
  369. // Save all downloads as files
  370. qs.popupDownload = true
  371. const { build: buildId } = fileByPath['output.pdf']
  372. $scope.pdf.downloadUrl =
  373. `/download/project/${$scope.project_id}/build/${buildId}/output/output.pdf` +
  374. createQueryString(qs)
  375. fetchLogs(fileByPath, { pdfDownloadDomain })
  376. } else if (response.status === 'timedout') {
  377. $scope.pdf.view = 'errors'
  378. $scope.pdf.timedout = true
  379. fetchLogs(fileByPath, { pdfDownloadDomain })
  380. if (
  381. !$scope.hasPremiumCompile &&
  382. ide.$scope.project.owner._id === ide.$scope.user.id
  383. ) {
  384. eventTracking.send(
  385. 'subscription-funnel',
  386. 'editor-click-feature',
  387. 'compile-timeout'
  388. )
  389. eventTracking.sendMB('paywall-prompt', {
  390. 'paywall-type': 'compile-timeout',
  391. })
  392. }
  393. } else if (response.status === 'terminated') {
  394. $scope.pdf.view = 'errors'
  395. $scope.pdf.compileTerminated = true
  396. fetchLogs(fileByPath, { pdfDownloadDomain })
  397. } else if (
  398. ['validation-fail', 'validation-pass'].includes(response.status)
  399. ) {
  400. $scope.pdf.view = 'pdf'
  401. $scope.pdf.url = lastPdfUrl
  402. $scope.shouldShowLogs = true
  403. if (response.status === 'validation-fail') {
  404. $scope.pdf.failedCheck = true
  405. }
  406. eventTracking.sendMB(`syntax-check-${response.status}`)
  407. fetchLogs(fileByPath, { validation: true, pdfDownloadDomain })
  408. } else if (response.status === 'exited') {
  409. $scope.pdf.view = 'pdf'
  410. $scope.pdf.compileExited = true
  411. $scope.pdf.url = lastPdfUrl
  412. $scope.shouldShowLogs = true
  413. fetchLogs(fileByPath, { pdfDownloadDomain })
  414. } else if (response.status === 'autocompile-backoff') {
  415. if ($scope.pdf.isAutoCompileOnLoad) {
  416. // initial autocompile
  417. $scope.pdf.view = 'uncompiled'
  418. } else {
  419. // background autocompile from typing
  420. $scope.pdf.view = 'errors'
  421. $scope.pdf.autoCompileDisabled = true
  422. $scope.autocompile_enabled = false // disable any further autocompiles
  423. }
  424. } else if (response.status === 'project-too-large') {
  425. $scope.pdf.view = 'errors'
  426. $scope.pdf.projectTooLarge = true
  427. } else if (response.status === 'failure') {
  428. $scope.pdf.view = 'errors'
  429. $scope.pdf.failure = true
  430. $scope.pdf.downloadUrl = null
  431. $scope.shouldShowLogs = true
  432. fetchLogs(fileByPath, { pdfDownloadDomain })
  433. } else if (response.status === 'clsi-maintenance') {
  434. $scope.pdf.view = 'errors'
  435. $scope.pdf.clsiMaintenance = true
  436. } else if (response.status === 'unavailable') {
  437. $scope.pdf.view = 'errors'
  438. $scope.pdf.clsiUnavailable = true
  439. } else if (response.status === 'too-recently-compiled') {
  440. $scope.pdf.view = 'errors'
  441. $scope.pdf.tooRecentlyCompiled = true
  442. } else if (response.status === 'validation-problems') {
  443. $scope.pdf.view = 'validation-problems'
  444. $scope.pdf.validation = response.validationProblems
  445. $scope.shouldShowLogs = false
  446. } else if (response.status === 'compile-in-progress') {
  447. $scope.pdf.view = 'errors'
  448. $scope.pdf.compileInProgress = true
  449. } else {
  450. // fall back to displaying an error
  451. $scope.pdf.view = 'errors'
  452. $scope.pdf.error = true
  453. }
  454. const IGNORE_FILES = ['output.fls', 'output.fdb_latexmk']
  455. $scope.pdf.outputFiles = []
  456. if (response.outputFiles == null) {
  457. return
  458. }
  459. // prepare list of output files for download dropdown
  460. qs = {}
  461. if (response.clsiServerId != null) {
  462. qs.clsiserverid = response.clsiServerId
  463. }
  464. for (const file of response.outputFiles) {
  465. if (IGNORE_FILES.indexOf(file.path) === -1) {
  466. const isOutputFile = /^output\./.test(file.path)
  467. $scope.pdf.outputFiles.push({
  468. // Turn 'output.blg' into 'blg file'.
  469. name: isOutputFile
  470. ? `${file.path.replace(/^output\./, '')} file`
  471. : file.path,
  472. url: file.url + createQueryString(qs),
  473. main: !!isOutputFile,
  474. fileName: file.path,
  475. type: file.type,
  476. })
  477. }
  478. }
  479. // sort the output files into order, main files first, then others
  480. $scope.pdf.outputFiles.sort(
  481. (a, b) => b.main - a.main || a.name.localeCompare(b.name)
  482. )
  483. }
  484. // In the existing compile UI, errors and validation problems are shown in the PDF pane, whereas in the new
  485. // one they're shown in the logs pane. This `$watch`er makes sure we change the view in the new logs UI.
  486. // This should be removed once we stop supporting the two different log UIs.
  487. if (window.logsUIVariant !== 'default') {
  488. $scope.$watch(
  489. () =>
  490. $scope.pdf.view === 'errors' ||
  491. $scope.pdf.view === 'validation-problems',
  492. newVal => {
  493. if (newVal) {
  494. $scope.shouldShowLogs = true
  495. $scope.pdf.compileFailed = true
  496. }
  497. }
  498. )
  499. }
  500. function fetchLogs(fileByPath, options) {
  501. let blgFile, chktexFile, logFile
  502. if (options != null ? options.validation : undefined) {
  503. chktexFile = fileByPath['output.chktex']
  504. } else {
  505. logFile = fileByPath['output.log']
  506. blgFile = fileByPath['output.blg']
  507. }
  508. function getFile(name, file) {
  509. const opts = {
  510. method: 'GET',
  511. url: buildPdfDownloadUrl(options.pdfDownloadDomain, file.url),
  512. params: {
  513. compileGroup: $scope.pdf.compileGroup,
  514. clsiserverid: $scope.pdf.clsiServerId,
  515. },
  516. }
  517. return $http(opts)
  518. }
  519. // accumulate the log entries
  520. const logEntries = {
  521. all: [],
  522. errors: [],
  523. warnings: [],
  524. typesetting: [],
  525. }
  526. function accumulateResults(newEntries) {
  527. for (const key of ['all', 'errors', 'warnings', 'typesetting']) {
  528. if (newEntries[key]) {
  529. if (newEntries.type != null) {
  530. for (const entry of newEntries[key]) {
  531. entry.type = newEntries.type
  532. }
  533. }
  534. logEntries[key] = logEntries[key].concat(newEntries[key])
  535. }
  536. }
  537. }
  538. // use the parsers for each file type
  539. function processLog(log) {
  540. $scope.pdf.rawLog = log
  541. const { errors, warnings, typesetting } = HumanReadableLogs.parse(log, {
  542. ignoreDuplicates: true,
  543. })
  544. const all = [].concat(errors, warnings, typesetting)
  545. accumulateResults({ all, errors, warnings, typesetting })
  546. }
  547. function processChkTex(log) {
  548. const errors = []
  549. const warnings = []
  550. for (const line of log.split('\n')) {
  551. let m
  552. if ((m = line.match(/^(\S+):(\d+):(\d+): (Error|Warning): (.*)/))) {
  553. const result = {
  554. file: m[1],
  555. line: m[2],
  556. column: m[3],
  557. level: m[4].toLowerCase(),
  558. message: `${m[4]}: ${m[5]}`,
  559. }
  560. if (result.level === 'error') {
  561. errors.push(result)
  562. } else {
  563. warnings.push(result)
  564. }
  565. }
  566. }
  567. const all = [].concat(errors, warnings)
  568. const logHints = HumanReadableLogs.parse({
  569. type: 'Syntax',
  570. all,
  571. errors,
  572. warnings,
  573. })
  574. eventTracking.sendMB('syntax-check-return-count', {
  575. errors: errors.length,
  576. warnings: warnings.length,
  577. })
  578. accumulateResults(logHints)
  579. }
  580. function processBiber(log) {
  581. const bibLogParser = new BibLogParser(log, {
  582. maxErrors: 100,
  583. buildMaxErrorsReachedMessage: true,
  584. })
  585. const { errors, warnings } = bibLogParser.parse(log, {})
  586. const all = [].concat(errors, warnings)
  587. accumulateResults({ type: 'BibTeX:', all, errors, warnings })
  588. }
  589. // output the results
  590. function handleError() {
  591. $scope.pdf.logEntries = {}
  592. $scope.pdf.rawLog = ''
  593. }
  594. function annotateFiles() {
  595. $scope.pdf.logEntries = logEntries
  596. $scope.pdf.logEntryAnnotations = {}
  597. for (const entry of logEntries.all) {
  598. if (entry.file != null) {
  599. entry.file = normalizeFilePath(entry.file)
  600. const entity = ide.fileTreeManager.findEntityByPath(entry.file)
  601. if (entity != null) {
  602. if (!$scope.pdf.logEntryAnnotations[entity.id]) {
  603. $scope.pdf.logEntryAnnotations[entity.id] = []
  604. }
  605. $scope.pdf.logEntryAnnotations[entity.id].push({
  606. row: entry.line - 1,
  607. type: entry.level === 'error' ? 'error' : 'warning',
  608. text: entry.message,
  609. })
  610. }
  611. }
  612. }
  613. }
  614. // retrieve the logfile and process it
  615. let response
  616. if (logFile != null) {
  617. response = getFile('output.log', logFile).then(response =>
  618. processLog(response.data)
  619. )
  620. if (blgFile != null) {
  621. // retrieve the blg file if present
  622. response = response.then(() =>
  623. getFile('output.blg', blgFile).then(
  624. response => processBiber(response.data),
  625. () => true
  626. )
  627. )
  628. }
  629. }
  630. if (response != null) {
  631. response.catch(handleError)
  632. } else {
  633. handleError()
  634. }
  635. if (chktexFile != null) {
  636. const getChkTex = () =>
  637. getFile('output.chktex', chktexFile).then(response =>
  638. processChkTex(response.data)
  639. )
  640. // always retrieve the chktex file if present
  641. if (response != null) {
  642. response = response.then(getChkTex, getChkTex)
  643. } else {
  644. response = getChkTex()
  645. }
  646. }
  647. // display the combined result
  648. if (response != null) {
  649. response.finally(() => {
  650. annotateFiles()
  651. sendCompileMetrics()
  652. })
  653. }
  654. }
  655. function sendCompileMetrics() {
  656. const hasCompiled =
  657. $scope.pdf.view !== 'errors' &&
  658. $scope.pdf.view !== 'validation-problems'
  659. if (hasCompiled && !window.user.alphaProgram) {
  660. const metadata = {
  661. errors: $scope.pdf.logEntries.errors.length,
  662. warnings: $scope.pdf.logEntries.warnings.length,
  663. typesetting: $scope.pdf.logEntries.typesetting.length,
  664. logsUIVariant: window.logsUIVariant,
  665. }
  666. eventTracking.sendMBSampled('compile-result', metadata, 0.01)
  667. }
  668. }
  669. function getRootDocOverrideId() {
  670. const rootDocId = $scope.project.rootDoc_id
  671. const currentDocId = ide.editorManager.getCurrentDocId()
  672. if (currentDocId === rootDocId) {
  673. return null // no need to override when in the root doc itself
  674. }
  675. const doc = ide.editorManager.getCurrentDocValue()
  676. if (doc == null) {
  677. return null
  678. }
  679. for (const line of doc.split('\n')) {
  680. if (/^[^%]*\\documentclass/.test(line)) {
  681. return ide.editorManager.getCurrentDocId()
  682. }
  683. }
  684. return null
  685. }
  686. function normalizeFilePath(path) {
  687. path = path.replace(
  688. /^(.*)\/compiles\/[0-9a-f]{24}(-[0-9a-f]{24})?\/(\.\/)?/,
  689. ''
  690. )
  691. path = path.replace(/^\/compile\//, '')
  692. const rootDocDirname = ide.fileTreeManager.getRootDocDirname()
  693. if (rootDocDirname) {
  694. path = path.replace(/^\.\//, rootDocDirname + '/')
  695. } else {
  696. path = path.replace(/^\.\//, '')
  697. }
  698. return path
  699. }
  700. $scope.recompile = function (options) {
  701. if (options == null) {
  702. options = {}
  703. }
  704. if ($scope.pdf.compiling) {
  705. return
  706. }
  707. $scope.lastStartedCompileAt = Date.now()
  708. $scope.pdf.compiling = true
  709. $scope.pdf.isAutoCompileOnLoad =
  710. options != null ? options.isAutoCompileOnLoad : undefined // initial autocompile
  711. if (options != null ? options.force : undefined) {
  712. // for forced compile, turn off validation check and ignore errors
  713. $scope.stop_on_validation_error = false
  714. $scope.shouldShowLogs = false // hide the logs while compiling
  715. eventTracking.sendMB('syntax-check-turn-off-checking')
  716. }
  717. if (options != null ? options.try : undefined) {
  718. $scope.shouldShowLogs = false // hide the logs while compiling
  719. eventTracking.sendMB('syntax-check-try-compile-anyway')
  720. }
  721. ide.$scope.$broadcast('flush-changes')
  722. options.rootDocOverride_id = getRootDocOverrideId()
  723. const t0 = performance.now()
  724. sendCompileRequest(options)
  725. .then(function (response) {
  726. const { data } = response
  727. const compileTimeClientE2E = performance.now() - t0
  728. $scope.pdf.view = 'pdf'
  729. $scope.pdf.compiling = false
  730. parseCompileResponse(data, compileTimeClientE2E)
  731. })
  732. .catch(function (response) {
  733. const { status } = response
  734. if (status === 429) {
  735. $scope.pdf.rateLimited = true
  736. }
  737. $scope.pdf.compiling = false
  738. $scope.pdf.renderingError = false
  739. $scope.pdf.error = true
  740. $scope.pdf.view = 'errors'
  741. })
  742. .finally(() => {
  743. $scope.lastFinishedCompileAt = Date.now()
  744. })
  745. }
  746. // This needs to be public.
  747. ide.$scope.recompile = $scope.recompile
  748. $scope.stop = function () {
  749. if (!$scope.pdf.compiling) {
  750. return
  751. }
  752. return $http({
  753. url: `/project/${$scope.project_id}/compile/stop`,
  754. method: 'POST',
  755. params: {
  756. clsiserverid: $scope.pdf.clsiServerId,
  757. },
  758. headers: {
  759. 'X-Csrf-Token': window.csrfToken,
  760. },
  761. })
  762. }
  763. $scope.clearCache = function () {
  764. $scope.pdf.clearingCache = true
  765. const deferred = $q.defer()
  766. // disable various download buttons
  767. $scope.pdf.url = null
  768. $scope.pdf.downloadUrl = null
  769. $scope.pdf.outputFiles = []
  770. $http({
  771. url: `/project/${$scope.project_id}/output`,
  772. method: 'DELETE',
  773. params: {
  774. clsiserverid: $scope.pdf.clsiServerId,
  775. },
  776. headers: {
  777. 'X-Csrf-Token': window.csrfToken,
  778. },
  779. })
  780. .then(function (response) {
  781. $scope.pdf.clearingCache = false
  782. return deferred.resolve()
  783. })
  784. .catch(function (response) {
  785. console.error(response)
  786. const error = response.data
  787. $scope.pdf.clearingCache = false
  788. $scope.pdf.renderingError = false
  789. $scope.pdf.error = true
  790. $scope.pdf.view = 'errors'
  791. return deferred.reject(error)
  792. })
  793. return deferred.promise
  794. }
  795. $scope.recompileFromScratch = function () {
  796. $scope.pdf.compiling = true
  797. return $scope
  798. .clearCache()
  799. .then(() => {
  800. $scope.pdf.compiling = false
  801. $scope.recompile()
  802. })
  803. .catch(error => {
  804. console.error(error)
  805. })
  806. }
  807. $scope.toggleLogs = function () {
  808. $scope.$applyAsync(() => {
  809. $scope.shouldShowLogs = !$scope.shouldShowLogs
  810. if ($scope.shouldShowLogs) {
  811. eventTracking.sendMBOnce('ide-open-logs-once')
  812. }
  813. })
  814. }
  815. $scope.showPdf = function () {
  816. $scope.pdf.view = 'pdf'
  817. $scope.shouldShowLogs = false
  818. }
  819. $scope.toggleRawLog = function () {
  820. $scope.pdf.showRawLog = !$scope.pdf.showRawLog
  821. if ($scope.pdf.showRawLog) {
  822. eventTracking.sendMB('logs-view-raw')
  823. }
  824. }
  825. $scope.openClearCacheModal = function () {
  826. $modal.open({
  827. templateUrl: 'clearCacheModalTemplate',
  828. controller: 'ClearCacheModalController',
  829. scope: $scope,
  830. })
  831. }
  832. $scope.syncToCode = function (position) {
  833. synctex.syncToCode(position).then(function (data) {
  834. const { doc, line } = data
  835. ide.editorManager.openDoc(doc, { gotoLine: line })
  836. })
  837. }
  838. $scope.setAutoCompile = function (isOn) {
  839. $scope.$applyAsync(function () {
  840. $scope.autocompile_enabled = isOn
  841. })
  842. }
  843. $scope.setDraftMode = function (isOn) {
  844. $scope.$applyAsync(function () {
  845. $scope.draft = isOn
  846. })
  847. }
  848. $scope.setSyntaxCheck = function (isOn) {
  849. $scope.$applyAsync(function () {
  850. $scope.stop_on_validation_error = isOn
  851. })
  852. }
  853. $scope.runSyntaxCheckNow = function () {
  854. $scope.$applyAsync(function () {
  855. $scope.recompile({ check: true })
  856. })
  857. }
  858. $scope.openInEditor = function (entry) {
  859. let column, line
  860. const entity = ide.fileTreeManager.findEntityByPath(entry.file)
  861. if (entity == null || entity.type !== 'doc') {
  862. return
  863. }
  864. if (entry.line != null) {
  865. line = entry.line
  866. }
  867. if (entry.column != null) {
  868. column = entry.column
  869. }
  870. ide.editorManager.openDoc(entity, {
  871. gotoLine: line,
  872. gotoColumn: column,
  873. })
  874. }
  875. }
  876. )
  877. App.controller('ClearCacheModalController', function ($scope, $modalInstance) {
  878. $scope.state = { error: false, inflight: false }
  879. $scope.clear = function () {
  880. $scope.state.inflight = true
  881. $scope
  882. .clearCache()
  883. .then(function () {
  884. $scope.state.inflight = false
  885. $modalInstance.close()
  886. })
  887. .catch(function () {
  888. $scope.state.error = true
  889. $scope.state.inflight = false
  890. })
  891. }
  892. $scope.cancel = () => $modalInstance.dismiss('cancel')
  893. })
  894. // Wrap React component as Angular component. Only needed for "top-level" component
  895. App.component(
  896. 'previewPane',
  897. react2angular(
  898. rootContext.use(PreviewPane),
  899. Object.keys(PreviewPane.propTypes)
  900. )
  901. )