├── .gitignore ├── example.html ├── README.md └── behave.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /example.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |
2 |
3 | ## What?
4 | Behave.js is a lightweight library for adding IDE style behaviors to plain text areas, making it much more enjoyable to write code in.
5 | * Supports Partial IE6, IE7+, Firefox 8+, Safari 4+, Chrome X+, Opera 12
6 | * No Dependencies
7 | * Custom Code/Behavior Fencing
8 | * Hard and Soft Tabs
9 | * Auto Open/Close Paranthesis, Brackets, Braces, Double and Single Quotes
10 | * Auto delete a paired character
11 | * Overwrite a paired character
12 | * Multi-line Indentation/Unindentation
13 | * Automatic Indentation
14 |
15 | ## Usage
16 | ```javascript
17 | var editor = new Behave({
18 | textarea: document.getElementById('myTextarea')
19 | });
20 | ```
21 |
22 | ## Defaults and Options
23 | ```javascript
24 | var editor = new Behave({
25 | textarea: null,
26 | replaceTab: true,
27 | softTabs: true,
28 | tabSize: 4,
29 | autoOpen: true,
30 | overwrite: true,
31 | autoStrip: true,
32 | autoIndent: true,
33 | fence: false
34 | });
35 | ```
36 | softTabs is set to true, the number of spaces used is defined here. If set to false, the CSS property tab-size will be used to define hard tab sizes.
54 | autoOpen set to true
67 | autoOpen set to true
73 | Copyright (c) 2013 Jacob Kelley
92 |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:
93 |The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
94 |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.
95 | 96 | -------------------------------------------------------------------------------- /behave.js: -------------------------------------------------------------------------------- 1 | (function(undefined){ 2 | 3 | 'use strict'; 4 | 5 | var Behave = Behave || function (userOpts) { 6 | 7 | if (typeof String.prototype.repeat !== 'function') { 8 | String.prototype.repeat = function(times) { 9 | if(times < 1){ 10 | return ''; 11 | } 12 | if(times % 2){ 13 | return this.repeat(times - 1) + this; 14 | } 15 | var half = this.repeat(times / 2); 16 | return half + half; 17 | }; 18 | } 19 | 20 | if (typeof Array.prototype.filter !== 'function') { 21 | Array.prototype.filter = function(func /*, thisp */) { 22 | if (this === null) { 23 | throw new TypeError(); 24 | } 25 | 26 | var t = Object(this), 27 | len = t.length >>> 0; 28 | if (typeof func != "function"){ 29 | throw new TypeError(); 30 | } 31 | var res = [], 32 | thisp = arguments[1]; 33 | for (var i = 0; i < len; i++) { 34 | if (i in t) { 35 | var val = t[i]; 36 | if (func.call(thisp, val, i, t)) { 37 | res.push(val); 38 | } 39 | } 40 | } 41 | return res; 42 | }; 43 | } 44 | 45 | var defaults = { 46 | textarea: null, 47 | replaceTab: true, 48 | softTabs: true, 49 | tabSize: 4, 50 | autoOpen: true, 51 | overwrite: true, 52 | autoStrip: true, 53 | autoIndent: true, 54 | fence: false 55 | }, 56 | tab, 57 | newLine, 58 | charSettings = { 59 | 60 | keyMap: [ 61 | { open: "\"", close: "\"", canBreak: false }, 62 | { open: "'", close: "'", canBreak: false }, 63 | { open: "(", close: ")", canBreak: false }, 64 | { open: "[", close: "]", canBreak: true }, 65 | { open: "{", close: "}", canBreak: true } 66 | ] 67 | 68 | }, 69 | utils = { 70 | defineNewLine: function(){ 71 | var ta = document.createElement('textarea'); 72 | ta.value = "\n"; 73 | 74 | if(ta.value.length==2){ 75 | newLine = "\r\n"; 76 | } else { 77 | newLine = "\n"; 78 | } 79 | }, 80 | defineTabSize: function(tabSize){ 81 | if(typeof defaults.textarea.style.OTabSize != "undefined"){ 82 | defaults.textarea.style.OTabSize = tabSize; return; 83 | } 84 | if(typeof defaults.textarea.style.MozTabSize != "undefined"){ 85 | defaults.textarea.style.MozTabSize = tabSize; return; 86 | } 87 | if(typeof defaults.textarea.style.tabSize != "undefined"){ 88 | defaults.textarea.style.tabSize = tabSize; return; 89 | } 90 | }, 91 | cursor: { 92 | get: function() { 93 | 94 | if (typeof document.createElement('textarea').selectionStart==="number") { 95 | return defaults.textarea.selectionStart; 96 | } else if (document.selection) { 97 | var caretPos = 0, 98 | range = defaults.textarea.createTextRange(), 99 | rangeDupe = document.selection.createRange().duplicate(), 100 | rangeDupeBookmark = rangeDupe.getBookmark(); 101 | range.moveToBookmark(rangeDupeBookmark); 102 | 103 | while (range.moveStart('character' , -1) !== 0) { 104 | caretPos++; 105 | } 106 | return caretPos; 107 | } 108 | }, 109 | set: function (start, end) { 110 | if(!end){ 111 | end = start; 112 | } 113 | if (defaults.textarea.setSelectionRange) { 114 | defaults.textarea.focus(); 115 | defaults.textarea.setSelectionRange(start, end); 116 | } else if (defaults.textarea.createTextRange) { 117 | var range = defaults.textarea.createTextRange(); 118 | range.collapse(true); 119 | range.moveEnd('character', end); 120 | range.moveStart('character', start); 121 | range.select(); 122 | } 123 | }, 124 | selection: function(){ 125 | var textAreaElement = defaults.textarea, 126 | start = 0, 127 | end = 0, 128 | normalizedValue, 129 | range, 130 | textInputRange, 131 | len, 132 | endRange; 133 | 134 | if (typeof textAreaElement.selectionStart == "number" && typeof textAreaElement.selectionEnd == "number") { 135 | start = textAreaElement.selectionStart; 136 | end = textAreaElement.selectionEnd; 137 | } else { 138 | range = document.selection.createRange(); 139 | 140 | if (range && range.parentElement() == textAreaElement) { 141 | 142 | normalizedValue = utils.editor.get(); 143 | len = normalizedValue.length; 144 | 145 | textInputRange = textAreaElement.createTextRange(); 146 | textInputRange.moveToBookmark(range.getBookmark()); 147 | 148 | endRange = textAreaElement.createTextRange(); 149 | endRange.collapse(false); 150 | 151 | if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) { 152 | start = end = len; 153 | } else { 154 | start = -textInputRange.moveStart("character", -len); 155 | start += normalizedValue.slice(0, start).split(newLine).length - 1; 156 | 157 | if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) { 158 | end = len; 159 | } else { 160 | end = -textInputRange.moveEnd("character", -len); 161 | end += normalizedValue.slice(0, end).split(newLine).length - 1; 162 | } 163 | } 164 | } 165 | } 166 | 167 | return start==end ? false : { 168 | start: start, 169 | end: end 170 | }; 171 | } 172 | }, 173 | editor: { 174 | get: function(){ 175 | return defaults.textarea.value.replace(/\r/g,''); 176 | }, 177 | set: function(data){ 178 | defaults.textarea.value = data; 179 | } 180 | }, 181 | fenceRange: function(){ 182 | if(typeof defaults.fence == "string"){ 183 | 184 | var data = utils.editor.get(), 185 | pos = utils.cursor.get(), 186 | hacked = 0, 187 | matchedFence = data.indexOf(defaults.fence), 188 | matchCase = 0; 189 | 190 | while(matchedFence>=0){ 191 | matchCase++; 192 | if( pos < (matchedFence+hacked) ){ 193 | break; 194 | } 195 | 196 | hacked += matchedFence+defaults.fence.length; 197 | data = data.substring(matchedFence+defaults.fence.length); 198 | matchedFence = data.indexOf(defaults.fence); 199 | 200 | } 201 | 202 | if( (hacked) < pos && ( (matchedFence+hacked) > pos ) && matchCase%2===0){ 203 | return true; 204 | } 205 | return false; 206 | } else { 207 | return true; 208 | } 209 | }, 210 | isEven: function(_this,i){ 211 | return i%2; 212 | }, 213 | levelsDeep: function(){ 214 | var pos = utils.cursor.get(), 215 | val = utils.editor.get(); 216 | 217 | var left = val.substring(0, pos), 218 | levels = 0, 219 | i, j; 220 | 221 | for(i=0; i