This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.coffee
113 lines (96 loc) · 2.64 KB
/
init.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
###
Copyright (c) 2014, dev-rke
###
class codiad.ToDo
###
basic plugin environment initialization
###
constructor: (global, jQuery) ->
@codiad = global.codiad
@amplify = global.amplify
@jQuery = jQuery
@scripts = document.getElementsByTagName('script')
@path = @scripts[@scripts.length - 1].src.split('?')[0]
@curpath = @path.split('/').slice(0, -1).join('/') + '/'
# wait until dom is loaded
@jQuery =>
@init()
###
main plugin initialization
###
init: =>
@initToDoListContainer()
###
init button and menu on bottom menu
and append handler
###
initToDoListContainer: =>
todoButton = '''
<div class="divider"></div>
<a id="todoButton">
<span class="icon-check"></span>TODO
</a>
'''
todoMenu = '<ul id="todoMenu" class="options-menu"></ul>'
@jQuery('#editor-bottom-bar').append(todoButton)
@$todoButton = @jQuery('#todoButton')
@$todoMenu = @jQuery(todoMenu)
@codiad.editor.initMenuHandler @$todoButton, @$todoMenu
@$todoMenu.on 'click', 'li a', (element) =>
line = @jQuery(element.currentTarget).data 'line'
if line
@codiad.active.gotoLine line
@codiad.editor.focus()
@amplify.subscribe 'active.onFocus', =>
@updateToDo()
@updateInterval = null
@amplify.subscribe 'active.onOpen', =>
@updateToDo()
activeInstance = @codiad.editor.getActive()
return if not activeInstance
activeInstance.getSession().on 'change', (e) =>
clearTimeout @updateInterval
@updateInterval = setTimeout @updateToDo, 1000
@amplify.subscribe 'active.onClose', =>
@disableToDo()
###
update todo button and menu
###
updateToDo: =>
editorInstance = @codiad.editor.getActive()
return if not editorInstance
content = @codiad.editor.getContent()
loc = content.split(/\r?\n/)
matches = []
editorToDo = []
for line, index in loc
if line.indexOf("TODO") > -1 and line.match(/TODO\s*:(.*)/)
matches.push (
'<li><a data-line="' + (index + 1) + '">' +
line.match(/TODO\s*:(.*)/)[1] +
'</a></li>'
)
editorToDo.push(
row: index
column: 1
text: line.match(/(TODO\s*:.*)/)[1]
type: "info"
)
if matches.length > 0
@$todoMenu.empty().append $(matches.join "")
session = editorInstance.getSession()
session.setAnnotations(editorToDo.concat session.getAnnotations())
@$todoButton.find('span').
removeClass('icon-check').
addClass('icon-clipboard')
else
@disableToDo()
###
disable ToDoList
###
disableToDo: =>
@$todoMenu.empty().append "<li><a>Nothing to do</a></li>"
@$todoButton.find('span').
removeClass('icon-clipboard').
addClass('icon-check')
new codiad.ToDo(this, jQuery)