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' ];
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2021-04-12 09:25:22 +00:00
|
|
|
/**
|
2021-06-15 14:50:09 +00:00
|
|
|
* @return {boolean}
|
2021-04-12 09:25:22 +00:00
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.static.isLineNumbering = function () {
|
2021-09-03 08:53:46 +00:00
|
|
|
// T285660: Backspace related bug on Android browsers as of 2021
|
|
|
|
if ( /Android\b/.test( navigator.userAgent ) ) {
|
|
|
|
return false;
|
2021-04-12 09:25:22 +00:00
|
|
|
}
|
2021-09-03 08:53:46 +00:00
|
|
|
|
|
|
|
var namespaces = mw.config.get( 'wgCodeMirrorLineNumberingNamespaces' );
|
|
|
|
// Set to [] to disable everywhere, or null to enable everywhere
|
|
|
|
return !namespaces ||
|
|
|
|
namespaces.indexOf( mw.config.get( 'wgNamespaceNumber' ) ) !== -1;
|
2021-04-12 09:25:22 +00:00
|
|
|
};
|
|
|
|
|
2017-03-21 14:42:04 +00:00
|
|
|
/**
|
|
|
|
* @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 ) {
|
2021-06-15 14:50:09 +00:00
|
|
|
var action = this,
|
2017-11-30 16:42:55 +00:00
|
|
|
surface = this.surface,
|
2017-03-21 14:42:04 +00:00
|
|
|
surfaceView = surface.getView(),
|
2021-06-15 14:50:09 +00:00
|
|
|
doc = surface.getModel().getDocument();
|
2017-03-21 14:42:04 +00:00
|
|
|
|
|
|
|
if ( !surface.mirror && enable !== false ) {
|
2020-03-19 19:47:22 +00:00
|
|
|
surface.mirror = true;
|
|
|
|
mw.loader.using( [
|
|
|
|
'ext.CodeMirror.lib',
|
|
|
|
'ext.CodeMirror.mode.mediawiki',
|
|
|
|
'jquery.client'
|
|
|
|
] ).then( function () {
|
2020-07-10 10:09:23 +00:00
|
|
|
var config = mw.config.get( 'extCodeMirrorConfig' );
|
|
|
|
|
2020-03-19 19:47:22 +00:00
|
|
|
if ( !surface.mirror ) {
|
|
|
|
// Action was toggled to false since promise started
|
|
|
|
return;
|
2017-03-21 14:42:04 +00:00
|
|
|
}
|
2020-07-10 10:09:23 +00:00
|
|
|
mw.loader.using( config.pluginModules, function () {
|
|
|
|
if ( !surface.mirror ) {
|
|
|
|
// Action was toggled to false since promise started
|
|
|
|
return;
|
2020-03-19 19:47:22 +00:00
|
|
|
}
|
2021-06-15 14:50:09 +00:00
|
|
|
var tabSizeValue = surfaceView.documentView.documentNode.$element.css( 'tab-size' );
|
|
|
|
var cmOptions = {
|
2020-07-10 10:09:23 +00:00
|
|
|
value: surface.getDom(),
|
|
|
|
mwConfig: config,
|
|
|
|
readOnly: 'nocursor',
|
|
|
|
lineWrapping: true,
|
|
|
|
scrollbarStyle: 'null',
|
|
|
|
specialChars: /^$/,
|
|
|
|
viewportMargin: 5,
|
|
|
|
tabSize: tabSizeValue ? +tabSizeValue : 8,
|
|
|
|
// select mediawiki as text input mode
|
|
|
|
mode: 'text/mediawiki',
|
|
|
|
extraKeys: {
|
|
|
|
Tab: false,
|
|
|
|
'Shift-Tab': false
|
2021-01-12 10:49:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-10 14:29:08 +00:00
|
|
|
cmOptions.matchBrackets = {
|
|
|
|
highlightNonMatching: false,
|
|
|
|
maxHighlightLineLength: 10000
|
|
|
|
};
|
2021-01-12 10:49:46 +00:00
|
|
|
|
2021-04-12 09:25:22 +00:00
|
|
|
if ( ve.ui.CodeMirrorAction.static.isLineNumbering() ) {
|
|
|
|
$.extend( cmOptions, {
|
|
|
|
// Set up a special "padding" gutter to create space between the line numbers
|
|
|
|
// and page content. The first column name is a magic constant which causes
|
|
|
|
// the built-in line number gutter to appear in the desired, leftmost position.
|
|
|
|
gutters: [
|
|
|
|
'CodeMirror-linenumbers',
|
|
|
|
'CodeMirror-linenumber-padding'
|
|
|
|
],
|
|
|
|
lineNumbers: true
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-01-12 10:49:46 +00:00
|
|
|
surface.mirror = CodeMirror( surfaceView.$element[ 0 ], cmOptions );
|
2020-07-10 10:09:23 +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
|
|
|
|
surfaceView.$element.removeClass( 'mw-editfont-sans-serif mw-editfont-serif' ).addClass( 'mw-editfont-monospace' );
|
|
|
|
|
2022-06-13 11:20:24 +00:00
|
|
|
if ( mw.user.options.get( 'usecodemirror-colorblind' ) ) {
|
2022-06-10 13:49:48 +00:00
|
|
|
surfaceView.$element.addClass( 'cm-mw-colorblind-colors' );
|
2021-03-04 14:42:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 14:50:09 +00:00
|
|
|
var profile = $.client.profile();
|
|
|
|
var supportsTransparentText = 'WebkitTextFillColor' in document.body.style &&
|
2020-07-10 10:09:23 +00:00
|
|
|
// Disable on Firefox+OSX (T175223)
|
|
|
|
!( profile.layout === 'gecko' && profile.platform === 'mac' );
|
|
|
|
|
|
|
|
surfaceView.$documentNode.addClass(
|
|
|
|
supportsTransparentText ?
|
|
|
|
've-ce-documentNode-codeEditor-webkit-hide' :
|
|
|
|
've-ce-documentNode-codeEditor-hide'
|
|
|
|
);
|
|
|
|
|
2020-08-06 12:05:42 +00:00
|
|
|
if ( cmOptions.lineNumbers ) {
|
|
|
|
// Transfer gutter width to VE overlay.
|
2021-06-15 14:50:09 +00:00
|
|
|
var updateGutter = function ( cmDisplay ) {
|
2020-08-06 12:05:42 +00:00
|
|
|
surfaceView.$documentNode.css( 'margin-left', cmDisplay.gutters.offsetWidth );
|
|
|
|
};
|
|
|
|
CodeMirror.on( surface.mirror.display, 'updateGutter', updateGutter );
|
|
|
|
updateGutter( surface.mirror.display );
|
|
|
|
}
|
|
|
|
|
2020-07-10 10:09:23 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
// As the action is regenerated each time, we need to store bound listeners
|
|
|
|
// in the mirror for later disconnection.
|
|
|
|
surface.mirror.veTransactionListener = action.onDocumentPrecommit.bind( action );
|
|
|
|
surface.mirror.veLangChangeListener = action.onLangChange.bind( action );
|
2020-12-18 16:57:14 +00:00
|
|
|
surface.mirror.veSelectListener = action.onSelect.bind( action );
|
2020-07-10 10:09:23 +00:00
|
|
|
|
|
|
|
doc.on( 'precommit', surface.mirror.veTransactionListener );
|
|
|
|
surfaceView.getDocument().on( 'langChange', surface.mirror.veLangChangeListener );
|
2020-12-18 16:57:14 +00:00
|
|
|
surface.getModel().on( 'select', surface.mirror.veSelectListener );
|
2020-07-10 10:09:23 +00:00
|
|
|
|
|
|
|
action.onLangChange();
|
|
|
|
|
|
|
|
ve.init.target.once( 'surfaceReady', function () {
|
|
|
|
if ( surface.mirror ) {
|
|
|
|
surface.mirror.refresh();
|
|
|
|
}
|
|
|
|
} );
|
2020-03-19 19:47:22 +00:00
|
|
|
} );
|
2017-03-21 14:42:04 +00:00
|
|
|
} );
|
|
|
|
} else if ( surface.mirror && enable !== true ) {
|
2020-03-19 19:47:22 +00:00
|
|
|
if ( surface.mirror !== true ) {
|
|
|
|
doc.off( 'precommit', surface.mirror.veTransactionListener );
|
|
|
|
surfaceView.getDocument().off( 'langChange', surface.mirror.veLangChangeListener );
|
2020-12-18 16:57:14 +00:00
|
|
|
surface.getModel().off( 'select', surface.mirror.veSelectListener );
|
2017-03-21 14:42:04 +00:00
|
|
|
|
2020-03-19 19:47:22 +00:00
|
|
|
// Restore edit-font
|
2020-06-12 21:51:38 +00:00
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2020-03-19 19:47:22 +00:00
|
|
|
surfaceView.$element.removeClass( 'mw-editfont-monospace' ).addClass( 'mw-editfont-' + mw.user.options.get( 'editfont' ) );
|
2017-07-20 13:59:37 +00:00
|
|
|
|
2020-03-19 19:47:22 +00:00
|
|
|
surfaceView.$documentNode.removeClass(
|
|
|
|
've-ce-documentNode-codeEditor-webkit-hide ve-ce-documentNode-codeEditor-hide'
|
|
|
|
);
|
2020-08-06 12:05:42 +00:00
|
|
|
// Reset gutter.
|
|
|
|
surfaceView.$documentNode.css( 'margin-left', '' );
|
2017-03-21 14:42:04 +00:00
|
|
|
|
2021-06-15 14:50:09 +00:00
|
|
|
var mirrorElement = surface.mirror.getWrapperElement();
|
2020-03-19 19:47:22 +00:00
|
|
|
mirrorElement.parentNode.removeChild( mirrorElement );
|
|
|
|
}
|
2017-03-21 14:42:04 +00:00
|
|
|
|
|
|
|
surface.mirror = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-12-18 16:57:14 +00:00
|
|
|
/**
|
|
|
|
* Handle select events from the surface model
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Selection} selection
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.prototype.onSelect = function ( selection ) {
|
2021-01-05 14:26:14 +00:00
|
|
|
var range = selection.getCoveringRange();
|
2020-12-18 16:57:14 +00:00
|
|
|
|
2021-01-05 14:26:14 +00:00
|
|
|
// Do not re-trigger bracket matching as long as something is selected
|
|
|
|
if ( !range || !range.isCollapsed() ) {
|
2020-12-18 16:57:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:57:32 +00:00
|
|
|
this.surface.mirror.setSelection( this.getPosFromOffset( range.from ) );
|
2020-12-18 16:57:14 +00:00
|
|
|
};
|
|
|
|
|
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,
|
2023-07-07 12:48:25 +00:00
|
|
|
doc = surface.getView().getDocument(),
|
|
|
|
dir = doc.getDir(), lang = doc.getLang();
|
2017-08-13 15:10:54 +00:00
|
|
|
|
2018-04-16 15:53:29 +00:00
|
|
|
surface.mirror.setOption( 'direction', dir );
|
2023-07-07 12:48:25 +00:00
|
|
|
|
|
|
|
// Set the wrapper to the appropriate language (T341342)
|
|
|
|
surface.mirror.getWrapperElement().setAttribute( 'lang', lang );
|
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.
|
|
|
|
*
|
2020-12-18 16:57:14 +00:00
|
|
|
* @param {ve.dm.Transaction} tx
|
2017-07-06 18:25:57 +00:00
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
|
2021-06-15 14:50:09 +00:00
|
|
|
var offset = 0,
|
2017-07-06 18:25:57 +00:00
|
|
|
replacements = [],
|
2021-01-06 14:57:32 +00:00
|
|
|
action = this,
|
|
|
|
store = this.surface.getModel().getDocument().getStore(),
|
2019-11-14 21:53:53 +00:00
|
|
|
mirror = this.surface.mirror;
|
2017-07-06 18:25:57 +00:00
|
|
|
|
|
|
|
tx.operations.forEach( function ( op ) {
|
|
|
|
if ( op.type === 'retain' ) {
|
|
|
|
offset += op.length;
|
|
|
|
} else if ( op.type === 'replace' ) {
|
|
|
|
replacements.push( {
|
2021-01-06 14:57:32 +00:00
|
|
|
start: action.getPosFromOffset( offset ),
|
2017-07-06 18:25:57 +00:00
|
|
|
// Don't bother recalculating end offset if not a removal, replaceRange works with just one arg
|
2021-01-06 14:57:32 +00:00
|
|
|
end: op.remove.length ? action.getPosFromOffset( offset + op.remove.length ) : undefined,
|
2019-11-14 21:53:53 +00:00
|
|
|
data: 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
|
2021-06-15 14:50:09 +00:00
|
|
|
for ( var i = replacements.length - 1; i >= 0; i-- ) {
|
2018-02-28 12:34:28 +00:00
|
|
|
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();
|
2021-12-09 09:06:41 +00:00
|
|
|
this.lastHeight = mirror.display.sizer.style.minHeight;
|
2018-02-28 12:34:28 +00:00
|
|
|
}
|
2017-03-21 14:42:04 +00:00
|
|
|
};
|
|
|
|
|
2021-01-06 14:57:32 +00:00
|
|
|
/**
|
|
|
|
* Convert a VE offset to a 2D CodeMirror position
|
|
|
|
*
|
|
|
|
* @param {number} veOffset VE linear model offset
|
|
|
|
* @return {Object} Code mirror position, containing 'line' and 'ch' numbers
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorAction.prototype.getPosFromOffset = function ( veOffset ) {
|
|
|
|
return this.surface.mirror.posFromIndex(
|
|
|
|
this.surface.getModel().getSourceOffsetFromOffset( veOffset )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-03-21 14:42:04 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.actionFactory.register( ve.ui.CodeMirrorAction );
|