Skip to content

Commit

Permalink
ante: add button to get latest rev of page
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Sep 9, 2023
1 parent e14f49b commit d2cedac
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
5 changes: 5 additions & 0 deletions i18n/ante/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
106 changes: 106 additions & 0 deletions src/modules/ante/ui/components/PageLatestRevisionGetButton.tsx
Original file line number Diff line number Diff line change
@@ -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<void> {
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 );
}
19 changes: 17 additions & 2 deletions src/modules/ante/ui/pages/CopiedTemplateRowPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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, {
Expand All @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit d2cedac

Please sign in to comment.