2017-08-04 00:48:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2023-05-05 06:10:57 +00:00
|
|
|
* ResourceLoader callback for ext.CodeMirror.data module
|
2017-08-04 00:48:59 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2022-02-06 15:13:05 +00:00
|
|
|
namespace MediaWiki\Extension\CodeMirror;
|
|
|
|
|
2018-08-04 22:29:24 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-10-19 21:29:39 +00:00
|
|
|
use MediaWiki\Registration\ExtensionRegistry;
|
2022-05-20 02:11:31 +00:00
|
|
|
use MediaWiki\ResourceLoader as RL;
|
|
|
|
use MediaWiki\ResourceLoader\ResourceLoader;
|
2018-08-04 22:29:24 +00:00
|
|
|
|
2017-08-04 00:48:59 +00:00
|
|
|
/**
|
2023-05-05 06:10:57 +00:00
|
|
|
* ResourceLoader callback for ext.CodeMirror.data
|
2017-08-04 00:48:59 +00:00
|
|
|
*/
|
2023-05-05 06:10:57 +00:00
|
|
|
class DataScript {
|
2017-08-04 00:48:59 +00:00
|
|
|
/**
|
2023-05-05 06:10:57 +00:00
|
|
|
* @param RL\Context $context
|
|
|
|
* @return string
|
2017-08-04 00:48:59 +00:00
|
|
|
*/
|
2023-05-05 06:10:57 +00:00
|
|
|
public static function makeScript( RL\Context $context ) {
|
2017-08-04 00:48:59 +00:00
|
|
|
return ResourceLoader::makeConfigSetScript(
|
2023-05-05 06:10:57 +00:00
|
|
|
[ 'extCodeMirrorConfig' => self::getFrontendConfiguraton() ]
|
|
|
|
);
|
2017-08-04 00:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of variables for CodeMirror to work (tags and so on)
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-05 06:10:57 +00:00
|
|
|
private static function getFrontendConfiguraton() {
|
2017-08-04 00:48:59 +00:00
|
|
|
// Use the content language, not the user language. (See T170130.)
|
2018-08-04 22:29:24 +00:00
|
|
|
$lang = MediaWikiServices::getInstance()->getContentLanguage();
|
2017-08-04 00:48:59 +00:00
|
|
|
$registry = ExtensionRegistry::getInstance();
|
2018-08-05 12:43:17 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
2024-03-11 18:10:08 +00:00
|
|
|
$mwConfig = MediaWikiServices::getInstance()->getMainConfig();
|
2017-08-04 00:48:59 +00:00
|
|
|
|
2022-02-17 18:28:30 +00:00
|
|
|
$tagModes = $registry->getAttribute( 'CodeMirrorTagModes' );
|
|
|
|
$tagNames = array_merge( $parser->getTags(), array_keys( $tagModes ) );
|
|
|
|
|
2017-08-04 00:48:59 +00:00
|
|
|
// initialize configuration
|
|
|
|
$config = [
|
2024-06-08 03:10:11 +00:00
|
|
|
'useV6' => $mwConfig->get( 'CodeMirrorV6' ),
|
CodeMirrorPreferences: add panel to tweak prefs with the editor open
This is toggled by pressing Mod-Shift-, (or Command-Shift-, on MacOS),
which then puts focus on the preferences panel. It can be closed with
the Escape key, just like other CM panels.
The CodeMirror class comes with these extension which can be toggled in
preferences:
* Bracket matching
* Line numbering
* Line wrapping
* Highlight the active line
* Show special characters
Only bracket matching, line numbering, and line wrapping are available
in the 2017 editor.
The bidi isolation and template folding extensions are registered in
CodeMirrorModeMediaWiki as they are MW-specific. CodeMirrorPreferences'
new registerExtension() method allows any consumer of CodeMirror to add
any arbitrary extensions to the preferences panel. This is expected to
be called *after* CodeMirror has finished initializing. The
'ext.CodeMirror.ready' hook now passes the CodeMirror instance to
accommodate this.
The preferences are stored as a single user option in the database,
called 'codemirror-preferences'. The defaults can be configured with the
$wgCodeMirrorDefaultPreferences configuration setting. The
sysadmin-facing values are the familiar boolean, but since CodeMirror is
widely used, we make extra efforts to reduce the storage footprint (see
T54777). This includes only storing preferences that differ from the
defaults, and using binary representation instead of boolean values,
since the user option is stored as a string.
For now, all preferences are ignored in the 2017 editor. In a future
patch, we may add some as toggleable Tools in the VE toolbar.
Other changes:
* Refactor CSS to use a .darkmode() mixin
* Add a method to create a CSS-only fieldset in CodeMirrorPanel
* Fix Jest tests now that there are more calls to mw.user.options.get()
* Adjust Selenium tests to always use CM6
* Adjust Selenium tests to delete test pages (useful for local dev)
* Remove unused code
Bug: T359498
Change-Id: I70dcf2f49418cea632c452c1266440effad634f3
2024-08-16 01:52:13 +00:00
|
|
|
'defaultPreferences' => $mwConfig->get( 'CodeMirrorDefaultPreferences' ),
|
2024-03-11 18:10:08 +00:00
|
|
|
'lineNumberingNamespaces' => $mwConfig->get( 'CodeMirrorLineNumberingNamespaces' ),
|
|
|
|
'templateFoldingNamespaces' => $mwConfig->get( 'CodeMirrorTemplateFoldingNamespaces' ),
|
2024-04-29 02:30:02 +00:00
|
|
|
'autocompleteNamespaces' => $mwConfig->get( 'CodeMirrorAutocompleteNamespaces' ),
|
2017-08-04 00:48:59 +00:00
|
|
|
'pluginModules' => $registry->getAttribute( 'CodeMirrorPluginModules' ),
|
2022-02-17 18:28:30 +00:00
|
|
|
'tagModes' => $tagModes,
|
|
|
|
'tags' => array_fill_keys( $tagNames, true ),
|
2017-08-04 00:48:59 +00:00
|
|
|
'doubleUnderscore' => [ [], [] ],
|
2018-08-14 05:55:13 +00:00
|
|
|
'functionSynonyms' => $parser->getFunctionSynonyms(),
|
|
|
|
'urlProtocols' => $parser->getUrlProtocols(),
|
2017-08-04 00:48:59 +00:00
|
|
|
'linkTrailCharacters' => $lang->linkTrail(),
|
|
|
|
];
|
|
|
|
|
|
|
|
$mw = $lang->getMagicWords();
|
2018-08-13 04:44:24 +00:00
|
|
|
$magicWordFactory = $parser->getMagicWordFactory();
|
|
|
|
foreach ( $magicWordFactory->getDoubleUnderscoreArray()->getNames() as $name ) {
|
2017-08-04 00:48:59 +00:00
|
|
|
if ( isset( $mw[$name] ) ) {
|
|
|
|
$caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
|
|
|
|
foreach ( $mw[$name] as $n ) {
|
|
|
|
$n = $caseSensitive ? $n : $lang->lc( $n );
|
|
|
|
$config['doubleUnderscore'][$caseSensitive][$n] = $name;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$config['doubleUnderscore'][0][] = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 04:44:24 +00:00
|
|
|
foreach ( $magicWordFactory->getVariableIDs() as $name ) {
|
2017-08-04 00:48:59 +00:00
|
|
|
if ( isset( $mw[$name] ) ) {
|
|
|
|
$caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
|
|
|
|
foreach ( $mw[$name] as $n ) {
|
|
|
|
$n = $caseSensitive ? $n : $lang->lc( $n );
|
|
|
|
$config['functionSynonyms'][$caseSensitive][$n] = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
}
|