2024-09-21 13:57:38 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { AppState as RNAppState, View, StyleSheet, NativeEventSubscription, ViewStyle, TextStyle } from 'react-native';
|
2023-02-18 17:05:48 +02:00
|
|
|
import { stateUtils } from '@joplin/lib/reducer';
|
|
|
|
import { connect } from 'react-redux';
|
2023-04-23 11:07:28 +02:00
|
|
|
import NoteList from '../NoteList';
|
2023-02-18 17:05:48 +02:00
|
|
|
import Folder from '@joplin/lib/models/Folder';
|
|
|
|
import Tag from '@joplin/lib/models/Tag';
|
2024-09-21 13:57:38 +02:00
|
|
|
import Note, { PreviewsOrder } from '@joplin/lib/models/Note';
|
2023-02-18 17:05:48 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2024-03-09 13:15:13 +02:00
|
|
|
import { themeStyle } from '../global-style';
|
2024-09-21 13:57:38 +02:00
|
|
|
import { FolderPickerOptions, ScreenHeader } from '../ScreenHeader';
|
2023-02-18 17:05:48 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
2024-08-02 15:51:49 +02:00
|
|
|
import ActionButton from '../buttons/FloatingActionButton';
|
2020-11-05 18:58:23 +02:00
|
|
|
const { dialogs } = require('../../utils/dialogs.js');
|
2018-03-09 22:59:12 +02:00
|
|
|
const DialogBox = require('react-native-dialogbox').default;
|
2024-09-21 19:28:41 +02:00
|
|
|
import BackButtonService from '../../services/BackButtonService';
|
2024-09-21 13:57:38 +02:00
|
|
|
import { BaseScreenComponent } from '../base-screen';
|
2023-02-18 17:05:48 +02:00
|
|
|
import { AppState } from '../../utils/types';
|
2024-09-21 13:57:38 +02:00
|
|
|
import { FolderEntity, NoteEntity, TagEntity } from '@joplin/lib/services/database/types';
|
2024-03-02 16:25:27 +02:00
|
|
|
import { itemIsInTrash } from '@joplin/lib/services/trash';
|
2024-08-02 15:51:49 +02:00
|
|
|
import AccessibleView from '../accessibility/AccessibleView';
|
2024-09-21 13:57:38 +02:00
|
|
|
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
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
interface State {
|
2023-02-18 17:05:48 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
}
|
2017-05-12 22:23:54 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
type Styles = Record<string, ViewStyle|TextStyle>;
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
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;
|
2018-07-20 15:59:55 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
private onAppStateChangeSub_: NativeEventSubscription = null;
|
|
|
|
private styles_: Record<number, Styles> = {};
|
|
|
|
private folderPickerOptions_: FolderPickerOptions;
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
public constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
}
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
private onAppStateChange_ = async () => {
|
|
|
|
// Force an update to the notes list when app state changes
|
|
|
|
const newProps = { ...this.props };
|
|
|
|
newProps.notesSource = '';
|
|
|
|
await this.refreshNotes(newProps);
|
|
|
|
};
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
private sortButton_press = async () => {
|
|
|
|
const buttons = [];
|
|
|
|
const sortNoteOptions = Setting.enumOptions('notes.sortOrder.field');
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
const makeCheckboxText = function(selected: boolean, sign: string, label: string) {
|
|
|
|
const s = sign === 'tick' ? '✓' : '⬤';
|
|
|
|
return (selected ? `${s} ` : '') + label;
|
|
|
|
};
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
for (const field in sortNoteOptions) {
|
|
|
|
if (!sortNoteOptions.hasOwnProperty(field)) continue;
|
2018-05-09 22:00:05 +02:00
|
|
|
buttons.push({
|
2024-09-21 13:57:38 +02:00
|
|
|
text: makeCheckboxText(Setting.value('notes.sortOrder.field') === field, 'bullet', sortNoteOptions[field]),
|
|
|
|
id: { name: 'notes.sortOrder.field', value: field },
|
2018-05-09 22:00:05 +02:00
|
|
|
});
|
2024-09-21 13:57:38 +02:00
|
|
|
}
|
2018-05-09 22:00:05 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
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') },
|
|
|
|
});
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
buttons.push({
|
|
|
|
text: makeCheckboxText(Setting.value('uncompletedTodosOnTop'), 'tick', `[ ${Setting.settingMetadata('uncompletedTodosOnTop').label()} ]`),
|
|
|
|
id: { name: 'uncompletedTodosOnTop', value: !Setting.value('uncompletedTodosOnTop') },
|
|
|
|
});
|
2020-05-14 17:03:13 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
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;
|
|
|
|
};
|
2018-02-22 20:58:15 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public styles() {
|
2019-07-12 20:36:12 +02:00
|
|
|
if (!this.styles_) this.styles_ = {};
|
2020-09-15 15:01:07 +02:00
|
|
|
const themeId = this.props.themeId;
|
2019-07-12 20:36:12 +02:00
|
|
|
const cacheKey = themeId;
|
|
|
|
|
|
|
|
if (this.styles_[cacheKey]) return this.styles_[cacheKey];
|
|
|
|
this.styles_ = {};
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const styles = {
|
2019-07-12 20:36:12 +02:00
|
|
|
noteList: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.styles_[cacheKey] = StyleSheet.create(styles);
|
|
|
|
return this.styles_[cacheKey];
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async componentDidMount() {
|
2021-03-11 16:31:39 +02:00
|
|
|
BackButtonService.addHandler(this.backHandler);
|
2017-07-25 20:09:01 +02:00
|
|
|
await this.refreshNotes();
|
2023-02-18 17:05:48 +02:00
|
|
|
this.onAppStateChangeSub_ = RNAppState.addEventListener('change', this.onAppStateChange_);
|
2018-07-20 15:59:55 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async componentWillUnmount() {
|
2023-02-18 17:05:48 +02:00
|
|
|
if (this.onAppStateChangeSub_) this.onAppStateChangeSub_.remove();
|
2020-05-14 17:03:13 +02:00
|
|
|
BackButtonService.removeHandler(this.backHandler);
|
2017-07-25 20:09:01 +02:00
|
|
|
}
|
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
public async componentDidUpdate(prevProps: Props) {
|
2023-11-24 15:48:41 +02:00
|
|
|
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
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
public async refreshNotes(props: Props|null = null) {
|
2017-07-25 20:09:01 +02:00
|
|
|
if (props === null) props = this.props;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const options = {
|
2017-07-26 20:36:16 +02:00
|
|
|
order: props.notesOrder,
|
|
|
|
uncompletedTodosOnTop: props.uncompletedTodosOnTop,
|
2018-05-09 22:00:05 +02:00
|
|
|
showCompletedTodos: props.showCompletedTodos,
|
2018-02-22 20:58:15 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2022-07-23 09:31:32 +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[] = [];
|
2019-06-28 01:51:02 +02:00
|
|
|
if (props.notesParentType === 'Folder') {
|
2017-07-25 20:36:52 +02:00
|
|
|
notes = await Note.previews(props.selectedFolderId, options);
|
2019-06-28 01:51:02 +02:00
|
|
|
} else if (props.notesParentType === 'Tag') {
|
2019-01-18 19:56:56 +02:00
|
|
|
notes = await Tag.notes(props.selectedTagId, options);
|
2019-06-28 01:51:02 +02:00
|
|
|
} 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({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NOTE_UPDATE_ALL',
|
2017-07-25 20:09:01 +02:00
|
|
|
notes: notes,
|
|
|
|
notesSource: source,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public newNoteNavigate = async (folderId: string, isTodo: boolean) => {
|
2023-07-16 18:42:42 +02:00
|
|
|
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));
|
|
|
|
}
|
2023-01-08 14:22:41 +02:00
|
|
|
};
|
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
public parentItem(props: Props|null = null) {
|
2017-07-25 20:36:52 +02:00
|
|
|
if (!props) props = this.props;
|
|
|
|
|
|
|
|
let output = null;
|
2022-07-23 09:31:32 +02:00
|
|
|
if (props.notesParentType === 'Folder') {
|
2017-07-25 20:36:52 +02:00
|
|
|
output = Folder.byId(props.folders, props.selectedFolderId);
|
2022-07-23 09:31:32 +02:00
|
|
|
} else if (props.notesParentType === 'Tag') {
|
2017-07-25 20:36:52 +02:00
|
|
|
output = Tag.byId(props.tags, props.selectedTagId);
|
2022-07-23 09:31:32 +02:00
|
|
|
} else if (props.notesParentType === 'SmartFilter') {
|
2019-06-28 01:51:02 +02:00
|
|
|
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;
|
2019-07-30 09:35:42 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public folderPickerOptions() {
|
2019-07-12 20:36:12 +02:00
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
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
|
|
|
|
2024-09-27 16:23:31 +02:00
|
|
|
const rootStyle = this.props.visible ? theme.rootStyle : theme.hiddenRootStyle;
|
2017-07-31 22:03:12 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const title = parent ? parent.title : null;
|
2017-08-02 19:55:03 +02:00
|
|
|
if (!parent) {
|
|
|
|
return (
|
|
|
|
<View style={rootStyle}>
|
2019-07-26 21:23:18 +02:00
|
|
|
<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
|
|
|
}
|
|
|
|
|
2021-11-15 19:19:51 +02:00
|
|
|
const icon = Folder.unserializeIcon(parent.icon);
|
|
|
|
const iconString = icon ? `${icon.emoji} ` : '';
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
let buttonFolderId = this.props.selectedFolderId !== Folder.conflictFolderId() ? this.props.selectedFolderId : null;
|
2020-09-07 18:42:16 +02:00
|
|
|
if (!buttonFolderId) buttonFolderId = this.props.activeFolderId;
|
|
|
|
|
2024-05-28 14:43:33 +02:00
|
|
|
const addFolderNoteButtons = !!buttonFolderId;
|
2023-01-08 14:22:41 +02:00
|
|
|
|
|
|
|
const makeActionButtonComp = () => {
|
2024-03-11 17:22:26 +02:00
|
|
|
if ((this.props.notesParentType === 'Folder' && itemIsInTrash(parent)) || !Folder.atLeastOneRealFolderExists(this.props.folders)) return null;
|
2024-03-02 16:25:27 +02:00
|
|
|
|
2023-01-08 14:22:41 +02:00
|
|
|
if (addFolderNoteButtons && this.props.folders.length > 0) {
|
|
|
|
const buttons = [];
|
|
|
|
buttons.push({
|
|
|
|
label: _('New to-do'),
|
2023-11-19 12:43:57 +02:00
|
|
|
onPress: async () => {
|
2023-01-08 14:22:41 +02:00
|
|
|
const isTodo = true;
|
2024-05-28 14:43:33 +02:00
|
|
|
void this.newNoteNavigate(buttonFolderId, isTodo);
|
2023-01-08 14:22:41 +02:00
|
|
|
},
|
|
|
|
color: '#9b59b6',
|
2023-10-07 18:25:03 +02:00
|
|
|
icon: 'checkbox-outline',
|
2023-01-08 14:22:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
buttons.push({
|
|
|
|
label: _('New note'),
|
2023-11-19 12:43:57 +02:00
|
|
|
onPress: async () => {
|
2023-01-08 14:22:41 +02:00
|
|
|
const isTodo = false;
|
2024-05-28 14:43:33 +02:00
|
|
|
void this.newNoteNavigate(buttonFolderId, isTodo);
|
2023-01-08 14:22:41 +02:00
|
|
|
},
|
|
|
|
color: '#9b59b6',
|
2023-10-07 18:25:03 +02:00
|
|
|
icon: 'document',
|
2023-01-08 14:22:41 +02:00
|
|
|
});
|
2023-11-26 13:37:45 +02:00
|
|
|
return <ActionButton buttons={buttons} dispatch={this.props.dispatch}/>;
|
2023-01-08 14:22:41 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const actionButtonComp = this.props.noteSelectionEnabled || !this.props.visible ? null : makeActionButtonComp();
|
2017-08-02 19:55:03 +02:00
|
|
|
|
2023-09-19 12:39:43 +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 (
|
2024-08-02 15:51:49 +02:00
|
|
|
<AccessibleView
|
2023-09-19 12:39:43 +02:00
|
|
|
style={rootStyle}
|
|
|
|
|
2024-08-02 15:51:49 +02:00
|
|
|
inert={accessibilityHidden}
|
2023-09-19 12:39:43 +02:00
|
|
|
>
|
2024-09-24 16:12:02 +02:00
|
|
|
<ScreenHeader title={iconString + title} showBackButton={false} sortButton_press={this.sortButton_press} folderPickerOptions={this.folderPickerOptions()} showSearchButton={true} showSideMenuButton={true} />
|
2023-04-23 11:07:28 +02:00
|
|
|
<NoteList />
|
2019-07-29 15:43:53 +02:00
|
|
|
{actionButtonComp}
|
|
|
|
<DialogBox
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-02-18 17:05:48 +02:00
|
|
|
ref={(dialogbox: any) => {
|
2019-07-29 15:43:53 +02:00
|
|
|
this.dialogbox = dialogbox;
|
|
|
|
}}
|
2017-11-23 20:47:51 +02:00
|
|
|
/>
|
2024-08-02 15:51:49 +02:00
|
|
|
</AccessibleView>
|
2017-05-12 22:23:54 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 17:05:48 +02:00
|
|
|
const NotesScreen = connect((state: AppState) => {
|
2019-07-29 15:43:53 +02:00
|
|
|
return {
|
|
|
|
folders: state.folders,
|
|
|
|
tags: state.tags,
|
2020-09-07 18:42:16 +02:00
|
|
|
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),
|
|
|
|
};
|
2024-09-21 13:57:38 +02:00
|
|
|
})(NotesScreenComponent);
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2024-09-21 13:57:38 +02:00
|
|
|
export default NotesScreen;
|