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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
((w) ->
w.gl ?= {}
w.gl.text ?= {}
w.gl.text.undoManager ?= {}
gl.text.randomString = -> Math.random().toString(36).substring(7)
gl.text.replaceRange = (s, start, end, substitute) ->
s.substring(0, start) + substitute + s.substring(end);
gl.text.wrap = (textArea, tag) ->
$textArea = $(textArea)
oldVal = $textArea.val()
$textArea.focus()
textArea = $textArea.get(0)
selObj = window.getSelection()
selRange = selObj.getRangeAt(0)
text = $textArea.val()
replaceWith = @replaceRange(
text,
textArea.selectionStart,
textArea.selectionEnd,
(tag+selObj.toString()+tag))
$textArea.data('old-val', text).val(replaceWith)
gl.text.undoManager.addUndo(oldVal, $textArea.val())
gl.text.prepend = (textArea, tag) ->
$textArea = $(textArea)
oldVal = $textArea.val()
$textArea.focus()
textArea = $textArea.get(0)
selObj = window.getSelection()
selRange = selObj.getRangeAt(0)
text = $textArea.val()
if textArea.selectionStart > 0
lineBreak = '\n'
else
lineBreak = ''
replaceWith = @replaceRange(
text,
textArea.selectionStart,
textArea.selectionEnd,
("#{lineBreak}#{tag} #{selObj.toString()} \n")
)
$textArea.data('old-val', text).val(replaceWith);
gl.text.undoManager.addUndo(oldVal, $textArea.val())
gl.text.undoManager.history = {}
gl.text.undoManager.undoHistory = {}
gl.text.undoManager.addUniqueIfNotExists = ($ta) ->
unique = $ta.attr('data-unique')
if not unique?
unique = gl.text.randomString()
$ta.attr('data-unique', unique)
gl.text.undoManager.history[unique] = []
gl.text.undoManager.undoHistory[unique] = []
unique
gl.text.undoManager.addUndo = (oldVal, newVal) ->
$thisTextarea = $('textarea:focus')
unique = gl.text.undoManager.addUniqueIfNotExists($thisTextarea)
gl.text.undoManager.history[unique].push({
oldVal: oldVal,
newVal: newVal
})
gl.text.undoManager.undo = () ->
$thisTextarea = $('textarea:focus')
unique = gl.text.undoManager.addUniqueIfNotExists($thisTextarea)
if not gl.text.undoManager.history[unique].length
return
latestChange = gl.text.undoManager.history[unique].pop()
gl.text.undoManager.undoHistory[unique].push(latestChange)
$thisTextarea.val(latestChange.oldVal)
gl.text.undoManager.redo = () ->
$thisTextarea = $('textarea:focus')
unique = gl.text.undoManager.addUniqueIfNotExists($thisTextarea)
if not gl.text.undoManager.undoHistory[unique].length
return
latestUndo = gl.text.undoManager.undoHistory[unique].pop()
gl.text.undoManager.history[unique].push(latestUndo)
$thisTextarea.val(latestUndo.newVal)
gl.text.addListeners = () ->
self = @
$('.js-md').on 'click', ->
$this = $(@)
if $this.data('md-wrap')?
self.wrap(
$this.closest('.md-area').find('textarea'),
$this.data('md-tag')
)
else if $this.data('md-prepend')?
self.prepend(
$this.closest('.md-area').find('textarea'),
$this.data('md-tag')
)
else
self.wrap(
$this.closest('.md-area').find('textarea'),
$this.data('md-tag')
)
gl.text._previousState = null
$(window).on 'keydown', (e) =>
$thisTextarea = $('textarea:focus')
if e.ctrlKey or e.metaKey
if String.fromCharCode(e.which).toLowerCase() is 'z' and !e.shiftKey
e.preventDefault()
self.undoManager.undo()
else if ((String.fromCharCode(e.which).toLowerCase() is 'z' and e.shiftKey) or (String.fromCharCode(e.which).toLowerCase() is 'y'))
e.preventDefault()
self.undoManager.redo()
else if e.which is 13 or e.which is 8 # enter key or backspace key has been pressed
if gl.text._previousState?
gl.text.undoManager.addUndo(
gl.text._previousState,
$thisTextarea.val()
)
gl.text._previousState = $thisTextarea.val()
gl.text.removeListeners = () ->
$('.js-md').off()
) window
|