Enable line numbering only on configured namespaces

Introduces a new config variable `CodeMirrorLineNumberingNamespaces`
that can restrict line numbering to only appear for specified
namespaces.  Setting to null enables everywhere.

This takes some liberties with the `lib` module, turning it into a
container for shared functionality.  This can be pursued in later
work, by cleaning up duplicated code in this repo.

FIXME: failed to deduplicate the code for now.

Bug: T267911
Change-Id: Ida2b33eef38edc57d29756ec472c6f2c83bd7b11
This commit is contained in:
Adam Wight 2021-04-12 11:25:22 +02:00
parent 6937d0e51c
commit ed2f9944ea
4 changed files with 53 additions and 9 deletions

View file

@ -24,6 +24,11 @@
"value": false,
"description": "Temporary feature flag for accessibility colors.",
"public": true
},
"CodeMirrorLineNumberingNamespaces": {
"value": [ 10 ],
"description": "List of namespace IDs where line numbering should be enabled, or `null` to enable for all namespaces. Defaults to the `Template` namespace.",
"public": true
}
},
"MessagesDirs": {

View file

@ -49,6 +49,8 @@ class CodeMirrorHooks {
/**
* Hook handler for enabling bracket matching.
*
* TODO: restrict to pages where codemirror might be enabled.
*
* @param array &$vars Array of variables to be added into the output of the startup module
*/
public static function onResourceLoaderGetConfigVars( array &$vars ) {
@ -61,6 +63,8 @@ class CodeMirrorHooks {
->getCookie( '-codemirror-bracket-matching-test', 'mw' );
$vars['wgCodeMirrorAccessibilityColors'] = $config->get( 'CodeMirrorAccessibilityColors' );
$vars['wgCodeMirrorLineNumberingNamespaces'] = $config->get( 'CodeMirrorLineNumberingNamespaces' );
}
/**

View file

@ -87,6 +87,21 @@
mw.user.options.set( 'usecodemirror', prefValue ? 1 : 0 );
}
/**
* TODO: remove once line numbering is fully deployed. (TBD: task)
*
* @return bool
*/
function isLineNumbering() {
var lineNumberingNamespaces = mw.config.get( 'wgCodeMirrorLineNumberingNamespaces' );
if ( lineNumberingNamespaces === null ) {
return true;
} else {
return lineNumberingNamespaces.indexOf( mw.config.get( 'wgNamespaceNumber' ) ) !== -1;
}
}
/**
* Replaces the default textarea with CodeMirror
*/
@ -113,7 +128,7 @@
mwConfig: config,
// styleActiveLine: true, // disabled since Bug: T162204, maybe should be optional
lineWrapping: true,
lineNumbers: true,
lineNumbers: isLineNumbering(),
readOnly: $textbox1[ 0 ].readOnly,
// select mediawiki as text input mode
mode: 'text/mediawiki',

View file

@ -32,6 +32,21 @@ ve.ui.CodeMirrorAction.static.methods = [ 'toggle' ];
/* Methods */
/**
* TODO: remove once line numbering is fully deployed. (TBD: task)
*
* @return bool
*/
ve.ui.CodeMirrorAction.static.isLineNumbering = function () {
var lineNumberingNamespaces = mw.config.get( 'wgCodeMirrorLineNumberingNamespaces' );
if ( lineNumberingNamespaces === null ) {
return true;
} else {
return lineNumberingNamespaces.indexOf( mw.config.get( 'wgNamespaceNumber' ) ) !== -1;
}
};
/**
* @method
* @param {boolean} [enable] State to force toggle to, inverts current state if undefined
@ -71,15 +86,7 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
mwConfig: config,
readOnly: 'nocursor',
lineWrapping: true,
// 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'
],
scrollbarStyle: 'null',
lineNumbers: true,
specialChars: /^$/,
viewportMargin: 5,
tabSize: tabSizeValue ? +tabSizeValue : 8,
@ -98,6 +105,19 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
};
}
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
} );
}
surface.mirror = CodeMirror( surfaceView.$element[ 0 ], cmOptions );
// The VE/CM overlay technique only works with monospace fonts (as we use width-changing bold as a highlight)