client: fix js error

pull/64/head
fantasticit 2022-06-03 19:26:20 +08:00
parent 8d910cf8b4
commit a56ff665c0
2 changed files with 7 additions and 7 deletions

View File

@ -232,11 +232,8 @@ export const SearchNReplace = Extension.create<SearchOptions>({
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<SearchOptions>({
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<SearchOptions>({
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));

View File

@ -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]);