PdfController.js 36 KB

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