overleaf/services/web/public/coffee/ide/labels/LabelsManager.coffee

47 lines
1 KiB
CoffeeScript
Raw Normal View History

2017-05-24 05:07:14 -04:00
define [
], () ->
2017-05-24 09:28:27 -04:00
2017-05-24 05:07:14 -04:00
class LabelsManager
constructor: (@ide, @$scope) ->
@$scope.$root._labels = this
2017-05-24 05:07:14 -04:00
@state =
2017-05-24 09:28:27 -04:00
documents: {} # map of DocId => List[Label]
2017-05-24 05:07:14 -04:00
@loadLabelsTimeout = null
2017-05-24 05:07:14 -04:00
setTimeout(
() =>
2017-05-24 09:28:27 -04:00
# listen for document open
@$scope.$on 'document:opened', (e, doc) =>
2017-05-24 05:07:14 -04:00
setTimeout(
() =>
@scheduleLoadLabelsFromOpenDoc()
, 0
2017-05-24 05:07:14 -04:00
)
, 0
)
loadLabelsFromOpenDoc: () ->
docId = @ide.editorManager.getCurrentDocId()
docText = @ide.editorManager.getCurrentDocValue()
2017-05-24 05:07:14 -04:00
labels = []
re = /\\label\{([^\}\n\\]{0,80})\}/g
while (labelMatch = re.exec(docText)) and labels.length < 1000
if labelMatch[1]
labels.push(labelMatch[1])
2017-05-24 05:07:14 -04:00
@state.documents[docId] = labels
scheduleLoadLabelsFromOpenDoc: () ->
if @loadLabelsTimeout
clearTimeout(@loadLabelsTimeout)
@loadLabelsTimeout = setTimeout(
() =>
@loadLabelsFromOpenDoc()
2017-05-24 09:28:15 -04:00
, 1000
, this
)
2017-05-24 05:07:14 -04:00
getAllLabels: () ->
_.flatten(labels for docId, labels of @state.documents)