Jiabao has uploaded a new change for review.
https://gerrit.wikimedia.org/r/78406
Change subject: [WIP] MathJax Live Preview II
......................................................................
[WIP] MathJax Live Preview II
(depend on "Support previews and concurrent updates in ce.GeneratedContentNode")
- rebased live preview to rely on above for removal of race condition.
- mathnode now uses the new generatedcontent code as transclusion node.
- rebased live preview to incorporate the math cleanup.
- fixed a lot of problems with the cleanup currently removing the wrapper.
- renamed mw variable to mwd to resolve errors with the global (look for better
name).
- removed mathjax queue control pending investigation.
- live preview is heavily effected by the fact mathjax doesnt render when VE
loads.
Change-Id: I50cc1943566c782ca37899de44009dd3eb28c763
---
M modules/ve-mw/ce/nodes/ve.ce.MWMathNode.js
M modules/ve-mw/dm/nodes/ve.dm.MWMathNode.js
M modules/ve-mw/ui/inspectors/ve.ui.MWMathInspector.js
3 files changed, 63 insertions(+), 60 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor
refs/changes/06/78406/1
diff --git a/modules/ve-mw/ce/nodes/ve.ce.MWMathNode.js
b/modules/ve-mw/ce/nodes/ve.ce.MWMathNode.js
index 17ccf52..1492192 100644
--- a/modules/ve-mw/ce/nodes/ve.ce.MWMathNode.js
+++ b/modules/ve-mw/ce/nodes/ve.ce.MWMathNode.js
@@ -21,16 +21,9 @@
* @param {Object} [config] Config options
*/
ve.ce.MWMathNode = function VeCeMWMathNode( model, config ) {
- var $wrapper;
// Parent constructor
ve.ce.LeafNode.call( this, model, config );
-
- // Wrap image
- this.$image = this.$;
- $wrapper = $( '<span> ');
- this.$.wrap( $wrapper );
- this.$ = $wrapper;
// Mixin constructors
ve.ce.FocusableNode.call( this );
@@ -38,7 +31,6 @@
ve.ce.GeneratedContentNode.call( this );
// Events
- this.model.connect( this, { 'update': 'onUpdate' } );
this.$.on( 'click', ve.bind( this.onClick, this ) );
// DOM Changes
@@ -57,20 +49,20 @@
ve.ce.MWMathNode.static.name = 'mwMath';
-ve.ce.MWMathNode.static.tagName = 'img';
+ve.ce.MWMathNode.static.tagName = 'span';
/* Methods */
/** */
-ve.ce.MWMathNode.prototype.generateContents = function () {
- var deferred = $.Deferred();
- $.ajax( {
+ve.ce.MWMathNode.prototype.generateContents = function ( config ) {
+ var xhr, promise, deferred = $.Deferred();
+ xhr = $.ajax( {
'url': mw.util.wikiScript( 'api' ),
'data': {
'action': 'visualeditor',
'paction': 'parsefragment',
'page': mw.config.get( 'wgRelevantPageName' ),
- 'wikitext': '<math>' + this.getModel().getAttribute(
'mw' ).body.extsrc + '</math>',
+ 'wikitext': '<math>' + this.model.getAttribute( 'mw'
).body.extsrc + '</math>',
'token': mw.user.tokens.get( 'editToken' ),
'format': 'json'
},
@@ -82,7 +74,11 @@
'success': ve.bind( this.onParseSuccess, this, deferred ),
'error': ve.bind( this.onParseError, this, deferred )
} );
- return deferred.promise();
+ promise = deferred.promise();
+ promise.abort = function () {
+ xhr.abort();
+ };
+ return promise;
};
/**
@@ -92,19 +88,15 @@
* @param {Object} response Response data
*/
ve.ce.MWMathNode.prototype.onParseSuccess = function ( deferred, response ) {
- var data = response.visualeditor, contentNodes = $( data.content
).get();
- // HACK: unwrap paragraph from PHP parser
+ var data = response.visualeditor, contentNodes = $( data.content
).get();//, self = this;
contentNodes = Array.prototype.slice.apply( contentNodes[0].childNodes
);
deferred.resolve( contentNodes );
- if ( $( contentNodes ).is( 'span.tex' ) ) {
- // MathJax
- MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub ] );
- } else {
- // Rerender after image load
- this.$.find( 'img' ).on( 'load', ve.bind( function () {
- this.emit( 'rerender' );
- }, this ) );
- }
+
+ this.reRender();
+};
+
+ve.ce.MWMathNode.prototype.reRender = function () {
+ MathJax.Hub.Queue(['Typeset',MathJax.Hub, this.$[0]]);
};
/**
diff --git a/modules/ve-mw/dm/nodes/ve.dm.MWMathNode.js
b/modules/ve-mw/dm/nodes/ve.dm.MWMathNode.js
index 87a76e9..d66ad19 100644
--- a/modules/ve-mw/dm/nodes/ve.dm.MWMathNode.js
+++ b/modules/ve-mw/dm/nodes/ve.dm.MWMathNode.js
@@ -41,6 +41,9 @@
ve.dm.MWMathNode.static.isContent = true;
ve.dm.MWMathNode.static.toDataElement = function ( domElements, converter ) {
+ var mycars = new Array();
+ mycars[0] = domElements[0];
+
var dataElement, index,
mwDataJSON = domElements[0].getAttribute( 'data-mw' ),
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
@@ -49,14 +52,13 @@
'type': 'mwMath',
'attributes': {
'mw': mwData,
- 'originalDomElements': ve.copyArray( domElements ),
+ 'originalDomElements': mycars,
'originalMw': mwDataJSON
}
};
index = this.storeDomElements( dataElement, domElements,
converter.getStore() );
dataElement.attributes.originalIndex = index;
-
return dataElement;
};
diff --git a/modules/ve-mw/ui/inspectors/ve.ui.MWMathInspector.js
b/modules/ve-mw/ui/inspectors/ve.ui.MWMathInspector.js
index 5f9dfc6..9c94e00 100644
--- a/modules/ve-mw/ui/inspectors/ve.ui.MWMathInspector.js
+++ b/modules/ve-mw/ui/inspectors/ve.ui.MWMathInspector.js
@@ -30,6 +30,8 @@
ve.ui.MWMathInspector.static.titleMessage =
'visualeditor-mwmathinspector-title';
+ve.ui.MWMathInspector.static.InputWidget = ve.ui.InputWidget;
+
/* Methods */
/**
@@ -41,37 +43,54 @@
// Parent method
ve.ui.Inspector.prototype.initialize.call( this );
- this.input = new ve.ui.TextInputWidget( {
- '$$': this.frame.$$,
- 'overlay': this.surface.$localOverlay,
- 'multiline': true
+ this.input = new this.constructor.static.InputWidget( {
+ '$$': this.frame.$$, 'overlay': this.surface.$localOverlay
} );
- this.input.$.addClass( 've-ui-mwMathInspector-input' );
+
+ this.changed = false;
+
+ this.input.on( 'change', ve.bind( this.livePreview, this ) );
// Initialization
this.$form.append( this.input.$ );
};
+ve.ui.MWMathInspector.prototype.livePreview = function () {
+
+ var newsrc = this.input.getValue(), self = this,
+ surfaceModel = this.surface.getModel();
+
+ // If change is called with no change it will unrender the mathnode
+ // If the input box is empty when closed it errors if change is called
in the onParseSuccess function
+ if (this.mathNode.getModel().getAttribute('mw').body.extsrc !== newsrc)
{
+ mwd = self.mathNode.getModel().getAttribute( 'mw' );
+ mwd.body.extsrc = self.input.getValue();
+ surfaceModel.change(
+ ve.dm.Transaction.newFromAttributeChanges(
+ surfaceModel.getDocument(),
self.mathNode.getOuterRange().start, { 'mw': mwd }
+ )
+ );
+ }
+};
/**
* Handle the inspector being opened.
*/
ve.ui.MWMathInspector.prototype.onOpen = function () {
- var extsrc = '';
// Parent method
ve.ui.Inspector.prototype.onOpen.call( this );
- this.node = this.surface.getView().getFocusedNode();
+ this.mathNode = this.surface.getView().getFocusedNode();
- if ( this.node ) {
- extsrc = this.node.getModel().getAttribute( 'mw' ).body.extsrc;
- }
+ mwd = this.mathNode.getModel().getAttribute( 'mw' );
+
+ var src = mwd.body.extsrc;
// Wait for animation to complete
setTimeout( ve.bind( function () {
// Setup input text
- this.input.setValue( extsrc );
+ this.input.setValue( src );
this.input.$input.focus().select();
}, this ), 200 );
};
@@ -82,37 +101,27 @@
* @param {string} action Action that caused the window to be closed
*/
ve.ui.MWMathInspector.prototype.onClose = function ( action ) {
- var mw,
+
+ var newsrc = this.input.getValue(),
surfaceModel = this.surface.getModel();
// Parent method
ve.ui.Inspector.prototype.onClose.call( this, action );
- if ( this.node instanceof ve.ce.MWMathNode ) {
- mw = this.node.getModel().getAttribute( 'mw' );
- mw.body.extsrc = this.input.getValue();
+ // If change is called with no change it will unrender the mathnode
+ // If the input box is empty when closed it errors if change is called
in the onParseSuccess function
+ if (newsrc !== '') {
+ if (this.mathNode.getModel().getAttribute('mw').body.extsrc !==
newsrc) {
+ mwd =
this.mathNode.getModel().getAttribute( 'mw' );
+ mwd.body.extsrc = this.input.getValue();
surfaceModel.change(
ve.dm.Transaction.newFromAttributeChanges(
- surfaceModel.getDocument(),
this.node.getOuterRange().start, { 'mw': mw }
+ surfaceModel.getDocument(),
this.mathNode.getOuterRange().start, { 'mw': mwd }
)
);
- } else {
- mw = {
- 'name': 'math',
- 'attrs': {},
- 'body': {
- 'extsrc': this.input.getValue()
- }
- };
- surfaceModel.getFragment().collapseRangeToEnd().insertContent( [
- {
- 'type': 'mwMath',
- 'attributes': {
- 'mw': mw
- }
- },
- { 'type': '/mwMath' }
- ] );
+ } else {
+ this.mathNode.reRender();
+ }
}
};
--
To view, visit https://gerrit.wikimedia.org/r/78406
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I50cc1943566c782ca37899de44009dd3eb28c763
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Jiabao <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits