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

60 lines
1.3 KiB
CoffeeScript
Raw Normal View History

2017-05-24 05:07:14 -04:00
define [
], () ->
2017-05-24 09:28:27 -04:00
AUTOMATIC_REFRESH_PERIOD = 1000 * 60 * 10
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 09:28:27 -04:00
@periodicLoadInterval = null
2017-05-24 05:07:14 -04:00
setTimeout(
() =>
2017-05-24 09:28:27 -04:00
# set up a regular re-load
setTimeout(
() =>
@periodicLoadInterval = setInterval(
() =>
@loadLabelsFromOpenDoc()
2017-05-24 09:28:27 -04:00
, AUTOMATIC_REFRESH_PERIOD
)
, AUTOMATIC_REFRESH_PERIOD
)
# listen for document open
@$scope.$on 'document:opened', (e, doc) =>
2017-05-24 05:07:14 -04:00
setTimeout(
() =>
@scheduleLoadLabelsFromOpenDoc()
2017-05-24 05:07:14 -04:00
, 1000
)
, 0
)
loadLabelsFromOpenDoc: () ->
docId = @ide.editorManager.getCurrentDocId()
docText = @ide.editorManager.getCurrentDocValue()
2017-05-24 05:07:14 -04:00
labels = []
re = /\\label{(.*)}/g
while labelMatch = re.exec(docText)
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)