Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/init/ve.init.mw.ViewportZoomHandler.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki Initialization ViewportZoomHandler class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Prevent iOS browsers from wrongly zooming in the page when the surface is focussed. (T216446)
10 *
11 * When the user places a cursor for text input anywhere on the page, iOS browsers zoom in the page
12 * to ensure the text size is legible and the cursor can be comfortably placed in the right place
13 * with a finger.
14 *
15 * There's a browser bug that, on some devices (e.g. iPhone XS, but not iPhone SE), causes this
16 * zoom to occur even though our text is already using the required minimum font size (16px).
17 * Additionally, the zoom occurs when placing the cursor in image captions, which intentionally
18 * use a smaller font size.
19 *
20 * In both cases the zoom is more problematic than helpful, because it causes parts of the toolbar
21 * to disappear outside the viewport.
22 *
23 * To prevent it, temporarily add a tag like `<meta name="viewport" content="maximum-scale=1.0">`
24 * to the page when the user is about to focus the editing surface. However, on iOS Chrome, this
25 * also prevents intentional pinch-zoom. To avoid this, immediately remove the tag again after
26 * focussing, or if it looks like the user is trying to zoom (used multi-touch or caused a scroll).
27 *
28 * @class
29 * @constructor
30 */
31 ve.init.mw.ViewportZoomHandler = function VeInitMwViewportZoomHandler() {
32 // eslint-disable-next-line no-jquery/no-global-selector
33 this.$viewportMeta = $( 'meta[name="viewport"]' );
34 if ( !this.$viewportMeta.length ) {
35 this.$viewportMeta = $( '<meta>' ).attr( 'name', 'viewport' ).appendTo( document.head );
36 }
37 this.origViewportMetaContent = this.$viewportMeta.attr( 'content' );
38
39 this.onTouchStartHandler = this.onTouchStart.bind( this );
40 this.onTouchMoveHandler = this.onTouchMove.bind( this );
41 this.onTouchEndHandler = this.onTouchEnd.bind( this );
42 };
43
44 /* Methods */
45
46 /**
47 * Change the `<meta name="viewport">` tag to prevent automatic zooming.
48 */
49 ve.init.mw.ViewportZoomHandler.prototype.preventZoom = function () {
50 this.$viewportMeta.attr( 'content', ( i, val ) => {
51 // Remove existing maximum-scale, if any, and add 'maximum-scale=1.0'. Don't change other values.
52 if ( val ) {
53 val = val.replace( /maximum-scale=[\d.]+(,\s*|$)/, '' );
54 val += ', ';
55 } else {
56 val = '';
57 }
58 return val + 'maximum-scale=1.0';
59 } );
60 };
61
62 /**
63 * Change the `<meta name="viewport">` tag to allow automatic zooming once again.
64 */
65 ve.init.mw.ViewportZoomHandler.prototype.allowZoom = function () {
66 this.$viewportMeta.attr( 'content', this.origViewportMetaContent );
67 };
68
69 /**
70 * Start listening to events and preventing zooming.
71 *
72 * @param {ve.ui.Surface} surface
73 */
74 ve.init.mw.ViewportZoomHandler.prototype.attach = function ( surface ) {
75 this.surface = surface;
76
77 this.surface.getView().$element.on( {
78 touchstart: this.onTouchStartHandler,
79 touchmove: this.onTouchMoveHandler,
80 touchend: this.onTouchEndHandler
81 } );
82 this.surface.getModel().connect( this, {
83 focus: 'onFocus'
84 } );
85 };
86
87 /**
88 * Stop listening to events.
89 */
90 ve.init.mw.ViewportZoomHandler.prototype.detach = function () {
91 this.surface.getView().$element.off( {
92 touchstart: this.onTouchStartHandler,
93 touchmove: this.onTouchMoveHandler,
94 touchend: this.onTouchEndHandler
95 } );
96 this.surface.getModel().disconnect( this, {
97 focus: 'onFocus'
98 } );
99
100 this.surface = null;
101 };
102
103 /**
104 * Handle touch start events.
105 *
106 * @param {jQuery.Event} e Touch start event
107 */
108 ve.init.mw.ViewportZoomHandler.prototype.onTouchStart = function ( e ) {
109 if ( e.touches.length === 1 ) {
110 this.wasMoved = false;
111 }
112
113 this.allowZoom();
114 };
115
116 /**
117 * Handle touch move events.
118 *
119 * @param {jQuery.Event} e Touch move event
120 */
121 ve.init.mw.ViewportZoomHandler.prototype.onTouchMove = function () {
122 this.wasMoved = true;
123 };
124
125 /**
126 * Handle touch end events.
127 *
128 * @param {jQuery.Event} e Touch end event
129 */
130 ve.init.mw.ViewportZoomHandler.prototype.onTouchEnd = function ( e ) {
131 if ( e.touches.length === 0 && !this.wasMoved ) {
132 // There was a single touch point, that hasn't moved, and now it's gone.
133 // Looks like we're going to focus the surface, so prevent automatic zoom.
134 this.preventZoom();
135 } else {
136 // Otherwise, allow zoom, so that the user can pinch-zoom
137 this.allowZoom();
138 }
139 };
140
141 /**
142 * Handle surface model focus events.
143 */
144 ve.init.mw.ViewportZoomHandler.prototype.onFocus = function () {
145 this.allowZoom();
146 };