mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
46 lines
995 B
CoffeeScript
46 lines
995 B
CoffeeScript
define [
|
|
], () ->
|
|
|
|
class LabelsManager
|
|
constructor: (@ide, @$scope) ->
|
|
@$scope.$root._labels = this
|
|
|
|
@state =
|
|
documents: {} # map of DocId => List[Label]
|
|
|
|
@loadLabelsTimeout = null
|
|
|
|
setTimeout(
|
|
() =>
|
|
# listen for document open
|
|
@$scope.$on 'document:opened', (e, doc) =>
|
|
setTimeout(
|
|
() =>
|
|
@scheduleLoadLabelsFromOpenDoc()
|
|
, 0
|
|
)
|
|
, 0
|
|
)
|
|
|
|
loadLabelsFromOpenDoc: () ->
|
|
docId = @ide.editorManager.getCurrentDocId()
|
|
docText = @ide.editorManager.getCurrentDocValue()
|
|
labels = []
|
|
re = /\\label{(.*)}/g
|
|
while labelMatch = re.exec(docText)
|
|
if labelMatch[1]
|
|
labels.push(labelMatch[1])
|
|
@state.documents[docId] = labels
|
|
|
|
scheduleLoadLabelsFromOpenDoc: () ->
|
|
if @loadLabelsTimeout
|
|
clearTimeout(@loadLabelsTimeout)
|
|
@loadLabelsTimeout = setTimeout(
|
|
() =>
|
|
@loadLabelsFromOpenDoc()
|
|
, 1000
|
|
, this
|
|
)
|
|
|
|
getAllLabels: () ->
|
|
_.flatten(labels for docId, labels of @state.documents)
|