Skip to content

Commit

Permalink
backwardscopy probably done
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Aug 4, 2022
1 parent fb3bb9b commit 7e58e22
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 44 deletions.
8 changes: 6 additions & 2 deletions src/modules/cte/models/templates/CopiedTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ export default class CopiedTemplate
* Saves the current template data to the Parsoid element.
*/
save() {
this.node.setParameter( 'collapse', yesNo( this.collapsed ) ? 'yes' : null );
this.node.setParameter( 'small', yesNo( this.small ) ? 'yes' : null );
if ( this.collapsed ) {
this.node.setParameter( 'collapse', yesNo( this.collapsed ) ? 'yes' : null );
}
if ( this.small ) {
this.node.setParameter( 'small', yesNo( this.small ) ? 'yes' : null );
}

const existingParameters = this.node.getParameters();
for ( const param in existingParameters ) {
Expand Down
8 changes: 5 additions & 3 deletions src/modules/cte/models/templates/MergedFromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ export default class MergedFromTemplate
save() {
this.node.setParameter( '1', this.article );
this.node.setParameter( '2', this.date );
this.node.setParameter(
'talk', yesNo( this.talk ) ? null : 'no'
);
if ( this.talk ) {
this.node.setParameter(
'talk', yesNo( this.talk ) ? null : 'no'
);
}
this.node.setParameter(
'target', ( this.target ?? '' ).length > 0 ? this.target : null
);
Expand Down
8 changes: 5 additions & 3 deletions src/modules/cte/models/templates/MergedToTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ export default class MergedToTemplate

this.node.setParameter( '1', this.to );
this.node.setParameter( '2', this.date );
this.node.setParameter(
'small', yesNo( this.small, false ) ? 'yes' : null
);
if ( this.small ) {
this.node.setParameter(
'small', yesNo( this.small ) ? 'yes' : null
);
}

this.dispatchEvent( new Event( 'save' ) );
}
Expand Down
4 changes: 3 additions & 1 deletion src/modules/cte/models/templates/SplitArticleTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export default class SplitArticleTemplate
}
}

this.node.setParameter( 'collapse', yesNo( this.collapse ) ? 'yes' : null );
if ( this.collapse ) {
this.node.setParameter( 'collapse', yesNo( this.collapse ) ? 'yes' : null );
}
this.node.setParameter( 'from', this.from );

this._rows.forEach( ( row, i ) => {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/cte/ui/RowPageShared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function renderPreviewPanel( template: AttributionNotice ): JSX.Element {
} );

// Infuse collapsibles
( $( previewPanel ).find( '.mw-collapsible' ) as any )
( $( previewPanel ).find( '.collapsible, .mw-collapsible' ) as any )
.makeCollapsible();
} );
}, 1000 );
Expand Down
2 changes: 1 addition & 1 deletion src/modules/cte/ui/pages/BackwardsCopyTemplatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function initBackwardsCopyTemplatePage() {
placeholder: mw.message(
'deputy.cte.backwardsCopy.id.placeholder'
).text(),
value: this.backwardsCopyTemplate.id?.trim()
value: this.backwardsCopyTemplate.revid?.trim()
} )
};
const fields = {
Expand Down
74 changes: 41 additions & 33 deletions src/modules/cte/ui/pages/BackwardsCopyTemplateRowPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import unwrapWidget from '../../../../util/unwrapWidget';
import CopiedTemplateEditorDialog from '../CopiedTemplateEditorDialog';
import { AttributionNoticePageLayout } from './AttributionNoticePageLayout';
import getObjectValues from '../../../../util/getObjectValues';
import matchAll from '../../../../util/matchAll';

export interface BackwardsCopyTemplateRowPageData {
/**
Expand Down Expand Up @@ -89,6 +90,9 @@ function initBackwardsCopyTemplateRowPage() {
this.backwardsCopyTemplateRow.parent.addEventListener( 'rowDelete', () => {
parent.rebuildPages();
} );
this.backwardsCopyTemplateRow.parent.addEventListener( 'save', () => {
this.refreshLabel();
} );

this.$element.append( this.render().$element );
}
Expand All @@ -99,7 +103,7 @@ function initBackwardsCopyTemplateRowPage() {
refreshLabel(): void {
this.label = mw.message(
'deputy.cte.backwardsCopy.entry.short',
this.backwardsCopyTemplateRow.title
this.backwardsCopyTemplateRow.title || '???'
).text();
if ( this.outlineItem ) {
this.outlineItem.setLabel( this.label );
Expand Down Expand Up @@ -178,6 +182,14 @@ function initBackwardsCopyTemplateRowPage() {
)
);

// TODO: l10n
const authorRegex = /(.+?, (?:[A-Z]\.\s?)*)(?:(?:&|[&;]|[,;] (?:&|[&;])?)\s*|$)/g;
const authors = matchAll(
authorRegex,
this.backwardsCopyTemplateRow.authorlist ??
this.backwardsCopyTemplateRow.author
).map( ( v ) => v[ 1 ] );

const inputs = {
title: new OO.ui.TextInputWidget( {
required: true,
Expand All @@ -200,17 +212,7 @@ function initBackwardsCopyTemplateRowPage() {
placeholder: mw.message(
'deputy.cte.backwardsCopy.entry.author.placeholder'
).text(),
selected: this.backwardsCopyTemplateRow.authorlist ?
(
this.backwardsCopyTemplateRow.authorlist.indexOf( ';' ) !== -1 ?
this.backwardsCopyTemplateRow.authorlist.split( /;\s*/g ) :
this.backwardsCopyTemplateRow.authorlist.split( /,\s*/g )
) :
(
this.backwardsCopyTemplateRow.author ?
[ this.backwardsCopyTemplateRow.author ] :
undefined
)
selected: authors
} ),
url: new OO.ui.TextInputWidget( {
placeholder: mw.message(
Expand Down Expand Up @@ -272,34 +274,40 @@ function initBackwardsCopyTemplateRowPage() {
const field = _field as keyof typeof inputs;
const input = inputs[ field ];

// Attach the change listener
input.on( 'change', ( value: string ) => {
if ( input instanceof mw.widgets.datetime.DateTimeInputWidget ) {
this.backwardsCopyTemplateRow[ field ] =
new Date( value ).toLocaleDateString( 'en-GB', {
year: 'numeric', month: 'long', day: 'numeric'
} );
if ( value.length > 0 ) {
fields[ field ].setWarnings( [] );
}
} else if ( input instanceof OO.ui.TagMultiselectWidget ) {
const data: string[] = input.selected;

if ( data.length > 1 ) {
if ( field === 'author' ) {
input.on( 'change', ( value: { data: string }[] ) => {
if ( value.length === 0 ) {
this.backwardsCopyTemplateRow.author = null;
this.backwardsCopyTemplateRow.authorlist = null;
} else if ( value.length > 1 ) {
this.backwardsCopyTemplateRow.author = null;
this.backwardsCopyTemplateRow.authorlist =
// TODO: l10n
data.join( '; ' );
value.map( ( v ) => v.data ).join( '; ' );
} else {
this.backwardsCopyTemplateRow.authorlist = null;
this.backwardsCopyTemplateRow.author =
data[ 0 ];
value[ 0 ].data;
}
} else {
this.backwardsCopyTemplateRow[ field ] = value;
}
this.backwardsCopyTemplateRow.parent.save();
} );
this.backwardsCopyTemplateRow.parent.save();
} );
} else {
// Attach the change listener
input.on( 'change', ( value: string ) => {
if ( input instanceof mw.widgets.datetime.DateTimeInputWidget ) {
this.backwardsCopyTemplateRow[ field ] =
new Date( value ).toLocaleDateString( 'en-GB', {
year: 'numeric', month: 'long', day: 'numeric'
} );
if ( value.length > 0 ) {
fields[ field ].setWarnings( [] );
}
} else {
this.backwardsCopyTemplateRow[ field ] = value;
}
this.backwardsCopyTemplateRow.parent.save();
} );
}

if ( input instanceof OO.ui.TextInputWidget ) {
// Rechecks the validity of the field.
Expand Down
20 changes: 20 additions & 0 deletions src/util/matchAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import cloneRegex from './cloneRegex';

/**
* Replacement for String.prototype.matchALl (ES2020 only)
*
* @param _regex The regular expression to exec with
* @param string The string to exec against
*/
export default function matchAll( _regex: RegExp, string: string ): RegExpExecArray[] {
const regex = cloneRegex( _regex );

const res = [];
let current = regex.exec( string );
while ( current != null ) {
res.push( current );
current = regex.exec( string );
}

return res;
}

0 comments on commit 7e58e22

Please sign in to comment.