PdfController.coffee 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. define [
  2. "base"
  3. "libs/latex-log-parser"
  4. ], (App, LogParser) ->
  5. App.controller "PdfController", ($scope, $http, ide, $modal, synctex, event_tracking, localStorage) ->
  6. autoCompile = true
  7. $scope.$on "project:joined", () ->
  8. return if !autoCompile
  9. autoCompile = false
  10. $scope.recompile(isAutoCompile: true)
  11. $scope.hasPremiumCompile = $scope.project.features.compileGroup == "priority"
  12. $scope.$on "pdf:error:display", () ->
  13. $scope.pdf.error = true
  14. sendCompileRequest = (options = {}) ->
  15. url = "/project/#{$scope.project_id}/compile"
  16. if options.isAutoCompile
  17. url += "?auto_compile=true"
  18. return $http.post url, {
  19. rootDoc_id: options.rootDocOverride_id or null
  20. _csrf: window.csrfToken
  21. }
  22. parseCompileResponse = (response) ->
  23. # Reset everything
  24. $scope.pdf.error = false
  25. $scope.pdf.timedout = false
  26. $scope.pdf.failure = false
  27. $scope.pdf.uncompiled = false
  28. $scope.pdf.projectTooLarge = false
  29. $scope.pdf.url = null
  30. if response.status == "timedout"
  31. $scope.pdf.timedout = true
  32. else if response.status == "autocompile-backoff"
  33. $scope.pdf.uncompiled = true
  34. else if response.status == "project-too-large"
  35. $scope.pdf.projectTooLarge = true
  36. else if response.status == "failure"
  37. $scope.pdf.failure = true
  38. fetchLogs()
  39. else if response.status == "success"
  40. # define the base url
  41. $scope.pdf.url = "/project/#{$scope.project_id}/output/output.pdf?cache_bust=#{Date.now()}"
  42. # add a query string parameter for the compile group
  43. if response.compileGroup?
  44. $scope.pdf.compileGroup = response.compileGroup
  45. $scope.pdf.url = $scope.pdf.url + "&compileGroup=#{$scope.pdf.compileGroup}"
  46. # make a cache to look up files by name
  47. fileByPath = {}
  48. for file in response.outputFiles
  49. fileByPath[file.path] = file
  50. # if the pdf file has a build number, pass it to the clsi
  51. if fileByPath['output.pdf']?.build?
  52. build = fileByPath['output.pdf'].build
  53. $scope.pdf.url = $scope.pdf.url + "&build=#{build}"
  54. fetchLogs(fileByPath['output.log'])
  55. IGNORE_FILES = ["output.fls", "output.fdb_latexmk"]
  56. $scope.pdf.outputFiles = []
  57. if !response.outputFiles?
  58. return
  59. for file in response.outputFiles
  60. if IGNORE_FILES.indexOf(file.path) == -1
  61. # Turn 'output.blg' into 'blg file'.
  62. if file.path.match(/^output\./)
  63. file.name = "#{file.path.replace(/^output\./, "")} file"
  64. else
  65. file.name = file.path
  66. $scope.pdf.outputFiles.push file
  67. fetchLogs = (outputFile) ->
  68. qs = if outputFile?.build? then "?build=#{outputFile.build}" else ""
  69. $http.get "/project/#{$scope.project_id}/output/output.log" + qs
  70. .success (log) ->
  71. $scope.pdf.rawLog = log
  72. logEntries = LogParser.parse(log, ignoreDuplicates: true)
  73. $scope.pdf.logEntries = logEntries
  74. $scope.pdf.logEntries.all = logEntries.errors.concat(logEntries.warnings).concat(logEntries.typesetting)
  75. $scope.pdf.logEntryAnnotations = {}
  76. for entry in logEntries.all
  77. if entry.file?
  78. entry.file = normalizeFilePath(entry.file)
  79. entity = ide.fileTreeManager.findEntityByPath(entry.file)
  80. if entity?
  81. $scope.pdf.logEntryAnnotations[entity.id] ||= []
  82. $scope.pdf.logEntryAnnotations[entity.id].push {
  83. row: entry.line - 1
  84. type: if entry.level == "error" then "error" else "warning"
  85. text: entry.message
  86. }
  87. .error () ->
  88. $scope.pdf.logEntries = []
  89. $scope.pdf.rawLog = ""
  90. getRootDocOverride_id = () ->
  91. doc = ide.editorManager.getCurrentDocValue()
  92. return null if !doc?
  93. for line in doc.split("\n")
  94. match = line.match /(.*)\\documentclass/
  95. if match and !match[1].match /%/
  96. return ide.editorManager.getCurrentDocId()
  97. return null
  98. normalizeFilePath = (path) ->
  99. path = path.replace(/^(.*)\/compiles\/[0-9a-f]{24}\/(\.\/)?/, "")
  100. path = path.replace(/^\/compile\//, "")
  101. rootDocDirname = ide.fileTreeManager.getRootDocDirname()
  102. if rootDocDirname?
  103. path = path.replace(/^\.\//, rootDocDirname + "/")
  104. return path
  105. $scope.recompile = (options = {}) ->
  106. return if $scope.pdf.compiling
  107. $scope.pdf.compiling = true
  108. ide.$scope.$broadcast("flush-changes")
  109. options.rootDocOverride_id = getRootDocOverride_id()
  110. sendCompileRequest(options)
  111. .success (data) ->
  112. $scope.pdf.view = "pdf"
  113. $scope.pdf.compiling = false
  114. parseCompileResponse(data)
  115. .error () ->
  116. $scope.pdf.compiling = false
  117. $scope.pdf.error = true
  118. # This needs to be public.
  119. ide.$scope.recompile = $scope.recompile
  120. $scope.clearCache = () ->
  121. $http {
  122. url: "/project/#{$scope.project_id}/output"
  123. method: "DELETE"
  124. headers:
  125. "X-Csrf-Token": window.csrfToken
  126. }
  127. $scope.toggleLogs = () ->
  128. if !$scope.pdf.view? or $scope.pdf.view == "pdf"
  129. $scope.pdf.view = "logs"
  130. else
  131. $scope.pdf.view = "pdf"
  132. $scope.showPdf = () ->
  133. $scope.pdf.view = "pdf"
  134. $scope.toggleRawLog = () ->
  135. $scope.pdf.showRawLog = !$scope.pdf.showRawLog
  136. $scope.openClearCacheModal = () ->
  137. modalInstance = $modal.open(
  138. templateUrl: "clearCacheModalTemplate"
  139. controller: "ClearCacheModalController"
  140. scope: $scope
  141. )
  142. $scope.syncToCode = (position) ->
  143. synctex
  144. .syncToCode(position)
  145. .then (data) ->
  146. {doc, line} = data
  147. ide.editorManager.openDoc(doc, gotoLine: line)
  148. $scope.switchToFlatLayout = () ->
  149. $scope.ui.pdfLayout = 'flat'
  150. $scope.ui.view = 'pdf'
  151. ide.localStorage "pdf.layout", "flat"
  152. $scope.switchToSideBySideLayout = () ->
  153. $scope.ui.pdfLayout = 'sideBySide'
  154. $scope.ui.view = 'editor'
  155. localStorage "pdf.layout", "split"
  156. if pdfLayout = localStorage("pdf.layout")
  157. $scope.switchToSideBySideLayout() if pdfLayout == "split"
  158. $scope.switchToFlatLayout() if pdfLayout == "flat"
  159. else
  160. $scope.switchToSideBySideLayout()
  161. $scope.startFreeTrial = (source) ->
  162. ga?('send', 'event', 'subscription-funnel', 'compile-timeout', source)
  163. window.open("/user/subscription/new?planCode=student_free_trial_7_days")
  164. $scope.startedFreeTrial = true
  165. App.factory "synctex", ["ide", "$http", "$q", (ide, $http, $q) ->
  166. synctex =
  167. syncToPdf: (cursorPosition) ->
  168. deferred = $q.defer()
  169. doc_id = ide.editorManager.getCurrentDocId()
  170. if !doc_id?
  171. deferred.reject()
  172. return deferred.promise
  173. doc = ide.fileTreeManager.findEntityById(doc_id)
  174. if !doc?
  175. deferred.reject()
  176. return deferred.promise
  177. path = ide.fileTreeManager.getEntityPath(doc)
  178. if !path?
  179. deferred.reject()
  180. return deferred.promise
  181. # If the root file is folder/main.tex, then synctex sees the
  182. # path as folder/./main.tex
  183. rootDocDirname = ide.fileTreeManager.getRootDocDirname()
  184. if rootDocDirname? and rootDocDirname != ""
  185. path = path.replace(RegExp("^#{rootDocDirname}"), "#{rootDocDirname}/.")
  186. {row, column} = cursorPosition
  187. $http({
  188. url: "/project/#{ide.project_id}/sync/code",
  189. method: "GET",
  190. params: {
  191. file: path
  192. line: row + 1
  193. column: column
  194. }
  195. })
  196. .success (data) ->
  197. deferred.resolve(data.pdf or [])
  198. .error (error) ->
  199. deferred.reject(error)
  200. return deferred.promise
  201. syncToCode: (position, options = {}) ->
  202. deferred = $q.defer()
  203. if !position?
  204. deferred.reject()
  205. return deferred.promise
  206. # It's not clear exactly where we should sync to if it wasn't directly
  207. # clicked on, but a little bit down from the very top seems best.
  208. if options.includeVisualOffset
  209. position.offset.top = position.offset.top + 80
  210. $http({
  211. url: "/project/#{ide.project_id}/sync/pdf",
  212. method: "GET",
  213. params: {
  214. page: position.page + 1
  215. h: position.offset.left.toFixed(2)
  216. v: position.offset.top.toFixed(2)
  217. }
  218. })
  219. .success (data) ->
  220. if data.code? and data.code.length > 0
  221. doc = ide.fileTreeManager.findEntityByPath(data.code[0].file)
  222. return if !doc?
  223. deferred.resolve({doc: doc, line: data.code[0].line})
  224. .error (error) ->
  225. deferred.reject(error)
  226. return deferred.promise
  227. return synctex
  228. ]
  229. App.controller "PdfSynctexController", ["$scope", "synctex", "ide", ($scope, synctex, ide) ->
  230. @cursorPosition = null
  231. ide.$scope.$on "cursor:editor:update", (event, @cursorPosition) =>
  232. $scope.syncToPdf = () =>
  233. return if !@cursorPosition?
  234. synctex
  235. .syncToPdf(@cursorPosition)
  236. .then (highlights) ->
  237. $scope.pdf.highlights = highlights
  238. $scope.syncToCode = () ->
  239. synctex
  240. .syncToCode($scope.pdf.position, includeVisualOffset: true)
  241. .then (data) ->
  242. {doc, line} = data
  243. ide.editorManager.openDoc(doc, gotoLine: line)
  244. ]
  245. App.controller "PdfLogEntryController", ["$scope", "ide", ($scope, ide) ->
  246. $scope.openInEditor = (entry) ->
  247. entity = ide.fileTreeManager.findEntityByPath(entry.file)
  248. return if !entity? or entity.type != "doc"
  249. if entry.line?
  250. line = entry.line
  251. ide.editorManager.openDoc(entity, gotoLine: line)
  252. ]
  253. App.controller 'ClearCacheModalController', ["$scope", "$modalInstance", ($scope, $modalInstance) ->
  254. $scope.state =
  255. inflight: false
  256. $scope.clear = () ->
  257. $scope.state.inflight = true
  258. $scope
  259. .clearCache()
  260. .then () ->
  261. $scope.state.inflight = false
  262. $modalInstance.close()
  263. $scope.cancel = () ->
  264. $modalInstance.dismiss('cancel')
  265. ]