From d2cedac6cddba1a7db32d4ee078270f0d61ab38e Mon Sep 17 00:00:00 2001 From: Chlod Alejandro Date: Sun, 10 Sep 2023 00:39:35 +0800 Subject: [PATCH] ante: add button to get latest rev of page --- i18n/ante/en.json | 5 + .../PageLatestRevisionGetButton.tsx | 106 ++++++++++++++++++ .../ante/ui/pages/CopiedTemplateRowPage.tsx | 19 +++- 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 src/modules/ante/ui/components/PageLatestRevisionGetButton.tsx diff --git a/i18n/ante/en.json b/i18n/ante/en.json index 43f1920b..5b150be8 100644 --- a/i18n/ante/en.json +++ b/i18n/ante/en.json @@ -50,6 +50,11 @@ "deputy.ante.dateAuto.failed": "Could not pull date from revision: $1", "deputy.ante.dateAuto.missing": "The revision $1 could not be found. Its page may have been deleted.", + "deputy.ante.revisionAuto": "Latest", + "deputy.ante.revisionAuto.title": "Pull the revision ID from the latest (current) revision of the page in `$1`.", + "deputy.ante.revisionAuto.failed": "Could not pull revision ID from page: $1", + "deputy.ante.revisionAuto.missing": "The page $1 could not be found. It may have been deleted.", + "deputy.ante.copied.label": "Copied $1", "deputy.ante.copied.remove": "Remove notice", diff --git a/src/modules/ante/ui/components/PageLatestRevisionGetButton.tsx b/src/modules/ante/ui/components/PageLatestRevisionGetButton.tsx new file mode 100644 index 00000000..3a967121 --- /dev/null +++ b/src/modules/ante/ui/components/PageLatestRevisionGetButton.tsx @@ -0,0 +1,106 @@ +import MwApi from '../../../../MwApi'; +import getApiErrorText from '../../../../wiki/util/getApiErrorText'; + +export interface PageLatestRevisionGetButtonData extends OO.ui.ButtonWidget.ConfigOptions { + titleInputWidget: OO.ui.TextInputWidget; + revisionInputWidget: OO.ui.InputWidget; +} + +let InternalPageLatestRevisionGetButton: any; + +/** + * Initializes the process element. + */ +function initPageLatestRevisionGetButton() { + InternalPageLatestRevisionGetButton = class PageLatestRevisionGetButton + extends OO.ui.ButtonWidget { + + titleInputWidget: OO.ui.TextInputWidget; + revisionInputWidget: OO.ui.InputWidget; + + /** + * @param config Configuration to be passed to the element. + */ + constructor( config: PageLatestRevisionGetButtonData ) { + super( Object.assign( { + icon: 'download', + invisibleLabel: true, + disabled: true + }, config ) ); + + this.titleInputWidget = config.titleInputWidget; + this.revisionInputWidget = config.revisionInputWidget; + + this.titleInputWidget.on( 'change', this.updateButton.bind( this ) ); + this.revisionInputWidget.on( 'change', this.updateButton.bind( this ) ); + this.on( 'click', this.setRevisionFromPageLatestRevision.bind( this ) ); + this.updateButton(); + } + + /** + * Update the disabled state of the button. + */ + updateButton(): void { + this.setDisabled( + this.titleInputWidget.getValue().trim().length === 0 || + this.revisionInputWidget.getValue().trim().length !== 0 || + !( this.titleInputWidget as any ).isQueryValid() + ); + } + + /** + * Set the revision ID from the page provided in the value of + * `this.titleInputWidget`. + */ + async setRevisionFromPageLatestRevision(): Promise { + this + .setIcon( 'ellipsis' ) + .setDisabled( true ); + this.revisionInputWidget.setDisabled( true ); + const title = this.titleInputWidget.getValue(); + await MwApi.action.get( { + action: 'query', + prop: 'revisions', + titles: title, + rvprop: 'ids' + } ).then( ( data ) => { + if ( ( data.query as any ).pages[ 0 ].missing ) { + mw.notify( + mw.msg( 'deputy.ante.revisionAuto.missing', title ), + { type: 'error' } + ); + this.updateButton(); + return; + } + + this.revisionInputWidget.setValue( data.query.pages[ 0 ].revisions[ 0 ].revid ); + this.revisionInputWidget.setDisabled( false ); + this.setIcon( 'download' ); + this.updateButton(); + }, ( _error, errorData ) => { + mw.notify( + + mw.msg( 'deputy.ante.revisionAuto.failed', getApiErrorText( errorData ) ), + { type: 'error' } + ); + this.revisionInputWidget.setDisabled( false ); + this.setIcon( 'download' ); + this.updateButton(); + } ); + } + + }; +} + +/** + * Creates a new PageLatestRevisionGetButton. + * + * @param config Configuration to be passed to the element. + * @return A PageLatestRevisionGetButton object + */ +export default function ( config: PageLatestRevisionGetButtonData ) { + if ( !InternalPageLatestRevisionGetButton ) { + initPageLatestRevisionGetButton(); + } + return new InternalPageLatestRevisionGetButton( config ); +} diff --git a/src/modules/ante/ui/pages/CopiedTemplateRowPage.tsx b/src/modules/ante/ui/pages/CopiedTemplateRowPage.tsx index 70f96fc0..53bba28d 100644 --- a/src/modules/ante/ui/pages/CopiedTemplateRowPage.tsx +++ b/src/modules/ante/ui/pages/CopiedTemplateRowPage.tsx @@ -14,6 +14,7 @@ import normalizeTitle from '../../../../wiki/util/normalizeTitle'; import equalTitle from '../../../../util/equalTitle'; import RevisionDateGetButton from '../components/RevisionDateGetButton'; import SmartTitleInputWidget from '../components/SmartTitleInputWidget'; +import PageLatestRevisionGetButton from '../components/PageLatestRevisionGetButton'; export interface CopiedTemplateRowPageData { /** @@ -335,6 +336,20 @@ function initCopiedTemplateRowPage() { revisionInputWidget: this.inputs.to_diff, dateInputWidget: this.inputs.date } ); + const revisionAutoFrom = PageLatestRevisionGetButton( { + invisibleLabel: false, + label: mw.msg( 'deputy.ante.revisionAuto' ), + title: mw.msg( 'deputy.ante.revisionAuto.title', 'from' ), + titleInputWidget: this.inputs.from, + revisionInputWidget: this.inputs.from_oldid + } ); + const revisionAutoTo = PageLatestRevisionGetButton( { + invisibleLabel: false, + label: mw.msg( 'deputy.ante.revisionAuto' ), + title: mw.msg( 'deputy.ante.revisionAuto.title', 'to' ), + titleInputWidget: this.inputs.to, + revisionInputWidget: this.inputs.to_diff + } ); this.fieldLayouts = { from: new OO.ui.FieldLayout( this.inputs.from, { @@ -343,7 +358,7 @@ function initCopiedTemplateRowPage() { align: 'top', help: mw.msg( 'deputy.ante.copied.from.help' ) } ), - from_oldid: new OO.ui.FieldLayout( this.inputs.from_oldid, { + from_oldid: new OO.ui.ActionFieldLayout( this.inputs.from_oldid, revisionAutoFrom, { $overlay: this.parent.$overlay, label: mw.msg( 'deputy.ante.copied.from_oldid.label' ), align: 'left', @@ -355,7 +370,7 @@ function initCopiedTemplateRowPage() { align: 'top', help: mw.msg( 'deputy.ante.copied.to.help' ) } ), - to_diff: new OO.ui.FieldLayout( this.inputs.to_diff, { + to_diff: new OO.ui.ActionFieldLayout( this.inputs.to_diff, revisionAutoTo, { $overlay: this.parent.$overlay, label: mw.msg( 'deputy.ante.copied.to_diff.label' ), align: 'left',