PdfController.js 34 KB

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