1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-mobile/components/screens/Notes.tsx

331 lines
10 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import { AppState as RNAppState, View, StyleSheet, NativeEventSubscription, ViewStyle, TextStyle } from 'react-native';
import { stateUtils } from '@joplin/lib/reducer';
import { connect } from 'react-redux';
import NoteList from '../NoteList';
import Folder from '@joplin/lib/models/Folder';
import Tag from '@joplin/lib/models/Tag';
import Note, { PreviewsOrder } from '@joplin/lib/models/Note';
import Setting from '@joplin/lib/models/Setting';
import { themeStyle } from '../global-style';
import { FolderPickerOptions, ScreenHeader } from '../ScreenHeader';
import { _ } from '@joplin/lib/locale';
import ActionButton from '../buttons/FloatingActionButton';
2020-11-05 18:58:23 +02:00
const { dialogs } = require('../../utils/dialogs.js');
const DialogBox = require('react-native-dialogbox').default;
import BackButtonService from '../../services/BackButtonService';
import { BaseScreenComponent } from '../base-screen';
import { AppState } from '../../utils/types';
import { FolderEntity, NoteEntity, TagEntity } from '@joplin/lib/services/database/types';
import { itemIsInTrash } from '@joplin/lib/services/trash';
import AccessibleView from '../accessibility/AccessibleView';
import { Dispatch } from 'redux';
interface Props {
dispatch: Dispatch;
themeId: number;
visible: boolean;
folders: FolderEntity[];
tags: TagEntity[];
notesSource: string;
notesOrder: PreviewsOrder[];
uncompletedTodosOnTop: boolean;
showCompletedTodos: boolean;
noteSelectionEnabled: boolean;
activeFolderId: string;
selectedFolderId: string;
selectedTagId: string;
selectedSmartFilterId: string;
notesParentType: string;
}
2017-05-12 22:23:54 +02:00
interface State {
}
2017-05-12 22:23:54 +02:00
type Styles = Record<string, ViewStyle|TextStyle>;
class NotesScreenComponent extends BaseScreenComponent<Props, State> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Partial refactor of old code from before rule was applied
private dialogbox: any;
private onAppStateChangeSub_: NativeEventSubscription = null;
private styles_: Record<number, Styles> = {};
private folderPickerOptions_: FolderPickerOptions;
public constructor(props: Props) {
super(props);
}
private onAppStateChange_ = async () => {
// Force an update to the notes list when app state changes
const newProps = { ...this.props };
newProps.notesSource = '';
await this.refreshNotes(newProps);
};
private sortButton_press = async () => {
const buttons = [];
const sortNoteOptions = Setting.enumOptions('notes.sortOrder.field');
const makeCheckboxText = function(selected: boolean, sign: string, label: string) {
const s = sign === 'tick' ? '✓' : '⬤';
return (selected ? `${s} ` : '') + label;
};
for (const field in sortNoteOptions) {
if (!sortNoteOptions.hasOwnProperty(field)) continue;
buttons.push({
text: makeCheckboxText(Setting.value('notes.sortOrder.field') === field, 'bullet', sortNoteOptions[field]),
id: { name: 'notes.sortOrder.field', value: field },
});
}
buttons.push({
text: makeCheckboxText(Setting.value('notes.sortOrder.reverse'), 'tick', `[ ${Setting.settingMetadata('notes.sortOrder.reverse').label()} ]`),
id: { name: 'notes.sortOrder.reverse', value: !Setting.value('notes.sortOrder.reverse') },
});
buttons.push({
text: makeCheckboxText(Setting.value('uncompletedTodosOnTop'), 'tick', `[ ${Setting.settingMetadata('uncompletedTodosOnTop').label()} ]`),
id: { name: 'uncompletedTodosOnTop', value: !Setting.value('uncompletedTodosOnTop') },
});
buttons.push({
text: makeCheckboxText(Setting.value('showCompletedTodos'), 'tick', `[ ${Setting.settingMetadata('showCompletedTodos').label()} ]`),
id: { name: 'showCompletedTodos', value: !Setting.value('showCompletedTodos') },
});
const r = await dialogs.pop(this, Setting.settingMetadata('notes.sortOrder.field').label(), buttons);
if (!r) return;
Setting.setValue(r.name, r.value);
};
private backHandler = () => {
if (this.dialogbox && this.dialogbox.state && this.dialogbox.state.isVisible) {
this.dialogbox.close();
return true;
}
return false;
};
public styles() {
if (!this.styles_) this.styles_ = {};
2020-09-15 15:01:07 +02:00
const themeId = this.props.themeId;
const cacheKey = themeId;
if (this.styles_[cacheKey]) return this.styles_[cacheKey];
this.styles_ = {};
const styles = {
noteList: {
flex: 1,
},
};
this.styles_[cacheKey] = StyleSheet.create(styles);
return this.styles_[cacheKey];
}
public async componentDidMount() {
BackButtonService.addHandler(this.backHandler);
2017-07-25 20:09:01 +02:00
await this.refreshNotes();
this.onAppStateChangeSub_ = RNAppState.addEventListener('change', this.onAppStateChange_);
}
public async componentWillUnmount() {
if (this.onAppStateChangeSub_) this.onAppStateChangeSub_.remove();
BackButtonService.removeHandler(this.backHandler);
2017-07-25 20:09:01 +02:00
}
public async componentDidUpdate(prevProps: Props) {
if (prevProps.notesOrder !== this.props.notesOrder || prevProps.selectedFolderId !== this.props.selectedFolderId || prevProps.selectedTagId !== this.props.selectedTagId || prevProps.selectedSmartFilterId !== this.props.selectedSmartFilterId || prevProps.notesParentType !== this.props.notesParentType || prevProps.uncompletedTodosOnTop !== this.props.uncompletedTodosOnTop || prevProps.showCompletedTodos !== this.props.showCompletedTodos) {
2019-07-29 15:43:53 +02:00
await this.refreshNotes(this.props);
2017-07-25 20:09:01 +02:00
}
2019-07-29 15:43:53 +02:00
}
2017-07-25 20:09:01 +02:00
public async refreshNotes(props: Props|null = null) {
2017-07-25 20:09:01 +02:00
if (props === null) props = this.props;
const options = {
order: props.notesOrder,
uncompletedTodosOnTop: props.uncompletedTodosOnTop,
showCompletedTodos: props.showCompletedTodos,
caseInsensitive: true,
2017-07-25 20:09:01 +02:00
};
2017-07-25 20:36:52 +02:00
const parent = this.parentItem(props);
2017-07-31 22:03:12 +02:00
if (!parent) return;
2017-07-25 20:36:52 +02:00
2017-07-25 20:09:01 +02:00
const source = JSON.stringify({
options: options,
2017-07-25 20:36:52 +02:00
parentId: parent.id,
2017-07-25 20:09:01 +02:00
});
if (source === props.notesSource) return;
2017-07-25 20:09:01 +02:00
2023-12-13 21:24:58 +02:00
let notes: NoteEntity[] = [];
if (props.notesParentType === 'Folder') {
2017-07-25 20:36:52 +02:00
notes = await Note.previews(props.selectedFolderId, options);
} else if (props.notesParentType === 'Tag') {
notes = await Tag.notes(props.selectedTagId, options);
} else if (props.notesParentType === 'SmartFilter') {
notes = await Note.previews(null, options);
2017-07-25 20:36:52 +02:00
}
2017-07-25 20:09:01 +02:00
this.props.dispatch({
type: 'NOTE_UPDATE_ALL',
2017-07-25 20:09:01 +02:00
notes: notes,
notesSource: source,
});
}
public newNoteNavigate = async (folderId: string, isTodo: boolean) => {
try {
const newNote = await Note.save({
parent_id: folderId,
is_todo: isTodo ? 1 : 0,
}, { provisional: true });
this.props.dispatch({
type: 'NAV_GO',
routeName: 'Note',
noteId: newNote.id,
});
} catch (error) {
alert(_('Cannot create a new note: %s', error.message));
}
};
public parentItem(props: Props|null = null) {
2017-07-25 20:36:52 +02:00
if (!props) props = this.props;
let output = null;
if (props.notesParentType === 'Folder') {
2017-07-25 20:36:52 +02:00
output = Folder.byId(props.folders, props.selectedFolderId);
} else if (props.notesParentType === 'Tag') {
2017-07-25 20:36:52 +02:00
output = Tag.byId(props.tags, props.selectedTagId);
} else if (props.notesParentType === 'SmartFilter') {
output = { id: this.props.selectedSmartFilterId, title: _('All notes') };
2017-07-25 20:36:52 +02:00
} else {
2017-07-31 22:03:12 +02:00
return null;
// throw new Error('Invalid parent type: ' + props.notesParentType);
2017-07-25 20:36:52 +02:00
}
return output;
2017-05-16 22:25:19 +02:00
}
public folderPickerOptions() {
const options = {
enabled: this.props.noteSelectionEnabled,
mustSelect: true,
};
if (this.folderPickerOptions_ && options.enabled === this.folderPickerOptions_.enabled) return this.folderPickerOptions_;
this.folderPickerOptions_ = options;
return this.folderPickerOptions_;
}
public render() {
2017-07-25 20:36:52 +02:00
const parent = this.parentItem();
2020-09-15 15:01:07 +02:00
const theme = themeStyle(this.props.themeId);
2017-08-01 19:59:01 +02:00
const rootStyle = this.props.visible ? theme.rootStyle : theme.hiddenRootStyle;
2017-07-31 22:03:12 +02:00
const title = parent ? parent.title : null;
2017-08-02 19:55:03 +02:00
if (!parent) {
return (
<View style={rootStyle}>
<ScreenHeader title={title} showSideMenuButton={true} showBackButton={false} />
2017-08-02 19:55:03 +02:00
</View>
2019-07-29 15:43:53 +02:00
);
2017-08-02 19:55:03 +02:00
}
const icon = Folder.unserializeIcon(parent.icon);
const iconString = icon ? `${icon.emoji} ` : '';
let buttonFolderId = this.props.selectedFolderId !== Folder.conflictFolderId() ? this.props.selectedFolderId : null;
if (!buttonFolderId) buttonFolderId = this.props.activeFolderId;
const addFolderNoteButtons = !!buttonFolderId;
const makeActionButtonComp = () => {
if ((this.props.notesParentType === 'Folder' && itemIsInTrash(parent)) || !Folder.atLeastOneRealFolderExists(this.props.folders)) return null;
if (addFolderNoteButtons && this.props.folders.length > 0) {
const buttons = [];
buttons.push({
label: _('New to-do'),
onPress: async () => {
const isTodo = true;
void this.newNoteNavigate(buttonFolderId, isTodo);
},
color: '#9b59b6',
icon: 'checkbox-outline',
});
buttons.push({
label: _('New note'),
onPress: async () => {
const isTodo = false;
void this.newNoteNavigate(buttonFolderId, isTodo);
},
color: '#9b59b6',
icon: 'document',
});
return <ActionButton buttons={buttons} dispatch={this.props.dispatch}/>;
}
return null;
};
const actionButtonComp = this.props.noteSelectionEnabled || !this.props.visible ? null : makeActionButtonComp();
2017-08-02 19:55:03 +02:00
// Ensure that screen readers can't focus the notes list when it isn't visible.
const accessibilityHidden = !this.props.visible;
2017-05-12 22:23:54 +02:00
return (
<AccessibleView
style={rootStyle}
inert={accessibilityHidden}
>
<ScreenHeader title={iconString + title} showBackButton={false} sortButton_press={this.sortButton_press} folderPickerOptions={this.folderPickerOptions()} showSearchButton={true} showSideMenuButton={true} />
<NoteList />
2019-07-29 15:43:53 +02:00
{actionButtonComp}
<DialogBox
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
ref={(dialogbox: any) => {
2019-07-29 15:43:53 +02:00
this.dialogbox = dialogbox;
}}
/>
</AccessibleView>
2017-05-12 22:23:54 +02:00
);
}
}
const NotesScreen = connect((state: AppState) => {
2019-07-29 15:43:53 +02:00
return {
folders: state.folders,
tags: state.tags,
activeFolderId: state.settings.activeFolderId,
2019-07-29 15:43:53 +02:00
selectedFolderId: state.selectedFolderId,
selectedNoteIds: state.selectedNoteIds,
selectedTagId: state.selectedTagId,
selectedSmartFilterId: state.selectedSmartFilterId,
notesParentType: state.notesParentType,
notes: state.notes,
notesSource: state.notesSource,
uncompletedTodosOnTop: state.settings.uncompletedTodosOnTop,
showCompletedTodos: state.settings.showCompletedTodos,
2020-09-15 15:01:07 +02:00
themeId: state.settings.theme,
2019-07-29 15:43:53 +02:00
noteSelectionEnabled: state.noteSelectionEnabled,
notesOrder: stateUtils.notesOrder(state.settings),
};
})(NotesScreenComponent);
export default NotesScreen;