A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
mobile: multi-tab support
Ammar Ahmede81c825fc729
diff --git a/apps/mobile/app/components/attachments/actions.tsx b/apps/mobile/app/components/attachments/actions.tsxindex 2e860ecd4..6d00516af 100644--- a/apps/mobile/app/components/attachments/actions.tsx+++ b/apps/mobile/app/components/attachments/actions.tsx@@ -182,18 +182,17 @@ const Actions = ({relations.map((relation) => relation.fromId).forEach(async (id) => {- const tab = useTabStore.getState().getTabForNote(id);- if (tab !== undefined) {- const isFocused = useTabStore.getState().currentTab === tab;+ useTabStore.getState().forEachNoteTab(id, async (tab) => {+ const isFocused = useTabStore.getState().currentTab === tab.id;if (isFocused) {eSendEvent(eOnLoadNote, {item: await db.notes.note(id),forced: true});} else {- editorController.current.commands.setLoading(true, tab);+ editorController.current.commands.setLoading(true, tab.id);}- }+ });});close?.();},diff --git a/apps/mobile/app/components/properties/index.js b/apps/mobile/app/components/properties/index.jsindex dd7753c3c..96cb1c5e4 100644--- a/apps/mobile/app/components/properties/index.js+++ b/apps/mobile/app/components/properties/index.js@@ -141,7 +141,7 @@ export const Properties = ({ close = () => {}, item, buttons = [] }) => {close();eSendEvent(eOnLoadNote, {item: item,- presistTab: true+ newTab: true});if (!DDS.isTab) {tabBarRef.current?.goToPage(1);diff --git a/apps/mobile/app/hooks/use-actions.tsx b/apps/mobile/app/hooks/use-actions.tsxindex ddd0e440e..24cb906bc 100644--- a/apps/mobile/app/hooks/use-actions.tsx+++ b/apps/mobile/app/hooks/use-actions.tsx@@ -19,14 +19,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>./* eslint-disable no-inner-declarations */import {Color,+ createInternalLink,ItemReference,Note,Notebook,Reminder,Tag,TrashItem,- VAULT_ERRORS,- createInternalLink+ VAULT_ERRORS} from "@notesnook/core";import { strings } from "@notesnook/intl";import { DisplayedNotification } from "@notifee/react-native";@@ -50,11 +50,11 @@ import ReminderSheet from "../components/sheets/reminder";import { useSideBarDraggingStore } from "../components/side-menu/dragging-store";import { useTabStore } from "../screens/editor/tiptap/use-tab-store";import {- ToastManager,eSendEvent,eSubscribeEvent,openVault,- presentSheet+ presentSheet,+ ToastManager} from "../services/event-manager";import Navigation from "../services/navigation";import Notifications from "../services/notifications";@@ -544,14 +544,11 @@ export const useActions = ({const toggleReadyOnlyMode = async () => {const currentReadOnly = (item as Note).readonly;await db.notes.readonly(!currentReadOnly, item?.id);-- if (useTabStore.getState().hasTabForNote(item.id)) {- const tabId = useTabStore.getState().getTabForNote(item.id);- if (!tabId) return;- useTabStore.getState().updateTab(tabId, {+ useTabStore.getState().forEachNoteTab(item.id, (tab) => {+ useTabStore.getState().updateTab(tab.id, {readonly: !currentReadOnly});- }+ });Navigation.queueRoutesForUpdate();close();};diff --git a/apps/mobile/app/screens/editor/tiptap/commands.ts b/apps/mobile/app/screens/editor/tiptap/commands.tsindex f3b0eeff4..3b97a7c26 100644--- a/apps/mobile/app/screens/editor/tiptap/commands.ts+++ b/apps/mobile/app/screens/editor/tiptap/commands.ts@@ -30,7 +30,7 @@ import { sleep } from "../../../utils/time";import { Settings } from "./types";import { useTabStore } from "./use-tab-store";import { getResponse, randId, textInput } from "./utils";-import { EditorSessionItem } from "./tab-history";+import { EditorSessionItem } from "@notesnook/common/dist/utils/editor-sessions";type Action = { job: string; id: string };@@ -217,11 +217,11 @@ if (typeof statusBar !== "undefined") {setTags = async (note: Note | null | undefined) => {if (!note) return;- const tabId = useTabStore.getState().getTabForNote(note.id);-- const tags = await db.relations.to(note, "tag").resolve();- await this.doAsync(- `+ useTabStore.getState().forEachNoteTab(note.id, async (tab) => {+ const tabId = tab.id;+ const tags = await db.relations.to(note, "tag").resolve();+ await this.doAsync(+ `const tags = editorTags[${tabId}];if (tags && tags.current) {tags.current.setTags(${JSON.stringify(@@ -234,8 +234,9 @@ if (typeof statusBar !== "undefined") {)});}`,- "setTags"- );+ "setTags"+ );+ });};clearTags = async (tabId: number) => {diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor-events.tsx b/apps/mobile/app/screens/editor/tiptap/use-editor-events.tsxindex 5d00fdd70..7b9530445 100644--- a/apps/mobile/app/screens/editor/tiptap/use-editor-events.tsx+++ b/apps/mobile/app/screens/editor/tiptap/use-editor-events.tsx@@ -597,10 +597,26 @@ export const useEditorEvents = (break;}case EditorEvents.tabFocused: {+ console.log(+ "Focused tab",+ editorMessage.tabId,+ editorMessage.noteId,+ "Content:",+ editorMessage.value+ );++ const { hasContent, isLoading, needsRefresh } = editorMessage.value;+eSendEvent(eEditorTabFocused, editorMessage.tabId);+ if (needsRefresh) {+ useTabStore.getState().updateTab(editorMessage.tabId, {+ needsRefresh: false+ });+ }+if (- (!editorMessage.value || editor.currentLoadingNoteId.current) &&+ (isLoading || !hasContent || needsRefresh) &&editorMessage.noteId) {if (!useSettingStore.getState().isAppLoading) {@@ -609,7 +625,8 @@ export const useEditorEvents = (eSendEvent(eOnLoadNote, {item: note,forced: true,- tabId: editorMessage.tabId+ tabId: editorMessage.tabId,+ refresh: hasContent && needsRefresh ? needsRefresh : false});}} else {diff --git a/apps/mobile/app/screens/editor/tiptap/use-tab-store.ts b/apps/mobile/app/screens/editor/tiptap/use-tab-store.tsindex d90ba527f..a0b0d0ec7 100644--- a/apps/mobile/app/screens/editor/tiptap/use-tab-store.ts+++ b/apps/mobile/app/screens/editor/tiptap/use-tab-store.ts@@ -81,6 +81,7 @@ export type TabItem = {locked?: boolean;noteLocked?: boolean;pinned?: boolean;+ needsRefresh?: boolean;};const history = new History();@@ -95,10 +96,12 @@ export type TabStore = {) => void;removeTab: (index: number) => void;moveTab: (index: number, toIndex: number) => void;- newTab: (options?: Omit<Partial<TabItem>, "id">) => void;+ newTab: (options?: Omit<Partial<TabItem>, "id">) => number;focusTab: (id: number) => void;getNoteIdForTab: (id: number) => string | undefined;getTabForNote: (noteId: string) => number | undefined;+ getTabsForNote: (noteId: string) => TabItem[];+ forEachNoteTab: (noteId: string, cb: (tab: TabItem) => void) => void;hasTabForNote: (noteId: string) => boolean;focusEmptyTab: () => void;getCurrentNoteId: () => string | undefined;@@ -230,18 +233,23 @@ export const useTabStore = create<TabStore>(const nextTabs = get().tabs.slice();nextTabs.splice(index, 1);history.remove(id);- if (nextTabs.length === 0) {- nextTabs.push({- id: 0- });- }tabHistory.clearStackForTab(id);set({tabs: nextTabs});- get().focusTab(- isFocused ? history.restoreLast() || 0 : get().currentTab- );+ syncTabs("tabs");+ setTimeout(() => {+ if (nextTabs.length === 0) {+ set({+ tabs: [{ id: 0 }]+ });+ get().focusTab(0);+ } else {+ get().focusTab(+ isFocused ? history.restoreLast() || 0 : get().currentTab+ );+ }+ });}},newTab: (options) => {@@ -262,6 +270,7 @@ export const useTabStore = create<TabStore>(tabs: nextTabs});get().focusTab(id);+ return id;},focusEmptyTab: () => {const index = get().tabs.findIndex((t) => !t.noteId);@@ -306,6 +315,13 @@ export const useTabStore = create<TabStore>(getTabForNote: (noteId: string) => {return get().tabs.find((t) => t.noteId === noteId)?.id;},+ getTabsForNote(noteId: string) {+ return get().tabs.filter((t) => t.noteId === noteId);+ },+ forEachNoteTab: (noteId: string, cb: (tab: TabItem) => void) => {+ const tabs = get().tabs.filter((t) => t.noteId === noteId);+ tabs.forEach(cb);+ },getCurrentNoteId: () => {return get().tabs.find((t) => t.id === get().currentTab)?.noteId;},diff --git a/apps/mobile/app/services/notifications.ts b/apps/mobile/app/services/notifications.tsindex a1230dcc5..b00f84bef 100644--- a/apps/mobile/app/services/notifications.ts+++ b/apps/mobile/app/services/notifications.ts@@ -458,7 +458,7 @@ async function loadNote(id: string, jump: boolean) {if (tab !== undefined) {useTabStore.getState().focusTab(tab);} else {- useTabStore.getState().focusPreviewTab(id, {+ useTabStore.getState().updateTab(useTabStore.getState().currentTab, {noteId: id,readonly: note.readonly,noteLocked: isLockeddiff --git a/packages/editor-mobile/src/components/editor.tsx b/packages/editor-mobile/src/components/editor.tsxindex 41395d12f..861a289d2 100644--- a/packages/editor-mobile/src/components/editor.tsx+++ b/packages/editor-mobile/src/components/editor.tsx@@ -314,8 +314,13 @@ const Tiptap = ({post(EditorEvents.tabFocused,- !!globalThis.editorControllers[tabRef.current.id]?.content.current &&- !editorControllers[tabRef.current.id]?.loading,+ {+ hasContent:+ !!globalThis.editorControllers[tabRef.current.id]?.content+ .current,+ isLoading: editorControllers[tabRef.current.id]?.loading,+ needsRefresh: tabRef.current?.needsRefresh+ },tabRef.current.id,state.getCurrentNoteId());diff --git a/packages/editor-mobile/src/hooks/useEditorController.ts b/packages/editor-mobile/src/hooks/useEditorController.tsindex 8bd500d8b..6e6604fcf 100644--- a/packages/editor-mobile/src/hooks/useEditorController.ts+++ b/packages/editor-mobile/src/hooks/useEditorController.ts@@ -143,7 +143,7 @@ export function useEditorController({const tabRef = useRef(tab);tabRef.current = tab;- const [loading, setLoading] = useState(true);+ const [loading, setLoading] = useState(false);const setTheme = useThemeEngineStore((store) => store.setTheme);const { colors } = useThemeColors("editor");const [title, setTitle] = useState("");@@ -244,18 +244,18 @@ export function useEditorController({clearTimeout(timers.current?.change);}- const params = [- {- html: htmlContentRef.current,- ignoreEdit: ignoreEdit- },- tabRef.current.id,- tabRef.current.noteId,- currentSessionId- ];-timers.current.change = setTimeout(async () => {htmlContentRef.current = editor.getHTML();+ const params = [+ {+ html: htmlContentRef.current,+ ignoreEdit: ignoreEdit+ },+ tabRef.current.id,+ tabRef.current.noteId,+ currentSessionId+ ];+const pendingContentIds =await pendingSaveRequests.getPendingContentIds();postAsyncWithTimeout(EditorEvents.content, ...params, 5000)diff --git a/packages/editor-mobile/src/hooks/useTabStore.ts b/packages/editor-mobile/src/hooks/useTabStore.tsindex aaaf05ec3..fa85ed6e1 100644--- a/packages/editor-mobile/src/hooks/useTabStore.ts+++ b/packages/editor-mobile/src/hooks/useTabStore.ts@@ -37,6 +37,7 @@ export type TabItem = {locked?: boolean;noteLocked?: boolean;pinned?: boolean;+ needsRefresh?: boolean;};export type NoteState = {