diff options
author | Phil Hughes <me@iamphill.com> | 2016-06-06 08:25:43 +0100 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2016-06-17 11:52:22 +0100 |
commit | d5b331b76bb6838e121e54cdf50122023932576c (patch) | |
tree | 6a611573ac16e9533f227cdc1ca435ff64c99e50 /app/assets/javascripts/lib | |
parent | 14d08d1400376a0bbae18890a54ff2b961062a32 (diff) | |
download | gitlab-ce-d5b331b76bb6838e121e54cdf50122023932576c.tar.gz |
Improved design
Updated JS to remove undo manager - instead let the browser handle it all
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r-- | app/assets/javascripts/lib/text_utility.js.coffee | 148 |
1 files changed, 43 insertions, 105 deletions
diff --git a/app/assets/javascripts/lib/text_utility.js.coffee b/app/assets/javascripts/lib/text_utility.js.coffee index 0a4ebb8381e..52ef001894c 100644 --- a/app/assets/javascripts/lib/text_utility.js.coffee +++ b/app/assets/javascripts/lib/text_utility.js.coffee @@ -1,129 +1,67 @@ ((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.selectedText = (text, textarea) -> + text.substring(textarea.selectionStart, textarea.selectionEnd) - 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' + gl.text.insertText = (textArea, text, tag, selected, wrap) -> + startChar = if not wrap and textArea.selectionStart > 0 then '\n' else '' + insertText = "#{startChar}#{tag}#{selected}#{if wrap then tag else ' '}" + + if document.queryCommandSupported('insertText') + document.execCommand 'insertText', false, insertText else - lineBreak = '' + try + document.execCommand("ms-beginUndoUnit") - 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()) + textArea.value = @replaceRange( + text, + textArea.selectionStart, + textArea.selectionEnd, + insertText) + try + document.execCommand("ms-endUndoUnit") - gl.text.undoManager.history = {} - gl.text.undoManager.undoHistory = {} + @moveCursor(textArea, tag, wrap) - 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.moveCursor = (textArea, tag, wrapped) -> + return unless textArea.setSelectionRange - 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 - }) + if textArea.selectionStart is textArea.selectionEnd + if wrapped + pos = textArea.selectionStart - tag.length + else + pos = textArea.selectionStart + + textArea.setSelectionRange pos, pos - 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.updateText = (textArea, tag, wrap) -> + $textArea = $(textArea) + oldVal = $textArea.val() + textArea = $textArea.get(0) + text = $textArea.val() + selected = @selectedText(text, textArea) + $textArea.focus() - 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) + @insertText(textArea, text, tag, selected, wrap) - gl.text.addListeners = () -> + 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() + self.updateText( + $this.closest('.md-area').find('textarea'), + $this.data('md-tag'), + not $this.data('md-prepend') + ) - gl.text.removeListeners = () -> + gl.text.removeListeners = -> $('.js-md').off() -) window
\ No newline at end of file +) window |