mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CodeMirror
synced 2024-12-12 14:25:06 +00:00
7d3482f89e
CodeMirror 6 requires the use of NPM, but we can still bundle all CM packages into one file, and then everything else (i.e. our code) is managed by ResourceLoader as per usual. This makes contribution considerably easier as we no longer need a build step for each change. CM5 files are now under resources/legacy, and the CM6 files are moved to the root of the resources/ directory. Only one file, codemirror.bundle.js, is managed by Rollup, while everything else is RL. The Rollup output for now is put under resources/lib/ alongside the CM5 upstream files. This patch is *mostly* renames of files, along with changing ECMAScript Module (ESM) syntax into the CommonJS style that ResourceLoader prefers. We also remove more modern JS syntax (i.e. private class methods) that we were able to use before because we had a build step with Babel. This patch should effectively make no user-facing changes, or to the ResourceLoader modules we offer in Extension:CodeMirror. Finally, bump version in extension.json to 6, to match the upstream lib, and add Bhsd as an author :-) Bug: T368053 Change-Id: Ie258e49f5df8db23a7344ac3c4c9300aaa991042
103 lines
2.8 KiB
JavaScript
103 lines
2.8 KiB
JavaScript
/**
|
|
* VisualEditor UserInterface CodeMirror tool.
|
|
*
|
|
* @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 );
|
|
|
|
this.extCodeMirror = require( 'ext.CodeMirror' );
|
|
|
|
// 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' );
|
|
ve.ui.CodeMirrorTool.static.icon = 'highlight';
|
|
ve.ui.CodeMirrorTool.static.group = 'utility';
|
|
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 );
|
|
|
|
const useCodeMirror = !!this.toolbar.surface.mirror;
|
|
this.setActive( useCodeMirror );
|
|
|
|
new mw.Api().saveOption( 'usecodemirror', useCodeMirror ? 1 : 0 );
|
|
mw.user.options.set( 'usecodemirror', useCodeMirror ? 1 : 0 );
|
|
|
|
this.extCodeMirror.logUsage( {
|
|
editor: 'wikitext-2017',
|
|
enabled: useCodeMirror,
|
|
toggled: true,
|
|
// eslint-disable-next-line camelcase
|
|
edit_start_ts_ms: ( this.toolbar.target.startTimeStamp * 1000 ) || 0
|
|
} );
|
|
};
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
ve.ui.CodeMirrorTool.prototype.onSurfaceChange = function ( oldSurface, newSurface ) {
|
|
const isDisabled = newSurface.getMode() !== 'source';
|
|
|
|
this.setDisabled( isDisabled );
|
|
if ( !isDisabled ) {
|
|
const command = this.getCommand();
|
|
const surface = this.toolbar.getSurface();
|
|
const useCodeMirror = mw.user.options.get( 'usecodemirror' ) > 0;
|
|
command.execute( surface, [ useCodeMirror ] );
|
|
this.setActive( useCodeMirror );
|
|
|
|
if ( this.toolbar.target.startTimeStamp ) {
|
|
this.extCodeMirror.logUsage( {
|
|
editor: 'wikitext-2017',
|
|
enabled: useCodeMirror,
|
|
toggled: false,
|
|
// eslint-disable-next-line camelcase
|
|
edit_start_ts_ms: ( this.toolbar.target.startTimeStamp * 1000 ) || 0
|
|
} );
|
|
}
|
|
}
|
|
};
|
|
|
|
ve.ui.CodeMirrorTool.prototype.onUpdateState = function () {};
|
|
|
|
{
|
|
// 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'
|
|
)
|
|
);
|
|
}
|
|
}
|