Skip to content

Commit 04d5ae9

Browse files
committed
refactor: code review suggestions
1 parent 85da6ef commit 04d5ae9

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

src/web/EnrichedTextInput.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,21 @@ 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();
91+
function runSafelyInEditor<T>(
92+
editor: Editor | null,
93+
toRun: (editor: Editor) => T
94+
): T | null {
95+
if (editor && !editor.isDestroyed) {
96+
return toRun(editor);
97+
}
98+
return null;
9399
}
94100

95101
function runFocused(
96102
editor: Editor,
97103
apply: (chain: ChainedCommands) => ChainedCommands
98104
) {
99-
runSafelyInEditor(editor, () => apply(editor.chain().focus()).run());
105+
runSafelyInEditor(editor, (e) => apply(e.chain().focus()).run());
100106
}
101107

102108
export const EnrichedTextInput = ({
@@ -184,9 +190,7 @@ export const EnrichedTextInput = ({
184190
const text = nativeLeafText(doc, 0, doc.content.size);
185191
onSubmitEditingRef.current?.(adaptWebToNativeEvent(event, { text }));
186192
if (sb === 'blurAndSubmit') {
187-
runSafelyInEditor(editorInstanceRef.current, () =>
188-
editorInstanceRef.current?.commands.blur()
189-
);
193+
runSafelyInEditor(editorInstanceRef.current, (e) => e.commands.blur());
190194
}
191195
return true;
192196
}
@@ -265,8 +269,8 @@ export const EnrichedTextInput = ({
265269
autofocus: autoFocus,
266270
onCreate: ({ editor: _editor }) => {
267271
// Setting initial content in this way ensures all custom plugins are run and applied
268-
runSafelyInEditor(_editor, () =>
269-
_editor.commands.setContent(tiptapContent ?? '')
272+
runSafelyInEditor(_editor, (e) =>
273+
e.commands.setContent(tiptapContent ?? '')
270274
);
271275
},
272276
onFocus: ({ event }) => {
@@ -327,8 +331,8 @@ export const EnrichedTextInput = ({
327331
}, [editor, returnKeyType]);
328332

329333
useEffect(() => {
330-
runSafelyInEditor(editor, () =>
331-
editor?.commands.normalizeBoldInStyledHeadings()
334+
runSafelyInEditor(editor, (e) =>
335+
e.commands.normalizeBoldInStyledHeadings()
332336
);
333337
}, [editor, resolvedHtmlStyle]);
334338

@@ -346,11 +350,11 @@ export const EnrichedTextInput = ({
346350
useImperativeHandle(
347351
ref,
348352
(): EnrichedTextInputInstance => ({
349-
focus: () => runSafelyInEditor(editor, () => editor.commands.focus()),
350-
blur: () => runSafelyInEditor(editor, () => editor.commands.blur()),
353+
focus: () => runSafelyInEditor(editor, (e) => e.commands.focus()),
354+
blur: () => runSafelyInEditor(editor, (e) => e.commands.blur()),
351355
setValue: (value: string) =>
352-
runSafelyInEditor(editor, () =>
353-
editor.commands.setContent(
356+
runSafelyInEditor(editor, (e) =>
357+
e.commands.setContent(
354358
prepareHtmlForTiptap(
355359
value,
356360
useHtmlNormalizerRef.current,

0 commit comments

Comments
 (0)