editor: load theme from local storage on init

This commit is contained in:
ammarahm-ed
2023-08-04 20:58:24 +05:00
committed by Ammar Ahmed
parent 985ae848d3
commit 3831b9409e
3 changed files with 28 additions and 2 deletions

View File

@@ -27,6 +27,12 @@ import Tiptap from "./components/editor";
import { EmotionEditorTheme } from "./theme-factory";
import { Global, css } from "@emotion/react";
import { useMemo } from "react";
import { getTheme } from "./utils";
const currentTheme = getTheme();
if (currentTheme) {
useThemeEngineStore.getState().setTheme(currentTheme);
}
function App(): JSX.Element {
return (

View File

@@ -18,6 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Editor } from "@notesnook/editor";
import {
ThemeDefinition,
useThemeColors,
useThemeEngineStore
} from "@notesnook/theme";
import {
MutableRefObject,
useCallback,
@@ -25,8 +30,7 @@ import {
useRef,
useState
} from "react";
import { EventTypes, isReactNative, post } from "../utils";
import { useThemeColors, useThemeEngineStore } from "@notesnook/theme";
import { EventTypes, isReactNative, post, saveTheme } from "../utils";
import { injectCss, transform } from "../utils/css";
type Attachment = {
@@ -190,6 +194,9 @@ export function useEditorController(update: () => void): EditorController {
break;
case "native:theme":
setTheme(message.value);
setTimeout(() => {
saveTheme(message.value as ThemeDefinition);
});
break;
case "native:title":
setTitle(value);

View File

@@ -21,6 +21,7 @@ import { ToolbarGroupDefinition } from "@notesnook/editor";
import { Editor } from "@notesnook/editor";
import { Dispatch, MutableRefObject, RefObject, SetStateAction } from "react";
import { useEditorController } from "../hooks/useEditorController";
import { ThemeDefinition } from "@notesnook/theme";
export type SafeAreaType = {
top: number;
@@ -191,3 +192,15 @@ export function post<T extends keyof typeof EventTypes>(
globalThis.logger = logger;
globalThis.post = post;
export function saveTheme(theme: ThemeDefinition) {
localStorage.setItem("editor-theme", JSON.stringify(theme));
}
export function getTheme() {
const json = localStorage.getItem("editor-theme");
if (json) {
return JSON.parse(json) as ThemeDefinition;
}
return undefined;
}