refactor: cleanup useTiptap hook

This commit is contained in:
thecodrr
2022-07-02 11:57:52 +05:00
parent 893b8854dc
commit 939ece46f1
7 changed files with 55 additions and 53 deletions

View File

@@ -1,4 +1,4 @@
import { EditorOptions } from "@tiptap/core"; import { EditorOptions } from "@tiptap/core";
import { DependencyList } from "react"; import { DependencyList } from "react";
import { Editor as EditorType } from "../types"; import { Editor as EditorType } from "../types";
export declare const useEditor: (options?: Partial<EditorOptions>, deps?: DependencyList) => EditorType; export declare const useEditor: (options?: Partial<EditorOptions>, deps?: DependencyList) => EditorType | null;

View File

@@ -1,5 +1,5 @@
import { Editor } from "@tiptap/core"; import { Editor } from "@tiptap/core";
import { useEffect, useState } from "react"; import { useEffect, useRef, useState } from "react";
function useForceUpdate() { function useForceUpdate() {
const [, setValue] = useState(0); const [, setValue] = useState(0);
return () => setValue((value) => value + 1); return () => setValue((value) => value + 1);
@@ -7,6 +7,7 @@ function useForceUpdate() {
export const useEditor = (options = {}, deps = []) => { export const useEditor = (options = {}, deps = []) => {
const [editor, setEditor] = useState(null); const [editor, setEditor] = useState(null);
const forceUpdate = useForceUpdate(); const forceUpdate = useForceUpdate();
const editorRef = useRef(editor);
useEffect(() => { useEffect(() => {
let isMounted = true; let isMounted = true;
const instance = new Editor(options); const instance = new Editor(options);
@@ -25,5 +26,26 @@ export const useEditor = (options = {}, deps = []) => {
isMounted = false; isMounted = false;
}; };
}, deps); }, 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; return editor;
}; };

View File

@@ -1,4 +1,3 @@
/// <reference types="react" />
import "./extensions"; import "./extensions";
import Toolbar from "./toolbar"; import Toolbar from "./toolbar";
import { Theme } from "@notesnook/theme"; import { Theme } from "@notesnook/theme";
@@ -6,7 +5,7 @@ import { AttachmentOptions } from "./extensions/attachment";
import { EditorOptions } from "@tiptap/core"; import { EditorOptions } from "@tiptap/core";
declare const useTiptap: (options?: Partial<EditorOptions & AttachmentOptions & { declare const useTiptap: (options?: Partial<EditorOptions & AttachmentOptions & {
theme: Theme; theme: Theme;
}>, deps?: import("react").DependencyList) => import("./types").Editor; }>, deps?: import("react").DependencyList) => import("./types").Editor | null;
export { useTiptap, Toolbar }; export { useTiptap, Toolbar };
export * from "./types"; export * from "./types";
export * from "./extensions/react"; export * from "./extensions/react";

View File

@@ -14,7 +14,7 @@ import CharacterCount from "@tiptap/extension-character-count";
import Placeholder from "@tiptap/extension-placeholder"; import Placeholder from "@tiptap/extension-placeholder";
import Underline from "@tiptap/extension-underline"; import Underline from "@tiptap/extension-underline";
import StarterKit from "@tiptap/starter-kit"; import StarterKit from "@tiptap/starter-kit";
import { useEffect, useMemo, useRef } from "react"; import { useMemo } from "react";
import { EditorView } from "prosemirror-view"; import { EditorView } from "prosemirror-view";
import Toolbar from "./toolbar"; import Toolbar from "./toolbar";
import TextAlign from "@tiptap/extension-text-align"; import TextAlign from "@tiptap/extension-text-align";
@@ -138,26 +138,6 @@ const useTiptap = (options = {}, deps = []) => {
isMobile, isMobile,
]); ]);
const editor = useEditor(Object.assign(Object.assign({}, defaultOptions), restOptions), deps); 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; return editor;
}; };
export { useTiptap, Toolbar }; export { useTiptap, Toolbar };

View File

@@ -1,5 +1,5 @@
import { EditorOptions, Editor } from "@tiptap/core"; 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"; import { Editor as EditorType } from "../types";
function useForceUpdate() { function useForceUpdate() {
@@ -12,8 +12,9 @@ export const useEditor = (
options: Partial<EditorOptions> = {}, options: Partial<EditorOptions> = {},
deps: DependencyList = [] deps: DependencyList = []
) => { ) => {
const [editor, setEditor] = useState<Editor | null>(null); const [editor, setEditor] = useState<EditorType | null>(null);
const forceUpdate = useForceUpdate(); const forceUpdate = useForceUpdate();
const editorRef = useRef<EditorType | null>(editor);
useEffect(() => { useEffect(() => {
let isMounted = true; let isMounted = true;
@@ -38,5 +39,30 @@ export const useEditor = (
}; };
}, deps); }, 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;
}; };

View File

@@ -154,30 +154,6 @@ const useTiptap = (
deps 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; return editor;
}; };

View File

@@ -1,4 +1,3 @@
import { Editor } from "@tiptap/core";
import create from "zustand"; import create from "zustand";
export type ToolbarLocation = "top" | "bottom"; export type ToolbarLocation = "top" | "bottom";