Skip to content

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

feat: implement pin to menu

thecodrr4a66d9f016b4
diff --git a/apps/web/src/app.js b/apps/web/src/app.js
index ccba8f87d..c3166fd00 100644
--- a/apps/web/src/app.js
+++ b/apps/web/src/app.js
@@ -28,6 +28,7 @@ import Banner from "./components/banner";
function App() {
const [show, setShow] = useState(true);
const refreshColors = useStore((store) => store.refreshColors);
+ const refreshMenuPins = useStore((store) => store.refreshMenuPins);
const isFocusMode = useStore((store) => store.isFocusMode);
const addReminder = useStore((store) => store.addReminder);
const initUser = useUserStore((store) => store.init);
@@ -42,6 +43,7 @@ function App() {
useEffect(
function initializeApp() {
refreshColors();
+ refreshMenuPins();
initUser();
initNotes();
(async function () {
@@ -53,7 +55,7 @@ function App() {
}
})();
},
- [refreshColors, initUser, initNotes, addReminder]
+ [refreshColors, refreshMenuPins, initUser, initNotes, addReminder]
);
useEffect(() => {
diff --git a/apps/web/src/components/global-menu-wrapper/index.js b/apps/web/src/components/global-menu-wrapper/index.js
index adcc7983d..94a5f6f4f 100644
--- a/apps/web/src/components/global-menu-wrapper/index.js
+++ b/apps/web/src/components/global-menu-wrapper/index.js
@@ -5,7 +5,7 @@ import Animated from "../animated";
import { useAnimation } from "framer-motion";
function GlobalMenuWrapper() {
- const [items, data, title, state, closeMenu] = useContextMenu();
+ const [items, title, state, closeMenu] = useContextMenu();
const animation = useAnimation();
useEffect(() => {
if (state === "open") {
@@ -33,7 +33,6 @@ function GlobalMenuWrapper() {
<Menu
id="globalContextMenu"
menuItems={items}
- data={data}
state={state}
title={title}
style={{
diff --git a/apps/web/src/components/icons/index.js b/apps/web/src/components/icons/index.js
index 48839b01d..0b2498dfa 100644
--- a/apps/web/src/components/icons/index.js
+++ b/apps/web/src/components/icons/index.js
@@ -45,11 +45,12 @@ function createIcon(name, rotate = false) {
export const Plus = createIcon(Icons.mdiPlus);
export const Note = createIcon(Icons.mdiHomeOutline);
export const Minus = createIcon(Icons.mdiMinus);
-export const Notebook = createIcon(Icons.mdiBookOutline);
+export const Notebook = createIcon(Icons.mdiNotebookOutline);
+export const Notebook2 = createIcon(Icons.mdiBookOpenOutline);
export const ArrowLeft = createIcon(Icons.mdiArrowLeft);
export const ArrowRight = createIcon(Icons.mdiArrowRight);
export const Move = createIcon(Icons.mdiBookPlusMultipleOutline);
-export const Topic = createIcon(Icons.mdiFormatTitle);
+export const Topic = createIcon(Icons.mdiBookmarkOutline);
export const Alert = createIcon(Icons.mdiAlert);
export const Vault = createIcon(Icons.mdiShieldOutline);
export const Unlock = createIcon(Icons.mdiLockOpenOutline);
@@ -80,7 +81,8 @@ export const AddToNotebook = createIcon(Icons.mdiBookPlusMultipleOutline);
export const ChevronLeft = createIcon(Icons.mdiChevronLeft);
export const ChevronRight = createIcon(Icons.mdiChevronRight);
export const Close = createIcon(Icons.mdiClose);
-export const Tag = createIcon(Icons.mdiTagTextOutline);
+export const Tag = createIcon(Icons.mdiTagMultipleOutline);
+export const Tag2 = createIcon(Icons.mdiTagOutline);
export const Pin = createIcon(Icons.mdiPinOutline);
export const PinFilled = createIcon(Icons.mdiPin);
diff --git a/apps/web/src/components/list-item/index.js b/apps/web/src/components/list-item/index.js
index fb26b80fa..881f70e75 100644
--- a/apps/web/src/components/list-item/index.js
+++ b/apps/web/src/components/list-item/index.js
@@ -1,10 +1,11 @@
-import React, { useEffect, useCallback } from "react";
+import React, { useEffect, useCallback, useMemo } from "react";
import { Flex, Text } from "rebass";
import * as Icon from "../icons";
import {
store as selectionStore,
useStore as useSelectionStore,
} from "../../stores/selection-store";
+import { useOpenContextMenu } from "../../utils/useContextMenu";
function selectMenuItem(isSelected, toggleSelection) {
return {
@@ -53,6 +54,8 @@ function ListItem(props) {
selectedItems.findIndex((item) => props.item.id === item.id) > -1;
const selectItem = useSelectionStore((store) => store.selectItem);
+ const openContextMenu = useOpenContextMenu();
+
const toggleSelection = useCallback(
function toggleSelection() {
selectItem(props.item);
@@ -60,41 +63,22 @@ function ListItem(props) {
[selectItem, props.item]
);
+ const menuItems = useMemo(() => {
+ let items = props.menuItems;
+ if (props.selectable)
+ items = [selectMenuItem(isSelected, toggleSelection), ...items];
+ return items;
+ }, [props.menuItems, isSelected, toggleSelection, props.selectable]);
+
useEffect(() => {
if (!isSelectionMode && isSelected) toggleSelection();
}, [isSelectionMode, toggleSelection, isSelected]);
- const openContextMenu = useCallback(
- (event, withClick) => {
- let items = props.menuItems;
- if (props.selectable)
- items = [selectMenuItem(isSelected, toggleSelection), ...items];
- window.dispatchEvent(
- new CustomEvent("globalcontextmenu", {
- detail: {
- state: "open",
- items,
- data: props.menuData,
- internalEvent: event,
- withClick,
- },
- })
- );
- },
- [
- isSelected,
- props.menuData,
- props.menuItems,
- props.selectable,
- toggleSelection,
- ]
- );
-
return (
<Flex
bg={props.focused || isSelected ? "shade" : "background"}
alignItems="center"
- onContextMenu={openContextMenu}
+ onContextMenu={(e) => openContextMenu(e, menuItems, false)}
p={2}
justifyContent="space-between"
sx={{
@@ -181,7 +165,7 @@ function ListItem(props) {
<Icon.MoreVertical
size={22}
color="icon"
- onClick={(event) => openContextMenu(event, true)}
+ onClick={(event) => openContextMenu(event, menuItems, true)}
/>
)}
</Flex>
diff --git a/apps/web/src/components/navigation-menu/index.js b/apps/web/src/components/navigation-menu/index.js
index c585424ed..9edbacbab 100644
--- a/apps/web/src/components/navigation-menu/index.js
+++ b/apps/web/src/components/navigation-menu/index.js
@@ -45,6 +45,7 @@ function NavigationMenu(props) {
const [selectedRoute, setSelectedRoute] = useState(path);
const isFocusMode = useAppStore((store) => store.isFocusMode);
const colors = useAppStore((store) => store.colors);
+ const pins = useAppStore((store) => store.menuPins);
const isSideMenuOpen = useAppStore((store) => store.isSideMenuOpen);
const toggleSideMenu = useAppStore((store) => store.toggleSideMenu);
const isSyncing = useUserStore((store) => store.isSyncing);
@@ -141,6 +142,31 @@ function NavigationMenu(props) {
}}
/>
))}
+ <Flex
+ flexDirection="column"
+ sx={{ borderTop: "1px solid", borderTopColor: "border" }}
+ >
+ {pins.map((pin) => (
+ <NavigationItem
+ key={pin.id}
+ title={pin.title}
+ menu={{
+ items: [{ title: "Unpin", onClick: () => {} }],
+ data: pin,
+ }}
+ icon={
+ pin.type === "notebook"
+ ? Icon.Notebook2
+ : pin.type === "tag"
+ ? Icon.Tag2
+ : Icon.Topic
+ }
+ onClick={() => {
+ navigate(``);
+ }}
+ />
+ ))}
+ </Flex>
</Flex>
<Flex flexDirection="column">
{theme === "light" ? (
diff --git a/apps/web/src/components/navigation-menu/navigation-item.js b/apps/web/src/components/navigation-menu/navigation-item.js
index f6196dd56..7d472fa6b 100644
--- a/apps/web/src/components/navigation-menu/navigation-item.js
+++ b/apps/web/src/components/navigation-menu/navigation-item.js
@@ -1,12 +1,14 @@
import React from "react";
import { Flex, Button, Text } from "rebass";
import { useStore as useAppStore } from "../../stores/app-store";
+import { useOpenContextMenu } from "../../utils/useContextMenu";
import useMobile from "../../utils/use-mobile";
function NavigationItem(props) {
const { icon: Icon, color, title, isLoading } = props;
const toggleSideMenu = useAppStore((store) => store.toggleSideMenu);
const isMobile = useMobile();
+ const openContextMenu = useOpenContextMenu();
return (
<Button
@@ -15,6 +17,10 @@ function NavigationItem(props) {
py={2}
label={title}
title={title}
+ onContextMenu={(event) => {
+ if (!props.menu) return;
+ openContextMenu(event, props.menu.items, props.menu.data, false);
+ }}
onClick={() => {
if (isMobile) toggleSideMenu(false);
props.onClick();
@@ -34,6 +40,11 @@ function NavigationItem(props) {
<Text
variant="body"
fontSize="subtitle"
+ sx={{
+ whiteSpace: "nowrap",
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+ }}
color={props.selected ? "primary" : "text"}
ml={1}
>
diff --git a/apps/web/src/components/note/index.js b/apps/web/src/components/note/index.js
index 61e906c35..750c1d1f0 100644
--- a/apps/web/src/components/note/index.js
+++ b/apps/web/src/components/note/index.js
@@ -219,7 +219,6 @@ function Note(props) {
</Flex>
}
pinned={pinnable && note.pinned}
- menuData={note}
menuItems={menuItems(note, props.context)}
/>
);
diff --git a/apps/web/src/components/notebook/index.js b/apps/web/src/components/notebook/index.js
index 9b8785e97..c8812991a 100644
--- a/apps/web/src/components/notebook/index.js
+++ b/apps/web/src/components/notebook/index.js
@@ -2,19 +2,29 @@ import React from "react";
import { Flex, Text } from "rebass";
import ListItem from "../list-item";
import { store } from "../../stores/notebook-store";
+import { store as appStore } from "../../stores/app-store";
import { showEditNotebookDialog } from "../dialogs/addnotebookdialog";
import { showDeleteConfirmation } from "../dialogs/confirm";
import { showItemDeletedToast, showUnpinnedToast } from "../../common/toasts";
+import { db } from "../../common";
+
const pin = async (notebook, index) => {
await store.pin(notebook, index);
if (notebook.pinned) showUnpinnedToast(notebook.id, "notebook");
};
+
function menuItems(notebook, index) {
return [
{
title: notebook.pinned ? "Unpin" : "Pin",
onClick: () => pin(notebook, index),
},
+ {
+ title: db.settings.isPinned(notebook.id)
+ ? "Unpin from Menu"
+ : "Pin to Menu",
+ onClick: () => appStore.pinItemToMenu(notebook),
+ },
{
title: "Edit",
onClick: async () => {
@@ -95,7 +105,6 @@ class Notebook extends React.Component {
}
pinned={notebook.pinned}
index={index}
- menuData={notebook}
menuItems={menuItems(notebook, index)}
/>
);
diff --git a/apps/web/src/components/tag/index.js b/apps/web/src/components/tag/index.js
index c77926edc..15ad8687c 100644
--- a/apps/web/src/components/tag/index.js
+++ b/apps/web/src/components/tag/index.js
@@ -2,6 +2,15 @@ import React from "react";
import ListItem from "../list-item";
import { navigate } from "hookrouter";
import { Text } from "rebass";
+import { store as appStore } from "../../stores/app-store";
+import { db } from "../../common";
+
+const menuItems = (item) => [
+ {
+ title: db.settings.isPinned(item.id) ? "Unpin from Menu" : "Pin to Menu",
+ onClick: () => appStore.pinItemToMenu(item),
+ },
+];
function Tag({ item, index }) {
const { title, noteIds } = item;
@@ -12,6 +21,7 @@ function Tag({ item, index }) {
index={index}
title={<TagNode title={title} />}
info={`${noteIds.length} notes`}
+ menuItems={menuItems(item)}
onClick={() => {
navigate(`/tags/${title}`);
}}
diff --git a/apps/web/src/components/topic/index.js b/apps/web/src/components/topic/index.js
index a98a36b24..a88d9f8bc 100644
--- a/apps/web/src/components/topic/index.js
+++ b/apps/web/src/components/topic/index.js
@@ -5,9 +5,14 @@ import { confirm } from "../dialogs/confirm";
import * as Icon from "../icons";
import { db } from "../../common";
import { store } from "../../stores/notebook-store";
+import { store as appStore } from "../../stores/app-store";
import { Text } from "rebass";
const menuItems = (item) => [
+ {
+ title: db.settings.isPinned(item.id) ? "Unpin from Menu" : "Pin to Menu",
+ onClick: () => appStore.pinItemToMenu(item),
+ },
{
title: "Edit",
onClick: () => {
@@ -57,7 +62,6 @@ function Topic({ item, index, onClick }) {
title={topic.title}
info={`${topic.totalNotes} Notes`}
index={index}
- menuData={topic}
menuItems={menuItems(topic)}
/>
);
diff --git a/apps/web/src/components/trash-item/index.js b/apps/web/src/components/trash-item/index.js
index e549211dd..4d04949ee 100644
--- a/apps/web/src/components/trash-item/index.js
+++ b/apps/web/src/components/trash-item/index.js
@@ -70,7 +70,6 @@ function TrashItem({ item, index }) {
<Text color="primary">{toTitleCase(item.itemType)}</Text>
</Flex>
}
- menuData={item}
menuItems={menuItems(item, index)}
/>
);
diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js
index 22251c172..83d887ae2 100644
--- a/apps/web/src/stores/app-store.js
+++ b/apps/web/src/stores/app-store.js
@@ -7,6 +7,8 @@ import { store as editorStore } from "./editor-store";
import { store as tagStore } from "./tag-store";
import BaseStore from "./index";
import { isMobile } from "../utils/dimensions";
+import { showToast } from "../utils/toast";
+import { toTitleCase } from "../utils/string";
class AppStore extends BaseStore {
// default state
@@ -16,6 +18,7 @@ class AppStore extends BaseStore {
colors = [];
globalMenu = { items: [], data: {} };
reminders = [];
+ menuPins = [];
refresh = async () => {
noteStore.refresh();
@@ -24,12 +27,17 @@ class AppStore extends BaseStore {
tagStore.refresh();
await editorStore.openLastSession();
this.refreshColors();
+ this.refreshMenuPins();
};
refreshColors = () => {
this.set((state) => (state.colors = db.colors.all));
};
+ refreshMenuPins = () => {
+ this.set((state) => (state.menuPins = db.settings.pins));
+ };
+
toggleFocusMode = () => {
this.set((state) => (state.isFocusMode = !state.isFocusMode));
};
@@ -65,6 +73,41 @@ class AppStore extends BaseStore {
})
);
};
+
+ pinItemToMenu = async (item) => {
+ if (db.settings.isPinned(item.id)) {
+ await db.settings.unpin(item.id);
+ await showToast(
+ "success",
+ `${toTitleCase(item.type)} unpinned from menu!`
+ );
+ } else {
+ await db.settings.pin(item.type, {
+ id: item.id,
+ notebookId: item.notebookId,
+ });
+ await showToast("success", `${toTitleCase(item.type)} pinned to menu!`);
+ }
+ this.refreshMenuPins();
+
+ // refresh the respective list
+ switch (item.type) {
+ case "notebook": {
+ notebookStore.refresh();
+ break;
+ }
+ case "topic": {
+ notebookStore.setSelectedNotebookTopics(item.notebookId);
+ break;
+ }
+ case "tag": {
+ tagStore.refresh();
+ break;
+ }
+ default:
+ return;
+ }
+ };
}
/**
diff --git a/apps/web/src/utils/useContextMenu.js b/apps/web/src/utils/useContextMenu.js
index cd829d4f6..b05c79103 100644
--- a/apps/web/src/utils/useContextMenu.js
+++ b/apps/web/src/utils/useContextMenu.js
@@ -1,4 +1,4 @@
-import { useEffect, useState } from "react";
+import { useCallback, useEffect, useState } from "react";
var isOpening = false;
@@ -92,18 +92,16 @@ function getPosition(e) {
function useContextMenu() {
const [items, setItems] = useState([]);
- const [data, setData] = useState({});
const [title, setTitle] = useState();
const [state, setState] = useState();
useEffect(() => {
const onGlobalContextMenu = (e) => {
- const { items, data, title, internalEvent, state } = e.detail;
+ const { items, title, internalEvent, state } = e.detail;
setState(state);
if (state === "close") {
return;
}
setItems(items);
- setData(data);
setTitle(title);
openMenu(internalEvent);
};
@@ -112,7 +110,23 @@ function useContextMenu() {
window.removeEventListener("globalcontextmenu", onGlobalContextMenu);
};
}, []);
- return [items, data, title, state, closeMenu];
+ return [items, title, state, closeMenu];
+}
+
+export function useOpenContextMenu() {
+ return useCallback((event, items, data, withClick) => {
+ window.dispatchEvent(
+ new CustomEvent("globalcontextmenu", {
+ detail: {
+ state: "open",
+ items,
+ data,
+ internalEvent: event,
+ withClick,
+ },
+ })
+ );
+ }, []);
}
export default useContextMenu;
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index d204cfb94..0ea7f7205 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -8963,8 +8963,8 @@ normalize-url@^3.0.0, normalize-url@^3.0.1:
integrity sha512-U+JJi7duF1o+u2pynbp2zXDW2/PADgC30f0GsHZtRh+HOcXHnw137TrNlyxxRvWW5fjKd3bcLHPxofWuCjaeZg==
"notes-core@git+ssh://[email protected]:streetwriters/notesnook-core.git":
- version "2.6.1"
- resolved "git+ssh://[email protected]:streetwriters/notesnook-core.git#a4d7c41bfefb3395183e3a1191ab453f3ac5de05"
+ version "2.6.2"
+ resolved "git+ssh://[email protected]:streetwriters/notesnook-core.git#423d4f84f61013cea247a4c2b6aa6ee428602531"
dependencies:
fast-sort "^2.0.1"
fuzzysearch "^1.0.3"