Skip to content

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

web: refactor default font settings

Abdullah Atta83762c668e17
diff --git a/apps/web/src/components/default-font/index.tsx b/apps/web/src/components/default-font/index.tsx
index d5567c7d2..2b4afd1cf 100644
--- a/apps/web/src/components/default-font/index.tsx
+++ b/apps/web/src/components/default-font/index.tsx
@@ -25,70 +25,70 @@ import { toTitleCase } from "../../utils/string";
import DropdownButton from "../dropdown-button";
export function DefaultFont() {
- const { setFontSize, setFontFamily } = useEditorStore((store) => store);
- const fontSize = useEditorStore((store) => store.session.fontSize);
+ return null;
+ // const fontSize = useEditorStore((store) => store.session.fontSize);
//const fontFamily = useEditorStore((store) => store.session.fontFamily);
- const fonts = ["sans-serif", "serif", "monospace"];
- const getOptions = () =>
- getFonts(Config.get("fontFamily", "sans-serif")).map((font) => ({
- title: () => toTitleCase(font),
- onClick: () => {
- const newFonts = [font];
- for (const item of fonts) {
- if (item !== font) {
- newFonts.push(item);
- }
- }
- Config.set("fontFamily", font);
- setFontFamily(font);
- setOptions(getOptions());
- },
- key: font
- }));
+ // const fonts = ["sans-serif", "serif", "monospace"];
+ // const getOptions = () =>
+ // getFonts(Config.get("fontFamily", "sans-serif")).map((font) => ({
+ // title: () => toTitleCase(font),
+ // onClick: () => {
+ // const newFonts = [font];
+ // for (const item of fonts) {
+ // if (item !== font) {
+ // newFonts.push(item);
+ // }
+ // }
+ // Config.set("fontFamily", font);
+ // setFontFamily(font);
+ // setOptions(getOptions());
+ // },
+ // key: font
+ // }));
- const [options, setOptions] = useState(getOptions());
+ // const [options, setOptions] = useState(getOptions());
- return (
- <Flex sx={{ justifyContent: "space-evenly", flexWrap: "wrap" }}>
- <Flex sx={{ flex: 3, minWidth: 100 }}>
- <Slider
- min={8}
- max={120}
- defaultValue={parseInt(fontSize.replace("px", ""))}
- step={1}
- onChange={(e) => {
- setFontSize(`${parseInt(e.target.value)}px`);
- Config.set("fontSize", `${e.target.value}px`);
- }}
- sx={{ width: "75%" }}
- />
- <Text
- sx={{
- width: "25%",
- fontSize: "12px",
- textAlign: "center"
- }}
- >
- {fontSize}
- </Text>
- </Flex>
- <DropdownButton
- options={options}
- title="Font Family"
- sx={{
- flex: 1,
- minWidth: 100
- }}
- buttonStyle={{
- width: "80%"
- }}
- chevronStyle={{
- width: "20%"
- }}
- />
- </Flex>
- );
+ // return (
+ // <Flex sx={{ justifyContent: "space-evenly", flexWrap: "wrap" }}>
+ // <Flex sx={{ flex: 3, minWidth: 100 }}>
+ // <Slider
+ // min={8}
+ // max={120}
+ // defaultValue={parseInt(fontSize.replace("px", ""))}
+ // step={1}
+ // onChange={(e) => {
+ // setFontSize(`${parseInt(e.target.value)}px`);
+ // Config.set("fontSize", `${e.target.value}px`);
+ // }}
+ // sx={{ width: "75%" }}
+ // />
+ // <Text
+ // sx={{
+ // width: "25%",
+ // fontSize: "12px",
+ // textAlign: "center"
+ // }}
+ // >
+ // {fontSize}
+ // </Text>
+ // </Flex>
+ // <DropdownButton
+ // options={options}
+ // title="Font Family"
+ // sx={{
+ // flex: 1,
+ // minWidth: 100
+ // }}
+ // buttonStyle={{
+ // width: "80%"
+ // }}
+ // chevronStyle={{
+ // width: "20%"
+ // }}
+ // />
+ // </Flex>
+ // );
}
function getFonts(font: string) {
diff --git a/apps/web/src/components/editor/context.ts b/apps/web/src/components/editor/context.ts
index ef5a44cd9..9a06b0aca 100644
--- a/apps/web/src/components/editor/context.ts
+++ b/apps/web/src/components/editor/context.ts
@@ -24,17 +24,26 @@ import BaseStore from "../../stores";
import { UseBoundStore } from "zustand";
import shallow from "zustand/shallow";
import type { ToolbarDefinition } from "@notesnook/editor";
+import Config from "../../utils/config";
+type EditorConfig = { fontFamily: string; fontSize: number };
type EditorSubState = {
editor?: IEditor;
canUndo?: boolean;
canRedo?: boolean;
searching?: boolean;
toolbarConfig?: ToolbarDefinition;
+ editorConfig: EditorConfig;
statistics?: NoteStatistics;
};
+
class EditorContext extends BaseStore {
- subState: EditorSubState = {};
+ subState: EditorSubState = {
+ editorConfig: Config.get("editorConfig", {
+ fontFamily: "sans-serif",
+ fontSize: 16
+ })
+ };
configure = (
partial:
@@ -116,3 +125,27 @@ export function useNoteStatistics(): NoteStatistics {
}
);
}
+
+export function useEditorConfig() {
+ const editorConfig = useEditorContext((store) => store.subState.editorConfig);
+ const configure = useEditorContext((store) => store.configure);
+ const setEditorConfig = useCallback(
+ (config: Partial<EditorConfig>) => {
+ if (editorConfig)
+ Config.set("editorConfig", {
+ ...editorConfig,
+ ...config
+ });
+
+ configure({
+ editorConfig: {
+ ...editorConfig,
+ ...config
+ }
+ });
+ },
+ [editorConfig, configure]
+ );
+
+ return { editorConfig, setEditorConfig };
+}
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index e9ac4c6ca..15691897e 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -253,8 +253,6 @@ export function Editor(props: EditorProps) {
focusMode: false,
isMobile: false
};
- const fontSize = useStore((store) => store.session.fontSize);
- const fontFamily = useStore((store) => store.session.fontFamily);
const [isLoading, setIsLoading] = useState(true);
const editor = useEditorInstance();
@@ -302,8 +300,6 @@ export function Editor(props: EditorProps) {
return (
<EditorChrome isLoading={isLoading} {...props}>
<Tiptap
- fontSize={fontSize}
- fontFamily={fontFamily}
isMobile={isMobile}
nonce={nonce}
readonly={readonly}
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index 3417274d9..460051744 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -36,7 +36,7 @@ import {
type DownloadOptions,
getTotalWords,
countWords,
- FONTS
+ getFontById
} from "@notesnook/editor";
import { Box, Flex } from "@theme-ui/components";
import { PropsWithChildren, useEffect, useRef, useState } from "react";
@@ -46,7 +46,8 @@ import {
useConfigureEditor,
useSearch,
useToolbarConfig,
- configureEditor
+ configureEditor,
+ useEditorConfig
} from "./context";
import { createPortal } from "react-dom";
import { getCurrentPreset } from "../../common/toolbar-config";
@@ -72,7 +73,7 @@ type TipTapProps = {
theme: Theme;
isMobile?: boolean;
downloadOptions?: DownloadOptions;
- fontSize: string;
+ fontSize: number;
fontFamily: string;
};
@@ -323,13 +324,20 @@ function TipTap(props: TipTapProps) {
);
}
-function TiptapWrapper(props: Omit<TipTapProps, "editorContainer" | "theme">) {
+function TiptapWrapper(
+ props: Omit<
+ TipTapProps,
+ "editorContainer" | "theme" | "fontSize" | "fontFamily"
+ >
+) {
const theme = useTheme() as Theme;
const [isReady, setIsReady] = useState(false);
const editorContainerRef = useRef<HTMLDivElement>(null);
+ const { editorConfig } = useEditorConfig();
useEffect(() => {
setIsReady(true);
}, []);
+
return (
<PortalProvider>
<Flex sx={{ flex: 1, flexDirection: "column" }}>
@@ -338,6 +346,8 @@ function TiptapWrapper(props: Omit<TipTapProps, "editorContainer" | "theme">) {
{...props}
editorContainer={editorContainerRef.current}
theme={theme}
+ fontFamily={editorConfig.fontFamily}
+ fontSize={editorConfig.fontSize}
/>
) : null}
<Box
@@ -348,8 +358,8 @@ function TiptapWrapper(props: Omit<TipTapProps, "editorContainer" | "theme">) {
cursor: "text",
color: theme.colors.text, // TODO!
paddingBottom: 150,
- fontSize: props.fontSize,
- fontFamily: FONTS[props.fontFamily]
+ fontSize: editorConfig.fontSize,
+ fontFamily: getFontById(editorConfig.fontFamily)?.font
}}
/>
</Flex>
diff --git a/apps/web/src/components/editor/title-box.tsx b/apps/web/src/components/editor/title-box.tsx
index 609d7f329..a2a207958 100644
--- a/apps/web/src/components/editor/title-box.tsx
+++ b/apps/web/src/components/editor/title-box.tsx
@@ -23,7 +23,8 @@ import { useStore, store } from "../../stores/editor-store";
import { debounceWithId } from "../../utils/debounce";
import useMobile from "../../hooks/use-mobile";
import useTablet from "../../hooks/use-tablet";
-import { FONTS } from "@notesnook/editor";
+import { useEditorConfig } from "./context";
+import { getFontById } from "@notesnook/editor";
type TitleBoxProps = {
readonly: boolean;
@@ -36,7 +37,7 @@ function TitleBox(props: TitleBoxProps) {
const id = useStore((store) => store.session.id);
const isMobile = useMobile();
const isTablet = useTablet();
- const fontFamily = useStore((store) => store.session.fontFamily);
+ const { editorConfig } = useEditorConfig();
const MAX_FONT_SIZE = useMemo(() => {
return isMobile || isTablet ? 1.625 : 2.625;
@@ -68,7 +69,7 @@ function TitleBox(props: TitleBoxProps) {
readOnly={readonly}
sx={{
p: 0,
- fontFamily: FONTS[fontFamily] || "heading",
+ fontFamily: getFontById(editorConfig.fontFamily)?.font || "heading",
fontSize: ["1.625em", "1.625em", "2.625em"],
fontWeight: "heading",
width: "100%"
diff --git a/apps/web/src/stores/editor-store.js b/apps/web/src/stores/editor-store.js
index 01d4aec6a..2d36cfd7e 100644
--- a/apps/web/src/stores/editor-store.js
+++ b/apps/web/src/stores/editor-store.js
@@ -58,9 +58,7 @@ export const getDefaultSession = (sessionId = Date.now()) => {
color: undefined,
dateEdited: 0,
attachmentsLength: 0,
- isDeleted: false,
- fontFamily: Config.get("fontFamily", "Sans-serif"),
- fontSize: Config.get("fontSize", "16px")
+ isDeleted: false
};
};
@@ -290,18 +288,6 @@ class EditorStore extends BaseStore {
return this._setTag(tag);
};
- setFontSize = (fontSize) => {
- this.set((state) => {
- state.session.fontSize = fontSize;
- });
- };
-
- setFontFamily = (fontFamily) => {
- this.set((state) => {
- state.session.fontFamily = fontFamily;
- });
- };
-
setSaveState = (saveState) => {
this.set((state) => {
state.session.saveState = saveState;
diff --git a/apps/web/src/views/settings.js b/apps/web/src/views/settings.js
index 5e752e96b..2a67db0b9 100644
--- a/apps/web/src/views/settings.js
+++ b/apps/web/src/views/settings.js
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { useEffect, useMemo, useState } from "react";
-import { Button, Flex, Text } from "@theme-ui/components";
+import { Button, Flex, Input, Text } from "@theme-ui/components";
import * as Icon from "../components/icons";
import { useStore as useUserStore } from "../stores/user-store";
import { useStore as useNoteStore } from "../stores/note-store";
@@ -77,12 +77,12 @@ import { clearLogs, downloadLogs } from "../utils/logger";
import { exportNotes } from "../common/export";
import { scheduleBackups } from "../common/reminders";
import usePrivacyMode from "../hooks/use-privacy-mode";
-import { DefaultFont } from "../components/default-font";
import { useTelemetry } from "../hooks/use-telemetry";
import useSpellChecker from "../hooks/use-spell-checker";
import useDesktopIntegration from "../hooks/use-desktop-integration";
import { writeText } from "clipboard-polyfill";
-
+import { useEditorConfig } from "../components/editor/context";
+import { getFonts } from "@notesnook/editor";
function subscriptionStatusToString(user) {
const status = user?.subscription?.type;
@@ -221,6 +221,7 @@ function Settings() {
"corsProxy",
"https://cors.notesnook.com"
);
+ const { editorConfig, setEditorConfig } = useEditorConfig();
useEffect(() => {
(async () => {
@@ -624,12 +625,51 @@ function Settings() {
/>
{groups.editor && (
<>
- <Tip
- text="Customize Default Font"
- tip="Set default font size and family"
- sx={{ py: 2 }}
+ <OptionsItem
+ title={"Default font family"}
+ tip={"Set default font family"}
+ selectedOption={editorConfig.fontFamily}
+ options={getFonts().map((font) => ({
+ value: font.id,
+ title: font.title
+ }))}
+ onSelectionChanged={(option) => {
+ setEditorConfig({ fontFamily: option.value });
+ }}
/>
- <DefaultFont />
+ <Flex
+ py={2}
+ sx={{
+ cursor: "pointer",
+ borderBottom: "1px solid",
+ borderBottomColor: "border",
+ ":hover": { borderBottomColor: "primary" },
+ alignItems: "center"
+ }}
+ >
+ <Tip
+ text="Default font size"
+ tip="Set default font size"
+ sx={{ flex: 1 }}
+ />
+ <Input
+ type="number"
+ min={8}
+ sx={{ width: 70 }}
+ defaultValue={editorConfig.fontSize}
+ onChange={(e) => {
+ setEditorConfig({ fontSize: e.currentTarget.valueAsNumber });
+ }}
+ />
+ </Flex>
+ {/* <Counter
+ value={editorConfig.fontSize}
+ onDecrease={() => {}}
+ onIncrease={() => {}}
+ title="Set default font size"
+ onReset={() => setEditorConfig({ fontFamily: "16px" })}
+ /> */}
+ {/* <DefaultFont /> */}
<Toggle
title="Use double spaced lines"
onTip="New lines will be double spaced (old ones won't be affected)."