|
25 | 25 | 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;'); |
26 | 26 | }; |
27 | 27 |
|
| 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 | + |
28 | 40 | // merge: make it easy to have a fallback |
29 | 41 | utils.merge = function(config) { |
30 | 42 |
|
|
113 | 125 |
|
114 | 126 | Pen.prototype.toolbar = function() { |
115 | 127 |
|
116 | | - var menu, that = this, icons = '', setpos; |
| 128 | + var that = this, icons = ''; |
117 | 129 |
|
118 | 130 | for(var i = 0, list = this.config.list; i < list.length; i++) { |
119 | 131 | var name = list[i], klass = 'pen-icon icon-' + name; |
120 | 132 | icons += '<i class="' + klass + '" data-action="' + name + '">' + (name.match(/^h[1-6]|p$/i) ? name.toUpperCase() : '') + '</i>'; |
121 | 133 | if((name === 'createlink')) icons += '<input class="pen-input" placeholder="http://" />'; |
122 | 134 | } |
123 | 135 |
|
124 | | - menu = doc.createElement('div'); |
| 136 | + var menu = doc.createElement('div'); |
125 | 137 | menu.setAttribute('class', this.config.class + '-menu pen-menu'); |
126 | 138 | menu.innerHTML = icons; |
127 | 139 | menu.style.display = 'none'; |
128 | 140 |
|
129 | 141 | doc.body.appendChild((this._menu = menu)); |
130 | 142 |
|
131 | | - setpos = function() { |
| 143 | + var setpos = function() { |
132 | 144 | if(menu.style.display === 'block') that.menu(); |
133 | 145 | }; |
134 | 146 |
|
135 | 147 | // change menu offset when window resize / scroll |
136 | 148 | window.addEventListener('resize', setpos); |
137 | 149 | window.addEventListener('scroll', setpos); |
138 | 150 |
|
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); |
146 | 167 | }); |
147 | 168 |
|
148 | 169 | // when to hide |
149 | | - this.config.editor.addEventListener('click', function() { |
| 170 | + editor.addEventListener('click', function() { |
150 | 171 | setTimeout(function() { |
151 | 172 | that._sel.isCollapsed && (that._menu.style.display = 'none'); |
152 | 173 | }, 0); |
|
0 commit comments