MediaWiki:Gadget-darkmode.js: Skirtumas tarp puslapio versijų

Iš MCSlime.LT.
Pereiti į navigaciją Jump to search
SNėra keitimo santraukos
SNėra keitimo santraukos
43 eilutė: 43 eilutė:


// Optional: update link text
// Optional: update link text
$(portletLink).text(isUsingDarkmode ? '🌙 Dark' : '☀️ Light');
$(portletLink).text(isUsingDarkmode ? '🌙 ' : '☀️');
});
});
}
}

21:44, 13 balandžio 2025 versija

;(function($, mw){
	var DARK_COOKIE = 'darkmode',
		THEME_COOKIE = 'theme',
		isUsingDarkmode = $.cookie(THEME_COOKIE) === 'dark' || ($.cookie(THEME_COOKIE) == null && $.cookie(DARK_COOKIE) === 'true'),
		isMobile = mw.config.get('wgMFMode') !== null,
		portletLink;

	var self = {
		init: function () {
			console.log("Dark mode toggle initialized");

			$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});

			portletLink = mw.util.addPortletLink(
				(isMobile ? 'p-navigation' : 'p-personal'),
				'#', // use '#' so it doesn't redirect
				(isMobile ? 'Toggle dark mode' : '🌓 '),
				'pt-dm-toggle',
				'Toggle dark mode',
				null,
				$('#pt-userpage, #pt-anonuserpage, #pt-createaccount')[0]
			);

			// If mw.util.addPortletLink returns an <a> directly, don't use .find('a')
			$(portletLink).click(function(e) {
				e.preventDefault();

				isUsingDarkmode = !isUsingDarkmode;
				$.cookie(THEME_COOKIE, isUsingDarkmode ? 'dark' : 'light', {expires: 365, path: '/'});
				$.cookie(DARK_COOKIE, isUsingDarkmode, {expires: 365, path: '/'});

				if (isUsingDarkmode) {
					mw.loader.using(['wgl.theme.dark']).then(function() {
						$('body').addClass('wgl-theme-dark');
						$('body').removeClass('wgl-theme-light');
						mw.hook('wgl.themeChanged').fire('dark');
					});
				} else {
					$('body').addClass('wgl-theme-light');
					$('body').removeClass('wgl-theme-dark');
					mw.hook('wgl.themeChanged').fire('light');
				}

				// Optional: update link text
				$(portletLink).text(isUsingDarkmode ? '🌙 ' : '☀️');
			});
		}
	};

	// Ensure mediawiki.util is loaded before init
	mw.loader.using(['mediawiki.util'], function () {
		$(self.init);
	});

}(jQuery, mediaWiki));