forked from InternationalScratchWiki/ScratchWikiSkin2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScratchWikiSkin.hooks.php
81 lines (68 loc) · 2.59 KB
/
ScratchWikiSkin.hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
use MediaWiki\Hook\OutputPageBodyAttributesHook;
use MediaWiki\Preferences\Hook\GetPreferencesHook;
use MediaWiki\User\UserOptionsLookup;
require_once __DIR__ . '/consts.php';
class HTMLColorField extends HTMLFormField {
public function getInputHTML($value) {
$attribs = [
'id' => $this->mID,
'name' => $this->mName,
'value' => $value,
'dir' => $this->mDir,
'pattern' => '#[0-9A-Fa-f]{6}',
];
if ($this->mClass !== '') {
$attribs['class'] = $this->mClass;
}
$allowedParams = [
'type',
'pattern',
'title',
'disabled',
'required',
'autofocus',
'readonly',
];
$attribs += $this->getAttributes($allowedParams);
return Html::input($this->mName, $value, 'color', $attribs);
}
public function validate($value, $alldata) {
if (preg_match('%^#[a-fA-F0-9]{6}$%', $value) === 0) {
return $this->msg('htmlform-invalid-input');
}
return parent::validate($value, $alldata);
}
}
class ScratchWikiSkinHooks implements OutputPageBodyAttributesHook, GetPreferencesHook {
private $userOptionsLookup;
public function __construct(UserOptionsLookup $userOptionsLookup) {
$this->userOptionsLookup = $userOptionsLookup;
}
public function onOutputPageBodyAttributes($out, $sk, &$bodyAttrs): void {
global $wgSWS2ForceDarkTheme;
$user = RequestContext::getMain()->getUser();
if ($this->userOptionsLookup->getOption($user, DARK_THEME_PREF) || $wgSWS2ForceDarkTheme) {
$bodyAttrs['class'] .= ' dark-theme';
}
}
public function onGetPreferences($user, &$preferences) {
HTMLForm::$typeMappings['color'] = HTMLColorField::class;
$origpref = $this->userOptionsLookup->getOption($user, HEADER_COLOR_PREF);
$preferences[HEADER_COLOR_PREF] = [
'type' => 'color',
'pattern' => '#[0-9A-Fa-f]{6}',
'label-message' => 'scratchwikiskin-pref-color',
'section' => 'rendering/skin',
// Only expose background color preference when the skin is selected
'default' => ($origpref ? $origpref : '#7953c4'),
'hide-if' => ['!==', 'wpskin', 'scratchwikiskin2'],
];
$preferences[DARK_THEME_PREF] = [
'type' => 'check',
'label-message' => 'scratchwikiskin-pref-dark',
'section' => 'rendering/skin',
'hide-if' => ['!==', 'wpskin', 'scratchwikiskin2'],
];
}
}