diff --git a/packages/client/src/tiptap/core/extensions/search.ts b/packages/client/src/tiptap/core/extensions/search.ts index 4f3b1cf..92d81e5 100644 --- a/packages/client/src/tiptap/core/extensions/search.ts +++ b/packages/client/src/tiptap/core/extensions/search.ts @@ -232,11 +232,8 @@ export const SearchNReplace = Extension.create({ this.options.searchTerm = searchTerm; this.options.results = []; this.options.currentIndex = 0; - - (editor as Editor).eventEmitter.emit(ON_SEARCH_RESULTS); - + (editor as Editor).eventEmitter && (editor as Editor).eventEmitter.emit(ON_SEARCH_RESULTS); updateView(state, dispatch); - return false; }, setReplaceTerm: @@ -316,6 +313,7 @@ export const SearchNReplace = Extension.create({ addProseMirrorPlugins() { // eslint-disable-next-line @typescript-eslint/no-this-alias const extensionThis = this; + const editor = this.editor as Editor; return [ new Plugin({ @@ -337,7 +335,7 @@ export const SearchNReplace = Extension.create({ searchResultClass ); extensionThis.options.results = results; - (extensionThis.editor as Editor).eventEmitter.emit(ON_SEARCH_RESULTS); + editor.eventEmitter && editor.eventEmitter.emit(ON_SEARCH_RESULTS); if (ctx.getMeta('directDecoration')) { const { fromPos, toPos, attrs } = ctx.getMeta('directDecoration'); decorationsToReturn.push(Decoration.inline(fromPos, toPos, attrs)); diff --git a/packages/client/src/tiptap/core/menus/search/index.tsx b/packages/client/src/tiptap/core/menus/search/index.tsx index 61f1c05..0884a65 100644 --- a/packages/client/src/tiptap/core/menus/search/index.tsx +++ b/packages/client/src/tiptap/core/menus/search/index.tsx @@ -44,6 +44,8 @@ export const Search: React.FC<{ editor: Editor }> = ({ editor }) => { }, [replaceValue, editor]); useEffect(() => { + if (!editor) return; + const searchExtension = editor.extensionManager.extensions.find((ext) => ext.name === SearchNReplace.name); if (!searchExtension) return; @@ -55,11 +57,11 @@ export const Search: React.FC<{ editor: Editor }> = ({ editor }) => { setResults(results); }; - editor.eventEmitter.on(ON_SEARCH_RESULTS, listener); + editor.eventEmitter && editor.eventEmitter.on(ON_SEARCH_RESULTS, listener); return () => { if (!searchExtension) return; - editor.eventEmitter.off(ON_SEARCH_RESULTS, listener); + editor.eventEmitter && editor.eventEmitter.off(ON_SEARCH_RESULTS, listener); }; }, [editor]);