Skip to content

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

feat: implement dynamic color routes

thecodrr4c51e2182248
diff --git a/apps/web/src/app.js b/apps/web/src/app.js
index 4fe202452..420173582 100644
--- a/apps/web/src/app.js
+++ b/apps/web/src/app.js
@@ -3,11 +3,18 @@ import "./app.css";
import Editor from "./components/editor";
import { Flex, Box, Button, Text } from "rebass";
import { ThemeProvider } from "./utils/theme";
-import RootNavigator from "./navigation/navigators/rootnavigator";
+import RootNavigator, {
+ bottomRoutes,
+ routes
+} from "./navigation/navigators/rootnavigator";
import "./app.css";
import { usePersistentState } from "./utils/hooks";
import { useTheme } from "emotion-theming";
-import useStore from "./common/store";
+import { useStore } from "./stores/app-store";
+import { useStore as useNotesStore } from "./stores/note-store";
+import { COLORS } from "./common";
+import { toTitleCase } from "./utils/string";
+import * as Icon from "react-feather";
const NavMenuItem = props => {
const [fill, setFill] = useState();
@@ -41,7 +48,7 @@ const NavMenuItem = props => {
<Flex
justifyContent={["flex-start", "center", "center"]}
alignItems="center"
- sx={{ marginLeft: [2, 0, 0] }}
+ sx={{ position: "relative", marginLeft: [2, 0, 0] }}
>
<props.item.icon
size={18}
@@ -58,13 +65,16 @@ const NavMenuItem = props => {
>
{props.item.title}
</Text>
+ {props.item.count && (
+ <Text sx={{ position: "absolute", top: -8, right: 10 }} fontSize={9}>
+ {props.item.count > 99 ? "99+" : props.item.count}
+ </Text>
+ )}
</Flex>
</Button>
);
};
-var startX, startWidth;
-
function App() {
const [selectedIndex, setSelectedIndex] = usePersistentState(
"navSelectedIndex",
@@ -72,11 +82,16 @@ function App() {
);
const [show, setShow] = usePersistentState("navContainerState", true);
- const isSideMenuOpen = useStore(state => state.isSideMenuOpen);
+ const isSideMenuOpen = useStore(store => store.isSideMenuOpen);
+ const refreshColors = useStore(store => store.refreshColors);
+ const setSelectedContext = useNotesStore(store => store.setSelectedContext);
useEffect(() => {
RootNavigator.navigate(Object.keys(RootNavigator.routes)[selectedIndex]);
+ refreshColors();
+ console.log(colors);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
+ const colors = useStore(store => store.colors);
return (
<ThemeProvider>
<Flex
@@ -95,64 +110,74 @@ function App() {
minWidth: ["85%", 50, 50],
maxWidth: ["85%", 50, 50],
display: [isSideMenuOpen ? "flex" : "none", "flex", "flex"],
- position: ["absolute", "relative", "relative"],
- overflow: "scroll",
- scrollbarWidth: "none",
- //TODO: need to test this on webkit and internet explorer
- "::-webkit-scrollbar": { width: 0, height: 0 },
- msOverflowStyle: "none"
- //"-ms-overflow-style": "none"
+ position: ["absolute", "relative", "relative"]
}}
bg={"shade"}
px={0}
>
- <Box>
- {Object.values(RootNavigator.routes).map(
- (item, index) =>
- !item.bottom && (
- <NavMenuItem
- onSelected={async () => {
- if (item.onClick) {
- return item.onClick();
- }
- if (selectedIndex === index) {
- setShow(!show);
- return;
- }
- if (RootNavigator.navigate(item.key)) {
- setSelectedIndex(index);
- }
- }}
- key={item.key}
- item={item}
- selected={selectedIndex === index}
- />
- )
- )}
+ <Box
+ sx={{
+ overflow: "scroll",
+ scrollbarWidth: "none",
+ //TODO: need to test this on webkit and internet explorer
+ "::-webkit-scrollbar": { width: 0, height: 0 },
+ msOverflowStyle: "none"
+ //"-ms-overflow-style": "none"
+ }}
+ >
+ {Object.values(routes).map((item, index) => (
+ <NavMenuItem
+ onSelected={async () => {
+ if (selectedIndex === index) {
+ setShow(!show);
+ return;
+ }
+ if (RootNavigator.navigate(item.key)) {
+ setSelectedIndex(index);
+ }
+ }}
+ key={item.key}
+ item={item}
+ selected={selectedIndex === index}
+ />
+ ))}
+ {colors.map(color => {
+ return (
+ <NavMenuItem
+ onSelected={async () => {
+ setSelectedContext({ type: "color", value: color.title });
+ RootNavigator.navigate("color", {
+ title: toTitleCase(color.title),
+ context: { colors: [color.title] }
+ });
+ }}
+ key={color.title}
+ item={{
+ color: COLORS[color.title],
+ title: toTitleCase(color.title),
+ icon: Icon.Circle,
+ count: color.count
+ }}
+ />
+ );
+ })}
</Box>
<Box>
- {Object.values(RootNavigator.routes).map(
- (item, index) =>
- item.bottom && (
- <NavMenuItem
- onSelected={async () => {
- if (item.onClick) {
- return item.onClick();
- }
- if (selectedIndex === index) {
- setShow(!show);
- return;
- }
- if (RootNavigator.navigate(item.key)) {
- setSelectedIndex(index);
- }
- }}
- key={item.key}
- item={item}
- selected={selectedIndex === index}
- />
- )
- )}
+ {Object.values(bottomRoutes).map((item, index) => (
+ <NavMenuItem
+ onSelected={async () => {
+ if (item.onClick) {
+ return item.onClick();
+ }
+ if (RootNavigator.navigate(item.key)) {
+ setSelectedIndex(index);
+ }
+ }}
+ key={item.key}
+ item={item}
+ selected={selectedIndex === index}
+ />
+ ))}
</Box>
</Flex>
<Flex flex="1 1 auto" flexDirection="row" alignContent="stretch" px={0}>
@@ -170,36 +195,6 @@ function App() {
flex="1 1 auto"
//style={{ width: "362px" }}
/>
- <Box
- className="resize-handle"
- bg="border"
- sx={{
- width: 5,
- display: ["none", "none", show ? "block" : "none"],
- opacity: 0,
- cursor: "col-resize"
- }}
- draggable={true}
- onMouseDown={e => {
- startX = e.clientX;
- let view = document
- .querySelector(".RootNavigator")
- .getBoundingClientRect();
- startWidth = parseInt(view.width, 10);
- }}
- onDrag={e => {
- let view = document.querySelector(".RootNavigator");
- view.style.width = `${startWidth + e.clientX - startX}px`;
- }}
- onDragEnd={() => {
- let view = document.querySelector(".RootNavigator");
- view.style.width = view.getBoundingClientRect().width;
- window.localStorage.setItem(
- "navigationViewWidth",
- view.style.width
- );
- }}
- />
<Editor />
</Flex>
<Box id="dialogContainer" />
diff --git a/apps/web/src/common/index.js b/apps/web/src/common/index.js
index a22df31ee..b99e76daa 100644
--- a/apps/web/src/common/index.js
+++ b/apps/web/src/common/index.js
@@ -5,14 +5,14 @@ import events from "events";
export const db = new Database(StorageInterface);
export const ev = new events.EventEmitter();
-export const COLORS = [
- { label: "red", code: "#ed2d37" },
- { label: "orange", code: "#ec6e05" },
- { label: "yellow", code: "yellow" },
- { label: "green", code: "green" },
- { label: "blue", code: "blue" },
- { label: "purple", code: "purple" },
- { label: "gray", code: "gray" }
-];
+export const COLORS = {
+ red: "#ed2d37",
+ orange: "#ec6e05",
+ yellow: "yellow",
+ green: "green",
+ blue: "blue",
+ purple: "purple",
+ gray: "gray"
+};
export const DEFAULT_CONTEXT = { colors: [], tags: [], notebook: {} };
diff --git a/apps/web/src/components/properties/index.js b/apps/web/src/components/properties/index.js
index 0f8651a2c..56c121fad 100644
--- a/apps/web/src/components/properties/index.js
+++ b/apps/web/src/components/properties/index.js
@@ -7,6 +7,7 @@ import { PinIcon } from "../icons";
import { usePersistentState } from "../../utils/hooks";
import { useStore } from "../../stores/editor-store";
import { COLORS } from "../../common";
+import { objectMap } from "../../utils/object";
const Properties = props => {
const [visible, setVisible] = usePersistentState("propertiesVisible", false);
@@ -141,21 +142,21 @@ const Properties = props => {
<Text sx={{ marginLeft: 1 }}>Colors:</Text>
</Flex>
<Flex flexWrap="wrap" sx={{ marginBottom: 2 }}>
- {COLORS.map(color => (
+ {objectMap(COLORS, (label, code) => (
<Flex
sx={{ position: "relative" }}
justifyContent="center"
alignItems="center"
- onClick={() => setColor(color.label)}
- key={color.label}
+ onClick={() => setColor(label)}
+ key={label}
>
<Icon.Circle
size={40}
style={{ cursor: "pointer" }}
- fill={color.code}
+ fill={code}
strokeWidth={0}
/>
- {colors.includes(color.label) && (
+ {colors.includes(label) && (
<Icon.Check
style={{
position: "absolute",
diff --git a/apps/web/src/navigation/navigators/rootnavigator.js b/apps/web/src/navigation/navigators/rootnavigator.js
index 6e2dcd666..c56974cfd 100644
--- a/apps/web/src/navigation/navigators/rootnavigator.js
+++ b/apps/web/src/navigation/navigators/rootnavigator.js
@@ -4,68 +4,16 @@ import {
Favorites,
Trash,
NotebooksContainer,
- Notes,
- TagsContainer
+ TagsContainer,
+ Notes
} from "../../views";
import * as Icon from "react-feather";
-import {
- createRoute,
- createNormalRoute,
- createColorRoute,
- createDeadRoute
-} from "../routes";
+import { createRoute, createNormalRoute, createDeadRoute } from "../routes";
import Navigator from "../index";
import { showLogInDialog } from "../../components/dialogs/logindialog";
import { changeTheme, isDarkTheme } from "../../utils/theme";
-import { db } from "../../common";
-/*For color Search*/
-const colorRoutes = {
- ...createColorRoute("Red", Notes, "#ed2d37", {
- onClick: () => {
- onClickMethod("Red", "red");
- }
- }),
- ...createColorRoute("Orange", Notes, "#ec6e05", {
- onClick: () => {
- onClickMethod("Orange", "orange");
- }
- }),
- ...createColorRoute("Yellow", Notes, "yellow", {
- onClick: () => {
- onClickMethod("Yellow", "yellow");
- }
- }),
- ...createColorRoute("Green", Notes, "green", {
- onClick: () => {
- onClickMethod("Green", "green");
- }
- }),
- ...createColorRoute("Blue", Notes, "blue", {
- onClick: () => {
- onClickMethod("Blue", "blue");
- }
- }),
- ...createColorRoute("Purple", Notes, "purple", {
- onClick: () => {
- onClickMethod("Purple", "purple");
- }
- }),
- ...createColorRoute("Gray", Notes, "gray", {
- onClick: () => {
- onClickMethod("Gray", "gray");
- }
- })
-};
-
-function onClickMethod(Title, label) {
- RootNavigator.navigate(Title, {
- notes: db.notes.colored(label),
- context: { colors: [label] }
- });
-}
-
-const bottomRoutes = {
+export const bottomRoutes = {
...createDeadRoute("nightmode", Icon.Moon, {
onClick: () => changeTheme(),
bottom: true,
@@ -81,19 +29,25 @@ const bottomRoutes = {
})
};
-const routes = {
+export const routes = {
...createNormalRoute("home", Home, Icon.Home),
...createRoute("notebooks", NotebooksContainer, {
icon: Icon.Book
}),
...createNormalRoute("favorites", Favorites, Icon.Star),
...createNormalRoute("trash", Trash, Icon.Trash2),
- ...createRoute("tags", TagsContainer, { icon: Icon.Tag }),
- ...colorRoutes,
- ...bottomRoutes
+ ...createRoute("tags", TagsContainer, { icon: Icon.Tag })
+};
+
+const invisibleRoutes = {
+ ...createNormalRoute("color", Notes, Icon.Circle)
};
-const RootNavigator = new Navigator("RootNavigator", routes, {
- backButtonEnabled: false
-});
+const RootNavigator = new Navigator(
+ "RootNavigator",
+ { ...routes, ...invisibleRoutes },
+ {
+ backButtonEnabled: false
+ }
+);
export default RootNavigator;
diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js
index 4139a219e..571fa26c2 100644
--- a/apps/web/src/stores/app-store.js
+++ b/apps/web/src/stores/app-store.js
@@ -1,4 +1,5 @@
import createStore from "../common/store";
+import { db } from "../common";
function appStore(set) {
return {
@@ -8,6 +9,12 @@ function appStore(set) {
},
openSideMenu: function() {
set(state => (state.isSideMenuOpen = true));
+ },
+ colors: [],
+ refreshColors: function() {
+ set(state => {
+ state.colors = db.colors.all;
+ });
}
};
}
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index b1fd12655..7b4e2c66c 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -1,5 +1,6 @@
import createStore from "../common/store";
import { store as notestore, LIST_TYPES } from "./note-store";
+import { store as appStore } from "./app-store";
import { db } from "../common";
const SESSION_STATES = {
@@ -61,7 +62,10 @@ function editorStore(set, get) {
};
db.notes.add(note).then(id => {
if (tags.length > 0) updateContext("tags", tags);
- if (colors.length > 0) updateContext("colors", colors);
+ if (colors.length > 0) {
+ updateContext("colors", colors);
+ appStore.getState().refreshColors();
+ }
set(state => {
state.session.id = id;
@@ -96,7 +100,7 @@ function editorStore(set, get) {
});
},
setColor: function(color) {
- setTagOrColor(get().session, "colors", color, "color", set);
+ setTagOrColor(get().session, "colors", color, "color", get().setSession);
},
setTag: function(tag) {
setTagOrColor(get().session, "tags", tag, "tag", get().setSession);
@@ -105,6 +109,7 @@ function editorStore(set, get) {
}
function setTagOrColor(session, array, value, func, set) {
+ console.log(arguments);
const { [array]: arr, id } = session;
let note = db.notes.note(id);
if (!note) return;
@@ -132,7 +137,6 @@ function updateContext(key, array) {
if (context.type === type) {
array.forEach(value => {
if (context.value === value) {
- console.log("updating according to context");
notestore.getState().setSelectedContext(context);
}
});
diff --git a/apps/web/src/utils/object.js b/apps/web/src/utils/object.js
new file mode 100644
index 000000000..10d9b5ceb
--- /dev/null
+++ b/apps/web/src/utils/object.js
@@ -0,0 +1,3 @@
+export function objectMap(obj, fn) {
+ return Object.entries(obj).map(([k, v], i) => fn(k, v, i));
+}
diff --git a/apps/web/src/utils/string.js b/apps/web/src/utils/string.js
new file mode 100644
index 000000000..9953d7717
--- /dev/null
+++ b/apps/web/src/utils/string.js
@@ -0,0 +1,3 @@
+export function toTitleCase(str) {
+ return str[0].toUpperCase() + str.substring(1);
+}
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index fe3dafbce..15c13441c 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -6813,7 +6813,7 @@ normalize-url@^3.0.0, normalize-url@^3.0.1:
"notes-core@https://github.com/thecodrr/notes-core.git":
version "1.1.0"
- resolved "https://github.com/thecodrr/notes-core.git#fafece9f7d74a4bbc64de9c6083444c56a636d45"
+ resolved "https://github.com/thecodrr/notes-core.git#418e48b357179db10ddb6b3116b61c5746c92e48"
dependencies:
fast-sort "^2.0.1"
fuzzysearch "^1.0.3"