Welcome to the Club Penguin Wiki! Log in or Create an account to join the community!

User:Lisured/common.js: Difference between revisions

From the Club Penguin Wiki, the free, editable encyclopedia about Club Penguin
Jump to navigation Jump to search
imported>Lisured
refresh preview on it’s all text update
imported>Lisured
only load when there is an editor
Line 3: Line 3:
// refresh preview on it’s all text update
// refresh preview on it’s all text update
$(function() {
$(function() {
var observer = new MutationObserver(function(mutations) {
if (textbox.length) {
mutations.forEach(function(mutation) {
var observer = new MutationObserver(function(mutations) {
if (mutation.attributeName === 'style' && window.getComputedStyle(textbox[0]).getPropertyValue('background-color') === 'rgb(255, 255, 0)') {
mutations.forEach(function(mutation) {
$('#wpPreview').click();
if (mutation.attributeName === 'style' && window.getComputedStyle(textbox[0]).getPropertyValue('background-color') === 'rgb(255, 255, 0)') {
}
$('#wpPreview').click();
}
});
});
});
});
var config = {attributes: true};
var config = {attributes: true};
observer.observe(textbox[0], config);
observer.observe(textbox[0], config);
}
});
});



Revision as of 10:43, 18 June 2015

var textbox = $('#wpTextbox1');

// refresh preview on it’s all text update
$(function() {
	if (textbox.length) {
		var observer = new MutationObserver(function(mutations) {
			mutations.forEach(function(mutation) {
				if (mutation.attributeName === 'style' && window.getComputedStyle(textbox[0]).getPropertyValue('background-color') === 'rgb(255, 255, 0)') {
					$('#wpPreview').click();
				}
			});
		});
		var config = {attributes: true};
		observer.observe(textbox[0], config);
	}
});

// add keyboard shortcut for bold
textbox.keydown(function(e) {
	if (textbox.is(':focus') && e.which == 66 && e.ctrlKey && !e.altKey) {
		$('.tool-button[rel="bold"]').click();
		e.preventDefault();
	}
});

// purge cache button
$(function() {
	if (!$('#ca-purge').length && mw.config.get('wgIsArticle')) {
		mw.util.addPortletLink(
			'p-cactions',
			mw.util.getUrl(mw.config.get('wgPageName')) + '?action=purge', 'Purge',
			'ca-purge',
			'Purge the server cache of this page',
			'*'
		);
	}
});

// add formatting fixes button
var customizeToolbar = function() {
	textbox.wikiEditor('addToToolbar', {
		section: 'main',
		group: 'format',
		tools: {
			buttonId: {
				label: 'Common fixes',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/a/a0/Toolbaricon_bold_F-1.png',
				action: {
					type: 'callback',
					execute: function(context) {
						console.log(context);
						context.$textarea.val(
							context.$textarea.val()
							// insert spaces around section headings (like wikipedia does)
							.replace(/^(==+)([^= ].+)\1 *$/gm, '$1 $2 $1')
							// remove redundant border from wikitable
							.replace(/border="1" class="wikitable"/gm, 'class="wikitable"')
							// remove redundant bolding in th
							.replace(/^(!)'''(.+?)'''(?:(!!)'''(.+?)'''(?:(!!)'''(.+?)'''(?:(!!)'''(.+?)'''(?:(!!)'''(.+?)'''(?:(!!)'''(.+?)''')?)?)?)?)?/gm, '$1$2$3$4$5$6$7$8$9$10$11$12')
							// insert spaces around equal signs in template params
							.replace(/^(\|[^=\n]*)(?:([^ ])| )=(?:([^ ])| )/gm, '$1$2 = $3')
							// fix table cell attributes incorrectly recognized as template params
							.replace(/^(\|(colspan|rowspan)) = /gm, '$1=')
							// use Photoswf template for background swfs
							.replace(/== SWFs? ==\n\* ?\[http:\/\/media1\.clubpenguin\.com\/play\/v2\/content\/global\/clothing\/icons\/(\d+)\.swf .+ \(icons?\)\]\n\* ?\[http:\/\/media1\.clubpenguin\.com\/play\/v2\/content\/global\/clothing\/photos\/\1\.swf .+ \(photos?\)\]/g, '{{Photoswf|$1}}')
							// use Itemswf template for item swfs
							.replace(/== SWFs? ==\n\* ?\[http:\/\/media1\.clubpenguin\.com\/play\/v2\/content\/global\/clothing\/icons\/(\d+)\.swf .+ \(icons?\)\]\n\* ?\[http:\/\/media1\.clubpenguin\.com\/play\/v2\/content\/global\/clothing\/paper\/\1\.swf .+ \(paper\)\]\n\* ?\[http:\/\/media1\.clubpenguin\.com\/play\/v2\/content\/global\/clothing\/sprites\/\1\.swf .+ \(sprites?\)\]/g, '{{Itemswf|$1}}')
							// insert a blank line before interwiki links
							.replace(/([^\n])(\n\[\[pt:(?!\]\]).+\]\])/g, '$1\n$2')
							// insert a blank line before headings
							.replace(/([^\n])\n(==+)(.+)\2$/gm, '$1\n\n$2$3$2')
							// insert blank line after infobox
							.replace(/^\}\}([^\n])/gm, '}}\n\n$1')
							// insert blank line after swf template
							.replace(/^(\{\{(?:Itemswf|Photoswf|Furnitureswf)\|[^}\n]+\}\}\n)([^\n])/gm, '$1\n$2')
							// insert a space after bullet point
							.replace(/^([#:*]+)([^ ])/gm, '$1 $2')
							// remove space between headings
							.replace(/^((=+).+\2)\n+(\n=\2.+=\2)/gm, '$1$3')
						);
					}
				}
			}
		}
	});
};


/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) {
	mw.loader.using('user.options', function () {
		// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
		if (mw.user.options.get('usebetatoolbar') == 1) {
			$.when(
				mw.loader.using('ext.wikiEditor.toolbar'), $.ready
			).then(customizeToolbar);
		}
	});
}

// Add the customizations to LiquidThreads' edit toolbar, if available
mw.hook('ext.lqt.textareaCreated').add(customizeToolbar);