Skip to content

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

conform to editor api changes

Ammar Ahmed064364d00cd9
diff --git a/packages/editor-mobile/src/components/editor.tsx b/packages/editor-mobile/src/components/editor.tsx
index c8a49e7c4..d20cafb8a 100644
--- a/packages/editor-mobile/src/components/editor.tsx
+++ b/packages/editor-mobile/src/components/editor.tsx
@@ -1,7 +1,8 @@
import { useTheme } from "@notesnook/theme";
-import { EditorContent } from "@tiptap/react";
import { PortalProvider, Toolbar, useTiptap } from "notesnook-editor";
+import { useLayoutEffect, useRef, useState } from "react";
import { useEditorController } from "../hooks/useEditorController";
+import { useSettings } from "../hooks/useSettings";
import { useEditorThemeStore } from "../state/theme";
import Header from "./header";
import StatusBar from "./statusbar";
@@ -9,7 +10,11 @@ import Tags from "./tags";
import Title from "./title";
const Tiptap = () => {
+ const settings = useSettings();
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: "green",
@@ -24,7 +29,7 @@ const Tiptap = () => {
theme: theme?.night ? "dark" : "light",
});
- editorTheme.space = [0, 6, 12, 20];
+ editorTheme.space = [0, 10, 12, 20];
toolbarTheme.space = [0, 10, 12, 18];
//@ts-ignore
toolbarTheme.space.small = "10px";
@@ -44,7 +49,7 @@ const Tiptap = () => {
toolbarTheme.fontSizes = {
...toolbarTheme.fontSizes,
subBody: "0.8rem",
- body: "0.8rem",
+ body: "0.9rem",
};
toolbarTheme.radii = {
@@ -58,25 +63,35 @@ const Tiptap = () => {
height: "45px",
};
- const editor = useTiptap({
- onUpdate: ({ editor }) => {
- global.editorController.contentChange(editor);
+ const editor = useTiptap(
+ {
+ onUpdate: ({ editor }) => {
+ global.editorController.contentChange(editor);
+ },
+ onSelectionUpdate: (props) => {
+ global.editorController.selectionChange(props.editor);
+ },
+ onOpenAttachmentPicker: (editor, type) => {
+ global.editorController.openFilePicker(type);
+ return true;
+ },
+ onDownloadAttachment: (editor, attachment) => {
+ global.editorController.downloadAttachment(attachment);
+ return true;
+ },
+ theme: editorTheme,
+ element: !layout ? undefined : contentRef.current || undefined,
},
- onSelectionUpdate: (props) => {
- global.editorController.selectionChange(props.editor);
- },
- onOpenAttachmentPicker: (editor, type) => {
- global.editorController.openFilePicker(type);
- return true;
- },
- onDownloadAttachment: (editor, attachment) => {
- global.editorController.downloadAttachment(attachment);
- return true;
- },
- theme: editorTheme,
- });
+ [layout]
+ );
const controller = useEditorController(editor);
+ const controllerRef = useRef(controller);
globalThis.editorController = controller;
+ globalThis.editor = editor;
+
+ useLayoutEffect(() => {
+ setLayout(true);
+ }, []);
return (
<>
@@ -89,9 +104,10 @@ const Tiptap = () => {
marginBottom: "5px",
}}
>
- <Header controller={controller} />
+ <Header />
<div
onScroll={controller.scroll}
+ ref={containerRef}
style={{
overflowY: "scroll",
flexDirection: "column",
@@ -101,16 +117,17 @@ const Tiptap = () => {
display: "flex",
}}
>
- <Tags controller={controller} />
- <Title controller={controller} />
- <StatusBar editor={editor} />
- <EditorContent
+ <Tags />
+ <Title controller={controllerRef} title={controller.title} />
+ <StatusBar container={containerRef} editor={editor} />
+ <div
+ ref={contentRef}
style={{
padding: 12,
paddingTop: 0,
color: theme?.pri,
+ flex: 1,
}}
- editor={editor}
/>
</div>
@@ -125,6 +142,7 @@ const Tiptap = () => {
theme={toolbarTheme}
editor={editor}
location="bottom"
+ tools={settings.tools}
/>
</div>
</div>
diff --git a/packages/editor-mobile/src/components/header.tsx b/packages/editor-mobile/src/components/header.tsx
index c542cf53a..1f4eac4e4 100644
--- a/packages/editor-mobile/src/components/header.tsx
+++ b/packages/editor-mobile/src/components/header.tsx
@@ -3,8 +3,7 @@ import CloudUploadOutlineIcon from "mdi-react/CloudUploadOutlineIcon";
import CrownIcon from "mdi-react/CrownIcon";
import DotsHorizontalIcon from "mdi-react/DotsHorizontalIcon";
import MagnifyIcon from "mdi-react/MagnifyIcon";
-import React from "react";
-import { EditorController } from "../hooks/useEditorController";
+import React, { useEffect } from "react";
import { useSafeArea } from "../hooks/useSafeArea";
import { useSettings } from "../hooks/useSettings";
import { EventTypes } from "../utils";
@@ -42,14 +41,9 @@ const Button = ({
);
};
-export default function Header({
- controller,
-}: {
- controller: EditorController;
-}) {
+export default React.memo(function Header() {
const insets = useSafeArea();
const settings = useSettings();
-
return (
<div
style={{
@@ -177,4 +171,4 @@ export default function Header({
</div>
</div>
);
-}
+});
diff --git a/packages/editor-mobile/src/components/tags.tsx b/packages/editor-mobile/src/components/tags.tsx
index ef099da4d..99c9ccced 100644
--- a/packages/editor-mobile/src/components/tags.tsx
+++ b/packages/editor-mobile/src/components/tags.tsx
@@ -1,9 +1,8 @@
import { useRef, useState } from "react";
-import { EditorController } from "../hooks/useEditorController";
import { EventTypes } from "../utils";
import styles from "./styles.module.css";
-export default function Tags({ controller }: { controller: EditorController }) {
+export default function Tags() {
const [tags, setTags] = useState<{ title: string; alias: string }[]>([]);
const editorTags = useRef({
setTags: setTags,
diff --git a/packages/editor-mobile/src/components/title.tsx b/packages/editor-mobile/src/components/title.tsx
index 52a4d244e..48b06f271 100644
--- a/packages/editor-mobile/src/components/title.tsx
+++ b/packages/editor-mobile/src/components/title.tsx
@@ -1,10 +1,13 @@
-import { useEffect, useRef } from "react";
+import React from "react";
+import { RefObject, useEffect, useRef } from "react";
import { EditorController } from "../hooks/useEditorController";
import styles from "./styles.module.css";
-export default function Title({
+function Title({
controller,
+ title,
}: {
- controller: EditorController;
+ controller: RefObject<EditorController>;
+ title: string;
}) {
const titleRef = useRef<HTMLInputElement>(null);
const emitUpdate = useRef(true);
@@ -13,10 +16,10 @@ export default function Title({
useEffect(() => {
if (titleRef.current) {
emitUpdate.current = false;
- titleRef.current.value = controller.title;
+ titleRef.current.value = title;
emitUpdate.current = true;
}
- }, [controller.title]);
+ }, [title]);
return (
<input
@@ -37,9 +40,14 @@ export default function Title({
}}
onChange={(event) => {
if (!emitUpdate.current) return;
- controller.titleChange(event.target.value);
+ controller.current?.titleChange(event.target.value);
}}
placeholder="Note title"
/>
);
}
+
+export default React.memo(Title, (prev, next) => {
+ if (prev.title !== next.title) return false;
+ return true;
+});
diff --git a/packages/editor-mobile/src/hooks/useEditorController.ts b/packages/editor-mobile/src/hooks/useEditorController.ts
index a4c884dd1..46549c086 100644
--- a/packages/editor-mobile/src/hooks/useEditorController.ts
+++ b/packages/editor-mobile/src/hooks/useEditorController.ts
@@ -25,7 +25,7 @@ export type EditorController = {
scroll: (event: React.UIEvent<HTMLDivElement, UIEvent>) => void;
title: string;
setTitle: React.Dispatch<React.SetStateAction<string>>;
- openFilePicker: (type: "image" | "file") => void;
+ openFilePicker: (type: "image" | "file" | "camera") => void;
downloadAttachment: (attachment: Attachment) => void;
};
@@ -93,7 +93,7 @@ export function useEditorController(editor: Editor | null): EditorController {
const scroll = useCallback(
(event: React.UIEvent<HTMLDivElement, UIEvent>) => {
//@ts-ignore
- post(EventTypes.scroll, event.target.scrollTop);
+ //post(EventTypes.scroll, event.target.scrollTop);
},
[]
);
diff --git a/packages/editor-mobile/src/hooks/useSafeArea.ts b/packages/editor-mobile/src/hooks/useSafeArea.ts
index 78952fd40..01feca5e5 100644
--- a/packages/editor-mobile/src/hooks/useSafeArea.ts
+++ b/packages/editor-mobile/src/hooks/useSafeArea.ts
@@ -1,16 +1,20 @@
import { useState } from "react";
-const initialState = {
- top: 0,
- bottom: 0,
- left: 0,
- right: 0,
-};
+const insetsStorage = localStorage.getItem("safeAreaInsets");
+const initialState = insetsStorage
+ ? JSON.parse(insetsStorage)
+ : {
+ top: 0,
+ bottom: 0,
+ left: 0,
+ right: 0,
+ };
global.safeAreaController = {
update: (safeArea) => {
if (safeAreaController.set) safeAreaController.set(safeArea);
safeAreaController.previous = safeArea;
+ localStorage.setItem("safeAreaInsets", JSON.stringify(safeArea));
},
reset: () => {
if (safeAreaController.set) safeAreaController.set(initialState);
diff --git a/packages/editor-mobile/src/hooks/useSettings.ts b/packages/editor-mobile/src/hooks/useSettings.ts
index 8f2a9f042..6adee853a 100644
--- a/packages/editor-mobile/src/hooks/useSettings.ts
+++ b/packages/editor-mobile/src/hooks/useSettings.ts
@@ -7,6 +7,36 @@ const initialState = {
fullscreen: false,
deviceMode: "mobile",
premium: false,
+ tools: [
+ [
+ "insertBlock",
+ "tableSettings",
+ "imageSettings",
+ "embedSettings",
+ "attachmentSettings",
+ "linkSettings",
+ "codeRemove",
+ ],
+ [
+ "bold",
+ "italic",
+ "underline",
+ [
+ "strikethrough",
+ "code",
+ "subscript",
+ "superscript",
+ "highlight",
+ "textColor",
+ ],
+ ],
+ ["fontSize"],
+ ["headings", "fontFamily"],
+ ["numberedList", "bulletList"],
+ ["addLink"],
+ ["alignCenter", ["alignLeft", "alignRight", "alignJustify", "ltr", "rtl"]],
+ ["clearformatting"],
+ ],
};
global.settingsController = {
diff --git a/packages/editor-mobile/src/utils/index.ts b/packages/editor-mobile/src/utils/index.ts
index b7aeb7478..55ffb2adf 100644
--- a/packages/editor-mobile/src/utils/index.ts
+++ b/packages/editor-mobile/src/utils/index.ts
@@ -1,4 +1,5 @@
-import { Editor } from "@tiptap/react";
+import { ToolbarGroupDefinition } from "notesnook-editor/dist/toolbar/types";
+import { Editor } from "notesnook-editor/dist/types";
import { Dispatch, MutableRefObject, RefObject, SetStateAction } from "react";
import { useEditorController } from "../hooks/useEditorController";
@@ -14,6 +15,7 @@ export type Settings = {
fullscreen: boolean;
deviceMode: "mobile" | "smallTablet" | "tablet";
premium: boolean;
+ tools: ToolbarGroupDefinition[];
};
declare global {