mediawiki-extensions-CodeMi.../resources/modules/ve-cm/ve.ui.CodeMirrorTool.js
MusikAnimal 293a15725d ext.CodeMirror.visualEditor: check language dir of html for new pages
New pages do not have body content yet, so the LTR check added with
If3825d6e54 failed. This commit adds the document root (<html> element)
as the fallback, so CodeMirror can be used on new pages.

This is just a quick fix. CM6 for VE 2017 wikitext editor is almost
done, and that will come with RTL support (T357482).

Bug: T366201
Bug: T363752
Follow-Up: If3825d6e5467d2bcff2d83e838081bf041243920
Change-Id: I9d4a4b817ac2462396c159ceae6f1510c0fae64d
2024-05-30 23:48:05 -04:00

101 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 );
var 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 ) {
var isDisabled = newSurface.getMode() !== 'source';
this.setDisabled( isDisabled );
if ( !isDisabled ) {
var command = this.getCommand();
var surface = this.toolbar.getSurface();
var 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
var 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'
)
);
}