From 374cf99ef2a7e20768498cfdc6db8b68127c69b9 Mon Sep 17 00:00:00 2001 From: James Allen Date: Tue, 8 Apr 2014 16:49:21 +0100 Subject: [PATCH] Double click on PDF syncs with code --- .../Features/Compile/CompileController.coffee | 3 ++ services/web/app/coffee/router.coffee | 3 ++ .../web/public/coffee/pdf/CompiledView.coffee | 1 + .../web/public/coffee/pdf/PDFjsView.coffee | 2 + .../web/public/coffee/pdf/PdfManager.coffee | 19 +++++++++ .../public/js/libs/pdfListView/PdfListView.js | 40 ++++++++++++++++++- .../js/libs/pdfListView/TextLayerBuilder.js | 7 ++++ 7 files changed, 74 insertions(+), 1 deletion(-) diff --git a/services/web/app/coffee/Features/Compile/CompileController.coffee b/services/web/app/coffee/Features/Compile/CompileController.coffee index 3641398d18..5a6538cc8b 100644 --- a/services/web/app/coffee/Features/Compile/CompileController.coffee +++ b/services/web/app/coffee/Features/Compile/CompileController.coffee @@ -35,6 +35,9 @@ module.exports = CompileController = getFileFromClsi: (req, res, next = (error) ->) -> CompileController.proxyToClsi("/project/#{req.params.Project_id}/output/#{req.params.file}", req, res, next) + proxySync: (req, res, next = (error) ->) -> + CompileController.proxyToClsi(req.url, req, res, next) + proxyToClsi: (url, req, res, next = (error) ->) -> logger.log url: url, "proxying to CLSI" url = "#{Settings.apis.clsi.url}#{url}" diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index 01fd6cc489..e96ec5afed 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -108,6 +108,9 @@ module.exports = class Router next() ), SecurityManager.requestCanAccessProject, CompileController.getFileFromClsi app.del "/project/:Project_id/output", SecurityManager.requestCanAccessProject, CompileController.deleteAuxFiles + app.get "/project/:Project_id/sync/code", SecurityManager.requestCanAccessProject, CompileController.proxySync + app.get "/project/:Project_id/sync/pdf", SecurityManager.requestCanAccessProject, CompileController.proxySync + app.del '/Project/:Project_id', SecurityManager.requestIsOwner, Project.deleteProject app.post '/Project/:Project_id/clone', SecurityManager.requestCanAccessProject, Project.cloneProject diff --git a/services/web/public/coffee/pdf/CompiledView.coffee b/services/web/public/coffee/pdf/CompiledView.coffee index 770524f1ce..61d9b4ae5e 100644 --- a/services/web/public/coffee/pdf/CompiledView.coffee +++ b/services/web/public/coffee/pdf/CompiledView.coffee @@ -64,6 +64,7 @@ define [ PdfView = PdfjsView @pdfjs = true @pdfView = new PdfView(manager: @) + @pdfView.on "dblclick", (e) => @trigger "dblclick", e render: () -> @setElement(@templates.pdfPanel) diff --git a/services/web/public/coffee/pdf/PDFjsView.coffee b/services/web/public/coffee/pdf/PDFjsView.coffee index b913fbbc82..c7eab560fa 100644 --- a/services/web/public/coffee/pdf/PDFjsView.coffee +++ b/services/web/public/coffee/pdf/PDFjsView.coffee @@ -33,6 +33,8 @@ define [ @pdfListView = new PDFListView @$(".pdfjs-list-view")[0], textLayerBuilder: TextLayerBuilder annotationsLayerBuilder: AnnotationsLayerBuilder + ondblclick: (e) => + @trigger "dblclick", e #logLevel: PDFListView.Logger.DEBUG @pdfListView.listView.pageWidthOffset = 20 @pdfListView.listView.pageHeightOffset = 20 diff --git a/services/web/public/coffee/pdf/PdfManager.coffee b/services/web/public/coffee/pdf/PdfManager.coffee index 03daaddc32..e7dee888e3 100644 --- a/services/web/public/coffee/pdf/PdfManager.coffee +++ b/services/web/public/coffee/pdf/PdfManager.coffee @@ -24,6 +24,7 @@ define [ createPdfPanel: () -> @view = new CompiledView manager: @, ide: @ide + @view.on "dblclick", (e) => @syncToCode(e) @view.render() if $.localStorage("layout.pdf") == "flat" @switchToFlatView() @@ -210,3 +211,21 @@ define [ }) }] + + syncToCode: (e) -> + $.ajax { + url: "/project/#{@ide.project_id}/sync/pdf" + data: + page: e.page + 1 + h: e.x.toFixed(2) + v: e.y.toFixed(2) + type: "GET" + headers: + "X-CSRF-Token": window.csrfToken + success: (response) => + data = JSON.parse(response) + if data.code and data.code.length > 0 + file = data.code[0].file + line = data.code[0].line + @ide.fileTreeManager.openDocByPath(file, line) + } \ No newline at end of file diff --git a/services/web/public/js/libs/pdfListView/PdfListView.js b/services/web/public/js/libs/pdfListView/PdfListView.js index 539a574973..151f379fed 100644 --- a/services/web/public/js/libs/pdfListView/PdfListView.js +++ b/services/web/public/js/libs/pdfListView/PdfListView.js @@ -215,6 +215,16 @@ ListView.prototype = { // per pageContainer. this.pdfDoc.pages.map(function(page) { var pageView = new PageView(page, this); + + // TODO: Switch over to a proper event handler + var that = this; + var index = that.pageViews.length; + pageView.ondblclick = function(e) { + e.page = index; + if (that.ondblclick) { + that.ondblclick.call(that, e); + } + } this.pageViews.push(pageView); var container = new PageContainerView(this); @@ -462,6 +472,27 @@ function PageView(page, listView) { var dom = this.dom = document.createElement('div'); dom.className = "plv-page-view page-view"; + var that = this; + dom.ondblclick = function(e) { + var layerX = e.layerX; + var layerY = e.layerY; + var element = e.target; + while (element.offsetParent && element.offsetParent !== dom) { + layerX = layerX + element.offsetLeft; + layerY = layerY + element.offsetTop; + element = element.offsetParent; + } + + var pdfPoint = that.viewport.convertToPdfPoint(layerX, layerY); + var event = { + x: pdfPoint[0], + y: that.normalHeight - pdfPoint[1] + }; + + if (that.ondblclick) { + that.ondblclick.call(that.listView, event) + } + } this.createNewCanvas(); } @@ -560,6 +591,7 @@ PageView.prototype = { scaled: pixelRatio != 1 }; }, + createNewCanvas: function() { if (this.canvas) { this.dom.removeChild(this.canvas); @@ -766,13 +798,19 @@ function PDFListView(mainDiv, options) { } logger.logLevel = options.logLevel; + var self = this; + this.listView = new ListView(mainDiv, options); + this.listView.ondblclick = function(e) { + if (options.ondblclick) { + options.ondblclick.call(self, e); + } + } this.renderController = new RenderController(); this.renderController.addListView(this.listView); this.renderController.updateRenderList(); - var self = this; mainDiv.addEventListener('scroll', function() { // This will update the list AND start rendering if needed. diff --git a/services/web/public/js/libs/pdfListView/TextLayerBuilder.js b/services/web/public/js/libs/pdfListView/TextLayerBuilder.js index 61dda351ea..0b168e40f5 100644 --- a/services/web/public/js/libs/pdfListView/TextLayerBuilder.js +++ b/services/web/public/js/libs/pdfListView/TextLayerBuilder.js @@ -84,6 +84,13 @@ TextLayerBuilder.prototype = { textDiv.style.left = geom.x + 'px'; textDiv.style.top = (geom.y - fontHeight) + 'px'; + textDiv.ondblclick = function(e) { + if (window.getSelection) + window.getSelection().removeAllRanges(); + else if (document.selection) + document.selection.empty(); + } + // The content of the div is set in the `setTextContent` function. this.textDivs.push(textDiv);