Skip to content

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

editor: fix `Applying mismatched transaction` errors on editor load

Abdullah Atta1852a216a247
diff --git a/packages/editor/src/hooks/use-editor.ts b/packages/editor/src/hooks/use-editor.ts
index 997d0b156..fadceb68c 100644
--- a/packages/editor/src/hooks/use-editor.ts
+++ b/packages/editor/src/hooks/use-editor.ts
@@ -21,6 +21,7 @@ import { EditorOptions, Editor as TiptapEditor } from "@tiptap/core";
import { DependencyList, useEffect, useMemo, useRef, useState } from "react";
import { Editor } from "../types.js";
import { useToolbarStore } from "../toolbar/stores/toolbar-store.js";
+import { EditorView } from "@tiptap/pm/view";
function useForceUpdate() {
const [, setValue] = useState(0);
@@ -46,7 +47,8 @@ export const useEditor = (
editor.options = { ...editor.options, ...options };
options.onBeforeCreate?.({ editor });
const oldIsFocused = editor.isFocused;
- editor.view.destroy();
+
+ destroyView(editor.view);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore instead of creating a new editor, we just create
// a new view. Due to some reason this is faster than resetting
@@ -78,7 +80,7 @@ export const useEditor = (
useEffect(() => {
return () => {
- editor.view.destroy();
+ destroyView(editor.view);
editor.destroy();
};
}, [editor]);
@@ -107,3 +109,15 @@ export const useEditor = (
return editor;
};
+
+function destroyView(view: EditorView) {
+ // we override all the methods to prevent any further interaction with the
+ // editor, otherwise we'll get errors.
+ view.dispatch = () => {};
+ view.update = () => {};
+ view.updateState = () => {};
+ view.updateRoot = () => {};
+ view.dispatchEvent = () => {};
+ view.setProps = () => {};
+ view.destroy();
+}