From 29e0b574b77b954c14cddd7627bbb815abbc8d1d Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Wed, 9 Aug 2023 07:51:26 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E8=87=AA=E5=8A=A8=E8=A1=A5=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extension.json | 1 + .../ext.isekai.createPage.base.less | 12 ++- modules/createPage/ext.isekai.createPage.js | 2 +- .../createPage/ext.isekai.createPageInput.js | 83 +++++++++++++++++++ src/createPage/ext.isekai.createPage.js | 2 +- 5 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 modules/createPage/ext.isekai.createPageInput.js diff --git a/extension.json b/extension.json index adcdd9c..01f762e 100644 --- a/extension.json +++ b/extension.json @@ -37,6 +37,7 @@ }, "ext.isekai.createPage": { "scripts": [ + "createPage/ext.isekai.createPageInput.js", "createPage/ext.isekai.createPage.js", "createPage/ext.isekai.createPage.base.js" ], diff --git a/modules/createPage/ext.isekai.createPage.base.less b/modules/createPage/ext.isekai.createPage.base.less index e357361..dfc2548 100644 --- a/modules/createPage/ext.isekai.createPage.base.less +++ b/modules/createPage/ext.isekai.createPage.base.less @@ -2,13 +2,16 @@ @text-size: 0.95em; .isekai-create-page { + &.isekai-card { + overflow: visible; + } + .card-body { flex: 1 1 auto; padding: 0.25em; font-size: 1.25em; .card-content { - overflow-y: auto; padding: 1em 0.5em; margin: 0 0.4em; min-height: @height; @@ -26,5 +29,10 @@ max-width: none; } } - } + } + + .oo-ui-menuSelectWidget { + // 修复重叠问题 + z-index: 100; + } } \ No newline at end of file diff --git a/modules/createPage/ext.isekai.createPage.js b/modules/createPage/ext.isekai.createPage.js index 4d412d2..9efea96 100644 --- a/modules/createPage/ext.isekai.createPage.js +++ b/modules/createPage/ext.isekai.createPage.js @@ -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;r0?(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}())})(); \ No newline at end of file +(()=>{"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;r0?(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}())})(); \ No newline at end of file diff --git a/modules/createPage/ext.isekai.createPageInput.js b/modules/createPage/ext.isekai.createPageInput.js new file mode 100644 index 0000000..b83fff5 --- /dev/null +++ b/modules/createPage/ext.isekai.createPageInput.js @@ -0,0 +1,83 @@ +if ( !window.isekai ) { + window.isekai = { + ui: {} + }; +} +if ( !window.isekai.ui ) { + window.isekai.ui = {}; +} + +/** + * Input for create page and auto complete parent page + * @class + * @extends OO.ui.TextInputWidget + * @mixins OO.ui.mixin.LookupElement + * + * @constructor + * @param {Object} config Configuration options + */ +isekai.ui.CreatePageInputWidget = function IsekaiUiCreatePageInputWidget( config ) { + // Parent constructor + OO.ui.TextInputWidget.call( this, config ); + // Mixin constructors + OO.ui.mixin.LookupElement.call( this, config ); + + this.api = new mw.Api(); + + this.loadedPageList = []; +}; +OO.inheritClass( isekai.ui.CreatePageInputWidget, OO.ui.TextInputWidget ); +OO.mixinClass( isekai.ui.CreatePageInputWidget, OO.ui.mixin.LookupElement ); + +/** + * @inheritdoc + */ +isekai.ui.CreatePageInputWidget.prototype.getLookupRequest = function () { + var + value = this.getValue(), + deferred = $.Deferred(); + var api = this.api; + var _this = this; + this.getValidity().then( function () { + api.get( { + action: "opensearch", + search: value, + } ).done( function (data) { + if ( Array.isArray(data) && data.length > 0 ) { + var pageList = data[1]; + + deferred.resolve( pageList ); + + // Set cache + pageList.forEach( function (pageTitle) { + if ( !_this.loadedPageList.includes( pageTitle ) ) { + _this.loadedPageList.push( pageTitle ); + } + } ); + } else { + deferred.resolve( [] ); + } + } ); + }, function () { + // No results when the input contains invalid content + deferred.resolve( [] ); + } ); + return deferred.promise( { abort: function () {} } ); +}; +/** + * @inheritdoc + */ +isekai.ui.CreatePageInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) { + return response || []; +}; +/** + * @inheritdoc + */ +isekai.ui.CreatePageInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) { + return ( data || [] ).map( function (one) { + return new OO.ui.MenuOptionWidget( { + data: one + "/", + label: one + } ) + } ); +}; \ No newline at end of file diff --git a/src/createPage/ext.isekai.createPage.js b/src/createPage/ext.isekai.createPage.js index 0ec3cad..874e123 100644 --- a/src/createPage/ext.isekai.createPage.js +++ b/src/createPage/ext.isekai.createPage.js @@ -12,7 +12,7 @@ class CreatePageWidget { } initDom() { - this.pageNameInput = new OO.ui.TextInputWidget({ + this.pageNameInput = new isekai.ui.CreatePageInputWidget({ placeholder: mw.message('isekai-createpage-page-title').parse(), }); this.pageNameInput.on('enter', this.createPage.bind(this));