removing labels service

This commit is contained in:
Nate Stemen 2017-12-05 13:57:36 -05:00
parent 2d66fd7280
commit 9bf5d1e14c
8 changed files with 4 additions and 197 deletions

View file

@ -1,28 +0,0 @@
EditorRealTimeController = require "../Editor/EditorRealTimeController"
LabelsHandler = require './LabelsHandler'
logger = require 'logger-sharelatex'
module.exports = LabelsController =
getAllLabels: (req, res, next) ->
project_id = req.params.project_id
logger.log {project_id}, "getting all labels for project"
LabelsHandler.getAllLabelsForProject project_id, (err, projectLabels) ->
if err?
logger.err {project_id, err}, "[LabelsController] error getting all labels from project"
return next(err)
res.json {projectId: project_id, projectLabels: projectLabels}
broadcastLabelsForDoc: (req, res, next) ->
project_id = req.params.project_id
doc_id = req.params.doc_id
logger.log {project_id, doc_id}, "getting labels for doc"
LabelsHandler.getLabelsForDoc project_id, doc_id, (err, docLabels) ->
if err?
logger.err {project_id, doc_id, err}, "[LabelsController] error getting labels from doc"
return next(err)
EditorRealTimeController.emitToRoom project_id, 'broadcastDocLabels', {
docId: doc_id, labels: docLabels
}
res.sendStatus(200)

View file

@ -1,43 +0,0 @@
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
module.exports = LabelsHandler =
labelCaptureRegex: () ->
/\\label\{([^\}\n\\]{0,80})\}/g
getAllLabelsForProject: (projectId, callback=(err, projectLabels)->) ->
DocumentUpdaterHandler.flushProjectToMongo projectId, (err) ->
if err?
return callback(err)
ProjectEntityHandler.getAllDocs projectId, (err, docs) ->
if err?
return callback(err)
projectLabels = LabelsHandler.extractLabelsFromProjectDocs docs
callback(null, projectLabels)
getLabelsForDoc: (projectId, docId, callback=(err, docLabels)->) ->
DocumentUpdaterHandler.flushDocToMongo projectId, docId, (err) ->
if err?
return callback(err)
ProjectEntityHandler.getDoc projectId, docId, (err, lines) ->
if err?
return callback(err)
docLabels = LabelsHandler.extractLabelsFromDoc lines
callback(null, docLabels)
extractLabelsFromDoc: (lines) ->
docLabels = []
for line in lines
re = LabelsHandler.labelCaptureRegex()
while (labelMatch = re.exec(line))
if labelMatch[1]
docLabels.push(labelMatch[1])
return docLabels
extractLabelsFromProjectDocs: (projectDocs) ->
projectLabels = {} # docId => List[Label]
for _path, doc of projectDocs
projectLabels[doc._id] = LabelsHandler.extractLabelsFromDoc(doc.lines)
return projectLabels

View file

@ -44,7 +44,6 @@ SudoModeMiddlewear = require('./Features/SudoMode/SudoModeMiddlewear')
AnalyticsRouter = require('./Features/Analytics/AnalyticsRouter')
AnnouncementsController = require("./Features/Announcements/AnnouncementsController")
MetaController = require('./Features/Metadata/MetaController')
LabelsController = require('./Features/Labels/LabelsController')
TokenAccessController = require('./Features/TokenAccess/TokenAccessController')
Features = require('./infrastructure/Features')
@ -206,8 +205,6 @@ module.exports = class Router
webRouter.get '/project/:project_id/metadata', AuthorizationMiddlewear.ensureUserCanReadProject, AuthenticationController.requireLogin(), MetaController.getMetadata
webRouter.post '/project/:project_id/doc/:doc_id/metadata', AuthorizationMiddlewear.ensureUserCanReadProject, AuthenticationController.requireLogin(), MetaController.broadcastMetadataForDoc
webRouter.get '/project/:project_id/labels', AuthorizationMiddlewear.ensureUserCanReadProject, AuthenticationController.requireLogin(), LabelsController.getAllLabels
webRouter.post '/project/:project_id/doc/:doc_id/labels', AuthorizationMiddlewear.ensureUserCanReadProject, AuthenticationController.requireLogin(), LabelsController.broadcastLabelsForDoc
webRouter.get '/tag', AuthenticationController.requireLogin(), TagsController.getAllTags
webRouter.post '/tag', AuthenticationController.requireLogin(), TagsController.createTag
@ -360,4 +357,3 @@ module.exports = class Router
TokenAccessController.readAndWriteToken
webRouter.get '*', ErrorController.notFound

View file

@ -10,13 +10,11 @@ define [
"ide/editor/directives/aceEditor/cursor-position/CursorPositionManager"
"ide/editor/directives/aceEditor/track-changes/TrackChangesManager"
"ide/editor/directives/aceEditor/metadata/MetadataManager"
"ide/editor/directives/aceEditor/labels/LabelsManager"
"ide/labels/services/labels"
"ide/metadata/services/metadata"
"ide/graphics/services/graphics"
"ide/preamble/services/preamble"
"ide/files/services/files"
], (App, Ace, SearchBox, ModeList, UndoManager, AutoCompleteManager, SpellCheckManager, HighlightsManager, CursorPositionManager, TrackChangesManager, MetadataManager, LabelsManager) ->
], (App, Ace, SearchBox, ModeList, UndoManager, AutoCompleteManager, SpellCheckManager, HighlightsManager, CursorPositionManager, TrackChangesManager, MetadataManager) ->
EditSession = ace.require('ace/edit_session').EditSession
ModeList = ace.require('ace/ext/modelist')
@ -38,7 +36,7 @@ define [
url = ace.config._moduleUrl(args...) + "?fingerprint=#{window.aceFingerprint}"
return url
App.directive "aceEditor", ($timeout, $compile, $rootScope, event_tracking, localStorage, $cacheFactory, labels, metadata, graphics, preamble, files, $http, $q) ->
App.directive "aceEditor", ($timeout, $compile, $rootScope, event_tracking, localStorage, $cacheFactory, metadata, graphics, preamble, files, $http, $q) ->
monkeyPatchSearch($rootScope, $compile)
return {
@ -105,9 +103,8 @@ define [
highlightsManager = new HighlightsManager(scope, editor, element)
cursorPositionManager = new CursorPositionManager(scope, editor, element, localStorage)
trackChangesManager = new TrackChangesManager(scope, editor, element)
labelsManager = new LabelsManager(scope, editor, element, labels)
metadataManager = new MetadataManager(scope, editor, element, metadata)
autoCompleteManager = new AutoCompleteManager(scope, editor, element, metadataManager, labelsManager, graphics, preamble, files)
autoCompleteManager = new AutoCompleteManager(scope, editor, element, metadataManager, graphics, preamble, files)
# Prevert Ctrl|Cmd-S from triggering save dialog

View file

@ -10,7 +10,7 @@ define [
aceSnippetManager = ace.require('ace/snippets').snippetManager
class AutoCompleteManager
constructor: (@$scope, @editor, @element, @metadataManager, @labelsManager, @graphics, @preamble, @files) ->
constructor: (@$scope, @editor, @element, @metadataManager, @graphics, @preamble, @files) ->
@monkeyPatchAutocomplete()

View file

@ -1,58 +0,0 @@
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()

View file

@ -1,13 +0,0 @@
define [], () ->
class LabelsManager
constructor: (@ide, @$scope, @labels) ->
@ide.socket.on 'broadcastDocLabels', (data) =>
@labels.onBroadcastDocLabels(data)
@$scope.$on 'entity:deleted', @labels.onEntityDeleted
@$scope.$on 'file:upload:complete', @labels.fileUploadComplete
loadProjectLabelsFromServer: () ->
@labels.loadProjectLabelsFromServer()

View file

@ -1,44 +0,0 @@
define [
"base"
], (App) ->
App.factory 'labels', ($http, ide) ->
state = {documents: {}}
labels = {
state: state
}
labels.onBroadcastDocLabels = (data) ->
if data.docId and data.labels
state.documents[data.docId] = data.labels
labels.onEntityDeleted = (e, entity) ->
if entity.type == 'doc'
delete state.documents[entity.id]
labels.onFileUploadComplete = (e, upload) ->
if upload.entity_type == 'doc'
labels.loadDocLabelsFromServer(upload.entity_id)
labels.getAllLabels = () ->
_.flatten(labels for docId, labels of state.documents)
labels.loadProjectLabelsFromServer = () ->
$http
.get("/project/#{window.project_id}/labels")
.then (response) ->
{ data } = response
if data.projectLabels
for docId, docLabels of data.projectLabels
state.documents[docId] = docLabels
labels.loadDocLabelsFromServer = (docId) ->
$http
.post(
"/project/#{window.project_id}/doc/#{docId}/labels",
{_csrf: window.csrfToken}
)
return labels