Files
notesnook/apps/mobile/app/components/sheets/add-notebook/index.js

559 lines
14 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
import React, { createRef } from "react";
import {
Keyboard,
StyleSheet,
TextInput,
TouchableOpacity,
View
} from "react-native";
import { notesnook } from "../../../../e2e/test.ids";
2023-03-16 21:22:21 +05:00
import { db } from "../../../common/database";
import { DDS } from "../../../services/device-detection";
2023-03-16 21:22:21 +05:00
import { presentSheet, ToastEvent } from "../../../services/event-manager";
import Navigation from "../../../services/navigation";
2023-03-16 21:22:21 +05:00
import { useMenuStore } from "../../../stores/use-menu-store";
import { useRelationStore } from "../../../stores/use-relation-store";
import { ph, pv, SIZE } from "../../../utils/size";
import { sleep } from "../../../utils/time";
import DialogHeader from "../../dialog/dialog-header";
2023-03-16 21:22:21 +05:00
import { Button } from "../../ui/button";
import { IconButton } from "../../ui/icon-button";
import Input from "../../ui/input";
import Seperator from "../../ui/seperator";
2023-03-16 21:22:21 +05:00
import { MoveNotes } from "../move-notes/movenote";
import { FlatList } from "react-native-actions-sheet";
2019-12-03 18:12:50 +05:00
2019-12-06 11:09:45 +05:00
let refs = [];
2022-02-28 15:32:55 +05:00
export class AddNotebookSheet extends React.Component {
2020-11-02 09:22:26 +05:00
constructor(props) {
super(props);
2023-03-16 21:22:21 +05:00
refs = [];
2020-11-02 09:22:26 +05:00
this.state = {
2023-03-16 21:22:21 +05:00
notebook: props.notebook,
topics:
props.notebook?.topics?.map((item) => {
return item.title;
}) || [],
2020-11-02 09:22:26 +05:00
description: null,
titleFocused: false,
descFocused: false,
count: 0,
topicInputFocused: false,
editTopic: false,
loading: false
2020-10-18 15:14:46 +05:00
};
2023-03-16 21:22:21 +05:00
this.title = props.notebook?.title;
this.description = props.notebook?.description;
2020-11-02 09:22:26 +05:00
this.listRef;
this.prevItem = null;
this.prevIndex = null;
this.currentSelectedInput = null;
2023-03-16 21:22:21 +05:00
this.id = props.notebook?.id;
2020-11-02 09:22:26 +05:00
this.backPressCount = 0;
this.currentInputValue = null;
this.titleRef;
this.descriptionRef;
this.topicsToDelete = [];
2020-12-31 18:53:05 +05:00
this.hiddenInput = createRef();
this.topicInputRef = createRef();
this.addingTopic = false;
2023-03-16 21:22:21 +05:00
this.actionSheetRef = props.actionSheetRef;
2022-01-07 23:42:58 +05:00
}
componentWillUnmount() {
2020-11-02 09:22:26 +05:00
refs = [];
2023-03-16 21:22:21 +05:00
}
2022-01-07 23:42:58 +05:00
2023-03-16 21:22:21 +05:00
componentDidMount() {
sleep(300).then(() => {
!this.state.notebook && this.titleRef?.focus();
});
2023-03-16 21:22:21 +05:00
}
2020-11-02 09:22:26 +05:00
close = () => {
refs = [];
2023-03-16 21:22:21 +05:00
this.props.close();
2020-11-02 09:22:26 +05:00
};
onDelete = (index) => {
2022-01-22 12:57:05 +05:00
let { topics } = this.state;
2020-11-02 09:22:26 +05:00
let prevTopics = topics;
refs = [];
prevTopics.splice(index, 1);
2022-01-07 23:42:58 +05:00
let edit = this.state.notebook;
2020-11-02 09:22:26 +05:00
if (edit && edit.id) {
2021-04-28 12:56:48 +05:00
let topicToDelete = edit.topics[index];
2020-11-23 15:57:31 +05:00
2020-11-02 09:22:26 +05:00
if (topicToDelete) {
this.topicsToDelete.push(topicToDelete.id);
}
}
let nextTopics = [...prevTopics];
if (this.prevIndex === index) {
this.prevIndex = null;
this.prevItem = null;
this.currentInputValue = null;
2020-12-31 18:53:05 +05:00
this.topicInputRef.current?.setNativeProps({
text: null
2020-11-02 09:22:26 +05:00
});
}
this.setState({
topics: nextTopics
2020-11-02 09:22:26 +05:00
});
};
2020-05-13 01:46:02 +05:00
2020-11-02 09:22:26 +05:00
addNewNotebook = async () => {
this.setState({
loading: true
});
2022-01-22 12:57:05 +05:00
let { topics, notebook } = this.state;
2020-05-12 14:27:22 +05:00
if (!this.title || this.title?.trim().length === 0) {
ToastEvent.show({
heading: "Notebook title is required",
type: "error",
context: "local"
});
this.setState({
loading: false
});
return;
}
2022-01-08 09:37:58 +05:00
let toEdit = null;
if (notebook) {
2022-01-07 23:42:58 +05:00
toEdit = db.notebooks.notebook(notebook.id).data;
2020-11-02 09:22:26 +05:00
}
2020-10-18 15:14:46 +05:00
2020-11-02 09:22:26 +05:00
let prevTopics = [...topics];
2020-10-18 15:14:46 +05:00
2020-11-02 09:22:26 +05:00
if (this.currentInputValue && this.currentInputValue.trim().length !== 0) {
if (this.prevItem != null) {
prevTopics[this.prevIndex] = this.currentInputValue;
} else {
prevTopics.push(this.currentInputValue);
this.currentInputValue = null;
}
}
let newNotebookId = null;
2022-01-08 09:37:58 +05:00
if (notebook) {
2020-11-02 09:22:26 +05:00
if (this.topicsToDelete?.length > 0) {
await db.notebooks
.notebook(toEdit.id)
.topics.delete(...this.topicsToDelete);
2020-11-02 09:22:26 +05:00
toEdit = db.notebooks.notebook(toEdit.id).data;
}
await db.notebooks.add({
title: this.title,
description: this.description,
2022-01-07 23:42:58 +05:00
id: notebook.id
2020-11-02 09:22:26 +05:00
});
let nextTopics = toEdit.topics.map((topic, index) => {
2022-01-22 12:57:05 +05:00
let copy = { ...topic };
2021-04-28 12:56:48 +05:00
copy.title = prevTopics[index];
2020-11-02 09:22:26 +05:00
return copy;
});
prevTopics.forEach((title, index) => {
2021-04-28 12:56:48 +05:00
if (!nextTopics[index]) {
2020-11-02 09:22:26 +05:00
nextTopics.push(title);
2020-05-12 14:09:56 +05:00
}
2020-11-02 09:22:26 +05:00
});
2022-01-07 23:42:58 +05:00
await db.notebooks.notebook(toEdit.id).topics.add(...nextTopics);
2020-11-02 09:22:26 +05:00
} else {
newNotebookId = await db.notebooks.add({
2020-11-02 09:22:26 +05:00
title: this.title,
description: this.description,
2022-01-08 09:37:58 +05:00
topics: prevTopics,
2022-01-22 12:57:05 +05:00
id: null
2020-11-02 09:22:26 +05:00
});
}
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setMenuPins();
2022-04-24 05:59:14 +05:00
Navigation.queueRoutesForUpdate(
"Notes",
"ColoredNotes",
"TaggedNotes",
"TopicNotes",
"Notebooks",
"Notebook"
2022-04-24 05:59:14 +05:00
);
2023-03-16 21:22:21 +05:00
useRelationStore.getState().update();
MoveNotes.present(db.notebooks.notebook(newNotebookId).data);
2020-11-02 09:22:26 +05:00
};
onSubmit = (forward = true) => {
2020-12-31 18:53:05 +05:00
this.hiddenInput.current?.focus();
2021-04-28 12:56:48 +05:00
let willFocus = true;
2022-01-22 12:57:05 +05:00
let { topics } = this.state;
if (!this.currentInputValue || this.currentInputValue?.trim().length === 0)
return;
2020-11-02 09:22:26 +05:00
let prevTopics = [...topics];
if (this.prevItem === null) {
prevTopics.push(this.currentInputValue);
this.setState({
topics: prevTopics
2020-11-02 09:22:26 +05:00
});
setTimeout(() => {
2022-01-22 12:57:05 +05:00
this.listRef.scrollToEnd({ animated: true });
2020-11-02 09:22:26 +05:00
}, 30);
this.currentInputValue = null;
} else {
prevTopics[this.prevIndex] = this.currentInputValue;
this.setState({
topics: prevTopics
2020-11-02 09:22:26 +05:00
});
this.currentInputValue = null;
2021-04-28 12:56:48 +05:00
2020-11-02 09:22:26 +05:00
if (this.state.editTopic) {
2020-12-31 18:53:05 +05:00
this.topicInputRef.current?.blur();
2020-11-02 09:22:26 +05:00
Keyboard.dismiss();
this.setState({
editTopic: false
2020-11-02 09:22:26 +05:00
});
2021-04-28 12:56:48 +05:00
willFocus = false;
2020-11-02 09:22:26 +05:00
}
this.prevItem = null;
this.prevIndex = null;
this.currentInputValue = null;
if (forward) {
setTimeout(() => {
2022-01-22 12:57:05 +05:00
this.listRef.scrollToEnd({ animated: true });
2020-11-02 09:22:26 +05:00
}, 30);
}
2020-01-06 19:03:21 +05:00
}
2021-01-13 13:23:01 +05:00
this.topicInputRef.current?.setNativeProps({
text: ""
});
2021-04-28 12:56:48 +05:00
willFocus && this.topicInputRef.current?.focus();
2020-11-02 09:22:26 +05:00
};
render() {
2022-01-22 12:57:05 +05:00
const { colors } = this.props;
2023-03-16 21:22:21 +05:00
const { topics, topicInputFocused, notebook } = this.state;
2020-01-18 18:14:08 +05:00
return (
2023-03-16 21:22:21 +05:00
<View
style={{
maxHeight: DDS.isTab ? "90%" : "96%",
borderRadius: DDS.isTab ? 5 : 0,
paddingHorizontal: 12
}}
2022-01-22 12:57:05 +05:00
>
2023-03-16 21:22:21 +05:00
<TextInput
ref={this.hiddenInput}
2021-07-24 13:15:53 +05:00
style={{
2023-03-16 21:22:21 +05:00
width: 1,
height: 1,
opacity: 0,
position: "absolute"
2022-01-22 12:57:05 +05:00
}}
2023-03-16 21:22:21 +05:00
blurOnSubmit={false}
/>
<DialogHeader
title={
notebook && notebook.dateCreated ? "Edit Notebook" : "New Notebook"
}
paragraph={
notebook && notebook.dateCreated
? "You are editing " + this.title + " notebook."
: "Notebooks are the best way to organize your notes."
}
/>
<Seperator half />
<Input
fwdRef={(ref) => (this.titleRef = ref)}
testID={notesnook.ids.dialogs.notebook.inputs.title}
onChangeText={(value) => {
this.title = value;
}}
placeholder="Enter a title"
onSubmit={() => {
this.descriptionRef.focus();
}}
returnKeyLabel="Next"
returnKeyType="next"
defaultValue={notebook ? notebook.title : null}
/>
<Input
fwdRef={(ref) => (this.descriptionRef = ref)}
testID={notesnook.ids.dialogs.notebook.inputs.description}
onChangeText={(value) => {
this.description = value;
}}
placeholder="Describe your notebook."
onSubmit={() => {
this.topicInputRef.current?.focus();
}}
returnKeyLabel="Next"
returnKeyType="next"
defaultValue={notebook ? notebook.description : null}
/>
<Input
fwdRef={this.topicInputRef}
testID={notesnook.ids.dialogs.notebook.inputs.topic}
onChangeText={(value) => {
this.currentInputValue = value;
if (this.prevItem !== null) {
refs[this.prevIndex].setNativeProps({
text: value,
style: {
borderBottomColor: colors.accent
}
});
}
2023-03-16 21:22:21 +05:00
}}
returnKeyLabel="Done"
returnKeyType="done"
onSubmit={() => {
this.onSubmit();
}}
blurOnSubmit={false}
button={{
testID: "topic-add-button",
icon: this.state.editTopic ? "check" : "plus",
onPress: this.onSubmit,
color: topicInputFocused ? colors.accent : colors.icon
}}
placeholder="Add a topic"
/>
<FlatList
data={topics}
ref={(ref) => (this.listRef = ref)}
nestedScrollEnabled
keyExtractor={(item, index) => item + index.toString()}
keyboardShouldPersistTaps="always"
keyboardDismissMode="interactive"
ListFooterComponent={<View style={{ height: 50 }} />}
renderItem={({ item, index }) => (
<TopicItem
item={item}
onPress={(item, index) => {
this.prevIndex = index;
this.prevItem = item;
this.topicInputRef.current?.setNativeProps({
text: item
});
2023-03-16 21:22:21 +05:00
this.topicInputRef.current?.focus();
this.currentInputValue = item;
this.setState({
editTopic: true
});
}}
onDelete={this.onDelete}
index={index}
colors={colors}
/>
)}
/>
<Seperator />
<Button
width="100%"
height={50}
fontSize={SIZE.md}
title={
notebook && notebook.dateCreated
? "Save changes"
: "Create notebook"
}
type="accent"
onPress={this.addNewNotebook}
/>
{/*
2021-12-31 16:30:43 +05:00
{Platform.OS === 'ios' && (
<View
style={{
height: 40
}}
/>
)} */}
2023-03-16 21:22:21 +05:00
</View>
2020-01-18 18:14:08 +05:00
);
2020-11-02 09:22:26 +05:00
}
}
2019-12-03 18:12:50 +05:00
2023-03-16 21:22:21 +05:00
AddNotebookSheet.present = (notebook) => {
presentSheet({
component: (ref, close, _update, colors) => (
<AddNotebookSheet
actionSheetRef={ref}
notebook={notebook}
close={close}
colors={colors}
/>
)
});
};
2022-01-22 12:57:05 +05:00
const TopicItem = ({ item, index, colors, onPress, onDelete }) => {
const topicRef = (ref) => (refs[index] = ref);
2020-09-07 13:12:41 +05:00
2020-11-02 09:22:26 +05:00
return (
<View
style={{
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
2021-07-10 11:49:59 +05:00
backgroundColor: colors.nav,
borderRadius: 5,
marginVertical: 5
2022-01-22 12:57:05 +05:00
}}
>
2020-11-02 09:22:26 +05:00
<TouchableOpacity
style={{
width: "80%",
backgroundColor: "transparent",
2020-11-02 09:22:26 +05:00
zIndex: 10,
position: "absolute",
height: 30
2020-11-02 09:22:26 +05:00
}}
onPress={() => {
onPress(item, index);
}}
/>
<TextInput
ref={topicRef}
editable={false}
style={[
styles.topicInput,
{
color: colors.pri
}
2020-11-02 09:22:26 +05:00
]}
defaultValue={item}
2021-12-30 10:42:03 +05:00
placeholderTextColor={colors.placeholder}
2020-11-02 09:22:26 +05:00
/>
<View
2020-11-02 20:45:56 +05:00
style={{
width: 80,
position: "absolute",
2020-11-02 20:45:56 +05:00
right: 0,
alignItems: "center",
flexDirection: "row",
justifyContent: "flex-end"
2022-01-22 12:57:05 +05:00
}}
>
2022-02-28 13:48:59 +05:00
<IconButton
2020-11-02 20:45:56 +05:00
onPress={() => {
2020-11-02 09:22:26 +05:00
onPress(item, index);
}}
name="pencil"
size={SIZE.lg - 5}
color={colors.icon}
2020-11-02 20:45:56 +05:00
/>
2022-02-28 13:48:59 +05:00
<IconButton
2020-11-02 09:22:26 +05:00
onPress={() => {
onDelete(index);
}}
name="minus"
size={SIZE.lg}
color={colors.icon}
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
wrapper: {
width: "100%",
height: "100%",
backgroundColor: "rgba(0,0,0,0.3)",
justifyContent: "center",
alignItems: "center"
2020-11-02 09:22:26 +05:00
},
overlay: {
width: "100%",
height: "100%",
position: "absolute"
2020-11-02 09:22:26 +05:00
},
headingContainer: {
flexDirection: "row",
justifyContent: "center",
alignItems: "center"
2020-11-02 09:22:26 +05:00
},
headingText: {
marginLeft: 5,
fontSize: SIZE.xl
2020-11-02 09:22:26 +05:00
},
input: {
2020-11-14 15:46:26 +05:00
paddingRight: 12,
paddingHorizontal: 0,
2020-11-02 20:45:56 +05:00
borderRadius: 0,
2020-11-02 09:22:26 +05:00
minHeight: 45,
2020-11-14 15:46:26 +05:00
fontSize: SIZE.md,
2020-11-02 09:22:26 +05:00
padding: pv - 2,
2020-11-02 20:45:56 +05:00
borderBottomWidth: 1,
2020-11-02 09:22:26 +05:00
marginTop: 10,
marginBottom: 5
2020-11-02 09:22:26 +05:00
},
addBtn: {
width: "12%",
2020-11-02 09:22:26 +05:00
minHeight: 45,
justifyContent: "center",
alignItems: "center",
position: "absolute",
right: 0
2020-11-02 09:22:26 +05:00
},
buttonContainer: {
justifyContent: "space-between",
alignItems: "center",
flexDirection: "row",
width: "100%",
marginTop: 20
2020-11-02 09:22:26 +05:00
},
topicContainer: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
marginTop: 10
2020-11-02 09:22:26 +05:00
},
topicInput: {
padding: pv - 5,
fontSize: SIZE.sm,
2021-01-03 10:38:07 +05:00
//fontFamily: "sans-serif",
2020-11-02 09:22:26 +05:00
paddingHorizontal: ph,
paddingRight: 40,
paddingVertical: 10,
width: "100%",
maxWidth: "100%"
2020-11-02 09:22:26 +05:00
},
topicBtn: {
borderRadius: 5,
width: 40,
height: 40,
justifyContent: "center",
alignItems: "center",
position: "absolute",
right: 0
}
2020-09-07 13:12:41 +05:00
});