PdfController.coffee 8.3 KB

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