Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: impl note moving

thecodrr349fdfd50977
diff --git a/apps/web/src/app.js b/apps/web/src/app.js
index 9faaa0acb..e344c0489 100644
--- a/apps/web/src/app.js
+++ b/apps/web/src/app.js
@@ -8,12 +8,13 @@ import theme from "./theme";
import { routes, navigate } from "./navigation";
import CheckBox from "./components/checkbox";
import * as Icon from "react-feather";
+
const NavMenuItem = props => {
useEffect(() => {
if (props.selected) {
navigate(props.item.key);
}
- });
+ }, []);
return (
<Button
onClick={props.onSelected}
@@ -54,8 +55,9 @@ function App() {
{Object.values(routes).map((item, index) => (
<NavMenuItem
onSelected={() => {
- navigate(item.key);
- setSelectedIndex(index);
+ if (navigate(item.key)) {
+ setSelectedIndex(index);
+ }
}}
key={item.key}
item={item}
diff --git a/apps/web/src/components/editor/index.js b/apps/web/src/components/editor/index.js
index 9ba5077d5..100b977e2 100644
--- a/apps/web/src/components/editor/index.js
+++ b/apps/web/src/components/editor/index.js
@@ -54,7 +54,6 @@ async function saveNote(quill) {
dateCreated: timestamp
};
timestamp = await db.addNote(note);
- console.log("note saved", note);
}
function startAutoSave(quill) {
setInterval(async () => {
@@ -114,7 +113,6 @@ const Editor = props => {
timestamp = note.dateCreated;
Editor.titleRef.value = note.title;
// quill.setText(note.content.text);
- console.log(note);
quill.setContents(note.content.delta);
quill.setSelection(note.content.text.length - 1, 0); //to move the cursor to the end
}, 0);
diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js
index 4d7272211..2c9595c3d 100644
--- a/apps/web/src/components/note/index.js
+++ b/apps/web/src/components/note/index.js
@@ -4,10 +4,19 @@ import TimeAgo from "timeago-react";
import { db, ev } from "../../common";
import { showSnack } from "../snackbar";
import ListItem from "../list-item";
+import { navigate, routes } from "../../navigation";
const dropdownRefs = [];
const menuItems = [
- { title: "Move" },
+ {
+ title: "Move",
+ onClick: note => {
+ navigate(routes.notebooks.key, undefined, {
+ intent: "moveNote",
+ data: note
+ });
+ }
+ },
{ title: "Pin" },
{ title: "Favorite" },
{ title: "Share" },
diff --git a/apps/web/src/navigation/index.js b/apps/web/src/navigation/index.js
index 643e466d7..e3a51f18c 100644
--- a/apps/web/src/navigation/index.js
+++ b/apps/web/src/navigation/index.js
@@ -34,33 +34,42 @@ export const routes = {
}
};
const history = {};
-
-export function navigate(routeName, root = "navigationView") {
+export const navigationEvents = {
+ onWillNavigateAway: undefined
+};
+var lastRoute = undefined;
+export function navigate(routeName, root = "navigationView", params = {}) {
let route = routes[routeName];
// do not navigate if the previous route is the same
if (!history[root]) {
history[root] = [];
}
- let historyContainer = history[root];
+ if (lastRoute === route) {
+ return false;
+ }
+
if (
- historyContainer.length > 0 &&
- historyContainer[historyContainer.length - 1].title === route.title
+ navigationEvents.onWillNavigateAway &&
+ !navigationEvents.onWillNavigateAway(routeName, params)
) {
- return;
+ navigationEvents.onWillNavigateAway = undefined;
+ return false;
}
let rootView = document.querySelector(`.${root}`);
- if (!rootView) return;
- ReactDOM.render(<ThemedComponent route={route} />, rootView);
-
- historyContainer[historyContainer.length] = {
- ...route
- };
- history[root] = historyContainer;
+ if (!rootView) return false;
+ if (lastRoute) {
+ history[root][history[root].length] = {
+ ...lastRoute
+ };
+ }
+ ReactDOM.render(<ThemedComponent route={route} params={params} />, rootView);
+ lastRoute = route;
+ return true;
}
-export function goBack(root) {
+export function goBack(root = "navigationView") {
if (!history[root] || history[root].length <= 0) {
return;
}
@@ -98,6 +107,7 @@ function ThemedComponent(props) {
backAction={action => setBackAction(prev => action)}
canGoBack={setCanGoBack}
changeTitle={setTitle}
+ {...props.params}
/>
)}
</ThemeProvider>
diff --git a/apps/web/src/views/Notebooks.js b/apps/web/src/views/Notebooks.js
index d6e9587b2..e2c804584 100644
--- a/apps/web/src/views/Notebooks.js
+++ b/apps/web/src/views/Notebooks.js
@@ -12,7 +12,7 @@ import { Virtuoso as List } from "react-virtuoso";
import Notebook from "../components/notebook";
import Topic from "../components/topic";
import Note from "../components/note";
-import { routes } from "../navigation";
+import { routes, navigationEvents, goBack as p_goBack } from "../navigation";
const inputRefs = [];
const history = [{}];
@@ -20,16 +20,30 @@ const Notebooks = props => {
const [open, setOpen] = useState(false);
const [notebooks, setNotebooks] = useState([]);
const [selected, setSelected] = useState({});
+ const [intent, setIntent] = useState(props.intent);
useEffect(() => {
Notebooks.onRefresh = () => {
setNotebooks(db.getNotebooks());
};
Notebooks.onRefresh();
+ navigationEvents.onWillNavigateAway = (routeName, params) => {
+ if (routeName !== "notebooks" && intent === "moveNote") {
+ return window.confirm(
+ "Are you sure you want to navigate away? Your note selection will be lost."
+ );
+ }
+ return true;
+ };
return () => {
Notebooks.onRefresh = undefined;
};
}, []);
function navigate(item, save = true, title = undefined) {
+ //transform notes in a topic to real notes
+ if (item.notes) {
+ item = { ...item };
+ item.notes = db.getTopic(selected.dateCreated, item.title);
+ }
if (save) {
history[history.length] = selected;
}
@@ -41,6 +55,7 @@ const Notebooks = props => {
props.changeTitle(title);
props.canGoBack(item.title !== undefined);
props.backAction(goBack);
+
setSelected((item.title && item) || {});
}
function goBack() {
@@ -55,6 +70,13 @@ const Notebooks = props => {
{selected.title}
</Text>
)}
+ {intent === "moveNote" && selected.type !== "topic" && (
+ <Text variant="body" color="primary" fontWeight="bold">
+ Please select a{" "}
+ {selected.type === "notebook" ? "topic" : "notebook"} to move the
+ note to:
+ </Text>
+ )}
<Search placeholder="Search" />
<List
style={{
@@ -92,13 +114,39 @@ const Notebooks = props => {
}}
/>
<Button
- Icon={Icon.Plus}
- onClick={() => setOpen(true)}
+ Icon={
+ intent === "moveNote" && selected.type === "topic"
+ ? Icon.Move
+ : Icon.Plus
+ }
+ onClick={async () => {
+ if (intent === "moveNote" && selected.type === "topic") {
+ let to = {
+ notebook: history[history.length - 1].dateCreated,
+ topic: selected.title
+ };
+ db.moveNote(props.data.dateCreated, props.data.notebook, to)
+ .then(
+ result =>
+ result &&
+ showSnack(
+ `Moved note to ${history[history.length - 1].title}.`
+ )
+ )
+ .catch(err => showSnack(err.message));
+ p_goBack();
+ setIntent(undefined);
+ } else {
+ setOpen(true);
+ }
+ }}
content={
selected.type === "notebook"
? "Add more topics"
: selected.type === "topic"
- ? "Make a new note"
+ ? intent === "moveNote"
+ ? "Move note here"
+ : "Make a new note"
: "Create a notebook"
}
/>
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index 6faedc6fa..321669f26 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -6991,7 +6991,7 @@ normalize-url@^3.0.0, normalize-url@^3.0.1:
"notes-core@https://github.com/thecodrr/notes-core.git":
version "0.2.0"
- resolved "https://github.com/thecodrr/notes-core.git#e7664608adbb20e0c7684aaa08fd3aaf074524d4"
+ resolved "https://github.com/thecodrr/notes-core.git#265c12f66aa05fc2353e97e01279fe226fcd9b06"
dependencies:
fuzzysearch "^1.0.3"
transfun "^1.0.2"