Jump to content

User:Nardog/IPAInput.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.loader.using([
	'ext.visualEditor.desktopArticleTarget.init', 'mediawiki.storage'
], function ipaInput() {
	if (!mw.libs.ve.isVisualAvailable &&
		!['edit', 'submit'].includes(mw.config.get('wgAction'))
	) {
		return;
	}
	mw.loader.addStyleTag(`.oo-ui-icon-schwa{background-image:url("data:image/svg+xml,%3Csvg width='20' height='20' version='1.1' viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='m0 3v14h4v-2h-2v-10h2v-2zm16 0v2h2v10h-2v2h4v-14zm-6 2c-2.41 0-4.43 1.73-4.9 4h2.08c0.41-1.17 1.5-2 2.82-2 1.67 0 3 1.33 3 3h-8c0 2.75 2.25 5 5 5 2.75 0 5-2.25 5-5 0-2.75-2.25-5-5-5zm-2.59 6.5h5.18c-0.516 0.895-1.47 1.5-2.59 1.5-1.12 0-2.07-0.605-2.59-1.5z'/%3E%3C/svg%3E")}`);
	let clicked;
	let openDialog = () => {
		if (window.ipaInputDialog) {
			window.ipaInputDialog.open();
			return;
		}
		if (clicked) return;
		clicked = true;
		mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Nardog/IPAInput-core.js&action=raw&ctype=text/javascript');
		mw.loader.using([
			'jquery.textSelection', 'oojs-ui-windows', 'oojs-ui-widgets',
			'oojs-ui.styles.icons-interactions', 'oojs-ui.styles.icons-editing-core',
			'oojs-ui.styles.icons-editing-advanced'
		]);
	};
	mw.hook('wikiEditor.toolbarReady').add($textarea => {
		$textarea.wikiEditor('addToToolbar', {
			section: 'main',
			group: 'insert',
			tools: {
				dialog: {
					label: 'IPAInput',
					type: 'button',
					oouiIcon: 'schwa',
					action: { type: 'callback', execute: openDialog }
				}
			}
		});
	});
	mw.hook('ve.loadModules').add(addPlugin => {
		addPlugin(() => {
			ve.ui.IpaInputCommand = function VeUiIpaInputCommand() {
				ve.ui.IpaInputCommand.super.call(this, 'ipaInput');
			};
			OO.inheritClass(ve.ui.IpaInputCommand, ve.ui.Command);
			ve.ui.IpaInputCommand.prototype.execute = () => {
				openDialog();
				return true;
			};
			ve.ui.commandRegistry.register(new ve.ui.IpaInputCommand());
			ve.ui.IpaInputTool = function VeUiIpaInputTool() {
				ve.ui.IpaInputTool.super.apply(this, arguments);
			};
			OO.inheritClass(ve.ui.IpaInputTool, ve.ui.Tool);
			ve.ui.IpaInputTool.static.name = 'ipaInput';
			ve.ui.IpaInputTool.static.group = 'insert';
			ve.ui.IpaInputTool.static.icon = 'schwa';
			ve.ui.IpaInputTool.static.title = 'IPA';
			ve.ui.IpaInputTool.static.commandName = 'ipaInput';
			ve.ui.toolFactory.register(ve.ui.IpaInputTool);
		});
	});
	mw.requestIdleCallback(() => {
		let expiry = mw.storage.get('_EXPIRY_ipainput-cache');
		if (!expiry) return;
		$.get(
			'//en.wikipedia.org/api/rest_v1/page/title/Module%3AIPA%2Fdata'
		).then(response => {
			if (Date.parse(response.items[0].timestamp) / 1000 > expiry - 604800) {
				mw.storage.remove('ipainput-cache');
			}
		});
	});
});