PdfController.js 35 KB

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