mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/CodeMirror
synced 2024-11-27 15:40:00 +00:00
b6c3eca38b
Change-Id: I2d9f6a9a8b7e0812a0a758455219aef826a0ffa5
113 lines
3.5 KiB
PHP
113 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\CodeMirror;
|
|
|
|
use MediaWiki\Config\Config;
|
|
use MediaWiki\Hook\BeforePageDisplayHook;
|
|
use MediaWiki\Output\OutputPage;
|
|
use MediaWiki\Preferences\Hook\GetPreferencesHook;
|
|
use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook;
|
|
use MediaWiki\User\User;
|
|
use MediaWiki\User\UserOptionsLookup;
|
|
use Skin;
|
|
|
|
class Hooks implements
|
|
BeforePageDisplayHook,
|
|
ResourceLoaderGetConfigVarsHook,
|
|
GetPreferencesHook
|
|
{
|
|
|
|
/** @var UserOptionsLookup */
|
|
private $userOptionsLookup;
|
|
|
|
/**
|
|
* @param UserOptionsLookup $userOptionsLookup
|
|
*/
|
|
public function __construct(
|
|
UserOptionsLookup $userOptionsLookup
|
|
) {
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
|
}
|
|
|
|
/**
|
|
* Checks if CodeMirror for textarea wikitext editor should be loaded on this page or not.
|
|
*
|
|
* @param OutputPage $out
|
|
* @return bool
|
|
*/
|
|
private function isCodeMirrorOnPage( OutputPage $out ) {
|
|
// Disable CodeMirror when CodeEditor is active on this page
|
|
// Depends on ext.codeEditor being added by \MediaWiki\EditPage\EditPage::showEditForm:initial
|
|
if ( in_array( 'ext.codeEditor', $out->getModules(), true ) ) {
|
|
return false;
|
|
}
|
|
// Disable CodeMirror when the WikiEditor toolbar is not enabled in preferences
|
|
if ( !$this->userOptionsLookup->getOption( $out->getUser(), 'usebetatoolbar' ) ) {
|
|
return false;
|
|
}
|
|
return in_array( $out->getActionName(), [ 'edit', 'submit' ], true ) &&
|
|
// CodeMirror on textarea wikitext editors doesn't support RTL (T170001)
|
|
!$out->getTitle()->getPageLanguage()->isRTL();
|
|
}
|
|
|
|
/**
|
|
* BeforePageDisplay hook handler
|
|
*
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
|
*
|
|
* @param OutputPage $out
|
|
* @param Skin $skin
|
|
* @return void This hook must not abort, it must return no value
|
|
*/
|
|
public function onBeforePageDisplay( $out, $skin ): void {
|
|
if ( $this->isCodeMirrorOnPage( $out ) ) {
|
|
$out->addModules( 'ext.CodeMirror.WikiEditor' );
|
|
|
|
if ( $this->userOptionsLookup->getOption( $out->getUser(), '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' ] );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
* @param string $skin
|
|
* @param Config $config
|
|
* @return void This hook must not abort, it must return no value
|
|
*/
|
|
public function onResourceLoaderGetConfigVars( array &$vars, $skin, Config $config ): void {
|
|
$vars['wgCodeMirrorLineNumberingNamespaces'] = $config->get( 'CodeMirrorLineNumberingNamespaces' );
|
|
}
|
|
|
|
/**
|
|
* GetPreferences hook handler
|
|
*
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
|
|
*
|
|
* @param User $user
|
|
* @param array &$defaultPreferences
|
|
* @return bool|void True or no return value to continue or false to abort
|
|
*/
|
|
public function onGetPreferences( $user, &$defaultPreferences ) {
|
|
// CodeMirror is disabled by default for all users. It can enabled for everyone
|
|
// by default by adding '$wgDefaultUserOptions['usecodemirror'] = 1;' into LocalSettings.php
|
|
$defaultPreferences['usecodemirror'] = [
|
|
'type' => 'api',
|
|
];
|
|
|
|
$defaultPreferences['usecodemirror-colorblind'] = [
|
|
'type' => 'toggle',
|
|
'label-message' => 'codemirror-prefs-colorblind',
|
|
'help-message' => 'codemirror-prefs-colorblind-help',
|
|
'section' => 'editing/accessibility',
|
|
];
|
|
}
|
|
|
|
}
|