PdfController.js 34 KB

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