2017-05-24 05:07:14 -04:00
|
|
|
define [
|
|
|
|
], () ->
|
|
|
|
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 05:07:14 -04:00
|
|
|
documents: {}
|
|
|
|
|
2017-05-24 06:40:12 -04:00
|
|
|
@loadLabelsTimeout = null
|
2017-05-24 05:07:14 -04:00
|
|
|
|
|
|
|
setTimeout(
|
|
|
|
(self) ->
|
|
|
|
self.$scope.$on 'document:opened', (e, doc) ->
|
2017-05-24 06:40:12 -04:00
|
|
|
# console.log ">> [LabelsManager] document opened"
|
2017-05-24 05:07:14 -04:00
|
|
|
setTimeout(
|
2017-05-24 06:40:12 -04:00
|
|
|
(self) ->
|
|
|
|
self.loadLabelsFromOpenDoc()
|
2017-05-24 05:07:14 -04:00
|
|
|
, 1000
|
|
|
|
, self
|
|
|
|
)
|
|
|
|
, 0
|
|
|
|
this
|
|
|
|
)
|
|
|
|
|
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 = []
|
|
|
|
re = /\\label{(.*)}/g
|
|
|
|
while labelMatch = re.exec(docText)
|
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(
|
|
|
|
(self) ->
|
|
|
|
self.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)
|