新建页面框增加namespace选择功能

master
落雨楓 5 months ago
parent f47a0f245f
commit b7b0a6f490

@ -246,6 +246,9 @@
},
"IsekaiGlobalWidgets": {
"value": ["baseWidgets", "offcanvasTOC"]
},
"IsekaiCreatePageNamespaces": {
"value": []
}
},
"manifest_version": 2

@ -1,6 +1,8 @@
<?php
namespace Isekai\Widgets;
use MediaWiki\MediaWikiServices;
class CreatePageWidget {
public static function getHtml() {
ob_start();
@ -10,6 +12,11 @@ class CreatePageWidget {
}
public static function create($text, $params, \Parser $parser, \PPFrame $frame) {
$config = MediaWikiServices::getInstance()->getMainConfig();
$configCreatePageNamespaces = $config->get('IsekaiCreatePageNamespaces');
$parser->getOutput()->setJsConfigVar('wgIsekaiCreatePageNamespaces', $configCreatePageNamespaces);
$parser->getOutput()->addModules(['ext.isekai.createPage']);
return self::getHtml();

@ -30,6 +30,18 @@
.oo-ui-actionFieldLayout.oo-ui-fieldLayout-align-top {
max-width: none;
}
.oo-ui-layout.oo-ui-horizontalLayout {
display: flex;
> .oo-ui-dropdownInputWidget {
width: 10em;
}
> .oo-ui-actionFieldLayout {
width: 100%;
}
}
}
}
}

@ -1 +1 @@
(()=>{"use strict";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(t,i){for(var r=0;r<i.length;r++){var a=i[r];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(t,(void 0,n=function(t,i){if("object"!==e(t)||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var a=r.call(t,"string");if("object"!==e(a))return a;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(a.key),"symbol"===e(n)?n:String(n)),a)}var n}!function(e,t){var i="ui.CreatePageWidget".split(".");"isekai"in window||(window.isekai={});for(var r=window.isekai,a=0;a<i.length-1;a++){var n=i[a];n in r||(r[n]={}),r=r[n]}r[i[a]]=t}(0,function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.baseDom=t,this.pageUrl=null,this.api=new mw.Api,this.hasError=!1,this.initDom()}var i,r;return i=e,(r=[{key:"initDom",value:function(){this.pageNameInput=new isekai.ui.CreatePageInputWidget({placeholder:mw.message("isekai-createpage-page-title").parse()}),this.pageNameInput.on("enter",this.createPage.bind(this)),this.pageNameInput.on("change",this.onPageNameChange.bind(this)),this.createButton=new OO.ui.ButtonWidget({label:mw.message("isekai-createpage-create-page-button").parse(),flags:["primary","progressive"]}),this.createButton.on("click",this.createPage.bind(this)),this.formGroup=new OO.ui.ActionFieldLayout(this.pageNameInput,this.createButton,{align:"top"}),this.baseDom.find(".card-body .card-content").append(this.formGroup.$element)}},{key:"createPage",value:function(){var e=this,t=this.pageNameInput.getValue();this.hasError&&this.clearError(),t.trim().length>0?(this.createButton.setDisabled(!0),this.pageExists(t).then((function(i){if(i)e.createButton.setDisabled(!1),e.setError(mw.message("isekai-createpage-page-exists").parse());else{var r=mw.util.getUrl(t,{veaction:"edit"});e.formGroup.setSuccess([mw.message("isekai-createpage-redirecting").parse()]),location.href=r}}))):this.setError(mw.message("isekai-createpage-title-empty").parse())}},{key:"onPageNameChange",value:function(){this.hasError&&this.clearError();var e=this.pageNameInput.getValue();if(-1!==e.indexOf("")||-1!==e.indexOf("`")){var t=this.pageNameInput.getRange();e=e.replace(//g,":").replace(/`/g,"·"),this.pageNameInput.setValue(e),this.pageNameInput.selectRange(t.from,t.to)}}},{key:"setError",value:function(e){this.formGroup.setErrors([e]),this.hasError=!0}},{key:"clearError",value:function(){this.formGroup.setErrors([]),this.hasError=!1}},{key:"pageExists",value:function(e){var t=this;return new Promise((function(i,r){t.api.get({action:"query",titles:e}).done((function(e){e.query&&e.query.pages?e.query.pages[-1]?i(!1):i(!0):i(!1)})).fail(r)}))}},{key:"setTitle",value:function(e){this.title.text(e)}}])&&t(i.prototype,r),Object.defineProperty(i,"prototype",{writable:!1}),e}())})();
(()=>{"use strict";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(t,i){for(var a=0;a<i.length;a++){var r=i[a];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(void 0,n=function(t,i){if("object"!==e(t)||null===t)return t;var a=t[Symbol.toPrimitive];if(void 0!==a){var r=a.call(t,"string");if("object"!==e(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(r.key),"symbol"===e(n)?n:String(n)),r)}var n}!function(e,t){var i="ui.CreatePageWidget".split(".");"isekai"in window||(window.isekai={});for(var a=window.isekai,r=0;r<i.length-1;r++){var n=i[r];n in a||(a[n]={}),a=a[n]}a[i[r]]=t}(0,function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.baseDom=t,this.pageUrl=null,this.api=new mw.Api,this.hasError=!1,this.initDom()}var i,a;return i=e,(a=[{key:"initDom",value:function(){var e=mw.config.get("wgIsekaiCreatePageNamespaces");Array.isArray(e)&&0!==e.length||(e=[{name:"Main",prefix:""}]),this.selectNamespaceDropdown=new OO.ui.DropdownInputWidget({options:e.map((function(e){return{data:e.prefix,label:e.name}}))}),this.selectNamespaceDropdown.on("change",this.onNamespaceChange.bind(this)),this.pageNameInput=new isekai.ui.CreatePageInputWidget({placeholder:mw.message("isekai-createpage-page-title").parse()}),this.pageNameInput.on("enter",this.createPage.bind(this)),this.pageNameInput.on("change",this.onPageNameChange.bind(this)),this.createButton=new OO.ui.ButtonWidget({label:mw.message("isekai-createpage-create-page-button").parse(),flags:["primary","progressive"]}),this.createButton.on("click",this.createPage.bind(this)),this.inputGroup=new OO.ui.ActionFieldLayout(this.pageNameInput,this.createButton,{align:"top"}),this.formGroup=new OO.ui.HorizontalLayout({items:[this.selectNamespaceDropdown,this.inputGroup]}),this.baseDom.find(".card-body .card-content").append(this.formGroup.$element)}},{key:"createPage",value:function(){var e=this,t=this.selectNamespaceDropdown.getValue()+this.pageNameInput.getValue();console.log(t),this.hasError&&this.clearError(),t.trim().length>0?(this.createButton.setDisabled(!0),this.pageExists(t).then((function(i){if(i)e.createButton.setDisabled(!1),e.setError(mw.message("isekai-createpage-page-exists").parse());else{var a=mw.util.getUrl(t,{veaction:"edit"});e.inputGroup.setSuccess([mw.message("isekai-createpage-redirecting").parse()]),location.href=a}}))):this.setError(mw.message("isekai-createpage-title-empty").parse())}},{key:"onPageNameChange",value:function(){this.hasError&&this.clearError();var e=this.pageNameInput.getValue();if(-1!==e.indexOf("")||-1!==e.indexOf("`")){var t=this.pageNameInput.getRange();e=e.replace(//g,":").replace(/`/g,"·"),this.pageNameInput.setValue(e),this.pageNameInput.selectRange(t.from,t.to)}}},{key:"onNamespaceChange",value:function(){this.pageNameInput.setPagePrefix(this.selectNamespaceDropdown.getValue())}},{key:"setError",value:function(e){this.inputGroup.setErrors([e]),this.hasError=!0}},{key:"clearError",value:function(){this.inputGroup.setErrors([]),this.hasError=!1}},{key:"pageExists",value:function(e){var t=this;return new Promise((function(i,a){t.api.get({action:"query",titles:e}).done((function(e){e.query&&e.query.pages?e.query.pages[-1]?i(!1):i(!0):i(!1)})).fail(a)}))}},{key:"setTitle",value:function(e){this.title.text(e)}}])&&t(i.prototype,a),Object.defineProperty(i,"prototype",{writable:!1}),e}())})();

@ -24,11 +24,17 @@ isekai.ui.CreatePageInputWidget = function IsekaiUiCreatePageInputWidget( config
this.api = new mw.Api();
this.pagePrefix = config.pagePrefix || "";
this.loadedPageList = [];
};
OO.inheritClass( isekai.ui.CreatePageInputWidget, OO.ui.TextInputWidget );
OO.mixinClass( isekai.ui.CreatePageInputWidget, OO.ui.mixin.LookupElement );
isekai.ui.CreatePageInputWidget.prototype.setPagePrefix = function ( pagePrefix ) {
this.pagePrefix = pagePrefix;
this.requestCache = {}; // Clear cache
}
/**
* @inheritdoc
*/
@ -38,14 +44,19 @@ isekai.ui.CreatePageInputWidget.prototype.getLookupRequest = function () {
deferred = $.Deferred();
var api = this.api;
var _this = this;
var pageName = this.pagePrefix + value;
this.getValidity().then( function () {
api.get( {
action: "opensearch",
search: value,
search: pageName,
} ).done( function (data) {
if ( Array.isArray(data) && data.length > 0 ) {
var pageList = data[1];
pageList = pageList.map( function (pageTitle) {
return pageTitle.substring( _this.pagePrefix.length );
} );
deferred.resolve( pageList );
// Set cache

File diff suppressed because it is too large Load Diff

@ -12,6 +12,24 @@ class CreatePageWidget {
}
initDom() {
let namespaces = mw.config.get('wgIsekaiCreatePageNamespaces');
if (!Array.isArray(namespaces) || namespaces.length === 0) {
namespaces = [
{
name: 'Main',
prefix: '',
}
];
}
this.selectNamespaceDropdown = new OO.ui.DropdownInputWidget({
options: namespaces.map((nsInfo) => ({
data: nsInfo.prefix,
label: nsInfo.name,
})),
});
this.selectNamespaceDropdown.on('change', this.onNamespaceChange.bind(this));
this.pageNameInput = new isekai.ui.CreatePageInputWidget({
placeholder: mw.message('isekai-createpage-page-title').parse(),
});
@ -27,14 +45,23 @@ class CreatePageWidget {
});
this.createButton.on('click', this.createPage.bind(this));
this.formGroup = new OO.ui.ActionFieldLayout(this.pageNameInput, this.createButton, {
this.inputGroup = new OO.ui.ActionFieldLayout(this.pageNameInput, this.createButton, {
align: 'top'
});
this.formGroup = new OO.ui.HorizontalLayout({
items: [
this.selectNamespaceDropdown,
this.inputGroup
]
});
this.baseDom.find('.card-body .card-content').append(this.formGroup.$element);
}
createPage() {
let title = this.pageNameInput.getValue();
let title = this.selectNamespaceDropdown.getValue() + this.pageNameInput.getValue();
console.log(title);
if (this.hasError) {
this.clearError(); //清除errors
}
@ -46,7 +73,7 @@ class CreatePageWidget {
this.setError(mw.message('isekai-createpage-page-exists').parse()); //提示页面已经存在
} else {
let targetUrl = mw.util.getUrl(title, { veaction: 'edit' });
this.formGroup.setSuccess([
this.inputGroup.setSuccess([
mw.message('isekai-createpage-redirecting').parse()
]); //提示正在跳转
location.href = targetUrl;
@ -71,13 +98,17 @@ class CreatePageWidget {
}
}
onNamespaceChange() {
this.pageNameInput.setPagePrefix(this.selectNamespaceDropdown.getValue());
}
setError(msg) {
this.formGroup.setErrors([msg]); //提示页面已经存在
this.inputGroup.setErrors([msg]); //提示页面已经存在
this.hasError = true;
}
clearError() {
this.formGroup.setErrors([]);
this.inputGroup.setErrors([]);
this.hasError = false;
}

Loading…
Cancel
Save