PdfController.coffee 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. define [
  2. "base"
  3. "libs/latex-log-parser"
  4. ], (App, LogParser) ->
  5. App.controller "PdfController", ["$scope", "$http", "ide", "$modal", "synctex", ($scope, $http, ide, $modal, synctex) ->
  6. autoCompile = true
  7. $scope.$on "doc:opened", () ->
  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. $scope.recompile = (options = {}) ->
  84. console.log "Recompiling", options
  85. return if $scope.pdf.compiling
  86. $scope.pdf.compiling = true
  87. options.rootDocOverride_id = getRootDocOverride_id()
  88. sendCompileRequest(options)
  89. .success (data) ->
  90. $scope.pdf.view = "pdf"
  91. $scope.pdf.compiling = false
  92. parseCompileResponse(data)
  93. .error () ->
  94. $scope.pdf.compiling = false
  95. $scope.pdf.error = true
  96. $scope.clearCache = () ->
  97. $http {
  98. url: "/project/#{$scope.project_id}/output"
  99. method: "DELETE"
  100. headers:
  101. "X-Csrf-Token": window.csrfToken
  102. }
  103. $scope.toggleLogs = () ->
  104. if !$scope.pdf.view? or $scope.pdf.view == "pdf"
  105. $scope.pdf.view = "logs"
  106. else
  107. $scope.pdf.view = "pdf"
  108. $scope.showPdf = () ->
  109. $scope.pdf.view = "pdf"
  110. $scope.toggleRawLog = () ->
  111. $scope.pdf.showRawLog = !$scope.pdf.showRawLog
  112. $scope.openOutputFile = (file) ->
  113. window.open("/project/#{$scope.project_id}/output/#{file.path}")
  114. $scope.openClearCacheModal = () ->
  115. modalInstance = $modal.open(
  116. templateUrl: "clearCacheModalTemplate"
  117. controller: "ClearCacheModalController"
  118. scope: $scope
  119. )
  120. $scope.syncToCode = (position) ->
  121. console.log "SYNCING VIA DBL CLICK", position
  122. synctex
  123. .syncToCode(position)
  124. .then (data) ->
  125. {doc, line} = data
  126. ide.editorManager.openDoc(doc, gotoLine: line)
  127. ]
  128. App.factory "synctex", ["ide", "$http", "$q", (ide, $http, $q) ->
  129. synctex =
  130. syncToPdf: (cursorPosition) ->
  131. deferred = $q.defer()
  132. doc_id = ide.editorManager.getCurrentDocId()
  133. if !doc_id?
  134. deferred.reject()
  135. return deferred.promise()
  136. doc = ide.fileTreeManager.findEntityById(doc_id)
  137. if !doc?
  138. deferred.reject()
  139. return deferred.promise()
  140. path = ide.fileTreeManager.getEntityPath(doc)
  141. if !path?
  142. deferred.reject()
  143. return deferred.promise()
  144. # If the root file is folder/main.tex, then synctex sees the
  145. # path as folder/./main.tex
  146. rootDocDirname = ide.fileTreeManager.getRootDocDirname()
  147. if rootDocDirname? and rootDocDirname != ""
  148. path = path.replace(RegExp("^#{rootDocDirname}"), "#{rootDocDirname}/.")
  149. {row, column} = cursorPosition
  150. $http({
  151. url: "/project/#{ide.project_id}/sync/code",
  152. method: "GET",
  153. params: {
  154. file: path
  155. line: row + 1
  156. column: column
  157. }
  158. })
  159. .success (data) ->
  160. console.log "SYNCTEX RESPONSE", data
  161. deferred.resolve(data.pdf or [])
  162. .error (error) ->
  163. deferred.reject(error)
  164. return deferred.promise
  165. syncToCode: (position, options = {}) ->
  166. deferred = $q.defer()
  167. if !position?
  168. deferred.reject()
  169. return deferred.promise()
  170. # It's not clear exactly where we should sync to if it wasn't directly
  171. # clicked on, but a little bit down from the very top seems best.
  172. if options.includeVisualOffset
  173. position.offset.top = position.offset.top + 80
  174. $http({
  175. url: "/project/#{ide.project_id}/sync/pdf",
  176. method: "GET",
  177. params: {
  178. page: position.page + 1
  179. h: position.offset.left.toFixed(2)
  180. v: position.offset.top.toFixed(2)
  181. }
  182. })
  183. .success (data) ->
  184. console.log "SYNCTEX RESPONSE", data
  185. if data.code? and data.code.length > 0
  186. doc = ide.fileTreeManager.findEntityByPath(data.code[0].file)
  187. return if !doc?
  188. deferred.resolve({doc: doc, line: data.code[0].line})
  189. .error (error) ->
  190. deferred.reject(error)
  191. return deferred.promise
  192. return synctex
  193. ]
  194. App.controller "PdfSynctexController", ["$scope", "synctex", "ide", ($scope, synctex, ide) ->
  195. $scope.syncToPdf = () ->
  196. synctex
  197. .syncToPdf($scope.editor.cursorPosition)
  198. .then (highlights) ->
  199. $scope.pdf.highlights = highlights
  200. $scope.syncToCode = () ->
  201. synctex
  202. .syncToCode($scope.pdf.position, includeVisualOffset: true)
  203. .then (data) ->
  204. {doc, line} = data
  205. console.log "OPENING DOC", doc, line
  206. ide.editorManager.openDoc(doc, gotoLine: line)
  207. ]
  208. App.controller "PdfLogEntryController", ["$scope", "ide", ($scope, ide) ->
  209. $scope.openInEditor = (entry) ->
  210. console.log "OPENING", entry.file, entry.line
  211. entity = ide.fileTreeManager.findEntityByPath(entry.file)
  212. return if !entity? or entity.type != "doc"
  213. if entry.line?
  214. line = entry.line
  215. ide.editorManager.openDoc(entity, gotoLine: line)
  216. ]
  217. App.controller 'ClearCacheModalController', ["$scope", "$modalInstance", ($scope, $modalInstance) ->
  218. $scope.state =
  219. inflight: false
  220. $scope.clear = () ->
  221. $scope.state.inflight = true
  222. $scope
  223. .clearCache()
  224. .then () ->
  225. $scope.state.inflight = false
  226. $modalInstance.close()
  227. $scope.cancel = () ->
  228. $modalInstance.dismiss('cancel')
  229. ]