PdfController.js 31 KB

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