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
fix: do not reload editor on theme change
thecodrr39bc1d58cf9a
diff --git a/packages/editor/src/components/action-sheet/index.tsx b/packages/editor/src/components/action-sheet/index.tsxindex 18a3237bb..083abaab9 100644--- a/packages/editor/src/components/action-sheet/index.tsx+++ b/packages/editor/src/components/action-sheet/index.tsx@@ -6,8 +6,6 @@ import React, {useEffect,} from "react";import { MenuItem } from "../menu/types";-import { useTheme } from "emotion-theming";-import { Theme } from "@notesnook/theme";import { Box, Button, Flex, Text } from "rebass";import { Icon } from "../../toolbar/components/icon";import { Icons } from "../../toolbar/icons";@@ -22,6 +20,7 @@ import {useTransform,useAnimation,} from "framer-motion";+import { useTheme } from "../../toolbar/stores/toolbar-store";const AnimatedFlex = motion(Flex);type ActionSheetHistoryItem = {@@ -84,7 +83,7 @@ export function ActionSheetPresenter(draggable = true,children,} = props;- const theme: Theme = useTheme();+ const theme = useTheme();const contentRef = useRef<HTMLDivElement>();const y = useMotionValue(0);@@ -205,7 +204,7 @@ export function ActionSheetPresenter(bg: "background",borderTopLeftRadius: 15,borderTopRightRadius: 15,- boxShadow: theme.shadows.menu,+ boxShadow: theme?.shadows.menu || "none",flex: 1,flexDirection: "column",}}diff --git a/packages/editor/src/components/popup-presenter/index.tsx b/packages/editor/src/components/popup-presenter/index.tsxindex 09d5d7eee..c57375689 100644--- a/packages/editor/src/components/popup-presenter/index.tsx+++ b/packages/editor/src/components/popup-presenter/index.tsx@@ -10,7 +10,6 @@ import { getPosition, PositionOptions } from "../../utils/position";import Modal from "react-modal";import ReactDOM from "react-dom";import { Editor } from "@tiptap/core";-import { ThemeProvider } from "emotion-theming";import { getPopupContainer, getToolbarElement } from "../../toolbar/utils/dom";import { Theme } from "@notesnook/theme";import {@@ -24,6 +23,7 @@ import {usePopupRenderer,} from "./popuprenderer";import { ResponsivePresenter, ResponsivePresenterProps } from "../responsive";+import { ThemeProvider } from "../theme-provider";export type PopupPresenterProps = {isOpen: boolean;@@ -345,18 +345,17 @@ export function usePopupHandler(options: UsePopupHandlerOptions) {}type ShowPopupOptions = {- theme: Theme;popup: (closePopup: () => void) => React.ReactNode;} & Partial<ResponsivePresenterProps>;export function showPopup(options: ShowPopupOptions) {- const { theme, popup, ...props } = options;+ const { popup, ...props } = options;function hide() {ReactDOM.unmountComponentAtNode(getPopupContainer());}ReactDOM.render(- <ThemeProvider theme={theme}>+ <ThemeProvider><ResponsivePresenterisOpenonClose={hide}diff --git a/packages/editor/src/components/theme-provider/index.tsx b/packages/editor/src/components/theme-provider/index.tsxnew file mode 100644index 000000000..ad20fc698--- /dev/null+++ b/packages/editor/src/components/theme-provider/index.tsx@@ -0,0 +1,12 @@+import { ThemeProvider as EmotionThemeProvider } from "emotion-theming";+import { PropsWithChildren } from "react";+import { useTheme } from "../../toolbar/stores/toolbar-store";++export function ThemeProvider(props: PropsWithChildren<{}>) {+ const theme = useTheme();+ return (+ <EmotionThemeProvider theme={theme || {}}>+ {props.children}+ </EmotionThemeProvider>+ );+}diff --git a/packages/editor/src/extensions/react/react-node-view.tsx b/packages/editor/src/extensions/react/react-node-view.tsxindex cf01666cd..cb2a3a925 100644--- a/packages/editor/src/extensions/react/react-node-view.tsx+++ b/packages/editor/src/extensions/react/react-node-view.tsx@@ -13,10 +13,10 @@ import {} from "./types";import { NodeViewRendererProps } from "@tiptap/core";import { Theme } from "@notesnook/theme";-import { ThemeProvider } from "emotion-theming";// @ts-ignoreimport { __serializeForClipboard } from "prosemirror-view";import { Editor } from "../../types";+import { ThemeProvider } from "../../components/theme-provider";export class ReactNodeView<P extends ReactNodeViewProps> implements NodeView {private domRef!: HTMLElement;@@ -129,10 +129,9 @@ export class ReactNodeView<P extends ReactNodeViewProps> implements NodeView {forwardRef?: ForwardRef): React.ReactElement<any> | null {if (!this.options.component) return null;- const theme = this.editor.storage.theme as Theme;return (- <ThemeProvider theme={theme}>+ <ThemeProvider><this.options.component{...props}editor={this.editor}diff --git a/packages/editor/src/extensions/react/selection-based-react-node-view.tsx b/packages/editor/src/extensions/react/selection-based-react-node-view.tsxindex a4ccdc486..1776c7a30 100644--- a/packages/editor/src/extensions/react/selection-based-react-node-view.tsx+++ b/packages/editor/src/extensions/react/selection-based-react-node-view.tsx@@ -18,8 +18,8 @@ import {import { ReactNodeView } from "./react-node-view";import { NodeViewRendererProps } from "@tiptap/core";import { Theme } from "@notesnook/theme";-import { ThemeProvider } from "emotion-theming";import { Editor } from "../../types";+import { ThemeProvider } from "../../components/theme-provider";/*** A ReactNodeView that handles React components sensitive@@ -76,13 +76,12 @@ export class SelectionBasedNodeView<forwardRef?: ForwardRef): React.ReactElement<any> | null {if (!this.options.component) return null;- const theme = this.editor.storage.theme as Theme;const isSelected =this.editor.isEditable &&(this.insideSelection() || this.nodeInsideSelection());return (- <ThemeProvider theme={theme}>+ <ThemeProvider><this.options.component{...props}editor={this.editor}diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.tsindex 7214f48ba..9fe406f3c 100644--- a/packages/editor/src/index.ts+++ b/packages/editor/src/index.ts@@ -66,14 +66,17 @@ const useTiptap = (isMobile,onDownloadAttachment,onOpenAttachmentPicker,+ onBeforeCreate,...restOptions} = options;const PortalProviderAPI = usePortalProvider();const setIsMobile = useToolbarStore((store) => store.setIsMobile);+ const setTheme = useToolbarStore((store) => store.setTheme);useEffect(() => {setIsMobile(isMobile || false);- }, [isMobile]);+ setTheme(theme);+ }, [isMobile, theme]);const defaultOptions = useMemo<Partial<EditorOptions>>(() => ({@@ -144,19 +147,16 @@ const useTiptap = (KeepInView,],onBeforeCreate: ({ editor }) => {- if (theme) {- editor.storage.theme = theme;- }editor.storage.portalProviderAPI = PortalProviderAPI;+ if (onBeforeCreate) onBeforeCreate({ editor });},injectCSS: false,}),[- theme,onDownloadAttachment,onOpenAttachmentPicker,PortalProviderAPI,- isMobile,+ onBeforeCreate,]);diff --git a/packages/editor/src/toolbar/components/icon.tsx b/packages/editor/src/toolbar/components/icon.tsxindex 526061d3a..d81213f55 100644--- a/packages/editor/src/toolbar/components/icon.tsx+++ b/packages/editor/src/toolbar/components/icon.tsx@@ -1,8 +1,8 @@import MDIIcon from "@mdi/react";import { Theme } from "@notesnook/theme";import { SchemeColors } from "@notesnook/theme/dist/theme/colorscheme";-import { useTheme } from "emotion-theming";import { Flex, FlexProps } from "rebass";+import { useTheme } from "../stores/toolbar-store";type IconProps = {title?: string;@@ -20,9 +20,9 @@ function MDIIconWrapper({stroke,rotate,}: IconProps) {- const theme: Theme = useTheme();+ const theme = useTheme();- const themedColor: string = theme.colors+ const themedColor: string = theme?.colors? (theme.colors[color] as string): color;@@ -32,7 +32,9 @@ function MDIIconWrapper({title={title}path={path}size={- typeof size === "string" ? `${theme.iconSizes[size]}px` : `${size}px`+ typeof size === "string"+ ? `${theme?.iconSizes[size] || 24}px`+ : `${size}px`}style={{strokeWidth: stroke || "0px",diff --git a/packages/editor/src/toolbar/components/tool-button.tsx b/packages/editor/src/toolbar/components/tool-button.tsxindex a73a87b19..b60d4fc15 100644--- a/packages/editor/src/toolbar/components/tool-button.tsx+++ b/packages/editor/src/toolbar/components/tool-button.tsx@@ -1,6 +1,5 @@import { Theme } from "@notesnook/theme";import { SchemeColors } from "@notesnook/theme/dist/theme/colorscheme";-import { useTheme } from "emotion-theming";import React from "react";import { ButtonProps } from "rebass";import { IconNames, Icons } from "../icons";diff --git a/packages/editor/src/toolbar/floating-menus/hover-popup/index.tsx b/packages/editor/src/toolbar/floating-menus/hover-popup/index.tsxindex 38dbd7052..7b4069244 100644--- a/packages/editor/src/toolbar/floating-menus/hover-popup/index.tsx+++ b/packages/editor/src/toolbar/floating-menus/hover-popup/index.tsx@@ -69,7 +69,6 @@ export function HoverPopupHandler(props: FloatingMenuProps) {}}/>),- theme: editor.storage.theme,blocking: false,focusOnRender: false,position: {diff --git a/packages/editor/src/toolbar/stores/toolbar-store.ts b/packages/editor/src/toolbar/stores/toolbar-store.tsindex af875a646..99f9c1c2e 100644--- a/packages/editor/src/toolbar/stores/toolbar-store.ts+++ b/packages/editor/src/toolbar/stores/toolbar-store.ts@@ -1,9 +1,12 @@+import { Theme } from "@notesnook/theme";import create from "zustand";export type ToolbarLocation = "top" | "bottom";export type PopupRef = { id: string; group: string };interface ToolbarState {+ theme?: Theme;+ setTheme: (theme?: Theme) => void;isMobile: boolean;openedPopups: Record<string, PopupRef | false>;setIsMobile: (isMobile: boolean) => void;@@ -16,12 +19,17 @@ interface ToolbarState {}export const useToolbarStore = create<ToolbarState>((set, get) => ({+ theme: undefined,isMobile: false,openedPopups: {},setIsMobile: (isMobile) =>set((state) => {state.isMobile = isMobile;}),+ setTheme: (theme) =>+ set((state) => {+ state.theme = theme;+ }),toolbarLocation: "top",setToolbarLocation: (location) =>set((state) => {@@ -60,3 +68,13 @@ export function useToolbarLocation() {export function useIsMobile() {return useToolbarStore((store) => store.isMobile);}++export const useTheme = Object.defineProperty(+ () => {+ return useToolbarStore((store) => store.theme);+ },+ "theme",+ {+ get: () => useToolbarStore.getState().theme,+ }+) as (() => Theme | undefined) & { theme: Theme | undefined };diff --git a/packages/editor/src/toolbar/tools/block.tsx b/packages/editor/src/toolbar/tools/block.tsxindex 2100516f9..adb143f1f 100644--- a/packages/editor/src/toolbar/tools/block.tsx+++ b/packages/editor/src/toolbar/tools/block.tsx@@ -207,7 +207,6 @@ const embedDesktop = (editor: Editor): MenuItem => ({onClick: () => {if (!editor) return;showPopup({- theme: editor.storage.theme,popup: (hide) => (<EmbedPopuptitle="Insert embed"@@ -289,7 +288,6 @@ const uploadImageFromURL = (editor: Editor): MenuItem => ({icon: "link",onClick: () => {showPopup({- theme: editor.storage.theme,popup: (hide) => (<ImageUploadPopuponInsert={(image) => {diff --git a/packages/editor/src/toolbar/tools/table.tsx b/packages/editor/src/toolbar/tools/table.tsxindex d735c71e4..0d4e9a94b 100644--- a/packages/editor/src/toolbar/tools/table.tsx+++ b/packages/editor/src/toolbar/tools/table.tsx@@ -390,7 +390,6 @@ const cellProperties = (editor: Editor): MenuButton => ({type: "button",onClick: () => {showPopup({- theme: editor.storage.theme,popup: (hide) => <CellPropertiesPopup onClose={hide} editor={editor} />,});},