From 00e3e8da9fd2cf668244541cbb39ab3d531606bc Mon Sep 17 00:00:00 2001 From: Nate Stemen Date: Thu, 16 Nov 2017 14:59:04 -0500 Subject: [PATCH] adding last labels configuration --- .../aceEditor/labels/LabelsManager.coffee | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee diff --git a/services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee b/services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee new file mode 100644 index 0000000000..a5d2c10625 --- /dev/null +++ b/services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee @@ -0,0 +1,58 @@ +define [ + "ace/ace" +], () -> + Range = ace.require("ace/range").Range + + getLastCommandFragment = (lineUpToCursor) -> + if m = lineUpToCursor.match(/(\\[^\\]+)$/) + return m[1] + else + return null + + class LabelsManager + constructor: (@$scope, @editor, @element, @Labels) -> + @debouncer = {} # DocId => Timeout + + onChange = (change) => + if change.remote + return + if change.action not in ['remove', 'insert'] + return + cursorPosition = @editor.getCursorPosition() + end = change.end + range = new Range(end.row, 0, end.row, end.column) + lineUpToCursor = @editor.getSession().getTextRange(range) + commandFragment = getLastCommandFragment(lineUpToCursor) + linesContainLabel = _.any(change.lines, (line) -> line.match(/\\label\{[^\}\n\\]{0,80}\}/)) + lastCommandFragmentIsLabel = commandFragment?.slice(0,7) == '\\label{' + if linesContainLabel or lastCommandFragmentIsLabel + @scheduleLoadCurrentDocLabelsFromServer() + + @editor.on "changeSession", (e) => + e.oldSession.off "change", onChange + e.session.on "change", onChange + + loadCurrentDocLabelsFromServer: () -> + currentDocId = @$scope.docId + @Labels.loadDocLabelsFromServer(currentDocId) + + loadDocLabelsFromServer: (docId) -> + @Labels.loadDocLabelsFromServer(docId) + + scheduleLoadCurrentDocLabelsFromServer: () -> + # De-bounce loading labels with a timeout + currentDocId = @$scope.docId + existingTimeout = @debouncer[currentDocId] + if existingTimeout? + clearTimeout(existingTimeout) + delete @debouncer[currentDocId] + @debouncer[currentDocId] = setTimeout( + () => + @loadDocLabelsFromServer(currentDocId) + delete @debouncer[currentDocId] + , 1000 + , this + ) + + getAllLabels: () -> + @Labels.getAllLabels()