From ed2f9944ea878a8f86b7e9de0249843891973248 Mon Sep 17 00:00:00 2001 From: Adam Wight Date: Mon, 12 Apr 2021 11:25:22 +0200 Subject: [PATCH] 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 --- extension.json | 5 +++ includes/CodeMirrorHooks.php | 4 +++ resources/ext.CodeMirror.js | 17 ++++++++- .../modules/ve-cm/ve.ui.CodeMirrorAction.js | 36 ++++++++++++++----- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/extension.json b/extension.json index 292ebd54..d4b6048f 100644 --- a/extension.json +++ b/extension.json @@ -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": { diff --git a/includes/CodeMirrorHooks.php b/includes/CodeMirrorHooks.php index 522c0cc3..4f428b23 100644 --- a/includes/CodeMirrorHooks.php +++ b/includes/CodeMirrorHooks.php @@ -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' ); } /** diff --git a/resources/ext.CodeMirror.js b/resources/ext.CodeMirror.js index db7f9478..ac04b37c 100644 --- a/resources/ext.CodeMirror.js +++ b/resources/ext.CodeMirror.js @@ -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', diff --git a/resources/modules/ve-cm/ve.ui.CodeMirrorAction.js b/resources/modules/ve-cm/ve.ui.CodeMirrorAction.js index 68d9080b..efad500f 100644 --- a/resources/modules/ve-cm/ve.ui.CodeMirrorAction.js +++ b/resources/modules/ve-cm/ve.ui.CodeMirrorAction.js @@ -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)