Skip to content

Commit 2f118a2

Browse files
committed
Display the toolbar on touchscreens
Use touch events to detect selection as well as mouse events. Always display the toolbar at the bottom because the browser's built-in toolbar appears at the top most of the time.
1 parent 4bd5f84 commit 2f118a2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
<html lang="en-US">
33
<head>
44
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
56
<title>Pen - What You See Is What You Get (WYSIWYG)</title>
6-
<style type="text/css">
7+
<style>
78
*{padding:0;margin:0;}
89
html{border-top:10px #1abf89 solid;}
9-
body{width:800px;margin:0 auto;padding:5% 20px 20px;font-family:Palatino, Optima, Georgia, serif;}
10-
@media all and (max-width:1024px){ body, pre a{width:60%;} }
10+
body{max-width:800px;margin:0 auto;padding:5% 20px 20px;font-family:Palatino, Optima, Georgia, serif;}
11+
@media all and (max-width:1024px){ body, pre a{max-width:60%;} }
1112
small{color:#999;}
1213
pre{white-space:pre-wrap;}
1314

src/pen.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,29 @@
204204

205205
// toggle toolbar on mouse select
206206
var selecting = false;
207-
addListener(ctx, editor, 'mousedown', function() {
207+
addListener(ctx, editor, 'touchstart', function() {
208208
selecting = true;
209209
});
210210
addListener(ctx, editor, 'mouseleave', function() {
211211
if (selecting) updateStatus(800);
212212
selecting = false;
213213
});
214+
addListener(ctx, editor, 'touchleave', function() {
215+
if (selecting) updateStatus(800);
216+
selecting = false;
217+
});
214218
addListener(ctx, editor, 'mouseup', function() {
215219
if (selecting) updateStatus(100);
216220
selecting = false;
217221
});
222+
addListener(ctx, editor, 'touchend', function() {
223+
if (selecting) updateStatus(100);
224+
selecting = false;
225+
});
226+
addListener(ctx, editor, 'touchcancel', function() {
227+
if (selecting) updateStatus(100);
228+
selecting = false;
229+
});
218230
// Hide menu when focusing outside of editor
219231
outsideClick = function(e) {
220232
if (ctx._menu && !containsNode(editor, e.target) && !containsNode(ctx._menu, e.target)) {
@@ -725,7 +737,7 @@
725737
if (!this._inputBar || !this._inputActive) return this;
726738
}
727739
var offset = this._range.getBoundingClientRect()
728-
, menuPadding = 10
740+
, menuPadding = 'ontouchstart' in window ? 20 : 10
729741
, top = offset.top - menuPadding
730742
, left = offset.left + (offset.width / 2)
731743
, menu = this._menu
@@ -760,7 +772,7 @@
760772
} else {
761773
stylesheet.insertRule('.pen-menu:after {left: 50%; }', 0);
762774
}
763-
if (menuOffset.y < 0) {
775+
if (menuOffset.y < 0 || 'ontouchstart' in window) {
764776
menu.classList.add('pen-menu-below');
765777
menuOffset.y = offset.top + offset.height + menuPadding;
766778
} else {

0 commit comments

Comments
 (0)