PdfController.js 37 KB

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