Commit 3e7770aa authored by Jacob Schatz's avatar Jacob Schatz Committed by Phil Hughes

Overwrite undo history

parent 3d5b1c35
...@@ -3,11 +3,14 @@ ...@@ -3,11 +3,14 @@
w.gl.text ?= {} w.gl.text ?= {}
w.gl.text.undoManager ?= {} w.gl.text.undoManager ?= {}
gl.text.randomString = -> Math.random().toString(36).substring(7)
gl.text.replaceRange = (s, start, end, substitute) -> gl.text.replaceRange = (s, start, end, substitute) ->
s.substring(0, start) + substitute + s.substring(end); s.substring(0, start) + substitute + s.substring(end);
gl.text.wrap = (textArea, tag) -> gl.text.wrap = (textArea, tag) ->
$textArea = $(textArea) $textArea = $(textArea)
oldVal = $textArea.val()
$textArea.focus() $textArea.focus()
textArea = $textArea.get(0) textArea = $textArea.get(0)
selObj = window.getSelection() selObj = window.getSelection()
...@@ -18,10 +21,12 @@ ...@@ -18,10 +21,12 @@
textArea.selectionStart, textArea.selectionStart,
textArea.selectionEnd, textArea.selectionEnd,
(tag+selObj.toString()+tag)) (tag+selObj.toString()+tag))
$textArea.data('old-val', text).val(replaceWith); $textArea.data('old-val', text).val(replaceWith)
gl.text.undoManager.addUndo(oldVal, $textArea.val())
gl.text.prepend = (textArea, tag) -> gl.text.prepend = (textArea, tag) ->
$textArea = $(textArea) $textArea = $(textArea)
oldVal = $textArea.val()
$textArea.focus() $textArea.focus()
textArea = $textArea.get(0) textArea = $textArea.get(0)
selObj = window.getSelection() selObj = window.getSelection()
...@@ -36,12 +41,45 @@ ...@@ -36,12 +41,45 @@
("#{lineBreak}#{tag} #{selObj.toString()} \n") ("#{lineBreak}#{tag} #{selObj.toString()} \n")
) )
$textArea.data('old-val', text).val(replaceWith); $textArea.data('old-val', text).val(replaceWith);
# $textArea.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 = () -> 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
gl.text.addListeners = () -> gl.text.addListeners = () ->
console.log('addListeners')
self = @ self = @
$('.js-md').on 'click', -> $('.js-md').on 'click', ->
$this = $(@) $this = $(@)
...@@ -61,12 +99,14 @@ ...@@ -61,12 +99,14 @@
$this.data('md-tag') $this.data('md-tag')
) )
$(window).on 'keydown', (e) -> $(window).on 'keydown', (e) =>
if e.ctrlKey or e.metaKey if e.ctrlKey or e.metaKey
if String.fromCharCode(e.which).toLowerCase() is 'z' and !e.shiftKey if String.fromCharCode(e.which).toLowerCase() is 'z' and !e.shiftKey
e.preventDefault() 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')) else if ((String.fromCharCode(e.which).toLowerCase() is 'z' and e.shiftKey) or (String.fromCharCode(e.which).toLowerCase() is 'y'))
e.preventDefault() e.preventDefault()
self.undoManager.redo()
gl.text.removeListeners = () -> gl.text.removeListeners = () ->
$('js-md.btn-bold').off() $('js-md.btn-bold').off()
......
...@@ -5,3 +5,9 @@ ...@@ -5,3 +5,9 @@
#notes #notes
= render 'projects/notes/notes_with_form' = render 'projects/notes/notes_with_form'
:javascript
$(function(){
gl.text.removeListeners();
gl.text.addListeners();
})
\ No newline at end of file
...@@ -11,4 +11,4 @@ ...@@ -11,4 +11,4 @@
$(function(){ $(function(){
gl.text.removeListeners(); gl.text.removeListeners();
gl.text.addListeners(); gl.text.addListeners();
}) })
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment