PdfController.coffee 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. compileCount = 0
  106. $scope.recompile = (options = {}) ->
  107. return if $scope.pdf.compiling
  108. $scope.pdf.compiling = true
  109. ide.$scope.$broadcast("flush-changes")
  110. if !options.isAutoCompile
  111. compileCount++
  112. if compileCount == 1
  113. event_tracking.send('editor-interaction', 'single-compile')
  114. else if compileCount == 3
  115. event_tracking.send('editor-interaction', 'multi-compile')
  116. options.rootDocOverride_id = getRootDocOverride_id()
  117. sendCompileRequest(options)
  118. .success (data) ->
  119. $scope.pdf.view = "pdf"
  120. $scope.pdf.compiling = false
  121. parseCompileResponse(data)
  122. .error () ->
  123. $scope.pdf.compiling = false
  124. $scope.pdf.error = true
  125. # This needs to be public.
  126. ide.$scope.recompile = $scope.recompile
  127. $scope.clearCache = () ->
  128. $http {
  129. url: "/project/#{$scope.project_id}/output"
  130. method: "DELETE"
  131. headers:
  132. "X-Csrf-Token": window.csrfToken
  133. }
  134. $scope.toggleLogs = () ->
  135. if !$scope.pdf.view? or $scope.pdf.view == "pdf"
  136. $scope.pdf.view = "logs"
  137. else
  138. $scope.pdf.view = "pdf"
  139. $scope.showPdf = () ->
  140. $scope.pdf.view = "pdf"
  141. $scope.toggleRawLog = () ->
  142. $scope.pdf.showRawLog = !$scope.pdf.showRawLog
  143. $scope.openOutputFile = (file) ->
  144. window.open("/project/#{$scope.project_id}/output/#{file.path}")
  145. $scope.openClearCacheModal = () ->
  146. modalInstance = $modal.open(
  147. templateUrl: "clearCacheModalTemplate"
  148. controller: "ClearCacheModalController"
  149. scope: $scope
  150. )
  151. $scope.syncToCode = (position) ->
  152. synctex
  153. .syncToCode(position)
  154. .then (data) ->
  155. {doc, line} = data
  156. ide.editorManager.openDoc(doc, gotoLine: line)
  157. $scope.switchToFlatLayout = () ->
  158. $scope.ui.pdfLayout = 'flat'
  159. $scope.ui.view = 'pdf'
  160. ide.localStorage "pdf.layout", "flat"
  161. $scope.switchToSideBySideLayout = () ->
  162. $scope.ui.pdfLayout = 'sideBySide'
  163. $scope.ui.view = 'editor'
  164. localStorage "pdf.layout", "split"
  165. if pdfLayout = localStorage("pdf.layout")
  166. $scope.switchToSideBySideLayout() if pdfLayout == "split"
  167. $scope.switchToFlatLayout() if pdfLayout == "flat"
  168. else
  169. $scope.switchToSideBySideLayout()
  170. App.factory "synctex", ["ide", "$http", "$q", (ide, $http, $q) ->
  171. synctex =
  172. syncToPdf: (cursorPosition) ->
  173. deferred = $q.defer()
  174. doc_id = ide.editorManager.getCurrentDocId()
  175. if !doc_id?
  176. deferred.reject()
  177. return deferred.promise
  178. doc = ide.fileTreeManager.findEntityById(doc_id)
  179. if !doc?
  180. deferred.reject()
  181. return deferred.promise
  182. path = ide.fileTreeManager.getEntityPath(doc)
  183. if !path?
  184. deferred.reject()
  185. return deferred.promise
  186. # If the root file is folder/main.tex, then synctex sees the
  187. # path as folder/./main.tex
  188. rootDocDirname = ide.fileTreeManager.getRootDocDirname()
  189. if rootDocDirname? and rootDocDirname != ""
  190. path = path.replace(RegExp("^#{rootDocDirname}"), "#{rootDocDirname}/.")
  191. {row, column} = cursorPosition
  192. $http({
  193. url: "/project/#{ide.project_id}/sync/code",
  194. method: "GET",
  195. params: {
  196. file: path
  197. line: row + 1
  198. column: column
  199. }
  200. })
  201. .success (data) ->
  202. deferred.resolve(data.pdf or [])
  203. .error (error) ->
  204. deferred.reject(error)
  205. return deferred.promise
  206. syncToCode: (position, options = {}) ->
  207. deferred = $q.defer()
  208. if !position?
  209. deferred.reject()
  210. return deferred.promise
  211. # It's not clear exactly where we should sync to if it wasn't directly
  212. # clicked on, but a little bit down from the very top seems best.
  213. if options.includeVisualOffset
  214. position.offset.top = position.offset.top + 80
  215. $http({
  216. url: "/project/#{ide.project_id}/sync/pdf",
  217. method: "GET",
  218. params: {
  219. page: position.page + 1
  220. h: position.offset.left.toFixed(2)
  221. v: position.offset.top.toFixed(2)
  222. }
  223. })
  224. .success (data) ->
  225. if data.code? and data.code.length > 0
  226. doc = ide.fileTreeManager.findEntityByPath(data.code[0].file)
  227. return if !doc?
  228. deferred.resolve({doc: doc, line: data.code[0].line})
  229. .error (error) ->
  230. deferred.reject(error)
  231. return deferred.promise
  232. return synctex
  233. ]
  234. App.controller "PdfSynctexController", ["$scope", "synctex", "ide", ($scope, synctex, ide) ->
  235. @cursorPosition = null
  236. ide.$scope.$on "cursor:editor:update", (event, @cursorPosition) =>
  237. $scope.syncToPdf = () =>
  238. return if !@cursorPosition?
  239. synctex
  240. .syncToPdf(@cursorPosition)
  241. .then (highlights) ->
  242. $scope.pdf.highlights = highlights
  243. $scope.syncToCode = () ->
  244. synctex
  245. .syncToCode($scope.pdf.position, includeVisualOffset: true)
  246. .then (data) ->
  247. {doc, line} = data
  248. ide.editorManager.openDoc(doc, gotoLine: line)
  249. ]
  250. App.controller "PdfLogEntryController", ["$scope", "ide", ($scope, ide) ->
  251. $scope.openInEditor = (entry) ->
  252. entity = ide.fileTreeManager.findEntityByPath(entry.file)
  253. return if !entity? or entity.type != "doc"
  254. if entry.line?
  255. line = entry.line
  256. ide.editorManager.openDoc(entity, gotoLine: line)
  257. ]
  258. App.controller 'ClearCacheModalController', ["$scope", "$modalInstance", ($scope, $modalInstance) ->
  259. $scope.state =
  260. inflight: false
  261. $scope.clear = () ->
  262. $scope.state.inflight = true
  263. $scope
  264. .clearCache()
  265. .then () ->
  266. $scope.state.inflight = false
  267. $modalInstance.close()
  268. $scope.cancel = () ->
  269. $modalInstance.dismiss('cancel')
  270. ]