From 3e7770aae17e30c4b6b89d3b1add2fa439ac97ac Mon Sep 17 00:00:00 2001 From: Jacob Schatz Date: Thu, 26 May 2016 22:10:01 -0400 Subject: Overwrite undo history --- app/assets/javascripts/lib/text_utility.js.coffee | 48 ++++++++++++++++++++-- app/views/projects/issues/_discussion.html.haml | 6 +++ .../projects/merge_requests/_discussion.html.haml | 2 +- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/lib/text_utility.js.coffee b/app/assets/javascripts/lib/text_utility.js.coffee index 830b5d6ec49..7a115a6d8b5 100644 --- a/app/assets/javascripts/lib/text_utility.js.coffee +++ b/app/assets/javascripts/lib/text_utility.js.coffee @@ -3,11 +3,14 @@ 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() @@ -18,10 +21,12 @@ textArea.selectionStart, textArea.selectionEnd, (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) -> $textArea = $(textArea) + oldVal = $textArea.val() $textArea.focus() textArea = $textArea.get(0) selObj = window.getSelection() @@ -36,12 +41,45 @@ ("#{lineBreak}#{tag} #{selObj.toString()} \n") ) $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 = () -> - + $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 = () -> + console.log('addListeners') self = @ $('.js-md').on 'click', -> $this = $(@) @@ -61,12 +99,14 @@ $this.data('md-tag') ) - $(window).on 'keydown', (e) -> + $(window).on 'keydown', (e) => 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() gl.text.removeListeners = () -> $('js-md.btn-bold').off() diff --git a/app/views/projects/issues/_discussion.html.haml b/app/views/projects/issues/_discussion.html.haml index b151393abab..8ed0e3d7e1d 100644 --- a/app/views/projects/issues/_discussion.html.haml +++ b/app/views/projects/issues/_discussion.html.haml @@ -5,3 +5,9 @@ #notes = render 'projects/notes/notes_with_form' + +:javascript + $(function(){ + gl.text.removeListeners(); + gl.text.addListeners(); + }) \ No newline at end of file diff --git a/app/views/projects/merge_requests/_discussion.html.haml b/app/views/projects/merge_requests/_discussion.html.haml index 6b9185c4a07..c8b6a25952b 100644 --- a/app/views/projects/merge_requests/_discussion.html.haml +++ b/app/views/projects/merge_requests/_discussion.html.haml @@ -11,4 +11,4 @@ $(function(){ gl.text.removeListeners(); gl.text.addListeners(); - }) + }) \ No newline at end of file -- cgit v1.2.1