Skip to content

Commit

Permalink
Fix Markdown and OrderedList blocks. Remove old heading block.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbell committed Oct 21, 2013
1 parent e6100b0 commit c3d204a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 127 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
49 changes: 0 additions & 49 deletions src/heading.js

This file was deleted.

84 changes: 45 additions & 39 deletions src/markdown.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
/*
Text Block
Markdown Block
*/

var md_template = '<div class="expanding-textarea"><pre><span></span><br></pre><textarea class="required <%= className %>"></textarea></div>';

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)
}
});
SirTrevor.Blocks.Markdown = (function(){

var md_template = _.template([
'<div class="expanding-textarea">',
'<pre><span></span><br></pre>',
'<textarea class="st-markdown st-required"></textarea>',
'</div>'
].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);
}

});

})();
87 changes: 50 additions & 37 deletions src/ordered-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,55 @@
Ordered List
*/

var od_template = '<div class="text-block <%= className %>" contenteditable="true"></div>';

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 = '<div class="st-text-block" contenteditable="true"><ol><li></li></ol></div>';

return SirTrevor.Block.extend({

type: "OrderedList",

icon_name: 'list',

editorHTML: function() {
return _.template(template, this);
},

loadData: function(data){
this.getTextBlock().html("<ol>" + SirTrevor.toHTML(data.text, this.type) + "<ol>");
},

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,"<li>$1</li>")
.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("<ol>" + this.instance._toHTML(data.text, this.type) + "</ol>");
},

toMarkdown: function(markdown){
return markdown.replace(/<\/li>/mg,"\n")
.replace(/<\/?[^>]+(>|$)/g, "")
.replace(/^(.+)$/mg," 1. $1");
},

toHTML: function(html) {
return html.replace(/^ 1. (.+)$/mg,"<li>$1</li>");
}
});

});

})();

0 comments on commit c3d204a

Please sign in to comment.