fix: load new content through state

This commit is contained in:
ammarahm-ed
2022-07-07 18:40:21 +05:00
parent a4d8bbf378
commit cbd400932b
3 changed files with 49 additions and 28 deletions

View File

@@ -34153,7 +34153,7 @@
}, },
"notesnook-editor": { "notesnook-editor": {
"version": "git+ssh://git@github.com/streetwriters/notesnook-editor.git#97094dba0bfdaaede6ab24e65079d6b305fe471c", "version": "git+ssh://git@github.com/streetwriters/notesnook-editor.git#97094dba0bfdaaede6ab24e65079d6b305fe471c",
"from": "notesnook-editor@https://github.com/streetwriters/notesnook-editor.git#develop", "from": "notesnook-editor@github:streetwriters/notesnook-editor#develop",
"requires": { "requires": {
"@_ueberdosis/prosemirror-tables": "^1.1.3", "@_ueberdosis/prosemirror-tables": "^1.1.3",
"@mdi/js": "^6.9.96", "@mdi/js": "^6.9.96",

View File

@@ -4,9 +4,15 @@ import {
PortalProvider, PortalProvider,
Toolbar, Toolbar,
usePermissionHandler, usePermissionHandler,
useTiptap, useTiptap
} from "notesnook-editor"; } from "notesnook-editor";
import { useEffect, useLayoutEffect, useRef, useState } from "react"; import {
useEffect,
useLayoutEffect,
useReducer,
useRef,
useState
} from "react";
import { useEditorController } from "../hooks/useEditorController"; import { useEditorController } from "../hooks/useEditorController";
import { useSettings } from "../hooks/useSettings"; import { useSettings } from "../hooks/useSettings";
import { useEditorThemeStore } from "../state/theme"; import { useEditorThemeStore } from "../state/theme";
@@ -18,6 +24,7 @@ import Title from "./title";
const Tiptap = () => { const Tiptap = () => {
const settings = useSettings(); const settings = useSettings();
const [tick, setTick] = useState(0);
const theme = useEditorThemeStore((state) => state.colors); const theme = useEditorThemeStore((state) => state.colors);
const [initialProps, setInitialProps] = useState<Partial<Settings>>({ const [initialProps, setInitialProps] = useState<Partial<Settings>>({
readonly: global.readonly || settings.readonly, readonly: global.readonly || settings.readonly,
@@ -26,7 +33,7 @@ const Tiptap = () => {
global.noToolbar || global.noToolbar ||
settings.noToolbar || settings.noToolbar ||
global.readonly || global.readonly ||
settings.readonly, settings.readonly
}); });
const contentRef = useRef<HTMLDivElement>(null); const contentRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
@@ -35,14 +42,14 @@ const Tiptap = () => {
//todo //todo
accent: "green", accent: "green",
scale: 1, scale: 1,
theme: theme?.night ? "dark" : "light", theme: theme?.night ? "dark" : "light"
}); });
const editorTheme = useTheme({ const editorTheme = useTheme({
//todo //todo
accent: "green", accent: "green",
scale: 1, scale: 1,
theme: theme?.night ? "dark" : "light", theme: theme?.night ? "dark" : "light"
}); });
editorTheme.space = [0, 10, 12, 20]; editorTheme.space = [0, 10, 12, 20];
@@ -54,37 +61,37 @@ const Tiptap = () => {
...toolbarTheme.buttons.menuitem, ...toolbarTheme.buttons.menuitem,
height: "50px", height: "50px",
paddingX: "20px", paddingX: "20px",
borderBottomWidth: 0, borderBottomWidth: 0
}; };
toolbarTheme.iconSizes = { toolbarTheme.iconSizes = {
big: 20, big: 20,
medium: 18, medium: 18,
small: 18, small: 18
}; };
toolbarTheme.fontSizes = { toolbarTheme.fontSizes = {
...toolbarTheme.fontSizes, ...toolbarTheme.fontSizes,
subBody: "0.8rem", subBody: "0.8rem",
body: "0.9rem", body: "0.9rem"
}; };
toolbarTheme.radii = { toolbarTheme.radii = {
...toolbarTheme.radii, ...toolbarTheme.radii,
small: 5, small: 5
}; };
toolbarTheme.buttons.menuitem = { toolbarTheme.buttons.menuitem = {
...toolbarTheme.buttons.menuitem, ...toolbarTheme.buttons.menuitem,
px: 5, px: 5,
height: "45px", height: "45px"
}; };
usePermissionHandler({ usePermissionHandler({
claims: { claims: {
premium: settings.premium, premium: settings.premium
}, },
onPermissionDenied: () => { onPermissionDenied: () => {
post(EventTypes.pro); post(EventTypes.pro);
}, }
}); });
const editor = useTiptap( const editor = useTiptap(
{ {
@@ -106,14 +113,14 @@ const Tiptap = () => {
element: !layout ? undefined : contentRef.current || undefined, element: !layout ? undefined : contentRef.current || undefined,
editable: !initialProps.readonly, editable: !initialProps.readonly,
editorProps: { editorProps: {
editable: () => !initialProps.readonly, editable: () => !initialProps.readonly
}, },
content: global.editorController?.content?.current, content: global.editorController?.content?.current,
isMobile: true, isMobile: true
}, },
[layout, initialProps.readonly] [layout, initialProps.readonly, tick]
); );
const controller = useEditorController(editor); const controller = useEditorController(editor, setTick);
const controllerRef = useRef(controller); const controllerRef = useRef(controller);
globalThis.editorController = controller; globalThis.editorController = controller;
globalThis.editor = editor; globalThis.editor = editor;
@@ -134,10 +141,15 @@ const Tiptap = () => {
flex: 1, flex: 1,
flexDirection: "column", flexDirection: "column",
maxWidth: "100vw", maxWidth: "100vw",
marginBottom: "5px", marginBottom: "5px"
}} }}
> >
<Header settings={settings} noHeader={initialProps.noHeader || false} /> <Header
hasRedo={false}
hasUndo={false}
settings={settings}
noHeader={initialProps.noHeader || false}
/>
<div <div
onScroll={controller.scroll} onScroll={controller.scroll}
ref={containerRef} ref={containerRef}
@@ -147,7 +159,7 @@ const Tiptap = () => {
height: "100%", height: "100%",
flexGrow: 1, flexGrow: 1,
flexShrink: 1, flexShrink: 1,
display: "flex", display: "flex"
}} }}
> >
{initialProps.noHeader ? null : ( {initialProps.noHeader ? null : (
@@ -167,7 +179,7 @@ const Tiptap = () => {
padding: 12, padding: 12,
paddingTop: 0, paddingTop: 0,
color: theme?.pri, color: theme?.pri,
flex: 1, flex: 1
}} }}
/> />
</div> </div>

View File

@@ -1,10 +1,12 @@
import { Editor } from "notesnook-editor"; import { Editor } from "notesnook-editor";
import { import {
Dispatch,
MutableRefObject, MutableRefObject,
SetStateAction,
useCallback, useCallback,
useEffect, useEffect,
useRef, useRef,
useState, useState
} from "react"; } from "react";
import { useEditorThemeStore } from "../state/theme"; import { useEditorThemeStore } from "../state/theme";
import { EventTypes, isReactNative, post, timerFn } from "../utils"; import { EventTypes, isReactNative, post, timerFn } from "../utils";
@@ -40,14 +42,18 @@ export type EditorController = {
openFilePicker: (type: "image" | "file" | "camera") => void; openFilePicker: (type: "image" | "file" | "camera") => void;
downloadAttachment: (attachment: Attachment) => void; downloadAttachment: (attachment: Attachment) => void;
content: MutableRefObject<string | null>; content: MutableRefObject<string | null>;
onUpdate: () => void;
}; };
export function useEditorController(editor: Editor | null): EditorController { export function useEditorController(
editor: Editor | null,
update: Dispatch<SetStateAction<number>>
): EditorController {
const [title, setTitle] = useState(""); const [title, setTitle] = useState("");
const htmlContentRef = useRef<string | null>(null); const htmlContentRef = useRef<string | null>(null);
const timers = useRef<Timers>({ const timers = useRef<Timers>({
selectionChange: null, selectionChange: null,
change: null, change: null
}); });
const selectionChange = useCallback((editor: Editor) => { const selectionChange = useCallback((editor: Editor) => {
@@ -112,6 +118,10 @@ export function useEditorController(editor: Editor | null): EditorController {
[] []
); );
const onUpdate = () => {
update((tick) => tick + 1);
};
const onMessage = useCallback( const onMessage = useCallback(
(data: Event) => { (data: Event) => {
//@ts-ignore //@ts-ignore
@@ -122,9 +132,7 @@ export function useEditorController(editor: Editor | null): EditorController {
switch (type) { switch (type) {
case "native:html": case "native:html":
htmlContentRef.current = value; htmlContentRef.current = value;
editor?.commands.setContent(htmlContentRef.current, false, { update((tick) => tick + 1);
preserveWhitespace: true,
});
break; break;
case "native:theme": case "native:theme":
useEditorThemeStore.getState().setColors(message.value); useEditorThemeStore.getState().setColors(message.value);
@@ -141,7 +149,7 @@ export function useEditorController(editor: Editor | null): EditorController {
} }
post(type); // Notify that message was delivered successfully. post(type); // Notify that message was delivered successfully.
}, },
[editor] [update]
); );
// useEffect(() => { // useEffect(() => {
@@ -186,5 +194,6 @@ export function useEditorController(editor: Editor | null): EditorController {
openFilePicker, openFilePicker,
downloadAttachment, downloadAttachment,
content: htmlContentRef, content: htmlContentRef,
onUpdate: onUpdate
}; };
} }