Skip to content

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

feat: implement selection options

thecodrr8e3f90b00545
diff --git a/apps/web/src/common/selectionoptions.js b/apps/web/src/common/selectionoptions.js
new file mode 100644
index 000000000..ddca36e7d
--- /dev/null
+++ b/apps/web/src/common/selectionoptions.js
@@ -0,0 +1,36 @@
+import * as Icon from "react-feather";
+import { store } from "../stores/app-store";
+import { db } from "./index";
+
+function createOption(icon, onClick) {
+ return { icon, onClick: onClick.bind(this, store.getState()) };
+}
+
+function createOptions(options = []) {
+ return [...options, DeleteOption];
+}
+
+const DeleteOption = createOption(Icon.Trash2, function(state) {
+ const items = state.selectedItems.map(item => item.id);
+ const item = items[0];
+ if (item.type === "note") {
+ db.notes.delete(...item);
+ }
+});
+const FavoriteOption = createOption(Icon.Star, function() {});
+const UnfavoriteOption = createOption(Icon.Star, function() {});
+const AddToNotebookOption = createOption(Icon.Plus, function() {});
+
+const NotesOptions = createOptions([AddToNotebookOption, FavoriteOption]);
+const NotebooksOptions = createOptions();
+const TopicOptions = createOptions();
+const TrashOptions = createOptions();
+const FavoritesOptions = createOptions([UnfavoriteOption]);
+
+export default {
+ NotebooksOptions,
+ NotesOptions,
+ TopicOptions,
+ TrashOptions,
+ FavoritesOptions
+};
diff --git a/apps/web/src/components/list-item/index.js b/apps/web/src/components/list-item/index.js
index 271cc5f77..915273b96 100644
--- a/apps/web/src/components/list-item/index.js
+++ b/apps/web/src/components/list-item/index.js
@@ -1,9 +1,8 @@
-import React, { useEffect, useState } from "react";
+import React, { useEffect, useState, useCallback } from "react";
import { Flex, Box, Text } from "rebass";
import * as Icon from "react-feather";
import Dropdown, { DropdownTrigger, DropdownContent } from "../dropdown";
import Menu from "../menu";
-import { useStore } from "../../stores/note-store";
import {
store as appStore,
useStore as useAppStore
@@ -20,22 +19,22 @@ const ActionsMenu = props => (
/>
);
-function selectMenuItem(isSelected, setIsSelected) {
+function selectMenuItem(isSelected, toggleSelection) {
return {
title: isSelected ? "Unselect" : "Select",
onClick: () => {
const appState = appStore.getState();
if (!appState.isSelectionMode) {
appState.enterSelectionMode();
- setIsSelected(true);
+ toggleSelection();
} else {
- setIsSelected(!isSelected);
+ toggleSelection();
}
}
};
}
-const ItemSelector = ({ isSelected, setIsSelected }) => {
+const ItemSelector = ({ isSelected, toggleSelection }) => {
return (
<Box
width={24}
@@ -45,7 +44,7 @@ const ItemSelector = ({ isSelected, setIsSelected }) => {
color: "primary",
cursor: "pointer"
}}
- onClick={() => setIsSelected(isSelected => !isSelected)}
+ onClick={() => toggleSelection()}
>
{isSelected ? <Icon.CheckCircle /> : <Icon.Circle />}
</Box>
@@ -59,14 +58,26 @@ const ListItem = props => {
`contextMenu${props.index}`
);
const isSelectionMode = useAppStore(store => store.isSelectionMode);
+ const selectItem = useAppStore(store => store.selectItem);
const [isSelected, setIsSelected] = useState(false);
const [menuItems, setMenuItems] = useState(props.menuItems);
+
+ const toggleSelection = useCallback(
+ function toggleSelection() {
+ setIsSelected(state => !state);
+ selectItem(props.item);
+ },
+ [setIsSelected, selectItem, props.item]
+ );
+
useEffect(() => {
- setMenuItems([
- selectMenuItem(isSelected, setIsSelected),
- ...props.menuItems
- ]);
- }, [props.menuItems, isSelected]);
+ if (props.selectable) {
+ setMenuItems([
+ selectMenuItem(isSelected, toggleSelection),
+ ...props.menuItems
+ ]);
+ }
+ }, [props.menuItems, isSelected, props.selectable, toggleSelection]);
return (
<Flex
@@ -79,7 +90,10 @@ const ListItem = props => {
}}
>
{isSelectionMode && (
- <ItemSelector isSelected={isSelected} setIsSelected={setIsSelected} />
+ <ItemSelector
+ isSelected={isSelected}
+ toggleSelection={toggleSelection}
+ />
)}
<Flex
flex="1 1 auto"
@@ -130,7 +144,7 @@ const ListItem = props => {
onClick={() => {
//e.stopPropagation();
if (isSelectionMode) {
- setIsSelected(state => !state);
+ toggleSelection();
} else if (props.onClick) {
props.onClick();
}
diff --git a/apps/web/src/components/listview/index.js b/apps/web/src/components/listview/index.js
index e7eb4f1c0..eb4fdde7b 100644
--- a/apps/web/src/components/listview/index.js
+++ b/apps/web/src/components/listview/index.js
@@ -13,6 +13,8 @@ function ListView({ items, menu, button, onClick, noType }) {
const item = items[index];
return (
<ListItem
+ selectable
+ item={item}
title={item.title}
body={item.headline}
index={index}
diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js
index f197c604a..60e9a9d7e 100644
--- a/apps/web/src/components/note/index.js
+++ b/apps/web/src/components/note/index.js
@@ -63,6 +63,8 @@ export default class Note extends React.Component {
const note = item;
return (
<ListItem
+ selectable
+ item={note}
title={note.title}
body={note.headline}
id={note.id}
diff --git a/apps/web/src/components/notebook/index.js b/apps/web/src/components/notebook/index.js
index 858dff279..72e92c15d 100644
--- a/apps/web/src/components/notebook/index.js
+++ b/apps/web/src/components/notebook/index.js
@@ -33,6 +33,8 @@ export default class Notebook extends React.Component {
console.log("rendering notebook", notebook.id);
return (
<ListItem
+ selectable
+ item={notebook}
onClick={onClick}
title={notebook.title}
body={notebook.description}
diff --git a/apps/web/src/components/topic/index.js b/apps/web/src/components/topic/index.js
index 5e91cc41e..63b39a571 100644
--- a/apps/web/src/components/topic/index.js
+++ b/apps/web/src/components/topic/index.js
@@ -17,6 +17,8 @@ const Topic = ({ item, index, onClick }) => {
const topic = item;
return (
<ListItem
+ selectable
+ item={topic}
onClick={onClick}
title={topic.title}
info={`${topic.totalNotes} Notes`}
diff --git a/apps/web/src/navigation/index.js b/apps/web/src/navigation/index.js
index eabd8454a..b6ea4b928 100644
--- a/apps/web/src/navigation/index.js
+++ b/apps/web/src/navigation/index.js
@@ -80,40 +80,55 @@ export default class Navigator {
const NavigationContainer = props => {
const openSideMenu = useStore(store => store.openSideMenu);
- console.log(props);
+ const isSelectionMode = useStore(store => store.isSelectionMode);
return (
<ThemeProvider>
<Flex flexDirection="column" px={2}>
{(props.route.title || props.route.params.title) && (
- <Flex alignItems="center" py={2}>
- {props.canGoBack && (
+ <Flex alignItems="center" justifyContent="space-between">
+ <Flex alignItems="center" py={2}>
+ {props.canGoBack && (
+ <Box
+ onClick={props.backAction}
+ height={38}
+ color="fontPrimary"
+ sx={{ marginLeft: -2 /*correction */, marginRight: 2 }}
+ >
+ <Icon.ChevronLeft size={38} />
+ </Box>
+ )}
<Box
- onClick={props.backAction}
+ onClick={openSideMenu}
height={38}
color="fontPrimary"
- sx={{ marginLeft: -2 /*correction */, marginRight: 2 }}
+ sx={{
+ marginLeft: -5 /*correction */,
+ marginRight: 1,
+ display: ["block", "none", "none"]
+ }}
>
- <Icon.ChevronLeft size={38} />
+ <Icon.Menu size={38} />
</Box>
+ <Heading
+ fontSize="heading"
+ color={props.route.titleColor || "text"}
+ >
+ {props.route.title || props.route.params.title}
+ </Heading>
+ </Flex>
+ {props.route.options && isSelectionMode && (
+ <Flex>
+ {props.route.options.map(option => (
+ <Box
+ onClick={option.onClick}
+ mx={2}
+ sx={{ cursor: "pointer" }}
+ >
+ <option.icon />
+ </Box>
+ ))}
+ </Flex>
)}
- <Box
- onClick={openSideMenu}
- height={38}
- color="fontPrimary"
- sx={{
- marginLeft: -5 /*correction */,
- marginRight: 1,
- display: ["block", "none", "none"]
- }}
- >
- <Icon.Menu size={38} />
- </Box>
- <Heading
- fontSize="heading"
- color={props.route.titleColor || "text"}
- >
- {props.route.title || props.route.params.title}
- </Heading>
</Flex>
)}
<Text variant="title" color="primary">
diff --git a/apps/web/src/navigation/navigators/nbnavigator.js b/apps/web/src/navigation/navigators/nbnavigator.js
index 0b9fccdaf..326a57ff5 100644
--- a/apps/web/src/navigation/navigators/nbnavigator.js
+++ b/apps/web/src/navigation/navigators/nbnavigator.js
@@ -1,11 +1,14 @@
import { Notebooks, Notes, Topics } from "../../views";
import Navigator from "../index";
import { createRoute } from "../routes";
+import SelectionModeOptions from "../../common/selectionoptions";
const routes = {
...createRoute("notebooks", Notebooks, { title: "Notebooks" }),
- ...createRoute("topics", Topics),
- ...createRoute("notes", Notes)
+ ...createRoute("topics", Topics, {
+ options: SelectionModeOptions.TopicOptions
+ }),
+ ...createRoute("notes", Notes, { options: SelectionModeOptions.NotesOptions })
};
const NotebookNavigator = new Navigator("NotebookNavigator", routes, {
backButtonEnabled: true
diff --git a/apps/web/src/navigation/navigators/rootnavigator.js b/apps/web/src/navigation/navigators/rootnavigator.js
index ab89e785b..071af55d5 100644
--- a/apps/web/src/navigation/navigators/rootnavigator.js
+++ b/apps/web/src/navigation/navigators/rootnavigator.js
@@ -12,6 +12,7 @@ import { createRoute, createNormalRoute, createDeadRoute } from "../routes";
import Navigator from "../index";
import { showLogInDialog } from "../../components/dialogs/logindialog";
import { changeTheme, isDarkTheme } from "../../utils/theme";
+import SelectionModeOptions from "../../common/selectionoptions";
export const bottomRoutes = {
...createDeadRoute("nightmode", Icon.Moon, {
@@ -30,19 +31,32 @@ export const bottomRoutes = {
};
export const routes = {
- ...createNormalRoute("home", Home, Icon.Home, { title: "Home" }),
+ ...createNormalRoute("home", Home, Icon.Home, {
+ title: "Home",
+ options: SelectionModeOptions.NotesOptions
+ }),
...createRoute("notebooks", NotebooksContainer, {
- icon: Icon.Book
+ icon: Icon.Book,
+ options: SelectionModeOptions.NotebooksOptions
}),
...createNormalRoute("favorites", Favorites, Icon.Star, {
- title: "Favorites"
+ title: "Favorites",
+ options: SelectionModeOptions.FavoritesOptions
+ }),
+ ...createNormalRoute("trash", Trash, Icon.Trash, {
+ title: "Trash",
+ options: SelectionModeOptions.TrashOptions
}),
- ...createNormalRoute("trash", Trash, Icon.Trash, { title: "Trash" }),
- ...createRoute("tags", TagsContainer, { icon: Icon.Tag })
+ ...createRoute("tags", TagsContainer, {
+ icon: Icon.Tag,
+ options: SelectionModeOptions.NotesOptions
+ })
};
const invisibleRoutes = {
- ...createNormalRoute("color", Notes, Icon.Circle)
+ ...createNormalRoute("color", Notes, Icon.Circle, {
+ options: SelectionModeOptions.NotesOptions
+ })
};
const RootNavigator = new Navigator(
diff --git a/apps/web/src/navigation/navigators/tagnavigator.js b/apps/web/src/navigation/navigators/tagnavigator.js
index 1dd507db5..55f8f73db 100644
--- a/apps/web/src/navigation/navigators/tagnavigator.js
+++ b/apps/web/src/navigation/navigators/tagnavigator.js
@@ -1,10 +1,11 @@
import { Notes, Tags } from "../../views";
import Navigator from "../index";
import { createRoute } from "../routes";
+import SelectionModeOptions from "../../common/selectionoptions";
const routes = {
...createRoute("tags", Tags, { title: "Tags" }),
- ...createRoute("notes", Notes)
+ ...createRoute("notes", Notes, { options: SelectionModeOptions.NotesOptions })
};
const TagNavigator = new Navigator("TagNavigator", routes, {
backButtonEnabled: true
diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js
index 345062491..13ca32e69 100644
--- a/apps/web/src/stores/app-store.js
+++ b/apps/web/src/stores/app-store.js
@@ -1,17 +1,18 @@
import createStore from "../common/store";
import { db } from "../common";
-function appStore(set) {
+function appStore(set, get) {
return {
isSideMenuOpen: false,
isSelectionMode: false,
+ selectedItems: [],
+ colors: [],
closeSideMenu: function() {
set(state => (state.isSideMenuOpen = false));
},
openSideMenu: function() {
set(state => (state.isSideMenuOpen = true));
},
- colors: [],
refreshColors: function() {
set(state => {
state.colors = db.colors.all;
@@ -25,7 +26,21 @@ function appStore(set) {
exitSelectionMode: function() {
set(state => {
state.isSelectionMode = false;
+ state.selectedItems = [];
+ });
+ },
+ selectItem: function(item) {
+ set(state => {
+ const index = state.selectedItems.findIndex(v => item.id === v.id);
+ if (index >= 0) {
+ state.selectedItems.splice(index, 1);
+ } else {
+ state.selectedItems.push(item);
+ }
});
+ if (get().selectedItems.length <= 0) {
+ get().exitSelectionMode();
+ }
}
};
}
diff --git a/apps/web/src/views/Tags.js b/apps/web/src/views/Tags.js
index fd3bfd0fd..94ac3ab6c 100644
--- a/apps/web/src/views/Tags.js
+++ b/apps/web/src/views/Tags.js
@@ -24,6 +24,7 @@ const Tags = props => {
return (
<ListItem
+ selectable={false}
index={index}
title={<TagNode title={title} />}
info={`${count} notes`}