Skip to content

Commit 8b84731

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 8b84731

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,29 @@
207207
addListener(ctx, editor, 'mousedown', function() {
208208
selecting = true;
209209
});
210+
addListener(ctx, editor, 'touchstart', function() {
211+
selecting = true;
212+
});
210213
addListener(ctx, editor, 'mouseleave', function() {
211214
if (selecting) updateStatus(800);
212215
selecting = false;
213216
});
217+
addListener(ctx, editor, 'touchleave', function() {
218+
if (selecting) updateStatus(800);
219+
selecting = false;
220+
});
214221
addListener(ctx, editor, 'mouseup', function() {
215222
if (selecting) updateStatus(100);
216223
selecting = false;
217224
});
225+
addListener(ctx, editor, 'touchend', function() {
226+
if (selecting) updateStatus(100);
227+
selecting = false;
228+
});
229+
addListener(ctx, editor, 'touchcancel', function() {
230+
if (selecting) updateStatus(100);
231+
selecting = false;
232+
});
218233
// Hide menu when focusing outside of editor
219234
outsideClick = function(e) {
220235
if (ctx._menu && !containsNode(editor, e.target) && !containsNode(ctx._menu, e.target)) {
@@ -725,7 +740,7 @@
725740
if (!this._inputBar || !this._inputActive) return this;
726741
}
727742
var offset = this._range.getBoundingClientRect()
728-
, menuPadding = 10
743+
, menuPadding = 'ontouchstart' in window ? 20 : 10
729744
, top = offset.top - menuPadding
730745
, left = offset.left + (offset.width / 2)
731746
, menu = this._menu
@@ -760,7 +775,7 @@
760775
} else {
761776
stylesheet.insertRule('.pen-menu:after {left: 50%; }', 0);
762777
}
763-
if (menuOffset.y < 0) {
778+
if (menuOffset.y < 0 || 'ontouchstart' in window) {
764779
menu.classList.add('pen-menu-below');
765780
menuOffset.y = offset.top + offset.height + menuPadding;
766781
} else {

0 commit comments

Comments
 (0)