跳转到内容

User:Addonian1123/QuickEdit-settings.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
 * QuickEdit+ Settings GUI
 * Main: [[User:Addonian1123/QuickEdit.js]]
 */
/*
var _addText="{{工具警告|QuickEdit+}}";
var _addText="{{Notice|<big>'''文檔請見:[[User:Addonian1123/QuickEdit+]]'''</big>}}";
var _addText="{{Notice|<big>'''【QuickEdit+】組件'''</big><br>主JavaScript:[[User:Addonian1123/QuickEdit.js]]<br>Core核心:[[User:Addonian1123/QuickEdit-core.js]]<br>CSS樣式表:[[User:Addonian1123/QuickEdit-core.css]]<br><!--Config-未啟用:[[User:Addonian1123/QuickEdit-config.js]]<br>-->GUI設定:[[User:Addonian1123/QuickEdit-settings.js]]}}";
*/

(function () {
	'use strict';

	const userLang = mw.config.get('wgUserLanguage') || mw.config.get('wgUserVariant') || 'zh-hant';
	const isHans = ['zh-hans', 'zh-cn', 'zh-my', 'zh-sg'].some(l => userLang.toLowerCase().startsWith(l));

	const msg = {
		dialogTitle: isHans ? 'QuickEdit+设置' : 'QuickEdit+設定',
		saveLabel: isHans ? '保存设置' : '儲存設定',
		cancelLabel: isHans ? '取消' : '取消',
		sectionEditHeader: isHans ? '编辑行为设置' : '編輯行為設定',
		multiEditLabel: isHans ? '[Beta] 允许多章节编辑' : '[Beta] 允許多章節編輯',
		multiEditHelp: isHans ? '开启后,编辑新章节不会自动关闭其他已打开的编辑框' : '開啟後,編輯新章節不會自動關閉其他已開啟的編輯框',
		displayHeader: isHans ? '显示设置' : '顯示設定',
		editNoticeLabel: isHans ? '显示通知' : '顯示通知',
		editNoticeHelp: isHans ? '保存更改或不保存更改时显示通知' : '發布變更或捨棄變更時顯示通知',
		debugHeader: isHans ? '高级设置' : '進階設定',
		debugLabel: isHans ? '侦错日志' : '偵錯日誌',
		debugHelp: isHans ? '在浏览器控制台输出QuickEdit+的运行日志' : '在瀏覽器控制台輸出QuickEdit+的執行日誌',
		saveSuccess: isHans ? 'QuickEdit+设置已成功保存,请刷新网页以查看更改。' : 'QuickEdit+設定已成功儲存,請重新整理網頁以生效。',
		portletTitle: isHans ? 'QE+设置' : 'QE+設定',
		portletTooltip: isHans ? '打开QuickEdit+设置界面' : '開啟QuickEdit+設定介面',
		warnHeader: isHans ? '注意:' : '注意:',
		multiWarn: isHans 
			? '同时开启多个章节编辑可能增加编辑冲突的风险,保存时请留意版本變更。' 
			: '同時開啟多個章節編輯可能增加編輯衝突的風險,儲存時請留意版本變更。',
		about: isHans ?
			$('<span><b><a href="' + mw.util.getUrl('User:Addonian1123/QuickEdit+') + '" target="_blank">QuickEdit+</a></b> | 版本号1.1a [<a href="' + mw.util.getUrl('User:Addonian1123/QuickEdit+#1.1a') + '" target="_blank">更多信息</a>]</span>') :
			$('<span><b><a href="' + mw.util.getUrl('User:Addonian1123/QuickEdit+') + '" target="_blank">QuickEdit+</a></b> | 版本1.1a [<a href="' + mw.util.getUrl('User:Addonian1123/QuickEdit+#1.1a') + '" target="_blank">了解更多</a>]</span>'),
		resetLabel: isHans ? '清除所有设置' : '清除所有設定',
		resetConfirm: isHans ? '确定要清除所有已保存的QuickEdit+设置吗?' : '確定要清除所有已儲存的QuickEdit+設定嗎?',
		resetSuccess: isHans ? '设置已成功清除,请刷新网页以生效。' : '設定已成功清除,請重新整理網頁以生效。',
	};

	function openQESettingsDialog() {
		function QESettingsDialog(config) {
			QESettingsDialog.super.call(this, config);
		}
		OO.inheritClass(QESettingsDialog, OO.ui.ProcessDialog);
		QESettingsDialog.static.name = 'qeSettingsDialog';
		QESettingsDialog.static.title = msg.dialogTitle;
		QESettingsDialog.static.actions = [
			{ action: 'save', label: msg.saveLabel, flags: ['primary', 'progressive'] },
			{ label: msg.cancelLabel, flags: 'safe' }
		];

		QESettingsDialog.prototype.initialize = function () {
			QESettingsDialog.super.prototype.initialize.apply(this, arguments);

			this.content = new OO.ui.PanelLayout({
				padded: true,
				expanded: false
			});

			// <hr>
			const hr = $('<hr>').css({
				'border': 'none',
				'border-top': '1px solid #a2a9b1',
				'margin': '12px 0'
			});
			
			// --- 編輯 ---
			const editFieldset = new OO.ui.FieldsetLayout({
				label: msg.sectionEditHeader
			});

			this.multiEditToggle = new OO.ui.ToggleSwitchWidget({
				value: localStorage.getItem('qe_multi_edit') === 'true'
			});

			const multiEditField = new OO.ui.FieldLayout(this.multiEditToggle, {
				label: msg.multiEditLabel,
				help: msg.multiEditHelp,
				align: 'top'
			});
			
	
				// --- 警告 ---
					const multiWarn = new OO.ui.MessageWidget({
					type: 'warning',
					inline: false,
					label: msg.multiWarn
				});
				// 邊距
				multiWarn.$element.css({ 'margin-top': '12px' });

			editFieldset.addItems([multiEditField, multiWarn]);

			// --- 顯示 ---

			const displayFieldset = new OO.ui.FieldsetLayout({
				label: msg.displayHeader
			});

			this.editNoticeToggle = new OO.ui.ToggleSwitchWidget({
				value: localStorage.getItem('qe_edit_notice') !== 'false'
			});

			const editNoticeField = new OO.ui.FieldLayout(this.editNoticeToggle, {
				label: msg.editNoticeLabel,
				help: msg.editNoticeHelp,
				align: 'top'
			});

			displayFieldset.addItems([editNoticeField]);

			// --- Debug ---
			const debugFieldset = new OO.ui.FieldsetLayout({
				label: msg.debugHeader
			});

			this.debugToggle = new OO.ui.ToggleSwitchWidget({
				value: localStorage.getItem('qeDebug') === '1'
			});

			const debugField = new OO.ui.FieldLayout(this.debugToggle, {
				label: msg.debugLabel,
				help: msg.debugHelp,
				align: 'top'
			});

			debugFieldset.addItems([debugField]);

			// 重設
			QESettingsDialog.static.name = 'qeSettingsDialog';
			QESettingsDialog.static.title = msg.dialogTitle;
			
			QESettingsDialog.static.actions = [
				{ action: 'save', label: msg.saveLabel, flags: ['primary', 'progressive'] },
				{ action: 'reset', label: msg.resetLabel, flags: ['destructive'] },
				{ label: msg.cancelLabel, flags: 'safe' }
			];
			
			// 關於
				const about = new OO.ui.MessageWidget({
				type: 'notice',
				inline: false,
				label: msg.about
			});
			// 邊距
			about.$element.css({ 'margin-top': '12px' });

			// 組裝元件
			this.content.$element.append(
				editFieldset.$element,
				displayFieldset.$element,
				debugFieldset.$element,
				hr,
				about.$element,
			);
			this.$body.append(this.content.$element);
		};

	// 處理按鈕觸發程序
	QESettingsDialog.prototype.getActionProcess = function (action) {
		// 儲存
		if (action === 'save') {
			return new OO.ui.Process(() => {
				const multiEditVal = this.multiEditToggle.getValue();
				const editNoticeVal = this.editNoticeToggle.getValue();
				const debugVal = this.debugToggle.getValue();
	
				// 寫入 localStorage
				localStorage.setItem('qe_multi_edit', multiEditVal ? 'true' : 'false');
				localStorage.setItem('qe_edit_notice', editNoticeVal ? 'true' : 'false');
				localStorage.setItem('qeDebug', debugVal ? '1' : '0');
	
				// 全域變數同步
				window.qeDebug = debugVal;
	
				mw.notify(msg.saveSuccess, { type: 'success' });
				this.close();
			}, this);
		}
	
		// 清除設定
		if (action === 'reset') {
			return new OO.ui.Process(() => {
				// 先關閉設定視窗
				this.close();
		
				// 先視窗關閉後開確認框
				OO.ui.confirm(msg.resetConfirm, {
					actions: [
						{ action: 'accept', label: isHans ? '清除设置' : '清除設定', flags: ['destructive'] },
						{ action: 'reject', label: msg.cancelLabel, flags: 'safe' }
					]
			}).then((confirmed) => {
					if (confirmed) {
						// 點擊確定清除localStorage
						const qeKeys = [
							'qe_multi_edit',
							'qe_edit_notice',
							'qe_enable_summary_buttons',
							'qe_enable_url_decoder',
							'qe_enable_wikificator',
							'qeDebug'
						];
		
						qeKeys.forEach(key => localStorage.removeItem(key));
						delete window.qeDebug;
		
						mw.notify(msg.resetSuccess, { type: 'success' });
					} else {
						// 點擊取消重新開啟
						openQESettingsDialog();
					}
				});
			}, this);
		}
	
		// 其他預設/取消動作交給父類別處理
		return QESettingsDialog.super.prototype.getActionProcess.call(this, action);
	};

		// 實例化並開啟視窗
		const windowManager = OO.ui.getWindowManager();
		const dialog = new QESettingsDialog({ size: 'medium' });
		
		windowManager.addWindows([dialog]);
		windowManager.openWindow(dialog);
	}

	// 註冊全域命名空間(方便core腳本或其他模組主動呼叫)
	mw.libs = mw.libs || {};
	mw.libs.QuickEditSettings = {
		open: openQESettingsDialog
	};

	// 載入必要模組並注入右上角工具列按鈕
	mw.loader.using(['mediawiki.util', 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']).then(function () {
	$(function () {
		const link = mw.util.addPortletLink(
			'p-tb',
			'#',
			msg.portletTitle,
			't-qe-settings',
			msg.portletTooltip,
			null,
			'#t-preferences'
		);

		if (link) {
			const $a = $(link).find('a');

			/*const $icon = $(document.createElementNS('http://www.w3.org/2000/svg', 'svg'))
				.attr({
					'width': '18',
					'height': '18',
					'viewBox': '0 0 20 20',
					'aria-hidden': 'true'
				})
				.css({
					'fill': 'var(--color-base, #202122)',
					'margin-right': '6px',
					'vertical-align': 'middle',
					'flex-shrink': '0',
				})
				.html('<path d="M10 14.4286L16.4 14.4286L16.4 7L14.6 7L14.6 14.4286L11 14.4286L15.5 20L20 14.4286Z" /><path fill-rule="evenodd" clip-rule="evenodd" d="M16.4 7.63119V7H15.7688L16.4 7.63119ZM14.6 10.1498L15.7094 9.04044L14.6 7.93109V10.1498ZM13.7688 5L18.4 5V6.31934L18.7092 6.00057C18.8955 5.81322 19 5.55978 19 5.2956C19 5.03143 18.8955 4.77799 18.7092 4.59063L15.3694 1.29078C15.182 1.10454 14.9286 1 14.6644 1C14.4002 1 14.1468 1.10454 13.9594 1.29078L11.9995 3.23069L13.7688 5ZM9.27453 15.4753L5.74979 19H1V14.2502L10.9596 4.29065L12.6 5.93109V12.1498L12.3212 12.4286H6.81374L9.27453 15.4753Z" />');*/

			const $existingIcon = $a.find('.vector-icon');
				/*if ($existingIcon.length) {
				$existingIcon.replaceWith($icon);
				} else {*/
				$a/*.prepend($icon)*/;
			//}

			$a.css({
				'display': 'inline-flex',
				'align-items': 'center'
			});

			$(link).on('click', function (e) {
				e.preventDefault();

			if (document.activeElement && typeof document.activeElement.blur === 'function') {
				document.activeElement.blur();
			}
				
				openQESettingsDialog();
			});
		}
	});
});
})();