Jump to content

User:Nardog/IPAInput.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nardog (talk | contribs) at 14:19, 19 October 2022 (more reliable VisualEditor detection). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
(function ipaInput() {
	let addButton = () => {
		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 opening;
		let openDialog = () => {
			if (window.ipaInputDialog) {
				window.ipaInputDialog.open();
				return;
			}
			if (opening) return;
			opening = true;
			mw.loader.load('//test.wikipedia.org/w/index.php?title=User:Nardog/sandbox4.js&action=raw&ctype=text/javascript');
		};
		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);
			});
		});
	};
	if (['edit', 'submit'].includes(mw.config.get('wgAction'))) {
		addButton();
		return;
	}
	mw.loader.using('ext.visualEditor.desktopArticleTarget.init', () => {
		if (mw.libs.ve.isVisualAvailable) addButton();
	});
})();