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) ->
|
2017-05-24 06:40:12 -04:00
|
|
|
@$scope.$root._labels = this
|
2017-05-24 05:07:14 -04:00
|
|
|
|
2017-05-24 06:40:12 -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
|
|
|
|
2017-05-24 06:40:12 -04:00
|
|
|
@loadLabelsTimeout = null
|
2017-05-24 05:07:14 -04:00
|
|
|
|
|
|
|
setTimeout(
|
2017-05-24 10:05:53 -04:00
|
|
|
() =>
|
2017-05-24 09:28:27 -04:00
|
|
|
# listen for document open
|
2017-05-24 10:05:53 -04:00
|
|
|
@$scope.$on 'document:opened', (e, doc) =>
|
2017-05-24 05:07:14 -04:00
|
|
|
setTimeout(
|
2017-05-24 10:05:53 -04:00
|
|
|
() =>
|
|
|
|
@scheduleLoadLabelsFromOpenDoc()
|
2017-05-25 04:31:36 -04:00
|
|
|
, 0
|
2017-05-24 05:07:14 -04:00
|
|
|
)
|
|
|
|
, 0
|
|
|
|
)
|
|
|
|
|
2017-05-24 06:40:12 -04:00
|
|
|
loadLabelsFromOpenDoc: () ->
|
|
|
|
docId = @ide.editorManager.getCurrentDocId()
|
|
|
|
docText = @ide.editorManager.getCurrentDocValue()
|
2017-05-24 05:07:14 -04:00
|
|
|
labels = []
|
2017-05-25 04:32:10 -04:00
|
|
|
re = /\\label\{([^\}\n\\]{0,80})\}/g
|
|
|
|
while (labelMatch = re.exec(docText)) and labels.length < 1000
|
2017-05-24 06:40:12 -04:00
|
|
|
if labelMatch[1]
|
|
|
|
labels.push(labelMatch[1])
|
2017-05-24 05:07:14 -04:00
|
|
|
@state.documents[docId] = labels
|
2017-05-24 06:40:12 -04:00
|
|
|
|
|
|
|
scheduleLoadLabelsFromOpenDoc: () ->
|
|
|
|
if @loadLabelsTimeout
|
|
|
|
clearTimeout(@loadLabelsTimeout)
|
|
|
|
@loadLabelsTimeout = setTimeout(
|
2017-05-24 10:05:53 -04:00
|
|
|
() =>
|
|
|
|
@loadLabelsFromOpenDoc()
|
2017-05-24 09:28:15 -04:00
|
|
|
, 1000
|
2017-05-24 06:40:12 -04:00
|
|
|
, this
|
|
|
|
)
|
2017-05-24 05:07:14 -04:00
|
|
|
|
|
|
|
getAllLabels: () ->
|
|
|
|
_.flatten(labels for docId, labels of @state.documents)
|