Skip to content

Commit

Permalink
feat(core): ✨ move theme preferences to clientPrefs
Browse files Browse the repository at this point in the history
Since clientPrefs and other related features are not avaliable until MW 1.42,
many of them are backported as polyfill. Instead of using cookies, the polyfill
are using localStorage only like Citizen in the past.

There are many changes behind the scene, but the most important one being that
`skin-citizen-*` theme classes are now soft-deprecated, and replaced by the
standardized `skin-theme-clientpref-*` classes. There will be sufficient
time before the hard deprecation.

Related: #780
  • Loading branch information
alistair3149 committed Apr 25, 2024
1 parent 755f42e commit a741639
Show file tree
Hide file tree
Showing 13 changed files with 616 additions and 162 deletions.
13 changes: 8 additions & 5 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@
"citizen-tagline-ns-help": "Help page",
"citizen-tagline-ns-category": "Category page",
"citizen-tagline-user-regdate": "Joined $1",
"prefs-citizen-theme-label": "Theme",
"prefs-citizen-theme-option-auto": "Auto",
"prefs-citizen-theme-option-light": "Light",
"prefs-citizen-theme-option-dark": "Dark",
"prefs-citizen-fontsize-label": "Font size",
"prefs-citizen-pagewidth-label": "Page width",
"prefs-citizen-lineheight-label": "Line height",
"prefs-citizen-resetbutton-label": "Reset to default"
"prefs-citizen-resetbutton-label": "Reset to default",

"skin-theme-name": "Color",

This comment has been minimized.

Copy link
@Nikerabbit

Nikerabbit Apr 25, 2024

These keys are already used by the Vector skin and causes issues in translatewiki.net: PHP Notice: Key 8:skin-theme-name already belongs to mediawiki-skin-vector, conflict with mwgithub-star-citizen.

Do you need these to be translated separately or not translated?

This comment has been minimized.

Copy link
@alistair3149

alistair3149 Apr 25, 2024

Author Member

They are the exact same messages with the exact same usage. We have to include it in Citizen because those messages are currently a part of the newer version of Vector, and they are not available to be included for most scenario.

This comment has been minimized.

Copy link
@Nikerabbit

Nikerabbit Apr 25, 2024

They are not exactly the same: https://translatewiki.net/w/i.php?title=MediaWiki:Skin-theme-description/en&diff=prev&oldid=12370374

Conflicting keys are not supported and are causing various issues so it needs to be fixed in one way or another.

This comment has been minimized.

Copy link
@alistair3149

alistair3149 Apr 25, 2024

Author Member

In that case I can update it to be exactly the same with Vector. Is there really no way to have the same key and value with what's in Vector? The reason being that those messages are likely going upstream into MW core. If there are really no other way then I'll assign completely new keys.

This comment has been minimized.

Copy link
@Nikerabbit

Nikerabbit Apr 25, 2024

We can map those keys on translatewiki.net side to different names. You will get separate translations (but translation memory is there).

This comment has been minimized.

Copy link
@alistair3149

alistair3149 Apr 25, 2024

Author Member

It is okay I'll make a new key then, will get to it in an hour. Things are changing rapidly upstream and it looks like it is still quite unstable yet. https://phabricator.wikimedia.org/T359983

This comment has been minimized.

Copy link
@alistair3149

alistair3149 Apr 25, 2024

Author Member

The message keys are updated. Thanks again for letting me know!

"skin-theme-description": "Reduces the light emitted by device screens.",
"skin-theme-day-label": "Day",
"skin-theme-night-label": "Night",
"skin-theme-os-label": "Automatic",
"skin-theme-exclusion-notice": "This page is always in day mode."
}
10 changes: 5 additions & 5 deletions i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
"citizen-tagline-ns-help": "Tagline for pages in help namespace",
"citizen-tagline-ns-category": "Tagline for pages in category namespace",
"citizen-tagline-user-regdate": "Label for registration date in taglines on userpages",
"prefs-citizen-theme-label": "Tooltip for the theme dropdown in Special:Preferences",
"prefs-citizen-theme-option-auto": "Label for the auto theme option",
"prefs-citizen-theme-option-light": "Label for the light theme option",
"prefs-citizen-theme-option-dark": "Label for the dark theme option",
"prefs-citizen-fontsize-label": "Label for the font size settings",
"prefs-citizen-pagewidth-label": "Label for the page width settings",
"prefs-citizen-lineheight-label": "Label for the line height settings",
"prefs-citizen-resetbutton-label": "Label for the reset button that restore default settings"
"prefs-citizen-resetbutton-label": "Label for the reset button that restore default settings",
"skin-theme-name": "Label for setting that allows you to change theme.",
"skin-theme-description": "Description for theme.",
"skin-theme-day-label": "Label for light theme (standard mode).",
"skin-theme-night-label": "Label for night theme (dark mode)."
}
1 change: 1 addition & 0 deletions includes/Hooks/SkinHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function onBeforePageDisplay( $out, $skin ): void {

$script = file_get_contents( MW_INSTALL_PATH . '/skins/Citizen/resources/skins.citizen.scripts/inline.js' );
$script = Html::inlineScript( $script );
// TODO: Consider turning on cache after this is more stable
$script = RL\ResourceLoader::filter( 'minify-js', $script, [ 'cache' => false ] );
$out->addHeadItem( 'skin.citizen.inline', $script );
}
Expand Down
13 changes: 12 additions & 1 deletion includes/Partials/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

namespace MediaWiki\Skins\Citizen\Partials;

const CLIENTPREFS_THEME_MAP = [
'auto' => 'os',
'light' => 'day',
'dark' => 'night'
];

/**
* Theme switcher partial of Skin Citizen
*/
Expand All @@ -42,7 +48,12 @@ public function setSkinTheme( array &$options ) {
// Set theme to site theme
$theme = $this->getConfigValue( 'CitizenThemeDefault' ) ?? 'auto';

// Add HTML class based on theme set
// Legacy class to be deprecated
$out->addHtmlClasses( 'skin-citizen-' . $theme );

// Add HTML class based on theme set
if ( CLIENTPREFS_THEME_MAP[ $theme ] ) {
$out->addHtmlClasses( 'skin-theme-clientpref-' . $theme );
}
}
}
88 changes: 88 additions & 0 deletions resources/skins.citizen.preferences/addPortlet.polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* TODO: Revisit when we move to MW 1.43 and the interface is more stable
*/

/**
* Creates default portlet.
* Based on Vector
*
* @param {Element} portlet
* @return {Element}
*/
function addDefaultPortlet( portlet ) {
const ul = portlet.querySelector( 'ul' );
if ( !ul ) {
return portlet;
}
ul.classList.add( 'citizen-menu__content-list' );
const label = portlet.querySelector( 'label' );
if ( label ) {
const labelDiv = document.createElement( 'div' );
labelDiv.classList.add( 'citizen-menu__heading' );
labelDiv.innerHTML = label.textContent || '';
portlet.insertBefore( labelDiv, label );
label.remove();
}
let wrapper = portlet.querySelector( 'div:last-child' );
if ( wrapper ) {
ul.remove();
wrapper.appendChild( ul );
wrapper.classList.add( 'citizen-menu__content' );
} else {
wrapper = document.createElement( 'div' );
wrapper.classList.add( 'citizen-menu__content' );
ul.remove();
wrapper.appendChild( ul );
portlet.appendChild( wrapper );
}
portlet.classList.add( 'citizen-menu' );
return portlet;
}

/**
* Polyfill for mw.util.addPortlet for < MW 1.42
*
* @return {Element}
*/
function addPortlet() {
if ( mw.util.addPortlet ) {
return addDefaultPortlet( mw.util.addPortlet );
}

return function ( id, label, before ) {
const portlet = document.createElement( 'div' );
portlet.classList.add( 'mw-portlet', 'mw-portlet-' + id, 'emptyPortlet',
// Additional class is added to allow skins to track portlets added via this mechanism.
'mw-portlet-js'
);
portlet.id = id;
if ( label ) {
const labelNode = document.createElement( 'label' );
labelNode.textContent = label;
portlet.appendChild( labelNode );
}
const listWrapper = document.createElement( 'div' );
const list = document.createElement( 'ul' );
listWrapper.appendChild( list );
portlet.appendChild( listWrapper );
if ( before ) {
let referenceNode;
try {
referenceNode = document.querySelector( before );
} catch ( e ) {
// CSS selector not supported by browser.
}
if ( referenceNode ) {
const parentNode = referenceNode.parentNode;
parentNode.insertBefore( portlet, referenceNode );
} else {
return null;
}
}
mw.hook( 'util.addPortlet' ).fire( portlet, before );
return addDefaultPortlet( portlet );
};
}

/** @module addPortlet */
module.exports = addPortlet;
Loading

0 comments on commit a741639

Please sign in to comment.