2019-07-29 15:43:53 +02:00
|
|
|
const React = require('react');
|
2023-04-23 23:56:24 +02:00
|
|
|
|
|
|
|
import { Component } from 'react';
|
|
|
|
|
2023-04-23 11:07:28 +02:00
|
|
|
import { connect } from 'react-redux';
|
2023-04-23 23:56:24 +02:00
|
|
|
import { FlatList, Text, StyleSheet, Button, View } from 'react-native';
|
2023-04-23 11:07:28 +02:00
|
|
|
import { FolderEntity, NoteEntity } from '@joplin/lib/services/database/types';
|
|
|
|
import { AppState } from '../utils/types';
|
2024-03-11 17:22:26 +02:00
|
|
|
import Folder from '@joplin/lib/models/Folder';
|
2023-04-23 23:56:24 +02:00
|
|
|
|
2020-11-07 17:59:37 +02:00
|
|
|
const { _ } = require('@joplin/lib/locale');
|
2020-11-05 18:58:23 +02:00
|
|
|
const { NoteItem } = require('./note-item.js');
|
2024-03-09 13:15:13 +02:00
|
|
|
import { themeStyle } from './global-style';
|
2017-07-25 19:49:31 +02:00
|
|
|
|
2023-04-23 11:07:28 +02:00
|
|
|
interface NoteListProps {
|
2024-03-09 13:15:13 +02:00
|
|
|
themeId: number;
|
2023-04-23 11:07:28 +02:00
|
|
|
dispatch: (action: any)=> void;
|
|
|
|
notesSource: string;
|
|
|
|
items: NoteEntity[];
|
|
|
|
folders: FolderEntity[];
|
|
|
|
noteSelectionEnabled?: boolean;
|
|
|
|
selectedFolderId?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class NoteListComponent extends Component<NoteListProps> {
|
2023-04-23 23:56:24 +02:00
|
|
|
private rootRef_: FlatList;
|
|
|
|
private styles_: Record<string, StyleSheet.NamedStyles<any>>;
|
2023-04-23 11:07:28 +02:00
|
|
|
|
|
|
|
public constructor(props: NoteListProps) {
|
|
|
|
super(props);
|
2020-02-09 16:51:12 +02:00
|
|
|
|
2017-07-25 19:49:31 +02:00
|
|
|
this.state = {
|
|
|
|
items: [],
|
|
|
|
selectedItemIds: [],
|
|
|
|
};
|
2017-07-30 23:33:54 +02:00
|
|
|
this.rootRef_ = null;
|
2017-08-01 20:29:01 +02:00
|
|
|
this.styles_ = {};
|
2019-07-30 11:36:56 +02:00
|
|
|
|
|
|
|
this.createNotebookButton_click = this.createNotebookButton_click.bind(this);
|
2017-08-01 20:29:01 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 11:07:28 +02:00
|
|
|
private styles() {
|
2020-09-15 15:01:07 +02:00
|
|
|
const themeId = this.props.themeId;
|
2017-08-01 20:29:01 +02:00
|
|
|
const theme = themeStyle(themeId);
|
|
|
|
|
|
|
|
if (this.styles_[themeId]) return this.styles_[themeId];
|
|
|
|
this.styles_ = {};
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const styles = {
|
2017-08-01 20:29:01 +02:00
|
|
|
noItemMessage: {
|
|
|
|
paddingLeft: theme.marginLeft,
|
|
|
|
paddingRight: theme.marginRight,
|
|
|
|
paddingTop: theme.marginTop,
|
|
|
|
paddingBottom: theme.marginBottom,
|
|
|
|
fontSize: theme.fontSize,
|
|
|
|
color: theme.color,
|
2019-07-30 11:36:56 +02:00
|
|
|
textAlign: 'center',
|
|
|
|
},
|
|
|
|
noNotebookView: {
|
|
|
|
|
2017-08-01 20:29:01 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.styles_[themeId] = StyleSheet.create(styles);
|
|
|
|
return this.styles_[themeId];
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 11:07:28 +02:00
|
|
|
private createNotebookButton_click() {
|
2019-07-30 11:36:56 +02:00
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Folder',
|
|
|
|
folderId: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-23 11:07:28 +02:00
|
|
|
public UNSAFE_componentWillReceiveProps(newProps: NoteListProps) {
|
2017-07-30 23:33:54 +02:00
|
|
|
// Make sure scroll position is reset when switching from one folder to another or to a tag list.
|
2022-07-23 09:31:32 +02:00
|
|
|
if (this.rootRef_ && newProps.notesSource !== this.props.notesSource) {
|
2020-02-09 16:51:12 +02:00
|
|
|
this.rootRef_.scrollToOffset({ offset: 0, animated: false });
|
2017-07-30 23:33:54 +02:00
|
|
|
}
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 11:07:28 +02:00
|
|
|
public render() {
|
2017-07-25 19:49:31 +02:00
|
|
|
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
|
|
|
|
|
2020-02-09 16:51:12 +02:00
|
|
|
if (this.props.items.length) {
|
|
|
|
return <FlatList
|
2023-04-23 23:56:24 +02:00
|
|
|
ref={ref => (this.rootRef_ = ref)}
|
2020-02-09 16:51:12 +02:00
|
|
|
data={this.props.items}
|
2023-04-23 23:56:24 +02:00
|
|
|
renderItem={({ item }) => <NoteItem note={item} />}
|
|
|
|
keyExtractor={item => item.id}
|
2020-02-09 16:51:12 +02:00
|
|
|
/>;
|
2017-07-25 19:49:31 +02:00
|
|
|
} else {
|
2024-03-11 17:22:26 +02:00
|
|
|
if (!Folder.atLeastOneRealFolderExists(this.props.folders)) {
|
2019-07-30 11:36:56 +02:00
|
|
|
const noItemMessage = _('You currently have no notebooks.');
|
|
|
|
return (
|
|
|
|
<View style={this.styles().noNotebookView}>
|
|
|
|
<Text style={this.styles().noItemMessage}>{noItemMessage}</Text>
|
|
|
|
<Button title={_('Create a notebook')} onPress={this.createNotebookButton_click} />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const noItemMessage = _('There are currently no notes. Create one by clicking on the (+) button.');
|
|
|
|
return <Text style={this.styles().noItemMessage}>{noItemMessage}</Text>;
|
2019-07-30 11:37:52 +02:00
|
|
|
}
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 21:46:34 +02:00
|
|
|
}
|
2017-05-15 21:10:00 +02:00
|
|
|
|
2024-03-11 17:22:26 +02:00
|
|
|
|
2023-04-23 11:07:28 +02:00
|
|
|
const NoteList = connect((state: AppState) => {
|
2019-07-29 15:43:53 +02:00
|
|
|
return {
|
|
|
|
items: state.notes,
|
2019-07-30 11:36:56 +02:00
|
|
|
folders: state.folders,
|
2019-07-29 15:43:53 +02:00
|
|
|
notesSource: state.notesSource,
|
2020-09-15 15:01:07 +02:00
|
|
|
themeId: state.settings.theme,
|
2019-07-29 15:43:53 +02:00
|
|
|
noteSelectionEnabled: state.noteSelectionEnabled,
|
|
|
|
};
|
2023-04-23 23:56:24 +02:00
|
|
|
})(NoteListComponent);
|
2017-05-15 21:10:00 +02:00
|
|
|
|
2023-04-23 11:07:28 +02:00
|
|
|
export default NoteList;
|