2017-03-21 14:42:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface CodeMirrorAction class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2017 VisualEditor Team and others; see http://ve.mit-license.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CodeMirror action
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.Action
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.Surface} surface Surface to act on
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction = function VeUiCodeMirrorAction() {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.CodeMirrorAction.super.apply( this, arguments );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.CodeMirrorAction, ve.ui.Action );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ui.CodeMirrorAction.static.name = 'codeMirror';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.static.methods = [ 'toggle' ];
|
|
|
|
|
2018-02-19 10:23:44 +00:00
|
|
|
/* Static methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make whitespace replacements to match ve.ce.TextNode functionality
|
|
|
|
*
|
|
|
|
* @param {string} text Text
|
|
|
|
* @return {string} Replaced text
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.static.fixWhitespace = ( function () {
|
|
|
|
var ws, symbol, pattern,
|
|
|
|
whitespaceHtmlCharacters = ve.ce.TextNode.static.whitespaceHtmlCharacters,
|
|
|
|
replacements = [];
|
|
|
|
|
|
|
|
// Convert whitespaceHtmlCharacters object into regex/symbol
|
|
|
|
// pairs and cache result locally.
|
|
|
|
for ( ws in whitespaceHtmlCharacters ) {
|
|
|
|
// We actally render newlines
|
|
|
|
if ( ws !== '\n' ) {
|
|
|
|
symbol = whitespaceHtmlCharacters[ ws ];
|
|
|
|
pattern = new RegExp( ws, 'g' );
|
|
|
|
replacements.push( [ pattern, symbol ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return function fixWhitespace( text ) {
|
|
|
|
replacements.forEach( function ( replacement ) {
|
|
|
|
text = text.replace( replacement[ 0 ], replacement[ 1 ] );
|
|
|
|
} );
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
}() );
|
|
|
|
|
2017-03-21 14:42:04 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
* @param {boolean} [enable] State to force toggle to, inverts current state if undefined
|
|
|
|
* @return {boolean} Action was executed
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
|
2018-03-27 20:46:00 +00:00
|
|
|
var profile, supportsTransparentText, mirrorElement,
|
2017-11-30 16:42:55 +00:00
|
|
|
surface = this.surface,
|
2017-03-21 14:42:04 +00:00
|
|
|
surfaceView = surface.getView(),
|
|
|
|
doc = surface.getModel().getDocument();
|
|
|
|
|
|
|
|
if ( !surface.mirror && enable !== false ) {
|
|
|
|
surface.mirror = CodeMirror( surfaceView.$element[ 0 ], {
|
2018-02-19 10:23:44 +00:00
|
|
|
value: this.constructor.static.fixWhitespace( surface.getDom() ),
|
2017-03-21 14:42:04 +00:00
|
|
|
mwConfig: mw.config.get( 'extCodeMirrorConfig' ),
|
2017-07-10 16:40:51 +00:00
|
|
|
readOnly: 'nocursor',
|
2017-03-21 14:42:04 +00:00
|
|
|
lineWrapping: true,
|
|
|
|
scrollbarStyle: 'null',
|
2017-07-12 14:23:49 +00:00
|
|
|
specialChars: /^$/,
|
2017-03-21 14:42:04 +00:00
|
|
|
viewportMargin: 5,
|
|
|
|
// select mediawiki as text input mode
|
|
|
|
mode: 'text/mediawiki',
|
|
|
|
extraKeys: {
|
2018-06-17 15:50:12 +00:00
|
|
|
Tab: false,
|
|
|
|
'Shift-Tab': false
|
2017-03-21 14:42:04 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2017-07-20 13:59:37 +00:00
|
|
|
// The VE/CM overlay technique only works with monospace fonts (as we use width-changing bold as a highlight)
|
|
|
|
// so revert any editfont user preference
|
2017-08-25 17:45:44 +00:00
|
|
|
surfaceView.$element.removeClass( 'mw-editfont-sans-serif mw-editfont-serif' ).addClass( 'mw-editfont-monospace' );
|
2017-07-20 13:59:37 +00:00
|
|
|
|
2017-11-30 16:42:55 +00:00
|
|
|
profile = $.client.profile();
|
|
|
|
supportsTransparentText = 'WebkitTextFillColor' in document.body.style &&
|
|
|
|
// Disable on Firefox+OSX (T175223)
|
|
|
|
!( profile.layout === 'gecko' && profile.platform === 'mac' );
|
|
|
|
|
2017-03-21 14:42:04 +00:00
|
|
|
surfaceView.$documentNode.addClass(
|
2017-11-30 16:42:55 +00:00
|
|
|
supportsTransparentText ?
|
2017-07-20 13:34:40 +00:00
|
|
|
've-ce-documentNode-codeEditor-webkit-hide' :
|
2017-11-30 16:35:57 +00:00
|
|
|
've-ce-documentNode-codeEditor-hide'
|
2017-03-21 14:42:04 +00:00
|
|
|
);
|
|
|
|
|
2017-08-13 15:10:54 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
// As the action is regenerated each time, we need to store bound listeners
|
2017-03-21 14:42:04 +00:00
|
|
|
// in the mirror for later disconnection.
|
2017-07-06 18:25:57 +00:00
|
|
|
surface.mirror.veTransactionListener = this.onDocumentPrecommit.bind( this );
|
2017-08-13 15:10:54 +00:00
|
|
|
surface.mirror.veLangChangeListener = this.onLangChange.bind( this );
|
2017-03-21 14:42:04 +00:00
|
|
|
|
2017-07-06 18:25:57 +00:00
|
|
|
doc.on( 'precommit', surface.mirror.veTransactionListener );
|
2017-08-13 15:10:54 +00:00
|
|
|
surfaceView.getDocument().on( 'langChange', surface.mirror.veLangChangeListener );
|
|
|
|
|
|
|
|
this.onLangChange();
|
|
|
|
|
2018-02-25 17:58:17 +00:00
|
|
|
ve.init.target.once( 'surfaceReady', function () {
|
|
|
|
if ( surface.mirror ) {
|
|
|
|
surface.mirror.refresh();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2017-03-21 14:42:04 +00:00
|
|
|
} else if ( surface.mirror && enable !== true ) {
|
2017-07-06 18:25:57 +00:00
|
|
|
doc.off( 'precommit', surface.mirror.veTransactionListener );
|
2017-08-13 15:10:54 +00:00
|
|
|
surfaceView.getDocument().off( 'langChange', surface.mirror.veLangChangeListener );
|
2017-03-21 14:42:04 +00:00
|
|
|
|
2017-07-20 13:59:37 +00:00
|
|
|
// Restore edit-font
|
|
|
|
surfaceView.$element.removeClass( 'mw-editfont-monospace' ).addClass( 'mw-editfont-' + mw.user.options.get( 'editfont' ) );
|
|
|
|
|
2017-03-21 14:42:04 +00:00
|
|
|
surfaceView.$documentNode.removeClass(
|
2017-11-30 16:35:57 +00:00
|
|
|
've-ce-documentNode-codeEditor-webkit-hide ve-ce-documentNode-codeEditor-hide'
|
2017-03-21 14:42:04 +00:00
|
|
|
);
|
|
|
|
|
2018-03-27 20:46:00 +00:00
|
|
|
mirrorElement = surface.mirror.getWrapperElement();
|
|
|
|
mirrorElement.parentNode.removeChild( mirrorElement );
|
2017-03-21 14:42:04 +00:00
|
|
|
|
|
|
|
surface.mirror = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2017-08-13 15:10:54 +00:00
|
|
|
/**
|
|
|
|
* Handle langChange events from the document view
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.prototype.onLangChange = function () {
|
|
|
|
var surface = this.surface,
|
|
|
|
dir = surface.getView().getDocument().getDir();
|
|
|
|
|
2018-04-16 15:53:29 +00:00
|
|
|
surface.mirror.setOption( 'direction', dir );
|
2017-08-13 15:10:54 +00:00
|
|
|
};
|
|
|
|
|
2017-07-06 18:25:57 +00:00
|
|
|
/**
|
|
|
|
* Handle precommit events from the document.
|
|
|
|
*
|
|
|
|
* The document is still in it's 'old' state before the transaction
|
|
|
|
* has been applied at this point.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Transaction} tx [description]
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
|
2018-02-28 12:34:28 +00:00
|
|
|
var i,
|
|
|
|
offset = 0,
|
2017-07-06 18:25:57 +00:00
|
|
|
replacements = [],
|
|
|
|
linearData = this.surface.getModel().getDocument().data,
|
|
|
|
store = linearData.getStore(),
|
2018-02-19 10:23:44 +00:00
|
|
|
mirror = this.surface.mirror,
|
|
|
|
fixWhitespace = this.constructor.static.fixWhitespace;
|
2017-07-06 18:25:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a VE offset to a 2D CodeMirror position
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Number} veOffset VE linear model offset
|
|
|
|
* @return {Object} Code mirror position, containing 'line' and 'ch'
|
|
|
|
*/
|
|
|
|
function convertOffset( veOffset ) {
|
|
|
|
var cmOffset = linearData.getSourceText( new ve.Range( 0, veOffset ) ).length;
|
|
|
|
return mirror.posFromIndex( cmOffset );
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.operations.forEach( function ( op ) {
|
|
|
|
if ( op.type === 'retain' ) {
|
|
|
|
offset += op.length;
|
|
|
|
} else if ( op.type === 'replace' ) {
|
|
|
|
replacements.push( {
|
|
|
|
start: convertOffset( offset ),
|
|
|
|
// Don't bother recalculating end offset if not a removal, replaceRange works with just one arg
|
|
|
|
end: op.remove.length ? convertOffset( offset + op.remove.length ) : undefined,
|
2018-02-19 10:23:44 +00:00
|
|
|
data: fixWhitespace( new ve.dm.ElementLinearData( store, op.insert ).getSourceText() )
|
2017-07-06 18:25:57 +00:00
|
|
|
} );
|
|
|
|
offset += op.remove.length;
|
2017-03-21 14:42:04 +00:00
|
|
|
}
|
2017-07-06 18:25:57 +00:00
|
|
|
} );
|
|
|
|
|
2018-02-28 12:34:28 +00:00
|
|
|
// Apply replacements in reverse to avoid having to shift offsets
|
|
|
|
for ( i = replacements.length - 1; i >= 0; i-- ) {
|
|
|
|
mirror.replaceRange(
|
|
|
|
replacements[ i ].data,
|
|
|
|
replacements[ i ].start,
|
|
|
|
replacements[ i ].end
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: The absolutely positioned CodeMirror doesn't calculate the viewport
|
|
|
|
// correctly when expanding from less than the viewport height. (T185184)
|
|
|
|
if ( mirror.display.sizer.style.minHeight !== this.lastHeight ) {
|
|
|
|
mirror.refresh();
|
|
|
|
}
|
|
|
|
this.lastHeight = mirror.display.sizer.style.minHeight;
|
2017-03-21 14:42:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.actionFactory.register( ve.ui.CodeMirrorAction );
|