Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/init/targets/ve.init.mw.MobileArticleTarget.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki Initialization MobileArticleTarget class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * @external mw.mobileFrontend.VisualEditorOverlay
10 */
11
12 /**
13 * MediaWiki mobile article target.
14 *
15 * @class
16 * @extends ve.init.mw.ArticleTarget
17 *
18 * @constructor
19 * @param {mw.mobileFrontend.VisualEditorOverlay} overlay Mobile frontend overlay
20 * @param {Object} [config] Configuration options
21 * @param {Object} [config.toolbarConfig]
22 * @param {string|null} [config.section] Number of the section target should scroll to
23 */
24 ve.init.mw.MobileArticleTarget = function VeInitMwMobileArticleTarget( overlay, config ) {
25 this.overlay = overlay;
26 this.$overlay = overlay.$el;
27 this.$overlaySurface = overlay.$el.find( '.surface' );
28
29 config = config || {};
30 config.toolbarConfig = ve.extendObject( {
31 actions: false
32 }, config.toolbarConfig );
33
34 // Parent constructor
35 ve.init.mw.MobileArticleTarget.super.call( this, config );
36
37 // eslint-disable-next-line no-jquery/no-global-selector
38 this.$editableContent = $( '#mw-content-text' );
39
40 if ( config.section !== undefined ) {
41 this.section = config.section;
42 }
43
44 // Initialization
45 this.$element.addClass( 've-init-mw-mobileArticleTarget ve-init-mobileTarget' );
46 };
47
48 /* Inheritance */
49
50 OO.inheritClass( ve.init.mw.MobileArticleTarget, ve.init.mw.ArticleTarget );
51
52 /* Static Properties */
53
54 ve.init.mw.MobileArticleTarget.static.toolbarGroups = [
55 // History
56 {
57 name: 'history',
58 include: [ 'undo' ]
59 },
60 // Style
61 {
62 name: 'style',
63 classes: [ 've-test-toolbar-style' ],
64 type: 'list',
65 icon: 'textStyle',
66 title: OO.ui.deferMsg( 'visualeditor-toolbar-style-tooltip' ),
67 label: OO.ui.deferMsg( 'visualeditor-toolbar-style-tooltip' ),
68 invisibleLabel: true,
69 include: [ { group: 'textStyle' }, 'language', 'clear' ],
70 forceExpand: [ 'bold', 'italic', 'clear' ],
71 promote: [ 'bold', 'italic' ],
72 demote: [ 'strikethrough', 'code', 'underline', 'language', 'clear' ]
73 },
74 // Link
75 {
76 name: 'link',
77 include: [ 'link' ]
78 },
79 // Placeholder for reference tools (e.g. Cite and/or Citoid)
80 {
81 name: 'reference'
82 }
83 ];
84
85 ve.init.mw.MobileArticleTarget.static.trackingName = 'mobile';
86
87 // FIXME Some of these users will be on tablets, check for this
88 ve.init.mw.MobileArticleTarget.static.platformType = 'phone';
89
90 /* Methods */
91
92 /**
93 * @inheritdoc
94 */
95 ve.init.mw.MobileArticleTarget.prototype.deactivateSurfaceForToolbar = function () {
96 // Parent method
97 ve.init.mw.MobileArticleTarget.super.prototype.deactivateSurfaceForToolbar.call( this );
98
99 if ( this.wasSurfaceActive && ve.init.platform.constructor.static.isIos() ) {
100 this.prevScrollPosition = this.$scrollContainer.scrollTop();
101 }
102 };
103
104 /**
105 * @inheritdoc
106 */
107 ve.init.mw.MobileArticleTarget.prototype.activateSurfaceForToolbar = function () {
108 // Parent method
109 ve.init.mw.MobileArticleTarget.super.prototype.activateSurfaceForToolbar.call( this );
110
111 if ( this.wasSurfaceActive && ve.init.platform.constructor.static.isIos() ) {
112 // Setting the cursor can cause unwanted scrolling on iOS, so manually
113 // restore the scroll offset from before the toolbar was opened (T218650).
114 this.$scrollContainer.scrollTop( this.prevScrollPosition );
115 }
116 };
117
118 /**
119 * @inheritdoc
120 */
121 ve.init.mw.MobileArticleTarget.prototype.clearSurfaces = function () {
122 if ( ve.init.platform.constructor.static.isIos() && this.viewportZoomHandler ) {
123 this.viewportZoomHandler.detach();
124 this.viewportZoomHandler = null;
125 }
126 // Parent method
127 ve.init.mw.MobileArticleTarget.super.prototype.clearSurfaces.call( this );
128 };
129
130 /**
131 * @inheritdoc
132 */
133 ve.init.mw.MobileArticleTarget.prototype.onContainerScroll = function () {
134 // Editor may not have loaded yet, in which case `this.surface` is undefined
135 const surfaceView = this.surface && this.surface.getView(),
136 isActiveWithKeyboard = surfaceView && surfaceView.isFocused() && !surfaceView.isDeactivated();
137
138 // On iOS Safari, when the keyboard is open, the layout viewport reported by the browser is not
139 // updated to match the real viewport reduced by the keyboard (diagram: T218414#5027607). On all
140 // modern non-iOS browsers the layout viewport is updated to match real viewport.
141 //
142 // This allows the fixed toolbar to be scrolled out of view, ignoring `position: fixed` (because
143 // it refers to the layout viewport).
144 //
145 // When this happens, bring it back in by scrolling down a bit and back up until the top of the
146 // fake viewport is aligned with the top of the real viewport.
147
148 clearTimeout( this.onContainerScrollTimer );
149 if ( !isActiveWithKeyboard ) {
150 return;
151 }
152
153 // Wait until after the scroll, because 'scroll' events are not emitted for every frame the
154 // browser paints, so the toolbar would lag behind in a very unseemly manner. Additionally,
155 // getBoundingClientRect returns incorrect values during scrolling, so make sure to calculate
156 // it only after the scrolling ends (https://openradar.appspot.com/radar?id=6668472289329152).
157 let animateToolbarIntoView;
158 this.onContainerScrollTimer = setTimeout( animateToolbarIntoView = () => {
159 if ( this.toolbarAnimating ) {
160 // We can't do this while the 'transform' transition is happening, because
161 // getBoundingClientRect() returns values that reflect that (and are negative).
162 return;
163 }
164
165 const $header = this.overlay.$el.find( '.overlay-header-container' );
166
167 // Check if toolbar is offscreen. In a better world, this would reject all negative values
168 // (pos >= 0), but getBoundingClientRect often returns funny small fractional values after
169 // this function has done its job (which triggers another 'scroll' event) and before the
170 // user scrolled again. If we allowed it to run, it would trigger a hilarious loop! Toolbar
171 // being 1px offscreen is not a big deal anyway.
172 const pos = $header[ 0 ].getBoundingClientRect().top;
173 if ( pos >= -1 ) {
174 return;
175 }
176
177 // We don't know how much we have to scroll because we don't know how large the real
178 // viewport is. This value is bigger than the screen height of all iOS devices.
179 const viewportHeight = 2000;
180 // OK so this one is really weird. Normally on iOS, the scroll position is set on <body>.
181 // But on our sites, when using iOS 13, it's on <html> instead - maybe due to some funny
182 // CSS we set on html and body? Anyway, this seems to work...
183 const scrollY = document.body.scrollTop || document.documentElement.scrollTop;
184 const scrollX = document.body.scrollLeft || document.documentElement.scrollLeft;
185
186 // Prevent the scrolling we're about to do from triggering this event handler again.
187 this.toolbarAnimating = true;
188
189 const $overlaySurface = this.$overlaySurface;
190 // Scroll down and translate the surface by the same amount, otherwise the content at new
191 // scroll position visibly flashes.
192 $overlaySurface.css( 'transform', 'translateY( ' + viewportHeight + 'px )' );
193 window.scroll( scrollX, scrollY + viewportHeight );
194
195 // Prepate to animate toolbar sliding into view
196 $header.removeClass( 'toolbar-shown toolbar-shown-done' );
197 const headerHeight = $header[ 0 ].offsetHeight;
198 const headerTranslateY = Math.max( -headerHeight, pos );
199 $header.css( 'transform', 'translateY( ' + headerTranslateY + 'px )' );
200
201 // The scroll back up must be after a delay, otherwise no scrolling happens and the
202 // viewports are not aligned.
203 setTimeout( () => {
204 // Scroll back up
205 $overlaySurface.css( 'transform', '' );
206 window.scroll( scrollX, scrollY );
207
208 // Animate toolbar sliding into view
209 $header.addClass( 'toolbar-shown' ).css( 'transform', '' );
210 setTimeout( () => {
211 $header.addClass( 'toolbar-shown-done' );
212 // Wait until the animation is done before allowing this event handler to trigger again
213 this.toolbarAnimating = false;
214 // Re-check after the animation is done, in case the user scrolls in the meantime.
215 animateToolbarIntoView();
216 // The animation takes 250ms but we need to wait longer for some reason…
217 // 'transitionend' event also doesn't seem to work reliably.
218 }, 300 );
219 // If the delays below are made any smaller, the weirdest graphical glitches happen,
220 // so don't mess with them
221 }, 50 );
222 }, 250 );
223 };
224
225 /**
226 * Handle surface scroll events
227 */
228 ve.init.mw.MobileArticleTarget.prototype.onSurfaceScroll = function () {
229 if ( ve.init.platform.constructor.static.isIos() && this.getSurface() ) {
230 // iOS has a bug where if you change the scroll offset of a
231 // contentEditable or textarea with a cursor visible, it disappears.
232 // This function works around it by removing and reapplying the selection.
233 const nativeSelection = this.getSurface().getView().nativeSelection;
234 if ( nativeSelection.rangeCount && document.activeElement.contentEditable === 'true' ) {
235 const range = nativeSelection.getRangeAt( 0 );
236 nativeSelection.removeAllRanges();
237 nativeSelection.addRange( range );
238 }
239 }
240 };
241
242 /**
243 * @inheritdoc
244 */
245 ve.init.mw.MobileArticleTarget.prototype.createSurface = function ( dmDoc, config ) {
246 if ( this.overlay.isNewPage ) {
247 config = ve.extendObject( {
248 placeholder: this.overlay.options.placeholder
249 }, config );
250 }
251
252 // Parent method
253 const surface = ve.init.mw.MobileArticleTarget
254 .super.prototype.createSurface.call( this, dmDoc, config );
255
256 surface.connect( this, { scroll: 'onSurfaceScroll' } );
257
258 return surface;
259 };
260
261 /**
262 * @inheritdoc
263 */
264 ve.init.mw.MobileArticleTarget.prototype.getSurfaceClasses = function () {
265 const classes = ve.init.mw.MobileArticleTarget.super.prototype.getSurfaceClasses.call( this );
266 return [ ...classes, 'content' ];
267 };
268
269 /**
270 * @inheritdoc
271 */
272 ve.init.mw.MobileArticleTarget.prototype.setSurface = function ( surface ) {
273 const changed = surface !== this.surface;
274
275 // Parent method
276 // FIXME This actually skips ve.init.mw.Target.prototype.setSurface. Why?
277 ve.init.mw.Target.super.prototype.setSurface.apply( this, arguments );
278
279 if ( changed ) {
280 this.$overlaySurface.append( surface.$element );
281 }
282 };
283
284 /**
285 * @inheritdoc
286 */
287 ve.init.mw.MobileArticleTarget.prototype.surfaceReady = function () {
288 if ( this.teardownPromise ) {
289 // Loading was cancelled, the overlay is already closed at this point. Do nothing.
290 // Otherwise e.g. scrolling from #goToHeading would kick in and mess things up.
291 return;
292 }
293
294 // Deactivate the surface so any initial selection set in surfaceReady
295 // listeners doesn't cause the keyboard to be shown.
296 this.getSurface().getView().deactivate( false );
297
298 // Parent method
299 ve.init.mw.MobileArticleTarget.super.prototype.surfaceReady.apply( this, arguments );
300
301 // If no selection has been set yet, set it to the start of the document.
302 if ( this.getSurface().getModel().getSelection().isNull() ) {
303 this.getSurface().getView().selectFirstSelectableContentOffset();
304 }
305
306 this.events.trackActivationComplete();
307
308 if ( ve.init.platform.constructor.static.isIos() ) {
309 if ( this.viewportZoomHandler ) {
310 this.viewportZoomHandler.detach();
311 }
312 this.viewportZoomHandler = new ve.init.mw.ViewportZoomHandler();
313 this.viewportZoomHandler.attach( this.getSurface() );
314 }
315 };
316
317 /**
318 * @inheritdoc
319 */
320 ve.init.mw.MobileArticleTarget.prototype.afterSurfaceReady = function () {
321 this.adjustContentPadding();
322
323 // Parent method
324 ve.init.mw.MobileArticleTarget.super.prototype.afterSurfaceReady.apply( this, arguments );
325 };
326
327 /**
328 * Match the content padding to the toolbar height
329 */
330 ve.init.mw.MobileArticleTarget.prototype.adjustContentPadding = function () {
331 const surface = this.getSurface(),
332 surfaceView = surface.getView(),
333 toolbarHeight = this.getToolbar().$element[ 0 ].clientHeight;
334
335 surface.setPadding( {
336 top: toolbarHeight
337 } );
338 surfaceView.$attachedRootNode.css( 'padding-top', toolbarHeight );
339 surface.$placeholder.css( 'padding-top', toolbarHeight );
340 surfaceView.emit( 'position' );
341 surface.scrollSelectionIntoView();
342 };
343
344 /**
345 * @inheritdoc
346 */
347 ve.init.mw.MobileArticleTarget.prototype.switchToFallbackWikitextEditor = function ( modified ) {
348 let dataPromise;
349 if ( modified ) {
350 dataPromise = this.getWikitextDataPromiseForDoc( modified ).then( ( response ) => {
351 const content = ve.getProp( response, 'visualeditoredit', 'content' );
352 return { text: content };
353 } );
354 }
355 this.overlay.switchToSourceEditor( dataPromise );
356 return dataPromise;
357 };
358
359 /**
360 * @inheritdoc
361 */
362 ve.init.mw.MobileArticleTarget.prototype.save = function () {
363 // Parent method
364 const promise = ve.init.mw.MobileArticleTarget.super.prototype.save.apply( this, arguments );
365
366 this.overlay.log( {
367 action: 'saveAttempt'
368 } );
369
370 return promise;
371 };
372
373 /**
374 * @inheritdoc
375 */
376 ve.init.mw.MobileArticleTarget.prototype.showSaveDialog = function () {
377 // Parent method
378 ve.init.mw.MobileArticleTarget.super.prototype.showSaveDialog.apply( this, arguments );
379
380 this.overlay.log( {
381 action: 'saveIntent'
382 } );
383 };
384
385 /**
386 * @inheritdoc
387 */
388 ve.init.mw.MobileArticleTarget.prototype.replacePageContent = function (
389 html, categoriesHtml, displayTitle, lastModified /* , contentSub, sections */
390 ) {
391 // Parent method
392 ve.init.mw.MobileArticleTarget.super.prototype.replacePageContent.apply( this, arguments );
393
394 if ( lastModified ) {
395 // TODO: Update the last-modified-bar with the correct info
396 // eslint-disable-next-line no-jquery/no-global-selector
397 $( '.last-modified-bar' ).remove();
398 }
399 };
400
401 /**
402 * @inheritdoc
403 */
404 ve.init.mw.MobileArticleTarget.prototype.saveComplete = function ( data ) {
405 // Set 'saved' flag before teardown (which is called in parent method) to avoid prompts
406 // This is set in this.overlay.onSaveComplete, but we can't call that until we have
407 // computed the fragment.
408 this.overlay.saved = true;
409
410 // Parent method
411 ve.init.mw.MobileArticleTarget.super.prototype.saveComplete.apply( this, arguments );
412
413 const fragment = this.getSectionHashFromPage().slice( 1 );
414
415 this.overlay.sectionId = fragment;
416 this.overlay.onSaveComplete( data.newrevid, data.tempusercreatedredirect, data.tempusercreated );
417 };
418
419 /**
420 * @inheritdoc
421 */
422 ve.init.mw.MobileArticleTarget.prototype.saveFail = function ( doc, saveData, code, data ) {
423 // parent method
424 ve.init.mw.MobileArticleTarget.super.prototype.saveFail.apply( this, arguments );
425
426 this.overlay.onSaveFailure( data );
427 };
428
429 /**
430 * @inheritdoc
431 */
432 ve.init.mw.MobileArticleTarget.prototype.tryTeardown = function () {
433 this.overlay.onExitClick( $.Event() );
434 };
435
436 /**
437 * @inheritdoc
438 */
439 ve.init.mw.MobileArticleTarget.prototype.setupToolbar = function ( surface ) {
440 const originalToolbarGroups = this.toolbarGroups;
441
442 // We don't want any of these tools to show up in subordinate widgets, so we
443 // temporarily add them here. We need to do it _here_ rather than in their
444 // own static variable to make sure that other tools which meddle with
445 // toolbarGroups (Cite, mostly) have a chance to do so.
446 this.toolbarGroups = [
447 // Back
448 {
449 name: 'back',
450 include: [ 'back' ]
451 },
452 ...this.toolbarGroups,
453 {
454 name: 'editMode',
455 type: 'list',
456 icon: 'edit',
457 title: ve.msg( 'visualeditor-mweditmode-tooltip' ),
458 label: ve.msg( 'visualeditor-mweditmode-tooltip' ),
459 invisibleLabel: true,
460 include: [ { group: 'editMode' } ]
461 },
462 {
463 name: 'save',
464 type: 'bar',
465 include: [ 'showSave' ]
466 }
467 ];
468
469 // Parent method
470 ve.init.mw.MobileArticleTarget.super.prototype.setupToolbar.call( this, surface );
471
472 this.toolbarGroups = originalToolbarGroups;
473
474 this.toolbar.$element.addClass( 've-init-mw-mobileArticleTarget-toolbar' );
475 this.toolbar.$popups.addClass( 've-init-mw-mobileArticleTarget-toolbar-popups' );
476 };
477
478 /**
479 * @inheritdoc
480 */
481 ve.init.mw.MobileArticleTarget.prototype.attachToolbar = function () {
482 // Move the toolbar to the overlay header
483 this.overlay.$el.find( '.overlay-header > .toolbar' ).append( this.toolbar.$element );
484 this.toolbar.initialize();
485 };
486
487 /**
488 * @inheritdoc
489 */
490 ve.init.mw.MobileArticleTarget.prototype.setupToolbarSaveButton = function () {
491 this.toolbarSaveButton = this.toolbar.getToolGroupByName( 'save' ).items[ 0 ];
492 };
493
494 /**
495 * @inheritdoc
496 */
497 ve.init.mw.MobileArticleTarget.prototype.goToHeading = function ( headingNode ) {
498 this.scrollToHeading( headingNode );
499 };
500
501 /**
502 * Done with the editing toolbar
503 */
504 ve.init.mw.MobileArticleTarget.prototype.done = function () {
505 this.getSurface().getModel().setNullSelection();
506 this.getSurface().getView().blur();
507 };
508
509 /* Registration */
510
511 ve.init.mw.targetFactory.register( ve.init.mw.MobileArticleTarget );