PdfController.js 36 KB

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