Jelajahi Sumber

catch errors in pdf viewer and reload if necessary

if more than 3 reloads, display error to the user
Brian Gough 11 tahun lalu
induk
melakukan
bf8bc27de0

+ 3 - 0
services/web/public/coffee/ide/pdf/controllers/PdfController.coffee

@@ -10,6 +10,9 @@ define [
 			$scope.recompile(isAutoCompile: true)
 			$scope.hasPremiumCompile = $scope.project.features.compileGroup == "priority"
 
+		$scope.$on "pdf:error:display", () ->
+			$scope.pdf.error = true
+
 		sendCompileRequest = (options = {}) ->
 			url = "/project/#{$scope.project_id}/compile"
 			if options.isAutoCompile

+ 12 - 1
services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee

@@ -25,7 +25,7 @@ define [
 			# $scope.pages = []
 
 			$scope.document.destroy() if $scope.document?
-
+			scope.loadCount = scope.loadCount? ? scope.loadCount + 1 : 1
 			# TODO need a proper url manipulation library to add to query string
 			$scope.document = new PDFRenderer($scope.pdfSrc + '&pdfng=true' , {
 				scale: 1,
@@ -293,6 +293,16 @@ define [
 					]
 					#scope.$apply()
 
+				scope.$on 'pdf:error', (event, error) ->
+					return if error == 'cancelled'
+					# check if too many retries or file is missing
+					if scope.loadCount > 3 || error.match(/^Missing PDF/i)
+						scope.$emit 'pdf:error:display'
+						return
+					ctrl.load()
+					# trigger a redraw
+					scope.scale = angular.copy (scope.scale)
+
 				element.on 'scroll', () ->
 					#console.log 'scroll event', element.scrollTop(), 'adjusting?', scope.adjustingScroll
 					if scope.adjustingScroll
@@ -317,6 +327,7 @@ define [
 				scope.$watch 'pdfSrc', (newVal, oldVal) ->
 					# console.log 'loading pdf', newVal, oldVal
 					return unless newVal?
+					scope.loadCount = 0; # new pdf, so reset load count
 					ctrl.load()
 					# trigger a redraw
 					scope.scale = angular.copy (scope.scale)