My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Editor manager that handles all incoming events and runs Zen Coding actions.
* This manager is also used for setting up editor preferences
* @author Sergey Chikuyonok (serge.che@gmail.com)
* @link http://chikuyonok.ru
*
* @include "actions.js"
* @include "editor.js"
* @include "shortcut.js"
*/
zen_textarea = (function(){ // should be global
var default_options = {
profile: 'xhtml',
syntax: 'html',
use_tab: false,
pretty_break: false
},

mac_char_map = {
'ctrl': '⌃',
'control': '⌃',
'meta': '⌘',
'shift': '⇧',
'alt': '⌥',
'enter': '⏎',
'tab': '⇥',
'left': '←',
'right': '→'
},

pc_char_map = {
'left': '←',
'right': '→'
},

shortcuts = {},
is_mac = /mac\s+os/i.test(navigator.userAgent),

/** Zen Coding parameter name/value regexp for getting options from element */
re_param = /\bzc\-(\w+)\-(\w+)/g;

/** @type {default_options} */
var options = {};

function copyOptions(opt) {
opt = opt || {};
var result = {};
for (var p in default_options) if (default_options.hasOwnProperty(p)) {
result[p] = (p in opt) ? opt[p] : default_options[p];
}

return result;
}

options = copyOptions();

/**
* Makes first letter of string in uppercase
* @param {String} str
*/
function capitalize(str) {
return str.charAt().toUpperCase() + str.substring(1);
}

function humanize(str) {
return capitalize(str.replace(/_(\w)/g, function(s, p){return ' ' + p.toUpperCase()}));
}

function formatShortcut(char_map, glue) {
var result = [];
if (typeof(glue) == 'undefined')
glue = '+';

for (var p in shortcuts) if (shortcuts.hasOwnProperty(p)) {
var keys = p.split('+'),
ar = [],
lp = p.toLowerCase();

if (lp == 'tab' || lp == 'enter')
continue;

for (var i = 0; i < keys.length; i++) {
var key = keys[i].toLowerCase();
ar.push(key in char_map ? char_map[key] : capitalize(key));
}

result.push({
'keystroke': ar.join(glue),
'action_name': humanize(shortcuts[p])
});
}

return result;
}


/**
* Get Zen Coding options from element's class name
* @param {Element} elem
*/
function getOptionsFromElement(elem) {
var param_str = elem.className || '',
m,
result = copyOptions(options);

while ( (m = re_param.exec(param_str)) ) {
var key = m[1].toLowerCase(),
value = m[2].toLowerCase();

if (value == 'true' || value == 'yes' || value == '1')
value = true;
else if (value == 'false' || value == 'no' || value == '0')
value = false;

result[key] = value;
}

return result;
}

/**
* Returns normalized action name
* @param {String} name Action name (like 'Expand Abbreviation')
* @return Normalized name for coding (like 'expand_abbreviation')
*/
function normalizeActionName(name) {
return name
.replace(/(^\s+|\s+$)/g, '') // remove trailing spaces
.replace(/\s+/g, '_')
.toLowerCase();
}

/**
* Runs actions called by user
* @param {String} name Normalized action name
* @param {Event} evt Event object
*/
function runAction(name, evt) {
/** @type {Element} */
var target_elem = evt.target || evt.srcElement,
key_code = evt.keyCode || evt.which;

if (target_elem && target_elem.nodeType == 1 && target_elem.nodeName == 'TEXTAREA') {
zen_editor.setTarget(target_elem);

var options = getOptionsFromElement(target_elem),
syntax = options.syntax,
profile_name = options.profile;

switch (name) {
case 'expand_abbreviation':
if (key_code == 9) {
if (options.use_tab)
expandAbbreviationWithTab(zen_editor, syntax, profile_name);
else
// user pressed Tab key but it's forbidden in
// Zen Coding: bubble up event
return true;

} else {
expandAbbreviation(zen_editor, syntax, profile_name);
}
break;
case 'match_pair_inward':
case 'balance_tag_inward':
matchPair(zen_editor, 'in');
break;
case 'match_pair_outward':
case 'balance_tag_outward':
matchPair(zen_editor, 'out');
break;
case 'wrap_with_abbreviation':
var abbr = prompt('Enter abbreviation', 'div');
if (abbr)
wrapWithAbbreviation(zen_editor, abbr, syntax, profile_name);
break;
case 'next_edit_point':
nextEditPoint(zen_editor);
break;
case 'previous_edit_point':
case 'prev_edit_point':
prevEditPoint(zen_editor);
break;
case 'pretty_break':
case 'format_line_break':
if (key_code == 13) {
if (options.pretty_break)
insertFormattedNewline(zen_editor);
else
// user pressed Enter but it's forbidden in
// Zen Coding: bubble up event
return true;
} else {
insertFormattedNewline(zen_editor);
}
break;
case 'select_line':
selectLine(zen_editor);
}
} else {
// allow event bubbling
return true;
}
}

/**
* Bind shortcut to Zen Coding action
* @param {String} keystroke
* @param {String} action_name
*/
function addShortcut(keystroke, action_name) {
action_name = normalizeActionName(action_name);
shortcuts[keystroke.toLowerCase()] = action_name;
shortcut.add(keystroke, function(evt){
return runAction(action_name, evt);
});
}

// add default shortcuts
addShortcut('Meta+E', 'Expand Abbreviation');
addShortcut('Tab', 'Expand Abbreviation');
addShortcut('Meta+D', 'Balance Tag Outward');
addShortcut('Shift+Meta+D', 'Balance Tag inward');
addShortcut('Shift+Meta+A', 'Wrap with Abbreviation');
addShortcut('Ctrl+Alt+RIGHT', 'Next Edit Point');
addShortcut('Ctrl+Alt+LEFT', 'Previous Edit Point');
addShortcut('Meta+L', 'Select Line');
addShortcut('Enter', 'Format Line Break');


return {
shortcut: addShortcut,

/**
* Removes shortcut binding
* @param {String} keystroke
*/
unbindShortcut: function(keystroke) {
keystroke = keystroke.toLowerCase();
if (keystroke in shortcuts)
delete shortcuts[keystroke];
shortcut.remove(keystroke);
},

/**
* Setup editor. Pass object with values defined in
* <code>default_options</code>
*/
setup: function(opt) {
options = copyOptions(opt);
},

/**
* Returns option value
*/
getOption: function(name) {
return options[name];
},

/**
* Returns array of binded actions and their keystrokes
* @return {Array}
*/
getShortcuts: function() {
return formatShortcut(is_mac ? mac_char_map : pc_char_map, is_mac ? '' : '+');
},

/**
* Show info window about Zen Coding
*/
showInfo: function() {
var message = 'All textareas on this page are powered by Zen Coding project: ' +
'a set of tools for fast HTML coding.\n\n' +
'Available shortcuts:\n';

var sh = this.getShortcuts(),
actions = [];

for (var i = 0; i < sh.length; i++) {
actions.push(sh[i].keystroke + ' — ' + sh[i].action_name)
}

message += actions.join('\n') + '\n\n';
message += 'More info on http://code.google.com/p/zen-coding/';

alert(message);

}
}
})();

Change log

r238 by serge.che on Dec 21, 2009   Diff
Fixed typo in action name
Go to: 
Project members, sign in to write a code review

Older revisions

r236 by serge.che on Dec 21, 2009   Diff
Added info window about Zen Coding for
<textarea>
r235 by serge.che on Dec 21, 2009   Diff
Renamed 'editor' to 'zen_editor' to
solve possible name conflicts
r234 by serge.che on Dec 21, 2009   Diff
Zen Coding for <textarea> works fine
IE6+
All revisions of this file

File info

Size: 7086 bytes, 290 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting