From c3d204ac766725156591a578de606498d754cedf Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Mon, 21 Oct 2013 18:09:58 -0400 Subject: [PATCH] Fix Markdown and OrderedList blocks. Remove old heading block. --- README.md | 3 +- src/heading.js | 49 ------------------------- src/markdown.js | 84 +++++++++++++++++++++++-------------------- src/ordered-list.js | 87 ++++++++++++++++++++++++++------------------- 4 files changed, 96 insertions(+), 127 deletions(-) delete mode 100644 src/heading.js diff --git a/README.md b/README.md index dabcd06..d093913 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ # Sir Trevor Blocks -A place for Sir Trevor Blocks that don't come packaged as the default set. +A place for Sir Trevor Blocks that don't come packaged as the default set. # Block Types 1. Gist - written by [Chris Bell](http://github.com/cjbell88) 2. Markdown - written by [Chris Bell](http://github.com/cjbell88) 3. Ordered List - written by [Chris Bell](http://github.com/cjbell88) -4. Header - written by [Andrew Sprinz](http://github.com/andrewsprinz) # Licence diff --git a/src/heading.js b/src/heading.js deleted file mode 100644 index d38d9de..0000000 --- a/src/heading.js +++ /dev/null @@ -1,49 +0,0 @@ -/* - A generic heading block level element -*/ - -SirTrevor.Blocks.Heading = SirTrevor.Block.extend({ - - title: "Heading", - className: "heading", - limit: 0, - toolbarEnabled: true, - dropEnabled: false, - formattingEnabled: false, - - editorHTML: function() { - return _.template('

', this); - }, - - loadData: function(data){ - this.$$('.heading').html(data.text); - }, - - onBlockRender: function(){ - /* Make our expanding text area */ - - var cont = this.$$('.expanding-textarea'), - area = cont.find('textarea'), - span = cont.find('span'); - - area.bind('input', function(){ - span.text(area.val()); - }); - - cont.addClass('active'); - - area.focus(); - - }, - - toData: function() { - var bl = this.$el, - dataObj = {} - - dataObj.text = this.$$('.heading').val(); - - this.setData(dataObj) - - } - -}); \ No newline at end of file diff --git a/src/markdown.js b/src/markdown.js index ca6272c..4a75df4 100644 --- a/src/markdown.js +++ b/src/markdown.js @@ -1,42 +1,48 @@ /* - Text Block + Markdown Block */ -var md_template = '

'; - -SirTrevor.Blocks.Markdown = SirTrevor.Block.extend({ - - title: "Markdown", - className: "markdown", - - editorHTML: function() { - return _.template(md_template, this); - }, - - loadData: function(data){ - this.$$('.markdown').html(data.text); - }, - - onBlockRender: function(){ - /* Make our expanding text area */ - - var cont = this.$$('.expanding-textarea'), - area = cont.find('textarea'), - span = cont.find('span'); - - area.bind('input', function(){ - span.text(area.val()); - }); - - cont.addClass('active'); - }, - - toData: function() { - var bl = this.$el, - dataObj = {} - - dataObj.text = this.$$('.markdown').val(); - - this.setData(dataObj) - } -}); \ No newline at end of file +SirTrevor.Blocks.Markdown = (function(){ + + var md_template = _.template([ + '
', + '

', + '', + '
' + ].join("\n")); + + return SirTrevor.Block.extend({ + + type: "Markdown", + + editorHTML: function() { + return md_template(this); + }, + + loadData: function(data){ + this.$('.st-markdown').html(data.text); + }, + + onBlockRender: function() { + /* Make our expanding text area */ + var cont = this.$('.expanding-textarea'), + area = cont.find('textarea'), + span = cont.find('span'); + + area.bind('input', function(){ + span.text(area.val()); + }); + + cont.addClass('active'); + }, + + toData: function() { + var dataObj = {}; + + dataObj.text = this.$('.st-markdown').val(); + this.setData(dataObj); + } + + }); + +})(); \ No newline at end of file diff --git a/src/ordered-list.js b/src/ordered-list.js index 92bb0c0..49dac2d 100644 --- a/src/ordered-list.js +++ b/src/ordered-list.js @@ -2,42 +2,55 @@ Ordered List */ -var od_template = '
'; - -SirTrevor.Blocks.Ol = SirTrevor.Block.extend({ - - title: "List", - className: "ordered-list", - - editorHTML: function() { - return _.template(od_template, this); - }, - - onBlockRender: function(){ - - this.$$('.text-block').bind('click', function(){ - if($(this).html().length === 0){ - document.execCommand("insertOrderedList",false,false); +SirTrevor.Blocks.OrderedList = (function() { + + var template = '
'; + + return SirTrevor.Block.extend({ + + type: "OrderedList", + + icon_name: 'list', + + editorHTML: function() { + return _.template(template, this); + }, + + loadData: function(data){ + this.getTextBlock().html("
    " + SirTrevor.toHTML(data.text, this.type) + "
      "); + }, + + onBlockRender: function() { + this.checkForList = _.bind(this.checkForList, this); + this.getTextBlock().on('click keyup', this.checkForList); + }, + + checkForList: function() { + if (this.$('ol').length === 0) { + document.execCommand("insertOrderedList", false, false); } - }); - - // Put in a list - if (_.isEmpty(this.data)) { - this.$$('.text-block').focus().click(); + }, + + toMarkdown: function(markdown) { + return markdown.replace(/<\/li>/mg,"\n") + .replace(/<\/?[^>]+(>|$)/g, "") + .replace(/^(.+)$/mg," 1. $1"); + }, + + toHTML: function(html) { + html = html.replace(/^ 1. (.+)$/mg,"
    1. $1
    2. ") + .replace(/\n/mg, ""); + + return html; + }, + + onContentPasted: function(event, target) { + var replace = this.pastedMarkdownToHTML(target[0].innerHTML), + list = this.$('ol').html(replace); + + this.getTextBlock().caretToEnd(); } - }, - - loadData: function(data){ - this.$$('.text-block').html("
        " + this.instance._toHTML(data.text, this.type) + "
      "); - }, - - toMarkdown: function(markdown){ - return markdown.replace(/<\/li>/mg,"\n") - .replace(/<\/?[^>]+(>|$)/g, "") - .replace(/^(.+)$/mg," 1. $1"); - }, - - toHTML: function(html) { - return html.replace(/^ 1. (.+)$/mg,"
    3. $1
    4. "); - } -}); \ No newline at end of file + + }); + +})();