mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CodeMirror
synced 2024-12-18 00:30:30 +00:00
4b17eb952c
* Per https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences, it is strict on $preferences being an array, so type hinted it to conform. * Remove legacy hook behavior for onResourceLoaderTestModules() hook in CodeMirrorHooks. See: https://w.wiki/3Az for more information. Also, added `@see` with link to the documentation of the hook used. Change-Id: I0a2fac4b4d491abf8d2f661be32bf2e0d791c90b
84 lines
2.4 KiB
PHP
84 lines
2.4 KiB
PHP
<?php
|
|
|
|
class CodeMirrorHooks {
|
|
|
|
/**
|
|
* Checks if CodeMirror for textarea wikitext editor should be loaded on this page or not.
|
|
*
|
|
* @param OutputPage $out
|
|
* @return bool
|
|
*/
|
|
private static function isCodeMirrorOnPage( OutputPage $out ) {
|
|
// Disable CodeMirror when CodeEditor is active on this page
|
|
// Depends on ext.codeEditor being added by EditPage::showEditForm:initial
|
|
if ( in_array( 'ext.codeEditor', $out->getModules() ) ) {
|
|
return false;
|
|
}
|
|
$context = $out->getContext();
|
|
return in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] ) &&
|
|
// CodeMirror on textarea wikitext editors doesn't support RTL (T170001)
|
|
!$context->getTitle()->getPageLanguage()->isRTL();
|
|
}
|
|
|
|
/**
|
|
* BeforePageDisplay hook handler
|
|
*
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
|
*
|
|
* @param OutputPage $out
|
|
* @param Skin $skin
|
|
*/
|
|
public static function onBeforePageDisplay( OutputPage $out, Skin $skin ) {
|
|
if ( self::isCodeMirrorOnPage( $out ) ) {
|
|
$out->addModules( 'ext.CodeMirror' );
|
|
|
|
if ( $out->getUser()->getOption( 'usecodemirror' ) ) {
|
|
// These modules are predelivered for performance when needed
|
|
// keep these modules in sync with ext.CodeMirror.js
|
|
$out->addModules( [ 'ext.CodeMirror.lib', 'ext.CodeMirror.mode.mediawiki' ] );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* GetPreferences hook handler
|
|
*
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
|
|
*
|
|
* @param User $user
|
|
* @param array &$defaultPreferences
|
|
*/
|
|
public static function onGetPreferences( User $user, array &$defaultPreferences ) {
|
|
// CodeMirror is enabled by default for users. It can
|
|
// be changed by adding '$wgDefaultUserOptions['usecodemirror'] = 0;' into LocalSettings.php
|
|
$defaultPreferences['usecodemirror'] = [
|
|
'type' => 'api',
|
|
'default' => '1',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Register test modules for CodeMirror.
|
|
*
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderTestModules
|
|
*
|
|
* @param array &$modules
|
|
* @param ResourceLoader $rl
|
|
*/
|
|
public static function onResourceLoaderTestModules( array &$modules, ResourceLoader $rl ) {
|
|
$modules['qunit']['ext.CodeMirror.test'] = [
|
|
'scripts' => [
|
|
'resources/mode/mediawiki/tests/qunit/CodeMirror.mediawiki.test.js',
|
|
],
|
|
'dependencies' => [
|
|
'ext.CodeMirror.data',
|
|
'ext.CodeMirror.lib',
|
|
'ext.CodeMirror.mode.mediawiki',
|
|
],
|
|
'localBasePath' => __DIR__ . '/../',
|
|
'remoteExtPath' => 'CodeMirror',
|
|
];
|
|
}
|
|
|
|
}
|