mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
43 lines
934 B
TypeScript
43 lines
934 B
TypeScript
|
|
import { EditorOptions, Editor } from "@tiptap/core";
|
||
|
|
import { DependencyList, useEffect, useState } from "react";
|
||
|
|
import { Editor as EditorType } from "../types";
|
||
|
|
|
||
|
|
function useForceUpdate() {
|
||
|
|
const [, setValue] = useState(0);
|
||
|
|
|
||
|
|
return () => setValue((value) => value + 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useEditor = (
|
||
|
|
options: Partial<EditorOptions> = {},
|
||
|
|
deps: DependencyList = []
|
||
|
|
) => {
|
||
|
|
const [editor, setEditor] = useState<Editor | null>(null);
|
||
|
|
const forceUpdate = useForceUpdate();
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
let isMounted = true;
|
||
|
|
|
||
|
|
const instance = new Editor(options);
|
||
|
|
|
||
|
|
setEditor(instance);
|
||
|
|
|
||
|
|
instance.on("transaction", () => {
|
||
|
|
requestAnimationFrame(() => {
|
||
|
|
requestAnimationFrame(() => {
|
||
|
|
if (isMounted) {
|
||
|
|
forceUpdate();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
return () => {
|
||
|
|
instance.destroy();
|
||
|
|
isMounted = false;
|
||
|
|
};
|
||
|
|
}, deps);
|
||
|
|
|
||
|
|
return editor as EditorType;
|
||
|
|
};
|