Skip to content

Commit 54e7361

Browse files
committed
Merge branch 'master' of github.com:sofish/pen
2 parents 73427d8 + 1023310 commit 54e7361

File tree

4 files changed

+56
-35
lines changed

4 files changed

+56
-35
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ <h2>Enjoy live editing (+markdown)</h2>
3939
var options = {
4040
editor: document.querySelector('[data-toggle="pen"]'),
4141
debug: true
42-
}
42+
};
4343

4444
var pen = new Pen(options);
4545
</script>

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ You can set `options.list` to an `Array`, add the following strings to make your
9898

9999
By default, Pen will prevent unsafe page redirect when editing, to shut down it, specific `options.stay` to `false`.
100100

101+
__NOTE:__ if `defautls.debug` is set to `true` and `default.stay` is not set: `defaults.stay == !defaults.debug`.
102+
101103
## 3. markdown syntax support
102104

103105
#### 3.1 install

src/markdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
// parse command
3333
covertor.parse = function(e) {
34-
var code = e.which;
34+
var code = e.keyCode || e.which;
3535

3636
// when `space` is pressed
3737
if(code === 32) {

src/pen.js

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,26 @@
2525
if(window._pen_debug_mode_on || force) console.log('%cPEN DEBUGGER: %c' + message, 'font-family:arial,sans-serif;color:#1abf89;line-height:2em;', 'font-family:cursor,monospace;color:#333;');
2626
};
2727

28+
// shift a function
29+
utils.shift = function(key, fn, time) {
30+
time = time || 50;
31+
var queue = this['_shift_fn' + key], timeout = 'shift_timeout' + key, current;
32+
queue ? queue.concat([fn, time]) : (queue = [[fn, time]]);
33+
current = queue.pop();
34+
clearTimeout(this[timeout]);
35+
this[timeout] = setTimeout(function() {
36+
current[0]();
37+
}, time);
38+
};
39+
2840
// merge: make it easy to have a fallback
2941
utils.merge = function(config) {
3042

3143
// default settings
3244
var defaults = {
3345
class: 'pen',
3446
debug: false,
35-
stay: true,
47+
stay: config.stay || !config.debug,
3648
textarea: '<textarea name="content"></textarea>',
3749
list: [
3850
'blockquote', 'h2', 'h3', 'p', 'insertorderedlist', 'insertunorderedlist', 'inserthorizontalrule',
@@ -65,9 +77,7 @@
6577
var editor = defaults.editor;
6678

6779
// set default class
68-
var klass = editor.getAttribute('class');
69-
klass = /\bpen\b/.test(klass) ? klass : (klass ? (klass + ' ' + defaults.class) : defaults.class);
70-
editor.setAttribute('class', klass);
80+
editor.classList.add(defaults.class);
7181

7282
// set contenteditable
7383
var editable = editor.getAttribute('contenteditable');
@@ -115,44 +125,51 @@
115125

116126
Pen.prototype.toolbar = function() {
117127

118-
var menu, that = this, icons = '', setpos;
128+
var that = this, icons = '';
119129

120130
for(var i = 0, list = this.config.list; i < list.length; i++) {
121131
var name = list[i], klass = 'pen-icon icon-' + name;
122132
icons += '<i class="' + klass + '" data-action="' + name + '">' + (name.match(/^h[1-6]|p$/i) ? name.toUpperCase() : '') + '</i>';
123133
if((name === 'createlink')) icons += '<input class="pen-input" placeholder="http://" />';
124134
}
125135

126-
menu = doc.createElement('div');
136+
var menu = doc.createElement('div');
127137
menu.setAttribute('class', this.config.class + '-menu pen-menu');
128138
menu.innerHTML = icons;
129139
menu.style.display = 'none';
130140

131141
doc.body.appendChild((this._menu = menu));
132142

133-
setpos = function() {
143+
var setpos = function() {
134144
if(menu.style.display === 'block') that.menu();
135-
}
145+
};
136146

137147
// change menu offset when window resize / scroll
138148
window.addEventListener('resize', setpos);
139149
window.addEventListener('scroll', setpos);
140150

141-
// show toolbar on select
142-
this.config.editor.addEventListener('mouseup', function(){
143-
var range = that._sel;
144-
if(!range.isCollapsed) {
145-
that._range = range.getRangeAt(0);
146-
that.menu().highlight();
147-
}
151+
var editor = this.config.editor;
152+
var show = function() {
153+
var range = that._sel;
154+
if(!range.isCollapsed) {
155+
that._range = range.getRangeAt(0);
156+
that.menu().highlight();
157+
}
158+
};
159+
160+
// show toolbar on mouse select
161+
editor.addEventListener('mouseup', show);
162+
163+
// show toolbar on arrow key select
164+
editor.addEventListener('keyup', function(e) {
165+
var code = e.keyCode || e.which;
166+
if(code > 36 && code < 41) utils.shift('select_text', show, 200);
148167
});
149168

150169
// when to hide
151-
this.config.editor.addEventListener('click', function() {
170+
editor.addEventListener('click', function() {
152171
setTimeout(function() {
153-
that._sel.isCollapsed ?
154-
(that._menu.style.display = 'none') :
155-
(that._menu.getElementsByTagName('input')[0].style.display = 'none');
172+
that._sel.isCollapsed && (that._menu.style.display = 'none');
156173
}, 0);
157174
});
158175

@@ -207,6 +224,9 @@
207224
el.classList.remove('active');
208225
});
209226

227+
// display link input
228+
menu.querySelector('input').style.display = 'none';
229+
210230
highlight = function(str) {
211231
var selector = '.icon-' + str
212232
, el = menu.querySelector(selector);
@@ -215,18 +235,17 @@
215235

216236
effects.forEach(function(item) {
217237
var tag = item.nodeName.toLowerCase();
218-
if(tag === 'a') {
219-
menu.querySelector('input').value = item.href;
220-
return highlight('createlink');
238+
switch(tag) {
239+
case 'a': return (menu.querySelector('input').value = item.href), highlight('createlink');
240+
case 'i': return highlight('italic');
241+
case 'u': return highlight('underline');
242+
case 'b': return highlight('bold');
243+
case 'ul': return highlight('insertunorderedlist');
244+
case 'ol': return highlight('insertorderedlist');
245+
case 'ol': return highlight('insertorderedlist');
246+
case 'li': return highlight('indent');
247+
default : highlight(tag);
221248
}
222-
if(tag === 'i') return highlight('italic');
223-
if(tag === 'u') return highlight('underline');
224-
if(tag === 'b') return highlight('bold');
225-
if(tag === 'ul') return highlight('insertunorderedlist');
226-
if(tag === 'ol') return highlight('insertorderedlist');
227-
if(tag === 'ol') return highlight('insertorderedlist');
228-
if(tag === 'li') return highlight('indent');
229-
return highlight(tag);
230249
});
231250

232251
return this;
@@ -267,10 +286,10 @@
267286
} else {
268287
if(this.config.debug) utils.log('can not find command function for name: ' + name + (value ? (', value: ' + value) : ''));
269288
}
270-
}
289+
};
271290

272291
return this;
273-
}
292+
};
274293

275294
// show menu
276295
Pen.prototype.menu = function() {
@@ -291,7 +310,7 @@
291310
Pen.prototype.stay = function() {
292311
!window.onbeforeunload && (window.onbeforeunload = function() {
293312
return 'Are you going to leave here?';
294-
})
313+
});
295314
};
296315

297316
// a fallback for old browers

0 commit comments

Comments
 (0)