PdfController.js 33 KB

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