2017-03-21 14:42:04 +00:00
|
|
|
/**
|
2023-12-18 17:01:58 +00:00
|
|
|
* VisualEditor UserInterface CodeMirror tool.
|
2017-03-21 14:42:04 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @extends ve.ui.Tool
|
|
|
|
* @constructor
|
|
|
|
* @param {OO.ui.ToolGroup} toolGroup
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorTool = function VeUiCodeMirrorTool() {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.CodeMirrorTool.super.apply( this, arguments );
|
|
|
|
|
2023-08-17 04:58:59 +00:00
|
|
|
this.extCodeMirror = require( 'ext.CodeMirror' );
|
|
|
|
|
2017-03-21 14:42:04 +00:00
|
|
|
// Events
|
|
|
|
this.toolbar.connect( this, { surfaceChange: 'onSurfaceChange' } );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.CodeMirrorTool, ve.ui.Tool );
|
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
ve.ui.CodeMirrorTool.static.name = 'codeMirror';
|
|
|
|
ve.ui.CodeMirrorTool.static.autoAddToCatchall = false;
|
|
|
|
ve.ui.CodeMirrorTool.static.title = OO.ui.deferMsg( 'codemirror-toggle-label' );
|
2017-08-23 17:22:23 +00:00
|
|
|
ve.ui.CodeMirrorTool.static.icon = 'highlight';
|
2023-06-21 16:09:44 +00:00
|
|
|
ve.ui.CodeMirrorTool.static.group = 'utility';
|
2017-03-21 14:42:04 +00:00
|
|
|
ve.ui.CodeMirrorTool.static.commandName = 'codeMirror';
|
|
|
|
ve.ui.CodeMirrorTool.static.deactivateOnSelect = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorTool.prototype.onSelect = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.CodeMirrorTool.super.prototype.onSelect.apply( this, arguments );
|
|
|
|
|
2024-06-14 11:53:32 +00:00
|
|
|
const useCodeMirror = !!this.toolbar.surface.mirror;
|
2017-03-21 14:42:04 +00:00
|
|
|
this.setActive( useCodeMirror );
|
|
|
|
|
|
|
|
new mw.Api().saveOption( 'usecodemirror', useCodeMirror ? 1 : 0 );
|
|
|
|
mw.user.options.set( 'usecodemirror', useCodeMirror ? 1 : 0 );
|
2020-11-19 16:20:25 +00:00
|
|
|
|
2023-08-17 04:58:59 +00:00
|
|
|
this.extCodeMirror.logUsage( {
|
2020-11-19 16:20:25 +00:00
|
|
|
editor: 'wikitext-2017',
|
|
|
|
enabled: useCodeMirror,
|
|
|
|
toggled: true,
|
2021-01-14 13:10:57 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
2020-11-19 16:20:25 +00:00
|
|
|
edit_start_ts_ms: ( this.toolbar.target.startTimeStamp * 1000 ) || 0
|
|
|
|
} );
|
2017-03-21 14:42:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.CodeMirrorTool.prototype.onSurfaceChange = function ( oldSurface, newSurface ) {
|
2024-06-14 11:53:32 +00:00
|
|
|
const isDisabled = newSurface.getMode() !== 'source';
|
2017-03-21 14:42:04 +00:00
|
|
|
|
|
|
|
this.setDisabled( isDisabled );
|
|
|
|
if ( !isDisabled ) {
|
2024-06-14 11:53:32 +00:00
|
|
|
const command = this.getCommand();
|
|
|
|
const surface = this.toolbar.getSurface();
|
|
|
|
const useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0;
|
2017-03-21 14:42:04 +00:00
|
|
|
command.execute( surface, [ useCodeMirror ] );
|
|
|
|
this.setActive( useCodeMirror );
|
2020-11-19 16:20:25 +00:00
|
|
|
|
|
|
|
if ( this.toolbar.target.startTimeStamp ) {
|
2023-08-17 04:58:59 +00:00
|
|
|
this.extCodeMirror.logUsage( {
|
2020-11-19 16:20:25 +00:00
|
|
|
editor: 'wikitext-2017',
|
|
|
|
enabled: useCodeMirror,
|
|
|
|
toggled: false,
|
2021-01-14 13:10:57 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
2020-11-19 16:20:25 +00:00
|
|
|
edit_start_ts_ms: ( this.toolbar.target.startTimeStamp * 1000 ) || 0
|
|
|
|
} );
|
|
|
|
}
|
2017-03-21 14:42:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.CodeMirrorTool.prototype.onUpdateState = function () {};
|
|
|
|
|
2024-06-14 11:53:32 +00:00
|
|
|
{
|
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
|
|
|
const contentDir = $( '.mw-body-content .mw-parser-output' ).attr( 'dir' ) ||
|
|
|
|
// New pages will use wgPageContentLanguage which is set on the html element.
|
|
|
|
document.documentElement.dir;
|
|
|
|
|
|
|
|
if ( contentDir === 'ltr' ) {
|
|
|
|
/* Registration */
|
|
|
|
ve.ui.toolFactory.register( ve.ui.CodeMirrorTool );
|
|
|
|
|
|
|
|
/* Command */
|
|
|
|
ve.ui.commandRegistry.register(
|
|
|
|
new ve.ui.Command(
|
|
|
|
'codeMirror', 'codeMirror', 'toggle'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-04-29 19:26:11 +00:00
|
|
|
}
|