WIP: basically working, with client sync

This commit is contained in:
Shane Kilkelly 2017-06-05 14:48:50 +01:00
parent 51881dbbcc
commit f7ad4a4786
5 changed files with 68 additions and 29 deletions

View file

@ -11,7 +11,7 @@ module.exports = LabelsController =
if err?
logger.err {project_id, err}, "[LabelsController] error getting all labels from project"
return next(err)
res.json {projectId: project_id, labels: projectLabels}
res.json {projectId: project_id, projectLabels: projectLabels}
getLabelsForDoc: (req, res, next) ->
project_id = req.params.Project_id
@ -20,4 +20,7 @@ module.exports = LabelsController =
if err?
logger.err {project_id, doc_id, err}, "[LabelsController] error getting labels from doc"
return next(err)
EditorRealTimeController.emitToRoom project_id, 'doc:labels:updated', {
docId: doc_id, labels: docLabels
}
res.json {projectId: project_id, docId: doc_id, labels: docLabels}

View file

@ -1,4 +1,5 @@
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
module.exports = LabelsHandler =
@ -16,6 +17,9 @@ module.exports = LabelsHandler =
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, rev) ->
if err?
return callback(err)

View file

@ -172,5 +172,7 @@ define [
# User can append ?ft=somefeature to url to activate a feature toggle
ide.featureToggle = location?.search?.match(/^\?ft=(\w+)$/)?[1]
ide.socket.on 'doc:labels:updated', (data) ->
$scope.$broadcast 'doc:labels:updated', data
angular.bootstrap(document.body, ["SharelatexApp"])

View file

@ -92,6 +92,7 @@ define [
highlightsManager = new HighlightsManager(scope, editor, element)
cursorPositionManager = new CursorPositionManager(scope, editor, element, localStorage)
trackChangesManager = new TrackChangesManager(scope, editor, element)
if scope.name == 'editor'
labelsManager = new LabelsManager(scope, editor, element)
autoCompleteManager = new AutoCompleteManager(scope, editor, element, labelsManager)

View file

@ -15,63 +15,92 @@ define [
window.GET_LABELS = () =>
@loadProjectLabelsFromServer()
window.GET_DOC_LABELS = () =>
@loadCurrentDocLabelsFromServer()
@state =
documents: {} # map of DocId => List[Label]
@loadLabelsTimeout = null
@$scope.$on 'doc:labels:updated', (e, data) =>
if data.docId and data.labels
@state.documents[data.docId] = data.labels
onChange = (change) =>
if change.remote
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)
if (
!change.remote and
change.action in ['remove', 'insert'] and
((_.any(change.lines, (line) -> line.match(/\\label\{[^\}\n\\]{0,80}\}/))) or
(commandFragment?.length > 2 and commandFragment.slice(0,7) == '\\label{'))
)
@scheduleLoadLabelsFromOpenDoc()
@scheduleLoadCurrentDocLabelsFromServer()
# @scheduleLoadLabelsFromOpenDoc()
@editor.on "changeSession", (e) =>
e.oldSession.off "change", onChange
e.session.on "change", onChange
setTimeout(
() =>
# @scheduleLoadLabelsFromOpenDoc()
# setTimeout(
# () =>
# # @scheduleLoadLabelsFromOpenDoc()
# @loadProjectLabelsFromServer()
# , 0
# )
# Load now
@loadProjectLabelsFromServer()
, 0
)
loadProjectLabelsFromServer: () ->
$.get(
"/project/#{window.project_id}/labels"
, (data) =>
console.log ">>", data
if data.labels
for docId, docLabels of data.labels
if data.projectLabels
for docId, docLabels of data.projectLabels
@state.documents[docId] = docLabels
console.log @state.documents
)
loadLabelsFromOpenDoc: () ->
docId = @$scope.docId
docText = @editor.getValue()
labels = []
re = /\\label\{([^\}\n\\]{0,80})\}/g
while (labelMatch = re.exec(docText)) and labels.length < 1000
if labelMatch[1]
labels.push(labelMatch[1])
@state.documents[docId] = labels
loadCurrentDocLabelsFromServer: () ->
$.get(
"/project/#{window.project_id}/#{@$scope.docId}/labels"
, (data) =>
if data.docId and data.labels
@state.documents[data.docId] = data.labels
)
scheduleLoadLabelsFromOpenDoc: () ->
# loadLabelsFromOpenDoc: () ->
# docId = @$scope.docId
# docText = @editor.getValue()
# labels = []
# re = /\\label\{([^\}\n\\]{0,80})\}/g
# while (labelMatch = re.exec(docText)) and labels.length < 1000
# if labelMatch[1]
# labels.push(labelMatch[1])
# @state.documents[docId] = labels
# scheduleLoadLabelsFromOpenDoc: () ->
# # De-bounce loading labels with a timeout
# if @loadLabelsTimeout
# clearTimeout(@loadLabelsTimeout)
# @loadLabelsTimeout = setTimeout(
# () =>
# @loadLabelsFromOpenDoc()
# , 1000
# , this
# )
scheduleLoadCurrentDocLabelsFromServer: () ->
# De-bounce loading labels with a timeout
if @loadLabelsTimeout
clearTimeout(@loadLabelsTimeout)
@loadLabelsTimeout = setTimeout(
() =>
@loadLabelsFromOpenDoc()
@loadCurrentDocLabelsFromServer()
, 1000
, this
)