Support new wikitext editor for edit helper

Register a hook for when VE is finished initializing to select the error
section, just like the textbox-based editor.

Use the BeforePageDisplay hook so it runs on VE page loads too.

Bug: T160102
Change-Id: I59a7e0a3e8be32e4689cbf41c4904970902c4dff
This commit is contained in:
Kunal Mehta 2017-03-10 14:09:17 -08:00 committed by Ed Sanders
parent f5fa3d3c37
commit 9c65d786e2
4 changed files with 40 additions and 11 deletions

View file

@ -19,6 +19,7 @@
"globals": {
"mediaWiki": false,
"ve": false,
"OO": false
}
}

View file

@ -26,7 +26,7 @@
},
"Hooks": {
"LoadExtensionSchemaUpdates": "MediaWiki\\Linter\\Hooks::onLoadExtensionSchemaUpdates",
"EditFormInitialText": "MediaWiki\\Linter\\Hooks::onEditFormInitialText",
"BeforePageDisplay": "MediaWiki\\Linter\\Hooks::onBeforePageDisplay",
"APIQuerySiteInfoGeneralInfo": "MediaWiki\\Linter\\Hooks::onAPIQuerySiteInfoGeneralInfo",
"InfoAction": "MediaWiki\\Linter\\Hooks::onInfoAction",
"WikiPageDeletionUpdates": "MediaWiki\\Linter\\Hooks::onWikiPageDeletionUpdates"

View file

@ -23,9 +23,9 @@ namespace MediaWiki\Linter;
use ApiQuerySiteInfo;
use Content;
use DatabaseUpdater;
use EditPage;
use IContextSource;
use MWCallableUpdate;
use OutputPage;
use WikiPage;
class Hooks {
@ -38,21 +38,23 @@ class Hooks {
}
/**
* Hook: EditFormInitialText
* Hook: BeforePageDisplay
*
* If there is a lintid parameter, look up that error in the database
* and setup and output our client-side helpers
*
* @param EditPage $editPage
* @param OutputPage &$out
*/
public static function onEditFormInitialText( EditPage $editPage ) {
$context = $editPage->getContext();
$request = $context->getRequest();
public static function onBeforePageDisplay( OutputPage &$out ) {
$request = $out->getRequest();
$lintId = $request->getInt( 'lintid' );
if ( !$lintId ) {
return;
}
$title = $editPage->getTitle();
$title = $out->getTitle();
if ( !$title ) {
return;
}
$lintError = ( new Database( $title->getArticleID() ) )->getFromId( $lintId );
if ( !$lintError ) {
@ -60,7 +62,6 @@ class Hooks {
return;
}
$out = $context->getOutput();
$out->addJsConfigVars( [
'wgLinterErrorCategory' => $lintError->category,
'wgLinterErrorLocation' => $lintError->location,

View file

@ -1,8 +1,35 @@
( function ( mw, $ ) {
$( function () {
var location = mw.config.get( 'wgLinterErrorLocation' );
var location = mw.config.get( 'wgLinterErrorLocation' ),
$textbox = $( '#wpTextbox1' );
/**
* Convert the normal offset for one that is usable
* by VE's DOM that changes newlines into <p>
*
* @param {ve.ui.Surface} surface
* @param {int} offset
* @return {int}
*/
function fixOffset( surface, offset ) {
return ( surface.getDom().slice( 0, offset ).match( /\n/g ) || [] ).length + 1 + offset;
}
if ( location ) {
$( '#wpTextbox1' ).focus().textSelection( 'setSelection', { start: location[ 0 ], end: location[ 1 ] } );
if ( $textbox.length ) {
$textbox.focus().textSelection( 'setSelection', { start: location[ 0 ], end: location[ 1 ] } );
}
// Register NWE code should it be loaded
// TODO: We should somehow force source mode if VE is opened
mw.hook( 've.activationComplete' ).add( function () {
var range,
surface = ve.init.target.getSurface();
if ( surface.getMode() === 'source' ) {
range = new ve.Range( fixOffset( surface, location[ 0 ] ), fixOffset( surface, location[ 1 ] ) );
surface.getModel().setLinearSelection( range );
}
} );
}
} );
}( mediaWiki, jQuery ) );