Skip to content

Commit

Permalink
feat: 迁移LoginToEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoruaFox committed Feb 20, 2024
1 parent 78bbb6c commit baae49a
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 0 deletions.
175 changes: 175 additions & 0 deletions dist/LoginToEdit/LoginToEdit.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/definition.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* DisambigLinks[ResourceLoader|default|rights=edit|requiresES6]|DisambigLinks.css
* HideConversionTab[ResourceLoader|default|requiresES6]|HideConversionTab.css
* InterwikiTips[ResourceLoader|default|requiresES6]|InterwikiTips.css
* LoginToEdit[ResourceLoader|default|dependencies=ext.gadget.i18n,ext.gadget.Util,mediawiki.util,oojs-ui-windows|hidden|requiresES6]|LoginToEdit.js

== browser ==
* AddSectionPlus[ResourceLoader|dependencies=ext.gadget.Util|peers=AddSectionPlus-pagestyles|rights=edit|requiresES6]|AddSectionPlus.js
Expand Down
7 changes: 7 additions & 0 deletions src/LoginToEdit/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* SPDX-License-Identifier: CC-BY-SA-4.0
* _addText: '{{Gadget Header|license=CC-BY-SA-4.0}}'
*
* @source {@link https://git.qiuwen.net.cn/InterfaceAdmin/QiuwenGadgets/src/branch/master/src/LoginToEdit}
* @license CC-BY-SA-4.0 {@link https://www.qiuwenbaike.cn/wiki/H:CC-BY-SA-4.0}
*/
11 changes: 11 additions & 0 deletions src/LoginToEdit/LoginToEdit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {WG_USER_NAME} from './modules/constant';
import {getBody} from 'ext.gadget.Util';
import {initDialog} from './modules/initDialog';

void getBody().then(function loginToEdit($body: JQuery<HTMLBodyElement>): void {
if (WG_USER_NAME) {
return;
}

initDialog($body);
});
7 changes: 7 additions & 0 deletions src/LoginToEdit/definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"enable": true,
"description": "<sup><abbr title=\"默认为所有用户启用\">D</abbr></sup><span id=\"Gadget-LoginToEdit\"></span> 提示“登录以贡献”",
"default": true,
"dependencies": ["ext.gadget.i18n", "ext.gadget.Util", "mediawiki.util", "oojs-ui-windows"],
"hidden": true
}
8 changes: 8 additions & 0 deletions src/LoginToEdit/modules/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const WG_ACTION: MediaWikiConfigMapWgAction = mw.config.get('wgAction');
const WG_PAGE_NAME: string = mw.config.get('wgPageName');
const WG_SKIN: string = mw.config.get('skin');
const WG_USER_NAME: string | null = mw.config.get('wgUserName');

const IS_WG_EDIT_OR_SUBMIT_ACTION: boolean = ['edit', 'submit'].includes(WG_ACTION);

export {WG_PAGE_NAME, WG_SKIN, WG_USER_NAME, IS_WG_EDIT_OR_SUBMIT_ACTION};
50 changes: 50 additions & 0 deletions src/LoginToEdit/modules/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {localize} from 'ext.gadget.i18n';

const getI18nMessages = () => {
return {
Cancel: localize({
en: 'Cancel',
ja: 'キャンセル',
'zh-hans': '暂不登录账号',
'zh-hant': '暫不登入賬號',
}),
Edit: localize({
en: 'Edit',
ja: '編集',
'zh-hans': '编辑',
'zh-hant': '編輯',
}),
Login: localize({
en: 'Login',
ja: 'ログイン',
'zh-hans': '登录已有账号',
'zh-hant': '登入已有賬號',
}),
Register: localize({
en: 'Register',
ja: 'アカウントを作成',
'zh-hans': '注册新的账号',
'zh-hant': '註冊新的賬號',
}),
DialogTitle: localize({
en: 'Welcome to Youshou Archives!',
ja: '有獣アーカイブスへようこそ!',
'zh-hans': '欢迎来到有兽档案馆!',
'zh-hant': '歡迎來到有獸檔案館!',
}),
DialogMessage: localize({
en: 'You have not yet logged in. Register and log in to your account to contribute.',
ja: 'あなたはまだ有獣アーカイブスにログインしていません。アカウントを作成し、ログインして有獣アーカイブスを改善することができます。',
'zh-hans': '您尚未登录到有兽档案馆。您可以注册并登录账号,帮助完善有兽档案馆。',
'zh-hant': '您尚未登錄到有獸檔案館。您可以注冊並登錄賬戶,幫助完善有獸檔案館。',
}),
};
};

const i18nMessages = getI18nMessages();

const getMessage: GetMessages<typeof i18nMessages> = (key) => {
return i18nMessages[key] || key;
};

export {getMessage};
73 changes: 73 additions & 0 deletions src/LoginToEdit/modules/initDialog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as OPTIONS from '~/AjaxLogin/options.json';
import {IS_WG_EDIT_OR_SUBMIT_ACTION, WG_PAGE_NAME, WG_SKIN} from './constant';
import {generateMessageDialogProperty} from './util/generateMessageDialogProperty';
import {getMessage} from './i18n';
import {initWindowManager} from './initWindowManager';

const isCitizen: boolean = WG_SKIN === 'citizen';
const loginURL: string = mw.util.getUrl('Special:UserLogin', {
returnto: WG_PAGE_NAME,
});
const registerURL: string = mw.util.getUrl('Special:CreateAccount', {
returnto: WG_PAGE_NAME,
});

let messageDialog: OO.ui.MessageDialog;

const initDialog = ($body: JQuery<HTMLBodyElement>): void => {
const windowManager: OO.ui.WindowManager = initWindowManager();
windowManager.$element.appendTo($body);

const messageDialogProperty: OO.ui.WindowManager.WindowOpeningData = generateMessageDialogProperty();

const openDialog = (): void => {
if (messageDialog) {
if (messageDialog.isOpened()) {
messageDialog.close();
} else {
messageDialog.open(messageDialogProperty);
}
return;
}

messageDialog = new OO.ui.MessageDialog();
messageDialog.getActionProcess = (action: string): OO.ui.Process => {
if (action === 'login') {
const $element: JQuery<HTMLAnchorElement> = $(OPTIONS.loginElementSelector);
if ($element.length) {
$element.trigger('click');
} else {
location.href = loginURL;
}
} else if (action === 'register') {
location.href = registerURL;
}
return new OO.ui.Process((): void => {
void windowManager.closeWindow(messageDialog);
});
};

windowManager.addWindows([messageDialog]);
void windowManager.openWindow(messageDialog, messageDialogProperty);
};

const $caViewsource: JQuery = $body.find('#ca-viewsource');
if ($caViewsource.length) {
const editIcon: string = isCitizen ? '<span class="citizen-ui-icon mw-ui-icon-wikimedia-edit"></span>' : '';
$caViewsource
.attr('id', 'ca-edit')
.find('a')
.attr('aria-label', getMessage('DialogMessage'))
.html(editIcon + getMessage('Edit'))
.on('click', (event: JQuery.ClickEvent): void => {
event.preventDefault();
openDialog();
});
}

if (IS_WG_EDIT_OR_SUBMIT_ACTION) {
openDialog();
}
};

export {initDialog};
7 changes: 7 additions & 0 deletions src/LoginToEdit/modules/initWindowManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const initWindowManager = (): OO.ui.WindowManager => {
const windowManager: OO.ui.WindowManager = new OO.ui.WindowManager();

return windowManager;
};

export {initWindowManager};
32 changes: 32 additions & 0 deletions src/LoginToEdit/modules/util/generateMessageDialogProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {getMessage} from '../i18n';

const generateMessageDialogProperty = (): OO.ui.WindowManager.WindowOpeningData => {
const $title: JQuery = $('<b>').addClass('oo-ui-window-head').text(getMessage('DialogTitle'));
const $message: JQuery = $('<span>').addClass('oo-ui-window-foot').text(getMessage('DialogMessage'));

const messageDialogProperty: OO.ui.WindowManager.WindowOpeningData = {
title: $title,
message: $message,
actions: [
{
action: 'login',
flags: ['primary', 'progressive'],
label: $('<b>').text(getMessage('Login')),
},
{
action: 'register',
flags: 'progressive',
label: $('<b>').text(getMessage('Register')),
},
{
action: 'cancel',
flags: ['safe', 'close'],
label: $('<b>').text(getMessage('Cancel')),
},
],
};

return messageDialogProperty;
};

export {generateMessageDialogProperty};

0 comments on commit baae49a

Please sign in to comment.