User:Canadabonk/sandbox.js: Difference between revisions

From TestWiki
Content added Content deleted
No edit summary
No edit summary
Line 9: Line 9:
}
}
#cosmostweaks-coloreditor td, #cosmostweaks-coloreditor th {
#cosmostweaks-coloreditor td, #cosmostweaks-coloreditor th {
border:1px solid black;
border:1px solid grey;
}
}
#cosmostweaks-coloreditor th {
#cosmostweaks-coloreditor th {
Line 46: Line 46:
}
}
const newVarsCSS = `
const lightCSS = mw.util.addCSS();
const darkCSS = mw.util.addCSS();
:root {
${lightmodevars}
}
.cosmostweaks-darkmode {
${darkmodevars}
}`;
const newVars = mw.util.addCSS(newVarsCSS);
function createinput(color) {
function createinput(color) {
Line 74: Line 67:
}
}
$(name).text(colorlist);
$(name).text(colorlist);
switch(name) {
case ".lightmodevars":
lightCSS.ownerNode.innerHTML = `:root { ${colorlist} }`;
break;
case ".lightmodevars":
darkCSS.ownerNode.innerHTML = `.cosmostweaks-darkmode { ${colorlist} }`;
}
}
}
Line 85: Line 85:
${CSSvarsRows()}
${CSSvarsRows()}
</table>
</table>
<table style="width:100%;border-collapse:collapse">
<table>
<tr><td style="width:50%">
<tr><td style="width:50%">
<p>Light mode colors:</p>
<p>Light mode colors:</p>

Revision as of 18:42, 26 June 2024

$(function(){
	if ($('#cosmostweaks-coloreditor').length) {
		mw.util.addCSS(`
			#cosmostweaks-coloreditor {
				background-color: #eee;
				color: black;
				padding:10px;
				color-scheme:light;
			}
			#cosmostweaks-coloreditor td, #cosmostweaks-coloreditor th {
				border:1px solid grey;
			}
			#cosmostweaks-coloreditor th {
				background-color:lightgrey;
			}
		`);
		
		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.util.addCSS();
		const darkCSS = mw.util.addCSS();
		
		function createinput(color) {
			return `<input type="color" value="${color}"><input type="text" value="${color}">`;
		}
		
		function CSSvarsRows() {
			text = "";
			for (const x of CSSvarsList) {
				text += `<tr data-row="${x}"><th>${x}</th><td class="lightcol">${createinput(lightmodevars[x])}</td><td class="darkcol">${createinput(darkmodevars[x])}</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.ownerNode.innerHTML = `:root { ${colorlist} }`;
					break;
				case ".lightmodevars":
					darkCSS.ownerNode.innerHTML = `.cosmostweaks-darkmode { ${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="width:100%;border-collapse:collapse">
				<tr><td style="width:30%"></td><th>Light mode</th><th>Dark mode</th></tr>
				${CSSvarsRows()}
			</table>
			<table style="width:100%;border-collapse:collapse">
			<tr><td style="width:50%">
			<p>Light mode colors:</p>
			<pre class="lightmodevars">
			</pre></td>
			<td><p>Dark mode colors:</p>
			<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).parent().attr('class')) {
				case 'lightcol':
					lightmodevars[variable] = color;
					printcolors(lightmodevars, ".lightmodevars");
					break;
				case 'darkcol':
					darkmodevars[variable] = color;
					printcolors(darkmodevars, ".darkmodevars");
			}
			$(newVars.ownerNode).html(newVarsCSS);
		});
		
		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;
	}
}