Cache generated content request when inserting templates

We request the generated content of a template to determine
if it will be block or inline. This is the same request as
the view makes later to show the rendering, so we should cache
this repsonse.

Bug: T156698
Change-Id: I0ffd36ccd0aa821aa44d99328f2e3a2abc23dc0f
This commit is contained in:
Ed Sanders 2017-02-07 21:35:06 +00:00 committed by Krinkle
parent 703e784d1a
commit 501d0c94d2

View file

@ -62,10 +62,11 @@
ve.dm.MWTransclusionModel.prototype.insertTransclusionNode = function ( surfaceFragment, forceType ) { ve.dm.MWTransclusionModel.prototype.insertTransclusionNode = function ( surfaceFragment, forceType ) {
var model = this, var model = this,
deferred = $.Deferred(), deferred = $.Deferred(),
nodeClass = ve.dm.MWTransclusionNode; baseNodeClass = ve.dm.MWTransclusionNode;
function insertNode( isInline ) { function insertNode( isInline, generatedContents ) {
var type = isInline ? nodeClass.static.inlineType : nodeClass.static.blockType, var hash, store, nodeClass,
type = isInline ? baseNodeClass.static.inlineType : baseNodeClass.static.blockType,
range = surfaceFragment.getSelection().getCoveringRange(), range = surfaceFragment.getSelection().getCoveringRange(),
data = [ data = [
{ {
@ -77,6 +78,15 @@
{ type: '/' + type } { type: '/' + type }
]; ];
// If we just fetched the generated contents, put them in the store
// so we don't do a duplicate API call later.
if ( generatedContents ) {
nodeClass = ve.dm.modelRegistry.lookup( type );
store = surfaceFragment.getDocument().getStore();
hash = OO.getHash( [ nodeClass.static.getHashObjectForRendering( data[ 0 ] ), undefined ] );
store.index( generatedContents, hash );
}
if ( range.isCollapsed() ) { if ( range.isCollapsed() ) {
surfaceFragment.insertContent( data ); surfaceFragment.insertContent( data );
} else { } else {
@ -97,7 +107,7 @@
action: 'visualeditor', action: 'visualeditor',
paction: 'parsefragment', paction: 'parsefragment',
page: mw.config.get( 'wgRelevantPageName' ), page: mw.config.get( 'wgRelevantPageName' ),
wikitext: nodeClass.static.getWikitext( this.getPlainObject() ), wikitext: baseNodeClass.static.getWikitext( this.getPlainObject() ),
pst: 1 pst: 1
} ) } )
.then( function ( response ) { .then( function ( response ) {
@ -107,7 +117,8 @@
contentNodes = $.parseHTML( response.visualeditor.content, surfaceFragment.getDocument().getHtmlDocument() ) || []; contentNodes = $.parseHTML( response.visualeditor.content, surfaceFragment.getDocument().getHtmlDocument() ) || [];
contentNodes = ve.ce.MWTransclusionNode.static.filterRendering( contentNodes ); contentNodes = ve.ce.MWTransclusionNode.static.filterRendering( contentNodes );
insertNode( insertNode(
nodeClass.static.isHybridInline( contentNodes, ve.dm.converter ) baseNodeClass.static.isHybridInline( contentNodes, ve.dm.converter ),
contentNodes
); );
} else { } else {
// Request failed - just assume inline // Request failed - just assume inline