diff --git a/packages/editor/dist/es/hooks/useEditor.d.ts b/packages/editor/dist/es/hooks/useEditor.d.ts index 9c4401648..407df0f3f 100644 --- a/packages/editor/dist/es/hooks/useEditor.d.ts +++ b/packages/editor/dist/es/hooks/useEditor.d.ts @@ -1,4 +1,4 @@ import { EditorOptions } from "@tiptap/core"; import { DependencyList } from "react"; import { Editor as EditorType } from "../types"; -export declare const useEditor: (options?: Partial, deps?: DependencyList) => EditorType; +export declare const useEditor: (options?: Partial, deps?: DependencyList) => EditorType | null; diff --git a/packages/editor/dist/es/hooks/useEditor.js b/packages/editor/dist/es/hooks/useEditor.js index 6194357e3..a716e87f5 100644 --- a/packages/editor/dist/es/hooks/useEditor.js +++ b/packages/editor/dist/es/hooks/useEditor.js @@ -1,5 +1,5 @@ import { Editor } from "@tiptap/core"; -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; function useForceUpdate() { const [, setValue] = useState(0); return () => setValue((value) => value + 1); @@ -7,6 +7,7 @@ function useForceUpdate() { export const useEditor = (options = {}, deps = []) => { const [editor, setEditor] = useState(null); const forceUpdate = useForceUpdate(); + const editorRef = useRef(editor); useEffect(() => { let isMounted = true; const instance = new Editor(options); @@ -25,5 +26,26 @@ export const useEditor = (options = {}, deps = []) => { isMounted = false; }; }, deps); + useEffect(() => { + editorRef.current = editor; + if (editor && !editor.current) + Object.defineProperty(editor, "current", { + get: () => editorRef.current, + }); + }, [editor]); + useEffect(() => { + // this is required for the drag/drop to work properly + // in the editor. + function onDragEnter(event) { + if (!!(editor === null || editor === void 0 ? void 0 : editor.view.dragging)) { + event.preventDefault(); + return true; + } + } + editor === null || editor === void 0 ? void 0 : editor.view.dom.addEventListener("dragenter", onDragEnter); + return () => { + editor === null || editor === void 0 ? void 0 : editor.view.dom.removeEventListener("dragenter", onDragEnter); + }; + }, [editor === null || editor === void 0 ? void 0 : editor.view.dom]); return editor; }; diff --git a/packages/editor/dist/es/index.d.ts b/packages/editor/dist/es/index.d.ts index cd47cb2da..0be27387d 100644 --- a/packages/editor/dist/es/index.d.ts +++ b/packages/editor/dist/es/index.d.ts @@ -1,4 +1,3 @@ -/// import "./extensions"; import Toolbar from "./toolbar"; import { Theme } from "@notesnook/theme"; @@ -6,7 +5,7 @@ import { AttachmentOptions } from "./extensions/attachment"; import { EditorOptions } from "@tiptap/core"; declare const useTiptap: (options?: Partial, deps?: import("react").DependencyList) => import("./types").Editor; +}>, deps?: import("react").DependencyList) => import("./types").Editor | null; export { useTiptap, Toolbar }; export * from "./types"; export * from "./extensions/react"; diff --git a/packages/editor/dist/es/index.js b/packages/editor/dist/es/index.js index b29e28b64..e810f1c2f 100644 --- a/packages/editor/dist/es/index.js +++ b/packages/editor/dist/es/index.js @@ -14,7 +14,7 @@ import CharacterCount from "@tiptap/extension-character-count"; import Placeholder from "@tiptap/extension-placeholder"; import Underline from "@tiptap/extension-underline"; import StarterKit from "@tiptap/starter-kit"; -import { useEffect, useMemo, useRef } from "react"; +import { useMemo } from "react"; import { EditorView } from "prosemirror-view"; import Toolbar from "./toolbar"; import TextAlign from "@tiptap/extension-text-align"; @@ -138,26 +138,6 @@ const useTiptap = (options = {}, deps = []) => { isMobile, ]); const editor = useEditor(Object.assign(Object.assign({}, defaultOptions), restOptions), deps); - const editorRef = useRef(editor); - useEffect(() => { - editorRef.current = editor; - if (editor && !editor.current) - Object.defineProperty(editor, "current", { - get: () => editorRef.current, - }); - }, [editor]); - useEffect(() => { - function onDragEnter(event) { - if (!!(editor === null || editor === void 0 ? void 0 : editor.view.dragging)) { - event.preventDefault(); - return true; - } - } - editor === null || editor === void 0 ? void 0 : editor.view.dom.addEventListener("dragenter", onDragEnter); - return () => { - editor === null || editor === void 0 ? void 0 : editor.view.dom.removeEventListener("dragenter", onDragEnter); - }; - }, [editor === null || editor === void 0 ? void 0 : editor.view.dom]); return editor; }; export { useTiptap, Toolbar }; diff --git a/packages/editor/src/hooks/use-editor.ts b/packages/editor/src/hooks/use-editor.ts index 6c707b6e4..e5f706dd1 100644 --- a/packages/editor/src/hooks/use-editor.ts +++ b/packages/editor/src/hooks/use-editor.ts @@ -1,5 +1,5 @@ import { EditorOptions, Editor } from "@tiptap/core"; -import { DependencyList, useEffect, useState } from "react"; +import { DependencyList, useEffect, useRef, useState } from "react"; import { Editor as EditorType } from "../types"; function useForceUpdate() { @@ -12,8 +12,9 @@ export const useEditor = ( options: Partial = {}, deps: DependencyList = [] ) => { - const [editor, setEditor] = useState(null); + const [editor, setEditor] = useState(null); const forceUpdate = useForceUpdate(); + const editorRef = useRef(editor); useEffect(() => { let isMounted = true; @@ -38,5 +39,30 @@ export const useEditor = ( }; }, deps); - return editor as EditorType; + useEffect(() => { + editorRef.current = editor; + + if (editor && !editor.current) + Object.defineProperty(editor, "current", { + get: () => editorRef.current, + }); + }, [editor]); + + useEffect(() => { + // this is required for the drag/drop to work properly + // in the editor. + function onDragEnter(event: DragEvent) { + if (!!editor?.view.dragging) { + event.preventDefault(); + return true; + } + } + + editor?.view.dom.addEventListener("dragenter", onDragEnter); + return () => { + editor?.view.dom.removeEventListener("dragenter", onDragEnter); + }; + }, [editor?.view.dom]); + + return editor; }; diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts index 382d00bab..dc12da397 100644 --- a/packages/editor/src/index.ts +++ b/packages/editor/src/index.ts @@ -154,30 +154,6 @@ const useTiptap = ( deps ); - const editorRef = useRef(editor); - useEffect(() => { - editorRef.current = editor; - - if (editor && !editor.current) - Object.defineProperty(editor, "current", { - get: () => editorRef.current, - }); - }, [editor]); - - useEffect(() => { - function onDragEnter(event: DragEvent) { - if (!!editor?.view.dragging) { - event.preventDefault(); - return true; - } - } - - editor?.view.dom.addEventListener("dragenter", onDragEnter); - return () => { - editor?.view.dom.removeEventListener("dragenter", onDragEnter); - }; - }, [editor?.view.dom]); - return editor; }; diff --git a/packages/editor/src/toolbar/stores/toolbar-store.ts b/packages/editor/src/toolbar/stores/toolbar-store.ts index 1328db36e..af875a646 100644 --- a/packages/editor/src/toolbar/stores/toolbar-store.ts +++ b/packages/editor/src/toolbar/stores/toolbar-store.ts @@ -1,4 +1,3 @@ -import { Editor } from "@tiptap/core"; import create from "zustand"; export type ToolbarLocation = "top" | "bottom";