Skip to content

Commit

Permalink
moved documentation into readme file
Browse files Browse the repository at this point in the history
modified etch.js a bit to better be ready to incorporate etch-image when it is ready
fixed weirdness with the 'clear formatting' icon that was bugging me
fixed issue where mouse.editor listener was doubling up
  • Loading branch information
joshontheweb committed Jan 26, 2014
0 parents commit 61946db
Show file tree
Hide file tree
Showing 7 changed files with 605 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.DS_Store
121 changes: 121 additions & 0 deletions README.md
@@ -0,0 +1,121 @@
### About
Etch is a content editor built on Backbone.js and is designed to be easily plugged into your Backbone app or stand alone.

### Include Dependencies

Etch depends on [jQuery](http://jquery.com), [Underscore](http://underscorejs.com), [Backbone](http://backbonejs.com), as well as [Rangy](http://code.google.com/p/rangy/) if you need support for legacy browsers (IE8 and prior).

_You can exclude Rangy and shave 41k off of your footprint if you don't need legacy support._

Once you have the dependencies squared away simply include the etch.js script after them and before the script where you define your Models/Views.

At this point your scripts section should look something like this:

```
<!-- dependencies -->
<script src="/media/scripts/lib/jquery.js"></script>
<script src="/media/scripts/lib/underscore.js"></script>
<script src="/media/scripts/lib/backbone.js"></script>
<!-- etch -->
<script src="/media/scripts/etch/scripts/etch.js"></script>
<!-- Your code -->
<script src="/media/scripts/article.js"></script>
```

_Ensure that your scripts are in the right order or everything will likely be broken._

Also you need to add etch.css to your stylesheets
```
<link rel="stylesheet" href="/media/scripts/etch/styles/etch.css" />
```

### Building The Model

This part is simple, we just define a model and give it a url and some defaults. Your model will probably end up being more complex but this is all it takes to get Etch working.

```
var article = Backbone.Model.extend({
url: '/some/api/url/',
defaults: {
title: 'Default Title',
body: 'Default body text'
}
});
```

### Building The View

Basically all we need to do is call `etch.editableInit` when a user clicks (mousedown) on an editable element. Because of how backbone delegates events we need to call an intermediate function, `editableClick`, which references `etch.editableInit`.

```
var articleView = Backbone.View.extend({
events: {
'mousedown .editable': 'editableClick'
},
editableClick: etch.editableInit
});
```

etch.editableInit handles everything else for you except for saving. Etch will trigger a 'save' event on your model when the save button is clicked. All we need to do is listen for it by adding a binding to the view like so:

```
var articleView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'save');
this.model.bind('save', this.model.save);
},
events: {
'mousedown .editable': 'editableClick'
},
editableClick: etch.editableInit
});
```

### Customizing

You may have noticed that the demo had different buttons in the editor widget depending on if you were editing the body or the title. Etch allows you to customize which buttons to show on a given 'editable' by adding a `data-button-class` attribute to the element.

The default classes are:

```
etch.config.buttonClasses = {
'default': ['save'],
'all': ['bold', 'italic', 'underline', 'unordered-list', 'ordered-list', 'link', 'clear-formatting', 'save'],
'title': ['bold', 'italic', 'underline', 'save']
};
```

_The 'default' button class will be used if no button class is defined on the element._

Defining your own button classes can be accomplished by extending `etch.config.buttonClasses`. Here we override 'default' to add more buttons and add a 'caption' class.
```
_.extend(etch.config.buttonClasses, {
'default': ['bold', 'italic', 'underline', 'save'],
'caption': ['bold', 'italic', 'underline', 'link', 'save']
});
```

_The order of buttons in the array is how they will be presented in the editor widget._

If the class '.editable' causes conflicts for you or you need to change it for any reason you can do so by setting `etch.config.selector`.

```
etch.config.selector = '.my-new-editable-class';
```

All functions are public and can be overridden to customize functionality

For instance, if you want to create a custom popup for the link url prompt:

```
etch.views.Editor.prototype.urlPrompt = function(callback) {
// Custom popup code to get url
callback(url)
}
```
64 changes: 64 additions & 0 deletions demo/demo.html
@@ -0,0 +1,64 @@
<!doctype html>
<html>
<head>
<title>Etch.js - Minimal demo</title>
<link rel="stylesheet" href="../styles/etch.css" />
<style>
.editable { background: lightblue; }
</style>
</head>
<body>
<h2>Very very basic demo, so developers can play with it while they're developing features.</h2>
<p>Just click on text below to see Etch in action.</p>
<div class="section demo">
<div class="article">
<h1 class="title editable">Here is a title</h1>
<div class="body editable" data-button-class="all">
Text with a light blue background is editable. You will find it easy to use etch to <u>underline</u> text as well as <b>bold</b> and <i>italic</i>.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<script src="../scripts/etch.js"></script>
<script>
</script>
<script>
(function() {
var article = Backbone.Model.extend({
defaults: {
title: 'Default Title',
body: 'Default body text'
}
});

var articleView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'save')
this.model.bind('save', this.save);
},

events: {
'mousedown .editable': 'editableClick'
},

editableClick: etch.editableInit,

save: function() {

// normally you would call model.save() here but this is a demo
alert('Saved! Not really, this is just a demo.');
}

});

$article = $('.article');
var model = new article();
var view = new articleView({model: model, el: $article[0], tagName: $article[0].tagName});
})()
</script>
</body>
</html>
13 changes: 13 additions & 0 deletions license.txt
@@ -0,0 +1,13 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit 61946db

Please sign in to comment.