overleaf/services/web/public/coffee/ide/pdf/controllers/PdfController.coffee

498 lines
16 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
"base"
"ace/ace"
"ide/human-readable-logs/HumanReadableLogs"
2016-03-08 11:18:02 -05:00
"libs/bib-log-parser"
], (App, Ace, HumanReadableLogs, BibLogParser) ->
App.controller "PdfController", ($scope, $http, ide, $modal, synctex, event_tracking, localStorage) ->
2016-03-22 06:24:58 -04:00
2016-06-28 03:40:30 -04:00
# enable per-user containers by default
perUserCompile = true
2014-07-08 07:02:26 -04:00
autoCompile = true
# pdf.view = uncompiled | pdf | errors
$scope.pdf.view = if $scope?.pdf?.url then 'pdf' else 'uncompiled'
2016-03-22 06:24:58 -04:00
$scope.shouldShowLogs = false
$scope.wikiEnabled = window.wikiEnabled;
# view logic to check whether the files dropdown should "drop up" or "drop down"
$scope.shouldDropUp = false
logsContainerEl = document.querySelector ".pdf-logs"
filesDropdownEl = logsContainerEl?.querySelector ".files-dropdown"
# get the top coordinate of the files dropdown as a ratio (to the logs container height)
# logs container supports scrollable content, so it's possible that ratio > 1.
getFilesDropdownTopCoordAsRatio = () ->
filesDropdownEl?.getBoundingClientRect().top / logsContainerEl?.getBoundingClientRect().height
$scope.$watch "shouldShowLogs", (shouldShow) ->
if shouldShow
$scope.$applyAsync () ->
$scope.shouldDropUp = getFilesDropdownTopCoordAsRatio() > 0.65
2016-06-30 06:30:30 -04:00
# log hints tracking
2016-07-05 11:27:14 -04:00
$scope.trackLogHintsLearnMore = () ->
event_tracking.sendCountly "logs-hints-learn-more"
2016-07-05 11:27:14 -04:00
2016-06-30 06:30:30 -04:00
trackLogHintsFeedback = (isPositive, hintId) ->
event_tracking.send "log-hints", (if isPositive then "feedback-positive" else "feedback-negative"), hintId
event_tracking.sendCountly (if isPositive then "log-hints-feedback-positive" else "log-hints-feedback-negative"), { hintId }
2016-06-30 06:30:30 -04:00
$scope.trackLogHintsPositiveFeedback = (hintId) -> trackLogHintsFeedback true, hintId
$scope.trackLogHintsNegativeFeedback = (hintId) -> trackLogHintsFeedback false, hintId
if ace.require("ace/lib/useragent").isMac
$scope.modifierKey = "Cmd"
else
$scope.modifierKey = "Ctrl"
2016-06-16 09:03:42 -04:00
# utility for making a query string from a hash, could use jquery $.param
createQueryString = (args) ->
qs_args = ("#{k}=#{v}" for k, v of args)
if qs_args.length then "?" + qs_args.join("&") else ""
$scope.stripHTMLFromString = (htmlStr) ->
tmp = document.createElement("DIV")
tmp.innerHTML = htmlStr
return tmp.textContent || tmp.innerText || ""
$scope.$on "project:joined", () ->
2014-07-08 07:02:26 -04:00
return if !autoCompile
autoCompile = false
$scope.recompile(isAutoCompile: true)
$scope.hasPremiumCompile = $scope.project.features.compileGroup == "priority"
2014-07-08 07:02:26 -04:00
$scope.$on "pdf:error:display", () ->
$scope.pdf.view = 'errors'
$scope.pdf.renderingError = true
2016-02-02 09:50:48 -05:00
$scope.draft = localStorage("draft:#{$scope.project_id}") or false
$scope.$watch "draft", (new_value, old_value) ->
if new_value? and old_value != new_value
localStorage("draft:#{$scope.project_id}", new_value)
2014-07-08 07:02:26 -04:00
sendCompileRequest = (options = {}) ->
url = "/project/#{$scope.project_id}/compile"
params = {}
2014-07-08 07:02:26 -04:00
if options.isAutoCompile
params["auto_compile"]=true
2014-07-08 07:02:26 -04:00
return $http.post url, {
rootDoc_id: options.rootDocOverride_id or null
2016-02-02 09:50:48 -05:00
draft: $scope.draft
2014-07-08 07:02:26 -04:00
_csrf: window.csrfToken
}, {params: params}
2014-07-08 07:02:26 -04:00
parseCompileResponse = (response) ->
2014-07-08 07:02:26 -04:00
# Reset everything
$scope.pdf.error = false
$scope.pdf.timedout = false
$scope.pdf.failure = false
$scope.pdf.url = null
$scope.pdf.clsiMaintenance = false
$scope.pdf.tooRecentlyCompiled = false
$scope.pdf.renderingError = false
2016-06-02 08:09:11 -04:00
$scope.pdf.projectTooLarge = false
2014-07-08 07:02:26 -04:00
2016-05-24 10:10:55 -04:00
# make a cache to look up files by name
fileByPath = {}
if response?.outputFiles?
for file in response?.outputFiles
fileByPath[file.path] = file
2016-05-24 10:10:55 -04:00
2014-07-08 07:02:26 -04:00
if response.status == "timedout"
$scope.pdf.view = 'errors'
2014-07-08 07:02:26 -04:00
$scope.pdf.timedout = true
else if response.status == "autocompile-backoff"
2016-05-12 06:02:24 -04:00
$scope.pdf.view = 'uncompiled'
else if response.status == "project-too-large"
$scope.pdf.view = 'errors'
$scope.pdf.projectTooLarge = true
2014-07-08 07:02:26 -04:00
else if response.status == "failure"
$scope.pdf.view = 'errors'
2014-07-08 07:02:26 -04:00
$scope.pdf.failure = true
2016-03-22 12:59:40 -04:00
$scope.shouldShowLogs = true
2016-05-24 10:10:55 -04:00
fetchLogs(fileByPath['output.log'], fileByPath['output.blg'])
else if response.status == 'clsi-maintenance'
$scope.pdf.view = 'errors'
$scope.pdf.clsiMaintenance = true
else if response.status == "too-recently-compiled"
$scope.pdf.view = 'errors'
$scope.pdf.tooRecentlyCompiled = true
2016-06-02 08:09:11 -04:00
else if response.status == "validation-problems"
$scope.pdf.view = "validation-problems"
$scope.pdf.validation = response.validationProblems
2016-03-22 06:32:44 -04:00
else if response.status == "success"
$scope.pdf.view = 'pdf'
2016-03-22 06:32:55 -04:00
$scope.shouldShowLogs = false
# prepare query string
qs = {}
# define the base url. if the pdf file has a build number, pass it to the clsi in the url
if fileByPath['output.pdf']?.url?
$scope.pdf.url = fileByPath['output.pdf'].url
else if fileByPath['output.pdf']?.build?
build = fileByPath['output.pdf'].build
$scope.pdf.url = "/project/#{$scope.project_id}/build/#{build}/output/output.pdf"
else
$scope.pdf.url = "/project/#{$scope.project_id}/output/output.pdf"
# check if we need to bust cache (build id is unique so don't need it in that case)
if not fileByPath['output.pdf']?.build?
qs.cache_bust = "#{Date.now()}"
# add a query string parameter for the compile group
if response.compileGroup?
$scope.pdf.compileGroup = response.compileGroup
qs.compileGroup = "#{$scope.pdf.compileGroup}"
if response.clsiServerId?
qs.clsiserverid = response.clsiServerId
ide.clsiServerId = response.clsiServerId
# convert the qs hash into a query string and append it
$scope.pdf.qs = createQueryString qs
$scope.pdf.url += $scope.pdf.qs
# Save all downloads as files
qs.popupDownload = true
$scope.pdf.downloadUrl = "/project/#{$scope.project_id}/output/output.pdf" + createQueryString(qs)
fetchLogs(fileByPath['output.log'], fileByPath['output.blg'])
2014-07-08 07:02:26 -04:00
IGNORE_FILES = ["output.fls", "output.fdb_latexmk"]
$scope.pdf.outputFiles = []
2015-02-23 12:43:22 -05:00
if !response.outputFiles?
return
# prepare list of output files for download dropdown
qs = {}
if response.clsiServerId?
qs.clsiserverid = response.clsiServerId
2014-07-08 07:02:26 -04:00
for file in response.outputFiles
if IGNORE_FILES.indexOf(file.path) == -1
$scope.pdf.outputFiles.push {
# Turn 'output.blg' into 'blg file'.
name: if file.path.match(/^output\./) then "#{file.path.replace(/^output\./, "")} file" else file.path
url: "/project/#{project_id}/output/#{file.path}" + createQueryString qs
}
2014-07-08 07:02:26 -04:00
fetchLogs = (logFile, blgFile) ->
getFile = (name, file) ->
opts =
method:"GET"
params:
clsiserverid:ide.clsiServerId
if file?.url? # FIXME clean this up when we have file.urls out consistently
opts.url = file.url
else if file?.build?
opts.url = "/project/#{$scope.project_id}/build/#{file.build}/output/#{name}"
else
opts.url = "/project/#{$scope.project_id}/output/#{name}"
return $http(opts)
# accumulate the log entries
logEntries =
all: []
errors: []
warnings: []
accumulateResults = (newEntries) ->
for key in ['all', 'errors', 'warnings']
logEntries[key] = logEntries[key].concat newEntries[key]
# use the parsers for each file type
processLog = (log) ->
$scope.pdf.rawLog = log
{errors, warnings, typesetting} = HumanReadableLogs.parse(log, ignoreDuplicates: true)
all = [].concat errors, warnings, typesetting
accumulateResults {all, errors, warnings}
processBiber = (log) ->
{errors, warnings} = BibLogParser.parse(log, {})
all = [].concat errors, warnings
accumulateResults {all, errors, warnings}
# output the results
handleError = () ->
$scope.pdf.logEntries = []
$scope.pdf.rawLog = ""
annotateFiles = () ->
$scope.pdf.logEntries = logEntries
$scope.pdf.logEntryAnnotations = {}
for entry in logEntries.all
if entry.file?
entry.file = normalizeFilePath(entry.file)
entity = ide.fileTreeManager.findEntityByPath(entry.file)
if entity?
$scope.pdf.logEntryAnnotations[entity.id] ||= []
$scope.pdf.logEntryAnnotations[entity.id].push {
row: entry.line - 1
type: if entry.level == "error" then "error" else "warning"
text: entry.message
}
# retrieve the logfile and process it
response = getFile('output.log', logFile)
.success processLog
.error handleError
if blgFile? # retrieve the blg file if present
response.success () ->
getFile('output.blg', blgFile)
# ignore errors in biber file
.success processBiber
# display the combined result
.then annotateFiles
else # otherwise just display the result
response.success annotateFiles
2014-07-08 07:02:26 -04:00
getRootDocOverride_id = () ->
doc = ide.editorManager.getCurrentDocValue()
return null if !doc?
for line in doc.split("\n")
match = line.match /^[^%]*\\documentclass/
if match
2014-07-08 07:02:26 -04:00
return ide.editorManager.getCurrentDocId()
return null
normalizeFilePath = (path) ->
path = path.replace(/^(.*)\/compiles\/[0-9a-f]{24}(-[0-9a-f]{24})?\/(\.\/)?/, "")
2014-07-08 07:02:26 -04:00
path = path.replace(/^\/compile\//, "")
rootDocDirname = ide.fileTreeManager.getRootDocDirname()
if rootDocDirname?
path = path.replace(/^\.\//, rootDocDirname + "/")
return path
$scope.recompile = (options = {}) ->
return if $scope.pdf.compiling
2016-07-05 11:02:46 -04:00
2016-07-08 06:17:18 -04:00
event_tracking.sendCountlySampled "editor-recompile-sampled", options
2014-07-08 07:02:26 -04:00
$scope.pdf.compiling = true
ide.$scope.$broadcast("flush-changes")
2014-07-08 07:02:26 -04:00
options.rootDocOverride_id = getRootDocOverride_id()
sendCompileRequest(options)
.success (data) ->
$scope.pdf.view = "pdf"
$scope.pdf.compiling = false
parseCompileResponse(data)
.error () ->
$scope.pdf.compiling = false
$scope.pdf.renderingError = false
2014-07-08 07:02:26 -04:00
$scope.pdf.error = true
$scope.pdf.view = 'errors'
2014-07-21 10:39:15 -04:00
# This needs to be public.
ide.$scope.recompile = $scope.recompile
2016-07-05 11:02:46 -04:00
# This method is a simply wrapper and exists only for tracking purposes.
ide.$scope.recompileViaKey = () ->
$scope.recompile { keyShortcut: true }
2014-07-08 07:02:26 -04:00
$scope.clearCache = () ->
$http {
url: "/project/#{$scope.project_id}/output"
method: "DELETE"
params:
clsiserverid:ide.clsiServerId
2014-07-08 07:02:26 -04:00
headers:
"X-Csrf-Token": window.csrfToken
}
$scope.toggleLogs = () ->
$scope.shouldShowLogs = !$scope.shouldShowLogs
event_tracking.sendCountly "ide-open-logs" if $scope.shouldShowLogs
2014-07-08 07:02:26 -04:00
$scope.showPdf = () ->
$scope.pdf.view = "pdf"
$scope.shouldShowLogs = false
2014-07-08 07:02:26 -04:00
$scope.toggleRawLog = () ->
$scope.pdf.showRawLog = !$scope.pdf.showRawLog
event_tracking.sendCountly "logs-view-raw" if $scope.pdf.showRawLog
2014-07-08 07:02:26 -04:00
$scope.openClearCacheModal = () ->
modalInstance = $modal.open(
templateUrl: "clearCacheModalTemplate"
controller: "ClearCacheModalController"
scope: $scope
)
$scope.syncToCode = (position) ->
synctex
.syncToCode(position)
.then (data) ->
{doc, line} = data
ide.editorManager.openDoc(doc, gotoLine: line)
2014-07-22 08:33:01 -04:00
$scope.switchToFlatLayout = () ->
$scope.ui.pdfLayout = 'flat'
$scope.ui.view = 'pdf'
ide.localStorage "pdf.layout", "flat"
2014-07-22 08:33:01 -04:00
$scope.switchToSideBySideLayout = () ->
$scope.ui.pdfLayout = 'sideBySide'
$scope.ui.view = 'editor'
localStorage "pdf.layout", "split"
if pdfLayout = localStorage("pdf.layout")
2014-07-22 08:33:01 -04:00
$scope.switchToSideBySideLayout() if pdfLayout == "split"
$scope.switchToFlatLayout() if pdfLayout == "flat"
else
$scope.switchToSideBySideLayout()
2014-07-08 07:02:26 -04:00
2015-10-15 06:38:01 -04:00
$scope.startFreeTrial = (source) ->
ga?('send', 'event', 'subscription-funnel', 'compile-timeout', source)
2016-07-12 11:41:22 -04:00
event_tracking.sendCountly "subscription-start-trial", { source }
2015-10-15 06:38:01 -04:00
window.open("/user/subscription/new?planCode=student_free_trial_7_days")
$scope.startedFreeTrial = true
2014-07-08 07:02:26 -04:00
App.factory "synctex", ["ide", "$http", "$q", (ide, $http, $q) ->
2016-06-28 03:40:30 -04:00
# enable per-user containers by default
perUserCompile = true
2016-06-14 03:40:15 -04:00
2014-07-08 07:02:26 -04:00
synctex =
syncToPdf: (cursorPosition) ->
deferred = $q.defer()
doc_id = ide.editorManager.getCurrentDocId()
if !doc_id?
deferred.reject()
return deferred.promise
doc = ide.fileTreeManager.findEntityById(doc_id)
if !doc?
deferred.reject()
return deferred.promise
path = ide.fileTreeManager.getEntityPath(doc)
if !path?
deferred.reject()
return deferred.promise
2014-07-08 07:02:26 -04:00
# If the root file is folder/main.tex, then synctex sees the
# path as folder/./main.tex
rootDocDirname = ide.fileTreeManager.getRootDocDirname()
if rootDocDirname? and rootDocDirname != ""
path = path.replace(RegExp("^#{rootDocDirname}"), "#{rootDocDirname}/.")
{row, column} = cursorPosition
$http({
url: "/project/#{ide.project_id}/sync/code",
2014-07-08 07:02:26 -04:00
method: "GET",
params: {
file: path
line: row + 1
column: column
clsiserverid:ide.clsiServerId
2014-07-08 07:02:26 -04:00
}
})
.success (data) ->
deferred.resolve(data.pdf or [])
.error (error) ->
deferred.reject(error)
return deferred.promise
syncToCode: (position, options = {}) ->
deferred = $q.defer()
if !position?
deferred.reject()
return deferred.promise
# FIXME: this actually works better if it's halfway across the
# page (or the visible part of the page). Synctex doesn't
# always find the right place in the file when the point is at
# the edge of the page, it sometimes returns the start of the
# next paragraph instead.
h = position.offset.left
# Compute the vertical position to pass to synctex, which
# works with coordinates increasing from the top of the page
# down. This matches the browser's DOM coordinate of the
# click point, but the pdf position is measured from the
# bottom of the page so we need to invert it.
if options.fromPdfPosition and position.pageSize?.height?
v = (position.pageSize.height - position.offset.top) or 0 # measure from pdf point (inverted)
else
v = position.offset.top or 0 # measure from html click position
2014-07-08 07:02:26 -04:00
# It's not clear exactly where we should sync to if it wasn't directly
# clicked on, but a little bit down from the very top seems best.
if options.includeVisualOffset
v += 72 # use the same value as in pdfViewer highlighting visual offset
2014-07-08 07:02:26 -04:00
$http({
url: "/project/#{ide.project_id}/sync/pdf",
2014-07-08 07:02:26 -04:00
method: "GET",
params: {
page: position.page + 1
h: h.toFixed(2)
v: v.toFixed(2)
clsiserverid:ide.clsiServerId
2014-07-08 07:02:26 -04:00
}
})
.success (data) ->
if data.code? and data.code.length > 0
doc = ide.fileTreeManager.findEntityByPath(data.code[0].file)
return if !doc?
deferred.resolve({doc: doc, line: data.code[0].line})
.error (error) ->
deferred.reject(error)
return deferred.promise
return synctex
]
App.controller "PdfSynctexController", ["$scope", "synctex", "ide", ($scope, synctex, ide) ->
2014-07-10 09:36:04 -04:00
@cursorPosition = null
ide.$scope.$on "cursor:editor:update", (event, @cursorPosition) =>
$scope.syncToPdf = () =>
return if !@cursorPosition?
2014-07-08 07:02:26 -04:00
synctex
2014-07-10 09:36:04 -04:00
.syncToPdf(@cursorPosition)
2014-07-08 07:02:26 -04:00
.then (highlights) ->
$scope.pdf.highlights = highlights
$scope.syncToCode = () ->
synctex
.syncToCode($scope.pdf.position, includeVisualOffset: true, fromPdfPosition: true)
2014-07-08 07:02:26 -04:00
.then (data) ->
{doc, line} = data
ide.editorManager.openDoc(doc, gotoLine: line)
]
2016-07-05 11:27:14 -04:00
App.controller "PdfLogEntryController", ["$scope", "ide", "event_tracking", ($scope, ide, event_tracking) ->
2014-07-08 07:02:26 -04:00
$scope.openInEditor = (entry) ->
event_tracking.sendCountly 'logs-jump-to-location'
2014-07-08 07:02:26 -04:00
entity = ide.fileTreeManager.findEntityByPath(entry.file)
return if !entity? or entity.type != "doc"
if entry.line?
line = entry.line
ide.editorManager.openDoc(entity, gotoLine: line)
]
App.controller 'ClearCacheModalController', ["$scope", "$modalInstance", ($scope, $modalInstance) ->
$scope.state =
inflight: false
$scope.clear = () ->
$scope.state.inflight = true
$scope
.clearCache()
.then () ->
$scope.state.inflight = false
$modalInstance.close()
$scope.cancel = () ->
$modalInstance.dismiss('cancel')
]