Skip to content

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

fix editor performance

ammarahm-ed62092732b23e
diff --git a/packages/editor-mobile/src/components/editor.tsx b/packages/editor-mobile/src/components/editor.tsx
index 010282836..a309e5edb 100644
--- a/packages/editor-mobile/src/components/editor.tsx
+++ b/packages/editor-mobile/src/components/editor.tsx
@@ -5,73 +5,37 @@ import {
usePermissionHandler,
useTiptap,
} from "@streetwriters/editor";
-import { useTheme } from "@streetwriters/theme";
-import { useCallback, useLayoutEffect, useRef, useState } from "react";
+import { Theme, useTheme } from "@streetwriters/theme";
+import {
+ forwardRef,
+ memo,
+ useCallback,
+ useLayoutEffect,
+ useRef,
+ useState,
+} from "react";
import { useEditorController } from "../hooks/useEditorController";
import { useSettings } from "../hooks/useSettings";
import { useEditorThemeStore } from "../state/theme";
-import { EventTypes } from "../utils";
+import { EventTypes, Settings } from "../utils";
import Header from "./header";
import StatusBar from "./statusbar";
import Tags from "./tags";
import Title from "./title";
-const Tiptap = () => {
- const settings = useSettings();
+const Tiptap = ({
+ editorTheme,
+ toolbarTheme,
+ settings,
+}: {
+ editorTheme: Theme;
+ toolbarTheme: Theme;
+ settings: Settings;
+}) => {
const [tick, setTick] = useState(0);
- const theme = useEditorThemeStore((state) => state.colors);
const contentRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
const [layout, setLayout] = useState(false);
- const toolbarTheme = useTheme({
- //todo
- accent: theme?.accent,
- scale: 1,
- theme: theme?.night ? "dark" : "light",
- });
-
- const editorTheme = useTheme({
- //todo
- accent: theme?.accent,
- scale: 1,
- theme: theme?.night ? "dark" : "light",
- });
-
- editorTheme.colors.background = theme?.bg || "#f0f0f0";
-
- editorTheme.space = [0, 10, 12, 20];
- toolbarTheme.space = [0, 10, 12, 18];
- //@ts-ignore
- toolbarTheme.space.small = "10px";
-
- toolbarTheme.buttons.menuitem = {
- ...toolbarTheme.buttons.menuitem,
- height: "50px",
- paddingX: "20px",
- borderBottomWidth: 0,
- };
-
- toolbarTheme.iconSizes = {
- big: 20,
- medium: 18,
- small: 18,
- };
- toolbarTheme.fontSizes = {
- ...toolbarTheme.fontSizes,
- subBody: "0.8rem",
- body: "0.9rem",
- };
-
- toolbarTheme.radii = {
- ...toolbarTheme.radii,
- small: 5,
- };
-
- toolbarTheme.buttons.menuitem = {
- ...toolbarTheme.buttons.menuitem,
- px: 5,
- height: "45px",
- };
usePermissionHandler({
claims: {
premium: settings.premium,
@@ -161,18 +125,12 @@ const Tiptap = () => {
controller={controllerRef}
title={controller.title}
/>
- <StatusBar container={containerRef} editor={_editor} />
+ <StatusBar container={containerRef} />
</>
)}
- <div
- ref={contentRef}
- style={{
- padding: 12,
- paddingTop: 0,
- color: theme?.pri,
- flex: 1,
- }}
- />
+
+ <ContentDiv ref={contentRef} />
+
<div
onDoubleClick={() => {
const lastPosition = globalThis.editor?.state.doc.content.size;
@@ -203,10 +161,86 @@ const Tiptap = () => {
);
};
+const ContentDiv = memo(
+ forwardRef<HTMLDivElement>((props, ref) => {
+ const theme = useEditorThemeStore((state) => state.colors);
+ return (
+ <div
+ ref={ref}
+ style={{
+ padding: 12,
+ paddingTop: 0,
+ flex: 1,
+ color: theme.pri,
+ marginTop: -12,
+ }}
+ />
+ );
+ }),
+ () => true
+);
+
+const modifyToolbarTheme = (toolbarTheme: Theme) => {
+ toolbarTheme.space = [0, 10, 12, 18];
+ //@ts-ignore
+ toolbarTheme.space.small = "10px";
+
+ toolbarTheme.buttons.menuitem = {
+ ...toolbarTheme.buttons.menuitem,
+ height: "50px",
+ paddingX: "20px",
+ borderBottomWidth: 0,
+ };
+
+ toolbarTheme.iconSizes = {
+ big: 20,
+ medium: 18,
+ small: 18,
+ };
+ toolbarTheme.fontSizes = {
+ ...toolbarTheme.fontSizes,
+ subBody: "0.8rem",
+ body: "0.9rem",
+ };
+
+ toolbarTheme.radii = {
+ ...toolbarTheme.radii,
+ small: 5,
+ };
+
+ toolbarTheme.buttons.menuitem = {
+ ...toolbarTheme.buttons.menuitem,
+ px: 5,
+ height: "45px",
+ };
+};
+
const TiptapProvider = () => {
+ const settings = useSettings();
+ const theme = useEditorThemeStore((state) => state.colors);
+ const toolbarTheme = useTheme({
+ //todo
+ accent: theme?.accent,
+ scale: 1,
+ theme: theme?.night ? "dark" : "light",
+ });
+ modifyToolbarTheme(toolbarTheme);
+ const editorTheme = useTheme({
+ //todo
+ accent: theme?.accent,
+ scale: 1,
+ theme: theme?.night ? "dark" : "light",
+ });
+ editorTheme.colors.background = theme?.bg || "#f0f0f0";
+ editorTheme.space = [0, 10, 12, 20];
+
return (
<PortalProvider>
- <Tiptap />
+ <Tiptap
+ editorTheme={editorTheme}
+ toolbarTheme={toolbarTheme}
+ settings={settings}
+ />
</PortalProvider>
);
};
diff --git a/packages/editor-mobile/src/components/statusbar.tsx b/packages/editor-mobile/src/components/statusbar.tsx
index 326086bb1..8a97c6369 100644
--- a/packages/editor-mobile/src/components/statusbar.tsx
+++ b/packages/editor-mobile/src/components/statusbar.tsx
@@ -1,13 +1,6 @@
-import { Editor } from "@streetwriters/editor";
import React, { RefObject, useEffect, useRef, useState } from "react";
-export default function StatusBar({
- editor,
- container,
-}: {
- editor: Editor | null;
- container: RefObject<HTMLDivElement>;
-}) {
+function StatusBar({ container }: { container: RefObject<HTMLDivElement> }) {
const [status, setStatus] = useState({
date: "",
saved: "",
@@ -16,7 +9,9 @@ export default function StatusBar({
const stickyRef = useRef(false);
const prevScroll = useRef(0);
const lastStickyChangeTime = useRef(0);
- const words = editor?.storage?.characterCount?.words() + " words";
+ const [words, setWords] = useState("0 words");
+ const currentWords = useRef(words);
+ const interval = useRef(0);
const statusBar = useRef({
set: setStatus,
});
@@ -47,6 +42,23 @@ export default function StatusBar({
prevScroll.current = currentOffset;
}, []);
+ useEffect(() => {
+ currentWords.current = words;
+ }, [words]);
+
+ useEffect(() => {
+ clearInterval(interval.current);
+ //@ts-ignore
+ interval.current = setInterval(() => {
+ const words = editor?.storage?.characterCount?.words() + " words";
+ if (currentWords.current === words) return;
+ setWords(words);
+ }, 3000);
+ return () => {
+ clearInterval(interval.current);
+ };
+ }, []);
+
useEffect(() => {
const node = container.current;
node?.addEventListener("scroll", onScroll);
@@ -61,6 +73,7 @@ export default function StatusBar({
fontSize: "12px",
color: "var(--nn_icon)",
marginRight: 8,
+ paddingBottom: 0,
};
return (
@@ -68,16 +81,15 @@ export default function StatusBar({
style={{
flexDirection: "row",
display: "flex",
- height: sticky ? 30 : 15,
paddingRight: 12,
paddingLeft: 12,
position: sticky ? "sticky" : "relative",
- top: -2,
+ top: -3,
backgroundColor: "var(--nn_bg)",
zIndex: 1,
- paddingTop: 5,
- paddingBottom: 3,
justifyContent: sticky ? "center" : "flex-start",
+ paddingTop: 2,
+ paddingBottom: 2,
}}
>
<p style={paragraphStyle}>{words}</p>
@@ -86,3 +98,5 @@ export default function StatusBar({
</div>
);
}
+
+export default React.memo(StatusBar, () => true);
diff --git a/packages/editor-mobile/src/components/title.tsx b/packages/editor-mobile/src/components/title.tsx
index 91b9ea7fa..a278b0224 100644
--- a/packages/editor-mobile/src/components/title.tsx
+++ b/packages/editor-mobile/src/components/title.tsx
@@ -33,7 +33,7 @@ function Title({
disabled={readonly}
style={{
height: 50,
- fontSize: 27,
+ fontSize: 25,
width: "100%",
boxSizing: "border-box",
borderWidth: 0,
diff --git a/packages/editor-mobile/src/state/theme.tsx b/packages/editor-mobile/src/state/theme.tsx
index d199f6969..d7a17b690 100644
--- a/packages/editor-mobile/src/state/theme.tsx
+++ b/packages/editor-mobile/src/state/theme.tsx
@@ -1,6 +1,6 @@
import create, { State } from "zustand";
import { injectCss, transform } from "../utils/css";
-type Colors = {
+export type Colors = {
night: boolean;
bg: string;
navbg: string;