MediaWiki:Gadget-code-snippets.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
(Created page with "→‎*<nowiki> * Gadget that loads a couple of snippets for easier access to commonly copy/pasted templates and sections * Authors: ** Joeytje50: $(function() { var today = new Date().toLocaleString('en-GB', {day: 'numeric', month: 'long', year: 'numeric', timeZone: 'UTC'}); var pagename = mw.config.get('wgTitle'); var preloadMsgs = [ 'NPC', 'equipment', 'item', 'location', 'monster', 'scenery', 'shop', //'navbox', //'navbox-oneline', //'navbox...")
 
No edit summary
Line 339: Line 339:
{
{
text: { html:
text: { html:
'You can <a href="https://oldschool.runescape.wiki/w/RuneScape:Administrator_requests?action=edit&section=new&preloadtitle=Requesting%20new%20code%20snippet%20in%20editor" target="_blank">'+
'You can <a href="https://oldschool.runescape.wiki/w/RuneScape:User_help?action=edit&section=new&preloadtitle=Requesting%20new%20code%20snippet%20in%20editor" target="_blank">'+
'request a snippet</a> to be added.'+
'request a snippet</a> to be added.'+
(mw.config.get('wgUserGroups').includes('sysop') ? '<br/>Admins may edit the <a href="/w/MediaWiki:Gadget-code-snippets.js" title="MediaWiki:Gadget-code-snippets.js">code snippets script</a>.' : ''),
(mw.config.get('wgUserGroups').includes('sysop') ? '<br/>Admins may edit the <a href="/w/MediaWiki:Gadget-code-snippets.js" title="MediaWiki:Gadget-code-snippets.js">code snippets script</a>.' : ''),
Line 360: Line 360:


// Show additional snippets, except for MediaWiki:, Module:, Exchange:, talk pages.
// Show additional snippets, except for MediaWiki:, Module:, Exchange:, talk mw.pages.
// Other namespaces might not necessarily need very many of these snippets, but there might be snippets for those in the future.
// Other namespaces might not necessarily need very many of these snippets, but there might be snippets for those in the future.
if (!$('body').is('.ns-8, .ns-114, .ns-828, .ns-talk')) {
if (!$('body').is('.ns-8, .ns-114, .ns-828, .ns-talk')) {
Line 368: Line 368:
'type': 'booklet',
'type': 'booklet',
'label': 'Presets/snippets',
'label': 'Presets/snippets',
'pages': groups,
'mw.pages': groups,
}
}
}
}

Revision as of 00:10, 17 October 2024

/**<nowiki>
 * Gadget that loads a couple of snippets for easier access to commonly copy/pasted templates and sections
 * Authors:
 ** Joeytje50
*/

$(function() {
	var today = new Date().toLocaleString('en-GB', {day: 'numeric', month: 'long', year: 'numeric', timeZone: 'UTC'});
	var pagename = mw.config.get('wgTitle');

	var preloadMsgs = [
		'NPC',
		'equipment',
		'item',
		'location',
		'monster',
		'scenery',
		'shop',
		//'navbox',
		//'navbox-oneline',
		//'navbox-subgroup',
	].map(function(msg) {return 'newarticletext-'+msg;});

	function snippet(label, parts) {
		if (typeof(parts) == 'string') parts = [parts]; // if string is given, only use pre
		return {
			label: label,
			action: {
				type: 'encapsulate',
				options: {
					pre: parts[0] || '',
					peri: parts[1] || '',
					post: parts[2] || '',
				}
			}
		};
	}
	
	function getLicense(name, chars, param) {
		var label = name;
		if (name instanceof Array) { // if the label shouldn't be the same as the name
			label = name[1];
			name = name[0];
		}
		return snippet(label, [
			'{{'+name+' license|',
			pagename.slice(0, -chars),
			(param || '') + '}}'
		]);
	}
	
	function getPreloads(preloads) {
		var arr = [];
		for (var i=0;i<preloads.length;i++) {
			var msg = mw.message(preloads[i], mw.config.get('wgTitle'), mw.config.get('wgTitle').split(' (')[0]).plain();
			msg = msg.replace(/<\/?includeonly>/g, '').replace(/<noinclude>[\s\S]*<\/noinclude>/g, ''); // remove include-tags
			var obj = {
				label: preloads[i].charAt(15).toUpperCase() + preloads[i].substr(16),
				action: {
					type: 'encapsulate',
					options: {
						pre: msg,
					},
				},
			};
			arr.push(obj);
		}
		return arr;
	}

	$( '#wpTextbox1' ).on('wikiEditor-toolbar-doneInitialSections', function() {
		if (typeof($('#wpTextbox1').wikiEditor) != 'function') return console.log($('#wpTextbox1'), $('#wpTextbox1').wikiEditor);
		
		var preloads = {
			label: 'Preloads',
			layout: 'characters',
			characters: [], // will be filled after MW messages have loaded.
		}
		var templates = {
			'label': 'Template presets',
			'layout': 'characters',
			'characters': [
				snippet('Single {{Map}}', [
					'{{Map|name='+pagename+'|x=3200|y=3200|plane=0|r=4|mapID=0|mtype=', 
					'square', 
					'}}'
				]),
				snippet('Multi-pin {{Map}}', [
					'{{Map|name='+pagename+'|', 
					'3200,3200|3232,3200', 
					'|plane=0|mapID=0|mtype=pin}}'
				]),
				snippet('{{Synced switch}}', [
					'{{Synced switch\n|version1 = ', 
					'', 
					'\n|version2 = \n|version3 = \n}}\n'
				]),
				snippet('{{DropLogProject}}', [
					'{{DropLogProject|kills=', 
					'0',
					'}}\n'
				]),
				snippet('{{DropLogProject|type=}}', [
					'{{DropLogProject|kills=0|type=', 
					'chests',
					'}}\n'
				]),
				snippet('{{Relative location}}', [
					'{{Relative location\n|location = '+pagename+'\n|north = ', 
					'', 
					'\n|west = \n|south = \n|east = \n}}\n'
				]),
				snippet('{{External}}', [
					'{{External|rs', 
					'|rsc', 
					'}}\n'
				]),
				snippet('{{Listen}}', [
					'{{Listen\n|title = \n|desc = \n|filename = ', 
					'', 
					'}}\n'
				]),
				snippet('{{Otheruses}}', [
					'{{Otheruses|', 
					'', 
					'}}\n'
				]),
				snippet('{{Redirect}}', [
					'{{Redirect|', 
					'', 
					'}}\n'
				]),
			]
		};
		var sections = {
			'label': 'Section presets',
			'layout': 'characters',
			'characters': [
				snippet('==References==', [
					'==References==\n{{Reflist', 
					'', 
					'}}\n'
				]),
				snippet('==NPC Locations==', [
					'==Locations==\n{{LocTableHead}}\n{{LocLine\n|name = '+pagename+'\n|location = ', 
					'', 
					'\n|levels = \n|members = \n|mapID = \n|plane = 0\n|x:3200,y:3200,icon:redPin\n|x:3232,y:3232,icon:redPin\n|mtype = pin\n|group = \n}}\n{{LocTableBottom}}\n'
				]),
				snippet('==Object Locations==', [
					'==Locations==\n{{ObjectTableHead}}\n{{ObjectLocLine\n|name = '+pagename+'\n|location = ', 
					'', 
					'\n|members = \n|mapID = \n|plane = 0\n|x:3200,y:3200,icon:redPin\n|x:3232,y:3232,icon:redPin\n|mtype = pin\n|group = \n}}\n{{ObjectTableBottom}}\n'
				]),
				snippet('==Monster Drops==', [
					'==Drops==\n\n===100%===\n{{DropsTableHead}}\n{{DropsLine|name=', 
					'', 
					'|quantity=1|rarity=Always}}\n{{DropsTableBottom}}\n'
				]),
				snippet('==Item Creation==', [
					'== Creation ==\n{{Recipe\n|skill1 = ', 
					'', 
					'\n|skill1lvl = \n|skill1boostable = \n|skill1exp = \n|ticks = \n|members = \n|mat1 = \n|mat2 = \n|output1 = '+pagename+'\n|output1quantity = 1}}\n'
				]),
				snippet('==Item Products==', [
					'==Products==\n{{Uses material list|',
					pagename,
					'}}\n'
				]),
				snippet('==Item sources==', [
					'==Item sources==\n{{Drop sources|',
					pagename,
					'}}\n'
				]),
				snippet('===Shop locations===', [
					'===Shop locations===\n{{Store locations list|',
					pagename,
					'}}\n'
				]),
				snippet('===Spawns===', [
					'===Spawns===\n{{ItemSpawnTableHead}}\n{{ItemSpawnLine|name='+pagename+'|location=', 
					'', 
					'|members=|3200,3200}}\n{{ItemSpawnTableBottom}}\n'
				]),
				snippet('==Changes==', [
					'==Changes==\n{{Subject changes header}}\n{{Subject changes\n|date = ',
					today,
					'\n|update = \n|poll = \n|change = \n}}\n{{Subject changes footer}}\n'
				]),
				snippet('==Combat styles==', [
					'==Combat styles==\n{{CombatStyles|', 
					'', 
					'}}\n'
				]),
				snippet('==Dialogue==', [
					'==Dialogue==\n{{Hastranscript|', 
					'npc', 
					'}}\n'
				]),
			]
		};
		let currentDate = new Date().toISOString().split('T')[0];
		var references = {
			'label': 'References',
			'layout': 'characters',
			'characters': [
				snippet('Ref section', [
					'==References==\n{{Reflist', 
					'', 
					'}}\n'
				]),
				snippet('Ref list', [
					'{{Reflist|group=', 
					'', 
					'}}\n'
				]),
				snippet('CiteTwitter', [
					'{{CiteTwitter|author=|url=|date=|archiveurl=|archivedate=', 
					currentDate,
					'|quote=|notes=|name=|group=}}'
				]),
				snippet('CiteReddit', [
					'{{CiteReddit|author=|url=|title=|quote=|date=|archiveurl=|archivedate=', 
					currentDate, 
					'|notes=|name=}}'
				]),
				snippet('CiteNews', [
					'{{CiteNews|author=|url=', 
					'', 
					'|title=|newsdate=|notes=|name=}}'
				]),
				snippet('CiteVideo', [
					'{{CiteVideo|author=|title=|url=',
					'',
					'|timestamp=|date=|site=|notes=|name=}}'
				]),
				snippet('CiteNPC', [
					'{{CiteNPC|npc=', 
					'', 
					'|quote=|loc=|notes=|name=}}'
				]),
				snippet('CiteGeneral', [
					'{{CiteGeneral|url=|title=|author=|date=|accessdate=',
					currentDate,
					'|publisher=|archiveurl=|archivedate=|quote=}}'
				]),
				snippet('NamedRef', [
					'{{CiteNPC|name=', 
					'', 
					'|group=}}'
				]),
			]
		};
		var transcript = {
			'label': 'Transcripts',
			'layout': 'characters',
			'characters': [
				snippet('==Dialogue==', [
					'==Dialogue==\n{{Hastranscript|', 
					'npc', 
					'}}\n'
				]),
				snippet('{{Transcript}}', [
					'{{Transcript|', 
					'npc', 
					'}}\n\n==Standard dialogue==\n* '
				]),
				snippet('{{overhead}}', [
					'{{overhead|',
					'Cor blimey mate, what are ye doing in me pockets?',
					'}}'
				]),
				snippet('{{mes}}', [
					'{{mes|',
					'Nothing interesting happens.',
					'}}'
				]),
				snippet('End of dialogue', '{{tact|end}}'),
				snippet('Members-only notice', '{{tact|members}}'),
				snippet('Interface opens', [
					'{{tact|opens=', 
					'Interface', 
					'}}'
				]),
				snippet('Unlinked interface opens', [
					'{{tact|opens2=', 
					'Interface', 
					'}}'
				]),
				snippet('Receives item', [
					'{{tact|receives=', 
					'an [[item]]', 
					'}}'
				]),
				snippet('Shows previous', '{{tact|previous}}'),
				snippet('Shows prev-to-prev', '{{tact|previous2}}'),
				snippet('Shows initial', '{{tact|initial}}'),
				snippet('Shows other options', '{{tact|other}}'),
				snippet('Same as above', '{{tact|above}}'),
				snippet('Same as below', '{{tact|below}}'),
				snippet('Continues below', '{{tact|continue}}'),
			]
		};
		var licenses = {
			'label': 'File licenses',
			'layout': 'characters',
			'characters': [
				getLicense('Inventory', 4),
				getLicense(['DII', 'Detailed item'], 11),
				getLicense('Worn equipment', 13),
				getLicense('NPC', 4),
				getLicense(['Chathead', 'NPC chathead'], 13, '|sub=npc'),
				getLicense(['Chathead', 'Pet chathead'], 13, '|sub=pet'),
				getLicense(['Chathead', 'player chathead'], 13, '|sub=player'),
				getLicense('Monster', 4),
				getLicense('Scenery', 4),
				getLicense(['POH', 'POH furniture'], 4),
				getLicense('Location', 4),
				getLicense('Map', 13),
				getLicense('Interface', 1e10),
				getLicense('Emote', 4),
				getLicense(['Prayer', 'Prayer icon'], 4),
				getLicense(['Spell', 'Spell icon'], 4),
				getLicense(['Quest', 'Quest scroll'], 18, '|sub=scroll'),
				getLicense(['Quest', 'Quest infobox'], 4, '|sub=infobox'),
				getLicense('Animation', 1e10),
				snippet('Audio', '{{Non-free audio}}'),
				getLicense('Miscellaneous', 4),
				snippet('Fair use', '{{Fair use}}'),
				snippet('CC-BY-SA', '{{CC-BY-SA}}'),
			]
		};
		var feedback = {
			'label': 'Missing something?',
			'layout': 'table',
			'headings': [
				{ text: 'Are you missing a code snippet that would be useful here?' }
			],
			'rows': [
				{
					text: { html: 
						'You can <a href="https://oldschool.runescape.wiki/w/RuneScape:User_help?action=edit&section=new&preloadtitle=Requesting%20new%20code%20snippet%20in%20editor" target="_blank">'+
						'request a snippet</a> to be added.'+
						(mw.config.get('wgUserGroups').includes('sysop') ? '<br/>Admins may edit the <a href="/w/MediaWiki:Gadget-code-snippets.js" title="MediaWiki:Gadget-code-snippets.js">code snippets script</a>.' : ''),
					},
				}
			],
		};
		
		
		var isRedirect = /^\#REDIRECT\s*\[\[[^\]]+\]\](\n|$)/i.test($('#wpTextbox1').val()) && $('#wikiPreview div').text().length == 0;
		
		var groups = {};
		if (isRedirect && $('body').is('.ns-0')) groups['section-preloads'] = preloads; // will be filled after messages have loaded.
		groups['section-templates'] = templates;
		if ($('body').is('.ns-0')) groups['section-sections'] = sections; // listed sections are only relevant to mainspace
		groups['section-refs'] = references;
		if ($('body').is('.ns-120')) groups['section-transcript'] = transcript; // only show on Transcript: namespace.
		if ($('body').is('.ns-6')) groups['section-licenses'] = licenses; // only show on File: namespace.
		groups['section-feedback'] = feedback;

		
		// Show additional snippets, except for MediaWiki:, Module:, Exchange:, talk mw.pages.
		// Other namespaces might not necessarily need very many of these snippets, but there might be snippets for those in the future.
		if (!$('body').is('.ns-8, .ns-114, .ns-828, .ns-talk')) {
			$('#wpTextbox1').wikiEditor('addToToolbar', {
				'sections': {
					'snippets': {
						'type': 'booklet',
						'label': 'Presets/snippets',
						'mw.pages': groups,
					}
				}
			});
		}
		
		if (isRedirect && $('body').is('.ns-0')) {
			$.when(mw.loader.using(['mediawiki.api', 'mediawiki.jqueryMsg']), $.ready).then(function() {
		        return new mw.Api().loadMessagesIfMissing(preloadMsgs);
		    }).then(function() {
				$('#wpTextbox1').wikiEditor('addToToolbar', {
					section: 'snippets',
					page: 'section-preloads',
					characters: getPreloads(preloadMsgs),
				});
			});
		}
	});
});
//</nowiki>