mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-14 18:45:12 +00:00
CitationNeededContext: Support encapsulated content
When a canonical 'encapsulate' param is defined, the contents of this parameter will get inserted into the document before adding a citation after it, effectively "unwrapping" the template around it. Bug: T265907 Depends-On: Ie9e41ad46e4154de7e9807c5216abbadd08365f9 Depends-On: Ia53c0a641b231bb81c25c011624357acf4dc42a3 Change-Id: Icf200465bcc8fe87ba8612dc8fa9f4359a2289a9
This commit is contained in:
parent
e3d34e6f9f
commit
349fc3b12c
|
@ -17,19 +17,13 @@
|
|||
* @param {Object} config Configuration options
|
||||
*/
|
||||
ve.ui.MWCitationNeededContextItem = function VeUiMWCitationNeededContextItem() {
|
||||
var contextItem = this;
|
||||
|
||||
// Parent constructor
|
||||
ve.ui.MWCitationNeededContextItem.super.apply( this, arguments );
|
||||
|
||||
this.addButton = new OO.ui.ButtonWidget( {
|
||||
label: ve.msg( 'cite-ve-citationneeded-button' ),
|
||||
flags: [ 'progressive' ]
|
||||
} ).on( 'click', function () {
|
||||
var action = ve.ui.actionFactory.create( 'citoid', contextItem.context.getSurface() );
|
||||
action.open( true );
|
||||
ve.track( 'activity.' + contextItem.constructor.static.name, { action: 'context-add-citation' } );
|
||||
} );
|
||||
} ).on( 'click', this.onAddClick.bind( this ) );
|
||||
|
||||
// Remove progressive flag from edit, as addButton is now the
|
||||
// main progressive action in the context.
|
||||
|
@ -53,6 +47,58 @@ ve.ui.MWCitationNeededContextItem.static.label = OO.ui.deferMsg( 'cite-ve-citati
|
|||
|
||||
/* Methods */
|
||||
|
||||
ve.ui.MWCitationNeededContextItem.prototype.onAddClick = function () {
|
||||
var promise,
|
||||
contextItem = this,
|
||||
surface = this.context.getSurface(),
|
||||
// TODO: This assumes Citoid is installed...
|
||||
action = ve.ui.actionFactory.create( 'citoid', surface ),
|
||||
encapsulatedWikitext = this.getCanonicalParam( 'encapsulate' );
|
||||
|
||||
if ( encapsulatedWikitext ) {
|
||||
this.addButton.setDisabled( true );
|
||||
promise = ve.init.target.parseWikitextFragment( encapsulatedWikitext, false, this.model.getDocument() ).then( function ( response ) {
|
||||
var dmDoc, nodes, range;
|
||||
|
||||
if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) {
|
||||
return ve.createDeferred().reject().promise();
|
||||
}
|
||||
|
||||
dmDoc = ve.ui.MWWikitextStringTransferHandler.static.createDocumentFromParsoidHtml(
|
||||
response.visualeditor.content,
|
||||
surface.getModel().getDocument()
|
||||
);
|
||||
|
||||
nodes = dmDoc.getDocumentNode().children.filter( function ( node ) {
|
||||
return !node.isInternal();
|
||||
} );
|
||||
|
||||
// Unwrap single content branch nodes to match internal copy/paste behaviour
|
||||
// (which wouldn't put the open and close tags in the clipboard to begin with).
|
||||
if (
|
||||
nodes.length === 1 &&
|
||||
nodes[ 0 ].canContainContent()
|
||||
) {
|
||||
range = nodes[ 0 ].getRange();
|
||||
}
|
||||
|
||||
surface.getModel().pushStaging();
|
||||
surface.getModel().getFragment().insertDocument( dmDoc, range ).collapseToEnd().select();
|
||||
return true;
|
||||
} );
|
||||
promise.always( function () {
|
||||
contextItem.addButton.setDisabled( false );
|
||||
} );
|
||||
} else {
|
||||
promise = ve.createDeferred().resolve( false ).promise();
|
||||
}
|
||||
|
||||
promise.then( function ( inStaging ) {
|
||||
action.open( true, undefined, inStaging );
|
||||
} );
|
||||
ve.track( 'activity.' + this.constructor.static.name, { action: 'context-add-citation' } );
|
||||
};
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue