diff options
author | Jacob Schatz <jschatz1@gmail.com> | 2016-05-26 17:47:36 +0300 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2016-06-17 13:52:22 +0300 |
commit | 0fd56975ea57c2c646034ab929f6839c6e7a6a02 (patch) | |
tree | bde91c72685b6c7a3c3a7434141054e78c0d0f3e /app/assets/javascripts/lib | |
parent | faee4763f7a166772bb40945f82da4b25a95e7d5 (diff) |
Initial markdown ez buttons
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r-- | app/assets/javascripts/lib/text_utility.js.coffee | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/text_utility.js.coffee b/app/assets/javascripts/lib/text_utility.js.coffee new file mode 100644 index 00000000000..830b5d6ec49 --- /dev/null +++ b/app/assets/javascripts/lib/text_utility.js.coffee @@ -0,0 +1,74 @@ +((w) -> + w.gl ?= {} + w.gl.text ?= {} + w.gl.text.undoManager ?= {} + + gl.text.replaceRange = (s, start, end, substitute) -> + s.substring(0, start) + substitute + s.substring(end); + + gl.text.wrap = (textArea, tag) -> + $textArea = $(textArea) + $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.prepend = (textArea, tag) -> + $textArea = $(textArea) + $textArea.focus() + textArea = $textArea.get(0) + selObj = window.getSelection() + selRange = selObj.getRangeAt(0) + text = $textArea.val() + lineBreak = '\n' if textArea.selectionStart > 0 + console.log(textArea.selectionStart,lineBreak) + replaceWith = @replaceRange( + text, + textArea.selectionStart, + textArea.selectionEnd, + ("#{lineBreak}#{tag} #{selObj.toString()} \n") + ) + $textArea.data('old-val', text).val(replaceWith); + # $textArea.val(replaceWith) + + gl.text.undoManager.undo = () -> + + + 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') + ) + + $(window).on 'keydown', (e) -> + if e.ctrlKey or e.metaKey + if String.fromCharCode(e.which).toLowerCase() is 'z' and !e.shiftKey + e.preventDefault() + else if ((String.fromCharCode(e.which).toLowerCase() is 'z' and e.shiftKey) or (String.fromCharCode(e.which).toLowerCase() is 'y')) + e.preventDefault() + + gl.text.removeListeners = () -> + $('js-md.btn-bold').off() + +) window
\ No newline at end of file |