Skip to content

Commit 1023310

Browse files
committed
fix #40 : thx @ nedrocks for the pull-request
1 parent fb82bc5 commit 1023310

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

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: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
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

@@ -113,40 +125,49 @@
113125

114126
Pen.prototype.toolbar = function() {
115127

116-
var menu, that = this, icons = '', setpos;
128+
var that = this, icons = '';
117129

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

124-
menu = doc.createElement('div');
136+
var menu = doc.createElement('div');
125137
menu.setAttribute('class', this.config.class + '-menu pen-menu');
126138
menu.innerHTML = icons;
127139
menu.style.display = 'none';
128140

129141
doc.body.appendChild((this._menu = menu));
130142

131-
setpos = function() {
143+
var setpos = function() {
132144
if(menu.style.display === 'block') that.menu();
133145
};
134146

135147
// change menu offset when window resize / scroll
136148
window.addEventListener('resize', setpos);
137149
window.addEventListener('scroll', setpos);
138150

139-
// show toolbar on select
140-
this.config.editor.addEventListener('mouseup', function(){
141-
var range = that._sel;
142-
if(!range.isCollapsed) {
143-
that._range = range.getRangeAt(0);
144-
that.menu().highlight();
145-
}
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);
146167
});
147168

148169
// when to hide
149-
this.config.editor.addEventListener('click', function() {
170+
editor.addEventListener('click', function() {
150171
setTimeout(function() {
151172
that._sel.isCollapsed && (that._menu.style.display = 'none');
152173
}, 0);

0 commit comments

Comments
 (0)