bgneal@1094: function editorReplaceText(textArea, i, j, newText) { bgneal@1094: textArea.value = textArea.value.substring(0, i) + newText + bgneal@1094: textArea.value.substring(j); bgneal@1094: } bgneal@1094: bgneal@1094: function editorWrapSelection(textArea, wrapText) { bgneal@1094: if (wrapText.length == 0) return; bgneal@1094: var i = textArea.selectionStart; bgneal@1094: var j = textArea.selectionEnd; bgneal@1094: if (i == j) return; bgneal@1094: var selection = textArea.value.substring(i, j); bgneal@1094: var newText = wrapText + selection + wrapText; bgneal@1094: editorReplaceText(textArea, i, j, newText); bgneal@1094: textArea.focus(); bgneal@1094: textArea.setSelectionRange(i, j + 2 * wrapText.length); bgneal@1094: } bgneal@1094: bgneal@1094: function editorPrependLines(textArea, s) { bgneal@1094: if (s.length == 0) return; bgneal@1094: var i = textArea.selectionStart; bgneal@1094: var j = textArea.selectionEnd; bgneal@1094: var newText = s; bgneal@1094: var selection = textArea.value.substring(i, j); bgneal@1094: newText += selection.replace(/\n/gm, '\n' + s); bgneal@1094: editorReplaceText(textArea, i, j, newText); bgneal@1094: } bgneal@1094: bgneal@1094: function editorLink(textArea) { bgneal@1094: var i = textArea.selectionStart; bgneal@1094: var j = textArea.selectionEnd; bgneal@1094: var url = window.prompt("Please enter a URL:", "http://"); bgneal@1094: if (!url) return; bgneal@1094: var link; bgneal@1094: if (i == j) { bgneal@1094: link = '[Link](' + url + ')'; bgneal@1094: } else { bgneal@1094: var selection = textArea.value.substring(i, j); bgneal@1094: link = '[' + selection + '](' + url + ')'; bgneal@1094: } bgneal@1094: editorReplaceText(textArea, i, j, link); bgneal@1094: textArea.focus(); bgneal@1094: } bgneal@1094: bgneal@312: $(document).ready(function() { bgneal@312: $('#id_start_date').datepicker({constrainInput: true, bgneal@312: dateFormat: 'mm/dd/yy', bgneal@312: onClose: function () { bgneal@312: var end = $('#id_end_date'); bgneal@312: if (this.value > end.val()) bgneal@312: { bgneal@312: end.val(this.value); bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@312: $('#id_end_date').datepicker({constrainInput: true, bgneal@312: dateFormat: 'mm/dd/yy', bgneal@312: onClose: function () { bgneal@312: var start = $('#id_start_date'); bgneal@312: if (this.value < start.val()) bgneal@312: { bgneal@312: start.val(this.value); bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@1094: if ($('#id_all_day:checked').length) { bgneal@1094: $('.all-day-hide').hide(); bgneal@312: } bgneal@312: $('#id_all_day').click(function () { bgneal@1094: $('.all-day-hide').toggle(); bgneal@1094: }); bgneal@1094: bgneal@1094: var editorPanel = $('#editor-panel textarea'); bgneal@1094: var previewPanel = $('#preview-panel'); bgneal@1094: previewPanel.css('height', editorPanel.css('height')); bgneal@1094: var observer = new MutationObserver(function(mutations) { bgneal@1094: if (previewPanel.is(':visible')) { bgneal@1094: alert(editorPanel[0].value); bgneal@1094: $.ajax({ bgneal@1094: url: '/comments/markdown/v3/', bgneal@1094: method: 'POST', bgneal@1094: data: {'data': editorPanel[0].value}, bgneal@1094: dataType: 'html', bgneal@1094: success: function(data, textStatus) { bgneal@1094: alert(data); bgneal@1094: previewPanel.html(data); bgneal@1094: }, bgneal@1094: error: function (xhr, textStatus, ex) { bgneal@1094: alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + bgneal@1094: xhr.responseText); bgneal@1094: } bgneal@1094: }); bgneal@1094: } bgneal@1094: }); bgneal@1094: var target = document.getElementById('preview-panel'); bgneal@1094: observer.observe(target, {attributes: true}); bgneal@1094: bgneal@1094: // Editor stuff bgneal@1094: $('.v3-editor').each(function (index) { bgneal@1094: var $this = $(this); bgneal@1094: var textArea = $this.find('textarea')[0]; bgneal@1094: $this.find('.editor-bold').click(function () { bgneal@1094: editorWrapSelection(textArea, '**'); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-italic').click(function () { bgneal@1094: editorWrapSelection(textArea, '_'); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-strike').click(function () { bgneal@1094: editorWrapSelection(textArea, '---'); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-link').click(function () { bgneal@1094: editorLink(textArea); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-quote').click(function () { bgneal@1094: editorPrependLines(textArea, '> '); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-code').click(function () { bgneal@1094: editorPrependLines(textArea, ' '); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-bullet').click(function () { bgneal@1094: editorPrependLines(textArea, '* '); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-number').click(function () { bgneal@1094: editorPrependLines(textArea, '1. '); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: }); bgneal@312: });