Skip to content

Commit 85da6ef

Browse files
committed
fix: destroyed editor check
1 parent 0585b57 commit 85da6ef

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

src/web/EnrichedTextInput.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ import {
8888
} from './sanitization/htmlSanitizer';
8989
import { assertBrowserEnvironment } from './utils/assertBrowserEnvironment';
9090

91+
function runSafelyInEditor(editor: Editor | null, toRun: () => void) {
92+
!!editor && !editor.isDestroyed && toRun();
93+
}
94+
9195
function runFocused(
9296
editor: Editor,
9397
apply: (chain: ChainedCommands) => ChainedCommands
9498
) {
95-
apply(editor.chain().focus()).run();
99+
runSafelyInEditor(editor, () => apply(editor.chain().focus()).run());
96100
}
97101

98102
export const EnrichedTextInput = ({
@@ -180,7 +184,9 @@ export const EnrichedTextInput = ({
180184
const text = nativeLeafText(doc, 0, doc.content.size);
181185
onSubmitEditingRef.current?.(adaptWebToNativeEvent(event, { text }));
182186
if (sb === 'blurAndSubmit') {
183-
editorInstanceRef.current?.commands.blur();
187+
runSafelyInEditor(editorInstanceRef.current, () =>
188+
editorInstanceRef.current?.commands.blur()
189+
);
184190
}
185191
return true;
186192
}
@@ -259,7 +265,9 @@ export const EnrichedTextInput = ({
259265
autofocus: autoFocus,
260266
onCreate: ({ editor: _editor }) => {
261267
// Setting initial content in this way ensures all custom plugins are run and applied
262-
_editor.commands.setContent(tiptapContent ?? '');
268+
runSafelyInEditor(_editor, () =>
269+
_editor.commands.setContent(tiptapContent ?? '')
270+
);
263271
},
264272
onFocus: ({ event }) => {
265273
onFocus?.(adaptWebToNativeEvent(event, { target: -1 }));
@@ -319,7 +327,9 @@ export const EnrichedTextInput = ({
319327
}, [editor, returnKeyType]);
320328

321329
useEffect(() => {
322-
editor?.commands.normalizeBoldInStyledHeadings();
330+
runSafelyInEditor(editor, () =>
331+
editor?.commands.normalizeBoldInStyledHeadings()
332+
);
323333
}, [editor, resolvedHtmlStyle]);
324334

325335
const getMentionCallbacks = useCallback(
@@ -336,14 +346,16 @@ export const EnrichedTextInput = ({
336346
useImperativeHandle(
337347
ref,
338348
(): EnrichedTextInputInstance => ({
339-
focus: () => editor.commands.focus(),
340-
blur: () => editor.commands.blur(),
349+
focus: () => runSafelyInEditor(editor, () => editor.commands.focus()),
350+
blur: () => runSafelyInEditor(editor, () => editor.commands.blur()),
341351
setValue: (value: string) =>
342-
editor.commands.setContent(
343-
prepareHtmlForTiptap(
344-
value,
345-
useHtmlNormalizerRef.current,
346-
sanitizationConfigRef.current
352+
runSafelyInEditor(editor, () =>
353+
editor.commands.setContent(
354+
prepareHtmlForTiptap(
355+
value,
356+
useHtmlNormalizerRef.current,
357+
sanitizationConfigRef.current
358+
)
347359
)
348360
),
349361
setSelection: (start, end) => {

0 commit comments

Comments
 (0)