User:Canadabonk/sandbox.js: Difference between revisions

From TestWiki
Content added Content deleted
No edit summary
No edit summary
 
(30 intermediate revisions by the same user not shown)
Line 1: Line 1:
$(function(){
/*
if ($('#cosmostweaks-coloreditor').length) {

mw.util.addCSS(`
this script achieves 1 thing: get all the colors input into the
#cosmostweaks-coloreditor {
"Manage this wiki's additional settings" styling tab for Cosmos,
background-color: #eee;
and then printing it into a <pre> block or a table.
color: black;
padding:1em;
color-scheme:light;
border:1px solid lightgrey;
}
#cosmostweaks-coloreditor th {
background-color:lightgrey;
}
#cosmostweaks-coloreditor td div {
display:flex;
justify-content:center;
}
#cosmostweaks-coloreditor input[type='color'] {
padding:0;
border:0;
cursor:pointer;
}
#cosmostweaks-coloreditor input[type='color']:hover {
opacity:0.6;
}
#cosmostweaks-coloreditor pre {
margin:0;
background-color:white;
}


you should then copy the <pre> block generated into cosmos.css
on your wiki, or whichever appropriate stylesheet.


`);
to generate the <pre> block, put
<div id="printCSSVars"></div>
in a sandbox page (or on any page)

to generate a table, put
<div id="printCSSVarsTable"></div>

after you've copied the contents of the <pre> block to the appropriate
css page, you can technically remove this script.

*/
$(document).ready(function(){
if (mw.config.get('skin') == 'cosmos') {
const
mainBackgroundColor = $('body').css('background-color'),
contentBackgroundColor = $('#mw-content').css('background-color'),
const CSSvarsList = [
fontColor = $('#mw-content').css('color'),
'--main-background-color',
linkColor = $('#content a:not(.new').css('color'),
'--content-background-color',
'--text-color',
'--link-color',
'--banner-color',
'--banner-text-color',
'--header-color',
'--header-text-color',
'--button-color',
'--button-text-color',
'--footer-color'];
const cosmostweaksCSS = [];
$.each(document.styleSheets, function(index, sheet){
const source = sheet.ownerNode.href || 'none';
if (source.match('Canadabonk')) {
cosmostweaksCSS.push(sheet.cssRules[0]);
cosmostweaksCSS.push(sheet.cssRules[1]);
}
});
const lightmodevars = {};
bannerColor = $('#cosmos-banner').css('background-color'),
const darkmodevars = {};
bannerTextColor = $('.cosmos-bannerOption-dropdownIcon').css('fill'),
for (const x of CSSvarsList) {
headerColor = $('.cosmos-header').css('background-color'),
lightmodevars[x] = tohex(cosmostweaksCSS[0].style.getPropertyValue(x));
headerTextColor = $('.cosmos-header__counter').css('color'),
darkmodevars[x] = tohex(cosmostweaksCSS[1].style.getPropertyValue(x));
}
const lightCSS = mw.loader.addStyleTag(':root{}');
buttonColor = $('.cosmos-button-primary').css('background-color'),
const darkCSS = mw.loader.addStyleTag('.cosmostweaks-darkmode{}');
buttonTextColor = $('.cosmos-button-primary').css('color'),
function createinput(color, name) {
footerColor = $('#cosmos-footer').css('background-color');
return `<input type="color" name="${name}-color" value="${color}"><input type="text" name="${name}-text" value="${color}">`;
}
function cssVar(name, val, desc) {
function CSSvarsRows() {
this.name = name;
text = "";
for (const x of CSSvarsList) {
this.val = val;
text += `<tr data-row="${x}"><th>${x}</th><td class="lightcol"><div>${createinput(lightmodevars[x], 'light' + x)}</div></td><td class="darkcol"><div>${createinput(darkmodevars[x], 'dark' + x)}</div></td></tr>\n`;
this.desc = desc ? desc : "none";
}
}
return text;
const variablesList = {
mainbgcolor: new cssVar('--main-background-color', mainBackgroundColor, "used for background behind content block. hidden if there is a background image"),
contentbgcolor: new cssVar('--content-background-color', contentBackgroundColor, "background of the content block"),
fontcolor: new cssVar('--text-color', fontColor, "main text color"),
linkcolor: new cssVar('--link-color', linkColor, "link color"),
bannercolor: new cssVar('--banner-color', bannerColor, "top banner background color (where the search bar is)"),
bannertextcolor: new cssVar('--banner-text-color', bannerTextColor, "banner text color"),
headercolor: new cssVar('--header-color', headerColor, "header background color (where the wiki name and logo is"),
headertextcolor: new cssVar('--header-text-color', headerTextColor, "header text color"),
buttoncolor: new cssVar('--button-color', buttonColor, "button background color"),
buttontext: new cssVar('--button-text-color', buttonTextColor, "button text color"),
footercolor: new cssVar('--footer-color', footerColor, "footer background color"),
};
function variablesListCSS() {
text = "";
for (var x in variablesList){
text += " " + variablesList[x].name + ": " + variablesList[x].val + ";\n";
}
}
return text;
}
function printcolors(list, name) {
const newCSSVariables =
colorlist = "";
`:root {
for (const x in list) {
${variablesListCSS()}
colorlist += x + ": " + list[x] + ';\n';
}`;
}
$(name).text(colorlist);
$('head link[rel=stylesheet]').first().after('<style>' + newCSSVariables + '</style>');
switch(name) {
case ".lightmodevars":
$('#printCSSVars').append(
lightCSS.innerHTML = `:root {\n${colorlist} }`;
`<pre>
break;
${newCSSVariables}
case ".darkmodevars":
</pre>`);
darkCSS.innerHTML = `.cosmostweaks-darkmode {\n${colorlist} }`;

}
$('#printCSSVarsTable').append(printCSSVarsTable());
function printCSSVarsTable() {
table = '<table class="wikitable"><tr><th>CSS variable</th><th>Value</th><th>Description</th></tr>';
for (var x in variablesList){
table += `<tr><td>${variablesList[x].name}</td><td><div style="display:inline-block;margin-right:0.5em;width:1em;height:1em;border:1px solid black;background-color:${variablesList[x].val};"></div>${variablesList[x].val}</td><td>${variablesList[x].desc}</td>`;
}
}
table += '</table>';
$('#cosmostweaks-coloreditor').html(`
return table;
<h3 style="margin:0;color:black">Use this to visualise what a color scheme will look like!</h3>
<p>Anything done here is not saved, this is just for preview purposes.<br>
Paste the resulting code into your "Mediawiki:Gadget-cosmostweaks.css" to apply it.<br>
This tool can only work with hexcodes (like "#000000"). Swap them out for RGBA values with opacity in the CSS page, if desired.</p>
<table style="margin:auto">
<tr><td></td><th>Light mode</th><th>Dark mode</th></tr>
${CSSvarsRows()}
</table>
<br>
<table style="width:100%;">
<tr><th>Light mode colors:</th><th>Dark mode colors:</th><tr>
<tr><td style="width:50%;">
<pre class="lightmodevars">
</pre></td>
<td>
<pre class="darkmodevars">
</pre></td></tr></table>
`);
$('#cosmostweaks-coloreditor input').change(e => {
const
color = e.target.value,
variable = $(e.target).closest('tr').attr('data-row');
$(e.target).attr('value', color);
$(e.target).siblings('input').attr('value', color);
switch($(e.target).closest('td').attr('class')) {
case 'lightcol':
lightmodevars[variable] = color;
printcolors(lightmodevars, ".lightmodevars");
break;
case 'darkcol':
darkmodevars[variable] = color;
printcolors(darkmodevars, ".darkmodevars");
}
});
printcolors(lightmodevars, ".lightmodevars");
printcolors(darkmodevars, ".darkmodevars");
}
}
});

if ($('.cosmos-header').css('background-image') == "none") {
function tohex(color){
$('head link[rel=stylesheet]').first().after('<style>.cosmos-header::before {background:none}</style>');
if (color.match(/^rgb/)) {
rgb = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
} else if (color.length == 4 && color.match(/^#.../)) {
return "#" + color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
} else {
return color;
}
}
}
}
});

Latest revision as of 20:08, 26 June 2024

$(function(){
	if ($('#cosmostweaks-coloreditor').length) {
		mw.util.addCSS(`
#cosmostweaks-coloreditor {
	background-color: #eee;
	color: black;
	padding:1em;
	color-scheme:light;
	border:1px solid lightgrey;
}
#cosmostweaks-coloreditor th {
	background-color:lightgrey;
}
#cosmostweaks-coloreditor td div {
	display:flex;
	justify-content:center;
}
#cosmostweaks-coloreditor input[type='color'] {
	padding:0;
	border:0;
	cursor:pointer;
}
#cosmostweaks-coloreditor input[type='color']:hover {
	opacity:0.6;
}
#cosmostweaks-coloreditor pre {
	margin:0;
	background-color:white;
}


`);
		
		const CSSvarsList = [
			'--main-background-color', 
			'--content-background-color', 
			'--text-color', 
			'--link-color', 
			'--banner-color', 
			'--banner-text-color', 
			'--header-color', 
			'--header-text-color', 
			'--button-color', 
			'--button-text-color', 
			'--footer-color'];
			
		const cosmostweaksCSS = [];
		$.each(document.styleSheets, function(index, sheet){
			const source = sheet.ownerNode.href || 'none';
			if (source.match('Canadabonk')) {
				cosmostweaksCSS.push(sheet.cssRules[0]);
				cosmostweaksCSS.push(sheet.cssRules[1]);	
			}
		});
		
		const lightmodevars = {};
		const darkmodevars = {};
		
		for (const x of CSSvarsList) {
			lightmodevars[x] = tohex(cosmostweaksCSS[0].style.getPropertyValue(x));
			darkmodevars[x] = tohex(cosmostweaksCSS[1].style.getPropertyValue(x));
		}
		
		const lightCSS = mw.loader.addStyleTag(':root{}');
		const darkCSS = mw.loader.addStyleTag('.cosmostweaks-darkmode{}');
		
		function createinput(color, name) {
			return `<input type="color" name="${name}-color" value="${color}"><input type="text" name="${name}-text" value="${color}">`;
		}
		
		function CSSvarsRows() {
			text = "";
			for (const x of CSSvarsList) {
				text += `<tr data-row="${x}"><th>${x}</th><td class="lightcol"><div>${createinput(lightmodevars[x], 'light' + x)}</div></td><td class="darkcol"><div>${createinput(darkmodevars[x], 'dark' + x)}</div></td></tr>\n`;
			}
			return text;
		}
		
		function printcolors(list, name) {
			colorlist = "";
			for (const x in list) {
				colorlist += x + ": " + list[x] + ';\n';
			}
			$(name).text(colorlist);
			switch(name) {
				case ".lightmodevars":
					lightCSS.innerHTML = `:root {\n${colorlist} }`;
					break;
				case ".darkmodevars":
					darkCSS.innerHTML = `.cosmostweaks-darkmode {\n${colorlist} }`;
			}
		}
		
		$('#cosmostweaks-coloreditor').html(`
			<h3 style="margin:0;color:black">Use this to visualise what a color scheme will look like!</h3>
			<p>Anything done here is not saved, this is just for preview purposes.<br>
			Paste the resulting code into your "Mediawiki:Gadget-cosmostweaks.css" to apply it.<br>
			This tool can only work with hexcodes (like "#000000"). Swap them out for RGBA values with opacity in the CSS page, if desired.</p>
			<table style="margin:auto">
				<tr><td></td><th>Light mode</th><th>Dark mode</th></tr>
				${CSSvarsRows()}
			</table>
			<br>
			<table style="width:100%;">
			<tr><th>Light mode colors:</th><th>Dark mode colors:</th><tr>
			<tr><td style="width:50%;">
			<pre class="lightmodevars">
			</pre></td>
			<td>
			<pre class="darkmodevars">
			</pre></td></tr></table>
		`);
		
		$('#cosmostweaks-coloreditor input').change(e => {
			const 
			color = e.target.value,
			variable = $(e.target).closest('tr').attr('data-row');
			
			$(e.target).attr('value', color);
			$(e.target).siblings('input').attr('value', color);
			switch($(e.target).closest('td').attr('class')) {
				case 'lightcol':
					lightmodevars[variable] = color;
					printcolors(lightmodevars, ".lightmodevars");
					break;
				case 'darkcol':
					darkmodevars[variable] = color;
					printcolors(darkmodevars, ".darkmodevars");
			}
		});
		
		printcolors(lightmodevars, ".lightmodevars");
		printcolors(darkmodevars, ".darkmodevars");
	}
});

function tohex(color){
	if (color.match(/^rgb/)) {
		rgb = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
		return (rgb && rgb.length === 4) ? "#" +
		("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
		("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
		("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
	} else if (color.length == 4 && color.match(/^#.../)) {
		return "#" + color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
	} else {
		return color;
	}
}