Skip to content

Commit

Permalink
Mobile: Added duplicate option when selecting notes. (laurent22#1969)
Browse files Browse the repository at this point in the history
* Adding duplicate button on screen-header.js when selecting notes; Adding 'duplicateMultipleNotes' function on Note.js;

* Using for-loop like the rest of the code does

* changing from 'uniqueTitle' to 'ensureUniqueTitle'
  • Loading branch information
alanfortlink authored and scoroi committed Nov 10, 2019
1 parent da3ff2e commit e573027
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
28 changes: 28 additions & 0 deletions ReactNativeClient/lib/components/screen-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ class ScreenHeaderComponent extends React.PureComponent {
NavService.go('Search');
}

async duplicateButton_press() {
const noteIds = this.props.selectedNoteIds;

//Duplicate all selected notes. ensureUniqueTitle is set to true to use the
//original note's name as a root for the new unique identifier.
await Note.duplicateMultipleNotes(noteIds, {ensureUniqueTitle: true});

this.props.dispatch({ type: 'NOTE_SELECTION_END' });
}

async deleteButton_press() {
// Dialog needs to be displayed as a child of the parent component, otherwise
// it won't be visible within the header component.
Expand Down Expand Up @@ -245,6 +255,16 @@ class ScreenHeaderComponent extends React.PureComponent {
);
}

function duplicateButton(styles, onPress) {
return (
<TouchableOpacity onPress={onPress}>
<View style={styles.iconButton}>
<Icon name="md-copy" style={styles.topIcon} />
</View>
</TouchableOpacity>
);
}

function sortButton(styles, onPress) {
return (
<TouchableOpacity onPress={onPress}>
Expand Down Expand Up @@ -282,6 +302,12 @@ class ScreenHeaderComponent extends React.PureComponent {
<Text style={this.styles().contextMenuItemText}>{_('Delete')}</Text>
</MenuOption>
);

menuOptionComponents.push(
<MenuOption value={() => this.duplicateButton_press()} key={'menuOption_duplicate'} style={this.styles().contextMenuItem}>
<Text style={this.styles().contextMenuItemText}>{_('Duplicate')}</Text>
</MenuOption>
);
}

const createTitleComponent = () => {
Expand Down Expand Up @@ -383,6 +409,7 @@ class ScreenHeaderComponent extends React.PureComponent {
const backButtonComp = !showBackButton ? null : backButton(this.styles(), () => this.backButton_press(), backButtonDisabled);
const searchButtonComp = !showSearchButton ? null : searchButton(this.styles(), () => this.searchButton_press());
const deleteButtonComp = this.props.noteSelectionEnabled ? deleteButton(this.styles(), () => this.deleteButton_press()) : null;
const duplicateButtonComp = this.props.noteSelectionEnabled ? duplicateButton(this.styles(), () => this.duplicateButton_press()) : null;
const sortButtonComp = !this.props.noteSelectionEnabled && this.props.sortButton_press ? sortButton(this.styles(), () => this.props.sortButton_press()) : null;
const windowHeight = Dimensions.get('window').height - 50;

Expand Down Expand Up @@ -419,6 +446,7 @@ class ScreenHeaderComponent extends React.PureComponent {
{titleComp}
{searchButtonComp}
{deleteButtonComp}
{duplicateButtonComp}
{sortButtonComp}
{menuComp}
</View>
Expand Down
19 changes: 19 additions & 0 deletions ReactNativeClient/lib/models/Note.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,25 @@ class Note extends BaseItem {
return note;
}

static async duplicateMultipleNotes(noteIds, options = null){
/*
if options.uniqueTitle is true, a unique title for the duplicated file will be assigned.
*/
const ensureUniqueTitle = options && options.ensureUniqueTitle;

for(const noteId of noteIds){
const noteOptions = {};

//If ensureUniqueTitle is truthy, set the original note's name as root for the unique title.
if(ensureUniqueTitle){
const originalNote = await Note.load(noteId);
noteOptions.uniqueTitle = originalNote.title;
}

await Note.duplicate(noteId, noteOptions);
}
}

static async duplicate(noteId, options = null) {
const changes = options && options.changes;
const uniqueTitle = options && options.uniqueTitle;
Expand Down

0 comments on commit e573027

Please sign in to comment.