Small side-by-side preview improvements

- load jsconfigvars
- trigger the hooks for wikipage.content and wikipage.diff
- rvdifftotextpst for diffs
- improve section support

This module is not used within WMF, but has some users amongst 3rd
parties.

Change-Id: I7f0b2de4a984d811ccd94149670390929a6182f6
This commit is contained in:
Derk-Jan Hartman 2016-04-16 00:27:35 +02:00
parent e2cf07f2f3
commit 6627f2cd94

View file

@ -60,26 +60,36 @@ $.wikiEditor.modules.preview = {
action: 'parse',
title: mw.config.get( 'wgPageName' ),
text: wikitext,
prop: 'text|modules',
pst: ''
pst: '',
prop: 'text|modules|jsconfigvars',
preview: true,
disableeditsection: true,
uselang: mw.config.get( 'wgUserLanguage' )
} ).done( function ( data ) {
if ( !data.parse || !data.parse.text ) {
return;
}
context.modules.preview.previewText = wikitext;
context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).hide();
context.modules.preview.$preview.find( '.wikiEditor-preview-contents' )
.html( data.parse.text )
.append( '<div class="visualClear"></div>' )
.find( 'a:not([href^=#])' )
.click( false );
if ( data.parse.jsconfigvars ) {
mw.config.set( data.parse.jsconfigvars );
}
var loadmodules = data.parse.modules.concat(
data.parse.modulescripts,
data.parse.modulestyles
);
mw.loader.load( loadmodules );
context.modules.preview.previewText = wikitext;
context.modules.preview.$preview.find( '.wikiEditor-preview-loading' ).hide();
var $content = context.modules.preview.$preview.find( '.wikiEditor-preview-contents' )
.detach()
.html( data.parse.text );
$content.append( '<div class="visualClear"></div>' )
.find( 'a:not([href^=#])' )
.click( false );
mw.hook( 'wikipage.content' ).fire( $content );
context.modules.preview.$preview.append( $content );
} );
}
} );
@ -97,32 +107,20 @@ $.wikiEditor.modules.preview = {
context.$changesTab.find( 'table.diff tbody' ).empty();
context.$changesTab.find( '.wikiEditor-preview-loading' ).show();
// Call the API. First PST the input, then diff it
api.post( {
formatversion: 2,
action: 'parse',
title: mw.config.get( 'wgPageName' ),
onlypst: '',
text: wikitext
} ).done( function ( data ) {
try {
var postdata2 = {
var section = $( '[name="wpSection"]' ).val();
var postdata = {
formatversion: 2,
action: 'query',
prop: 'revisions',
titles: mw.config.get( 'wgPageName' ),
rvdifftotext: data.parse.text,
rvprop: ''
rvdifftotext: wikitext,
rvdifftotextpst: true,
rvprop: '',
rvsection: section === '' ? undefined : section
};
var section = $( '[name="wpSection"]' ).val();
if ( section !== '' ) {
postdata2.rvsection = section;
}
api.post( postdata2 )
api.post( postdata )
.done( function ( data ) {
// Add diff CSS
mw.loader.load( 'mediawiki.action.history.diff' );
try {
var diff = data.query.pages[ 0 ]
.revisions[ 0 ].diff.body;
@ -130,16 +128,14 @@ $.wikiEditor.modules.preview = {
context.$changesTab.find( 'table.diff tbody' )
.html( diff )
.append( '<div class="visualClear"></div>' );
mw.hook( 'wikipage.diff' )
.fire( context.$changesTab.find( 'table.diff' ) );
context.modules.preview.changesText = wikitext;
} catch ( e ) {
// "data.blah is undefined" error, ignore
}
context.$changesTab.find( '.wikiEditor-preview-loading' ).hide();
} );
} catch ( e ) {
// "data.blah is undefined" error, ignore
}
} );
}
} );