PdfController.js 34 KB

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