2019-07-29 15:43:53 +02:00
|
|
|
const React = require('react');
|
2019-07-29 15:58:33 +02:00
|
|
|
|
2023-05-29 12:31:21 +02:00
|
|
|
const { View, StyleSheet } = require('react-native');
|
2018-03-09 22:59:12 +02:00
|
|
|
const { connect } = require('react-redux');
|
2021-01-22 19:41:11 +02:00
|
|
|
const Folder = require('@joplin/lib/models/Folder').default;
|
2020-11-07 17:59:37 +02:00
|
|
|
const BaseModel = require('@joplin/lib/BaseModel').default;
|
2022-08-27 14:36:59 +02:00
|
|
|
const { ScreenHeader } = require('../ScreenHeader');
|
2023-11-09 21:19:08 +02:00
|
|
|
const { BaseScreenComponent } = require('../base-screen');
|
2020-11-05 18:58:23 +02:00
|
|
|
const { dialogs } = require('../../utils/dialogs.js');
|
2020-11-07 17:59:37 +02:00
|
|
|
const { _ } = require('@joplin/lib/locale');
|
2023-05-29 12:31:21 +02:00
|
|
|
const { default: FolderPicker } = require('../FolderPicker');
|
2023-01-10 14:08:13 +02:00
|
|
|
const TextInput = require('../TextInput').default;
|
2017-05-15 21:10:00 +02:00
|
|
|
|
2017-07-14 20:49:14 +02:00
|
|
|
class FolderScreenComponent extends BaseScreenComponent {
|
2019-09-13 00:16:42 +02:00
|
|
|
static navigationOptions() {
|
2017-05-16 21:57:09 +02:00
|
|
|
return { header: null };
|
|
|
|
}
|
2017-05-15 21:10:00 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2017-07-15 20:37:17 +02:00
|
|
|
this.state = {
|
|
|
|
folder: Folder.new(),
|
|
|
|
lastSavedFolder: null,
|
|
|
|
};
|
2017-05-15 21:10:00 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:38:19 +02:00
|
|
|
UNSAFE_componentWillMount() {
|
2017-05-23 22:39:01 +02:00
|
|
|
if (!this.props.folderId) {
|
2017-07-15 20:37:17 +02:00
|
|
|
const folder = Folder.new();
|
|
|
|
this.setState({
|
|
|
|
folder: folder,
|
2023-06-01 13:02:36 +02:00
|
|
|
lastSavedFolder: { ...folder },
|
2017-07-15 20:37:17 +02:00
|
|
|
});
|
2017-05-23 22:39:01 +02:00
|
|
|
} else {
|
2022-09-30 18:23:14 +02:00
|
|
|
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
|
2020-05-21 10:14:33 +02:00
|
|
|
Folder.load(this.props.folderId).then(folder => {
|
2017-07-15 20:37:17 +02:00
|
|
|
this.setState({
|
|
|
|
folder: folder,
|
2023-06-01 13:02:36 +02:00
|
|
|
lastSavedFolder: { ...folder },
|
2017-07-15 20:37:17 +02:00
|
|
|
});
|
2017-05-23 22:39:01 +02:00
|
|
|
});
|
|
|
|
}
|
2017-05-15 21:10:00 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 20:37:17 +02:00
|
|
|
isModified() {
|
|
|
|
if (!this.state.folder || !this.state.lastSavedFolder) return false;
|
2020-03-14 01:46:14 +02:00
|
|
|
const diff = BaseModel.diffObjects(this.state.folder, this.state.lastSavedFolder);
|
2017-07-15 20:37:17 +02:00
|
|
|
delete diff.type_;
|
|
|
|
return !!Object.getOwnPropertyNames(diff).length;
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
folderComponent_change(propName, propValue) {
|
2019-09-13 00:16:42 +02:00
|
|
|
this.setState((prevState) => {
|
2023-06-01 13:02:36 +02:00
|
|
|
const folder = { ...prevState.folder };
|
2017-05-15 21:10:00 +02:00
|
|
|
folder[propName] = propValue;
|
2019-07-29 15:43:53 +02:00
|
|
|
return { folder: folder };
|
2017-05-15 21:10:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:01:43 +02:00
|
|
|
title_changeText(text) {
|
2018-03-09 22:59:12 +02:00
|
|
|
this.folderComponent_change('title', text);
|
2017-05-15 21:10:00 +02:00
|
|
|
}
|
|
|
|
|
2023-05-29 12:31:21 +02:00
|
|
|
parent_changeValue(parent) {
|
|
|
|
this.folderComponent_change('parent_id', parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-05 23:29:00 +02:00
|
|
|
async saveFolderButton_press() {
|
2023-06-01 13:02:36 +02:00
|
|
|
let folder = { ...this.state.folder };
|
2017-07-05 23:29:00 +02:00
|
|
|
|
2017-07-15 18:25:33 +02:00
|
|
|
try {
|
2023-05-29 12:31:21 +02:00
|
|
|
if (folder.id && !(await Folder.canNestUnder(folder.id, folder.parent_id))) throw new Error(_('Cannot move notebook to this location'));
|
2017-07-17 21:19:01 +02:00
|
|
|
folder = await Folder.save(folder, { userSideValidation: true });
|
2017-07-15 18:25:33 +02:00
|
|
|
} catch (error) {
|
2020-06-13 17:20:59 +02:00
|
|
|
dialogs.error(this, _('The notebook could not be saved: %s', error.message));
|
2017-07-15 18:25:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-15 20:37:17 +02:00
|
|
|
this.setState({
|
2023-06-01 13:02:36 +02:00
|
|
|
lastSavedFolder: { ...folder },
|
2017-07-15 20:37:17 +02:00
|
|
|
folder: folder,
|
|
|
|
});
|
2017-07-09 00:57:09 +02:00
|
|
|
|
2017-07-25 20:09:01 +02:00
|
|
|
this.props.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Notes',
|
2017-07-25 20:09:01 +02:00
|
|
|
folderId: folder.id,
|
|
|
|
});
|
2017-05-15 21:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-05-29 12:31:21 +02:00
|
|
|
const saveButtonDisabled = !this.isModified() || !this.state.folder.title;
|
2017-07-15 20:13:31 +02:00
|
|
|
|
2017-05-15 21:10:00 +02:00
|
|
|
return (
|
2020-09-15 15:01:07 +02:00
|
|
|
<View style={this.rootStyle(this.props.themeId).root}>
|
2019-07-29 15:43:53 +02:00
|
|
|
<ScreenHeader title={_('Edit notebook')} showSaveButton={true} saveButtonDisabled={saveButtonDisabled} onSaveButtonPress={() => this.saveFolderButton_press()} showSideMenuButton={false} showSearchButton={false} />
|
2023-01-10 14:08:13 +02:00
|
|
|
<TextInput
|
|
|
|
themeId={this.props.themeId}
|
|
|
|
placeholder={_('Enter notebook title')}
|
|
|
|
autoFocus={true}
|
|
|
|
value={this.state.folder.title}
|
|
|
|
onChangeText={text => this.title_changeText(text)}
|
2023-05-29 12:31:21 +02:00
|
|
|
disabled={this.state.folder.encryption_applied}
|
2023-01-10 14:08:13 +02:00
|
|
|
/>
|
2023-05-29 12:31:21 +02:00
|
|
|
<View style={styles.folderPickerContainer}>
|
|
|
|
<FolderPicker
|
|
|
|
themeId={this.props.themeId}
|
|
|
|
placeholder={_('Select parent notebook')}
|
|
|
|
folders={this.props.folders}
|
|
|
|
selectedFolderId={this.state.folder.parent_id}
|
|
|
|
onValueChange={newValue => this.parent_changeValue(newValue)}
|
|
|
|
mustSelect
|
|
|
|
darkText
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<View style={{ flex: 1 }} />
|
2020-06-13 17:20:59 +02:00
|
|
|
<dialogs.DialogBox
|
|
|
|
ref={dialogbox => {
|
|
|
|
this.dialogbox = dialogbox;
|
|
|
|
}}
|
|
|
|
/>
|
2017-05-15 21:10:00 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
const FolderScreen = connect(state => {
|
2019-07-29 15:43:53 +02:00
|
|
|
return {
|
|
|
|
folderId: state.selectedFolderId,
|
2020-09-15 15:01:07 +02:00
|
|
|
themeId: state.settings.theme,
|
2023-05-29 12:31:21 +02:00
|
|
|
folders: state.folders.filter((folder) => folder.id !== state.selectedFolderId),
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
|
|
|
})(FolderScreenComponent);
|
2017-05-15 21:10:00 +02:00
|
|
|
|
2023-05-29 12:31:21 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
folderPickerContainer: {
|
|
|
|
height: 46,
|
|
|
|
paddingLeft: 14,
|
|
|
|
paddingRight: 14,
|
|
|
|
paddingTop: 12,
|
|
|
|
paddingBottom: 12,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { FolderScreen };
|