Files
paseo/packages/react-native-enriched-markdown/src/EnrichedMarkdownTextInput.tsx
T
Mohamed Boudra 80c44321cc Move the Markdown engine into the repository and fix two Android list layouts (#10499)
* chore(app): vendor react-native-enriched-markdown with the Paseo patch applied

Copy react-native-enriched-markdown@1.2.0-nightly-20261014-766fc0e67 (upstream
software-mansion/enriched-markdown 766fc0e67) into
packages/react-native-enriched-markdown, with
patches/react-native-enriched-markdown+1.2.0-nightly-20261014-766fc0e67.patch
applied. Nothing else changes in this commit; the package is not a workspace yet.

Reproduce:

  npm pack react-native-enriched-markdown@1.2.0-nightly-20261014-766fc0e67
  tar xzf react-native-enriched-markdown-*.tgz
  mkdir -p t/node_modules && mv package t/node_modules/react-native-enriched-markdown
  (cd t && git init -q && git apply ../patches/react-native-enriched-markdown+*.patch)
  diff -r t/node_modules/react-native-enriched-markdown packages/react-native-enriched-markdown

The diff is empty.

* chore(app): consume react-native-enriched-markdown from its workspace

Make packages/react-native-enriched-markdown a private workspace and point the
app at it, then remove the patch-package patch it replaces.

- package.json: private, version follows the root like every workspace, only the
  postinstall script that downloads tree-sitter grammars. Upstream's
  devDependencies would install a second react-native, so they are gone.
- The package keeps its name, so autolinking, the Gradle project
  :react-native-enriched-markdown, the CocoaPods spec and imports are unchanged.
- Lockfile: drop the nested registry copy under packages/app/node_modules; npm
  links node_modules/react-native-enriched-markdown to the workspace.
- Formatting, lint and knip skip the vendored code. CI routes changes to it
  through the app and browser suites.
- The codegen test checks the vendored package's generated props.
- docs/markdown-renderer.md describes the fork and how to edit it.

* fix(app): keep numbered sub-list markers off their text on Android

From #9964, moved from the package patch into the vendored package.

On Android, a numbered list nested in a bulleted item drew "1." over
the first letters of each item. A nested item's text indent summed the
root list's narrow bullet column with a fixed marginLeft, while its
marker was right-aligned to its own wider "99." column, so the number
landed on the text. iOS already indents from the item's own column.

Backport the Android hunks of upstream enriched-markdown #1015 (size
the number column to the widest marker) and #1023 (nested items start
their text from their own marker column).

test:android now also runs the backported ListRenderingTest and
TaskListInteractionTest suites. docs/markdown-renderer.md lists the
fork's backports, this one and #924, which the removed postinstall
comment used to track.

The vendored tree equals the #9964 patch applied to the pristine
package.

* fix(app): keep Android assistant replies inside their turns

From #10380, moved from the package patch into the vendored package.

On Android, an assistant reply with blockquotes nested inside a list
could overlap its "Worked for ..." footer and the next user message:
the displayed text and the layout measurement applied spans in
different orders, and the list-wide line height inflated the blockquote
spacers when it ran after them.

Use the same span order for Android Markdown measurement and display.
Apply list line height before blockquote spacers so nested quotes keep
their configured padding and the reply fits above the turn footer.

test:android now also runs ListBlockquoteMeasurementTest.

Applied on top of #9964; the two changes share only ListRenderer.kt,
where #10380's import and line-height call sit beside #9964's marker
width code. Every other file equals the #10380 patch applied to the
pristine package.

* chore(app): match the vendored Markdown web block renderer source to its build

The package patch changed lib/module/web/renderers/BlockRenderers.js (the
renderers.codeBlock override and the zoom-in cursor on images) but not
src/web/renderers/BlockRenderers.tsx. The app runs lib, so behavior does
not change. Port both changes into src so src stays the source of lib.
2026-11-23 10:15:43 +07:00

793 lines
25 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
useCallback,
useEffect,
useImperativeHandle,
useLayoutEffect,
useMemo,
useRef,
} from 'react';
import type React from 'react';
import EnrichedMarkdownTextInputNativeComponent, {
Commands,
type NativeProps,
type OnChangeTextEvent,
type OnChangeMarkdownEvent,
type OnChangeSelectionEvent,
type OnChangeStateEvent,
type OnKeyPressEvent,
type OnRequestMarkdownResultEvent,
type OnRequestCaretRectResultEvent,
type OnCaretRectChangeEvent,
type OnContextMenuItemPressEvent,
type OnLinkDetected,
type OnStartMentionEvent,
type OnChangeMentionEvent,
type OnEndMentionEvent,
} from './EnrichedMarkdownTextInputNativeComponent';
export type {
OnKeyPressEvent,
OnLinkDetected,
OnStartMentionEvent,
OnChangeMentionEvent,
OnEndMentionEvent,
} from './EnrichedMarkdownTextInputNativeComponent';
import type {
GestureResponderEvent,
HostInstance,
NativeSyntheticEvent,
TextInputProps,
ViewProps,
TextStyle,
StyleProp,
ColorValue,
} from 'react-native';
import { Platform } from 'react-native';
import { normalizeMarkdownShortcuts } from './normalizeMarkdownShortcuts';
import { normalizeMarkdownTextInputStyle } from './normalizeMarkdownTextInputStyle';
import { normalizeMenuItem } from './normalizeMenuItem';
import { toNativeRegexConfig } from './utils/regexParser';
import { TextInputState } from './utils/textInputState';
import { usePressability } from './utils/usePressability';
import type { RefObject } from 'react';
type NativeRef = HostInstance;
export interface LinkStyle {
color?: string;
underline?: boolean;
backgroundColor?: string;
}
export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
const VALID_HEADING_LEVELS = new Set<number>([1, 2, 3, 4, 5, 6]);
function toHeadingLevel(n: number): HeadingLevel {
return (VALID_HEADING_LEVELS.has(n) ? n : 1) as HeadingLevel;
}
export interface HeadingStyle {
fontSize?: number;
fontWeight?: string;
color?: string;
}
export interface MarkdownTextInputStyle {
strong?: {
color?: string;
};
em?: {
color?: string;
};
link?: LinkStyle;
linkVariants?: Record<string, LinkStyle>;
spoiler?: {
color?: string;
backgroundColor?: string;
};
/**
* Per-level heading styling for the editor, mirroring the readonly
* renderer's `markdownStyle` h1..h6. Omitted levels fall back to defaults
* (font sizes 30/24/20/18/16/14).
*/
h1?: HeadingStyle;
h2?: HeadingStyle;
h3?: HeadingStyle;
h4?: HeadingStyle;
h5?: HeadingStyle;
h6?: HeadingStyle;
/** List styling shared by bullet and numbered lists. */
list?: {
/**
* Vertical spacing (points) added above each list item so items read as
* separate rows. iOS uses `paragraphSpacingBefore`; Android a `LineHeightSpan`.
* @default 0
*/
itemSpacing?: number;
};
}
export interface StyleState {
bold: { isActive: boolean };
italic: { isActive: boolean };
underline: { isActive: boolean };
strikethrough: { isActive: boolean };
spoiler: { isActive: boolean };
/**
* The link at the selection: the one containing the first selected
* character, or, for a collapsed caret, the one the caret is inside or right
* after. `setLink` and `removeLink` act on this same link. `destination` is
* `""` when there is no link, and can also be `""` for a link with an empty
* URL.
*/
link: { isActive: boolean; destination: string };
heading: { isActive: boolean; level: HeadingLevel };
unorderedList: { isActive: boolean; depth: number };
orderedList: { isActive: boolean; depth: number };
}
export interface ContextMenuItem {
text: string;
onPress: (event: {
text: string;
selection: { start: number; end: number };
styleState: StyleState;
}) => void;
icon?: string;
visible?: boolean;
}
export interface CaretRect {
x: number;
y: number;
width: number;
height: number;
}
export interface EnrichedMarkdownTextInputInstance {
focus: () => void;
blur: () => void;
measure: HostInstance['measure'];
measureInWindow: HostInstance['measureInWindow'];
measureLayout: HostInstance['measureLayout'];
setValue: (markdown: string) => void;
setSelection: (start: number, end: number) => void;
toggleBold: () => void;
toggleItalic: () => void;
toggleUnderline: () => void;
toggleStrikethrough: () => void;
toggleSpoiler: () => void;
toggleHeading: (level: HeadingLevel) => void;
toggleUnorderedList: () => void;
toggleOrderedList: () => void;
indentList: () => void;
outdentList: () => void;
setLink: (url: string) => void;
insertLink: (text: string, url: string) => void;
insertText: (text: string) => void;
insertMention: (displayText: string, url: string) => void;
startMention: (indicator: string) => void;
removeLink: () => void;
copyToClipboard: () => void;
getMarkdown: () => Promise<string>;
getCaretRect: () => Promise<CaretRect>;
}
/**
* Per-item shape: `{ enabled }` toggles visibility, `label` overrides the
* English default. Wire `label` to your i18n library to localize the menu.
*/
type MenuItem = { enabled?: boolean; label?: string };
export interface InputSelectionMenuConfig {
/**
* The built-in "Format" submenu (Bold, Italic, Underline, etc.) in the
* text selection context menu. `label` overrides the submenu title.
* @default { enabled: true, label: "Format" }
*/
format?: MenuItem;
/**
* The built-in "Copy as Markdown" action in the text selection context
* menu. `label` overrides the action title.
* @default { enabled: true, label: "Copy as Markdown" }
*/
copyAsMarkdown?: MenuItem;
}
/** Controls the individual items inside the Format submenu. */
export interface FormatMenuConfig {
/** @default { enabled: true, label: "Bold" } */
bold?: MenuItem;
/** @default { enabled: true, label: "Italic" } */
italic?: MenuItem;
/** @default { enabled: true, label: "Underline" } */
underline?: MenuItem;
/** @default { enabled: true, label: "Strikethrough" } */
strikethrough?: MenuItem;
/** @default { enabled: true, label: "Spoiler" } */
spoiler?: MenuItem;
/** @default { enabled: true, label: "Link" } */
link?: MenuItem;
}
/**
* Which typed markdown block prefixes convert into blocks. An omitted key is
* off, so `{ heading: true }` enables headings and nothing else.
*/
export interface MarkdownShortcutsConfig {
/** `#`–`######` + space. @default false */
heading?: boolean;
/** `-`/`*`/`+` + space. @default false */
unorderedList?: boolean;
/** `1.` or `1)` + space. @default false */
orderedList?: boolean;
}
export interface EnrichedMarkdownTextInputProps
extends
Omit<ViewProps, 'style' | 'children'>,
// Same press props as React Native TextInput; `hitSlop` comes from ViewProps.
Pick<
TextInputProps,
'onPress' | 'onPressIn' | 'onPressOut' | 'rejectResponderTermination'
> {
ref?: RefObject<EnrichedMarkdownTextInputInstance | null>;
defaultValue?: string;
placeholder?: string;
placeholderTextColor?: ColorValue;
editable?: boolean;
autoFocus?: boolean;
scrollEnabled?: boolean;
/**
* Converts markdown block prefixes typed at the start of a paragraph into
* blocks, the way Notion, Bear and Obsidian do: `#`–`######` + space becomes
* a heading, `-`/`*`/`+` + space a bullet item, `1.` (or `1)`) + space a
* numbered item. The prefix and its space are removed from the text.
* `true` enables all three; pass a {@link MarkdownShortcutsConfig} to enable
* a subset. Off by default so apps that treat `#` or `-` as literal text
* (tags, dashes) see no change.
* @default false
*/
markdownShortcuts?: boolean | MarkdownShortcutsConfig;
autoCapitalize?: string;
multiline?: boolean;
cursorColor?: ColorValue;
selectionColor?: ColorValue;
markdownStyle?: MarkdownTextInputStyle;
style?: StyleProp<TextStyle>;
onChangeText?: (text: string) => void;
onChangeMarkdown?: (markdown: string) => void;
onChangeSelection?: (selection: { start: number; end: number }) => void;
onChangeState?: (state: StyleState) => void;
onKeyPress?: (e: NativeSyntheticEvent<OnKeyPressEvent>) => void;
onCaretRectChange?: (rect: CaretRect) => void;
onLinkDetected?: (event: OnLinkDetected) => void;
mentionIndicators?: string[];
onStartMention?: (event: OnStartMentionEvent) => void;
onChangeMention?: (event: OnChangeMentionEvent) => void;
onEndMention?: (event: OnEndMentionEvent) => void;
onFocus?: () => void;
onBlur?: () => void;
contextMenuItems?: ContextMenuItem[];
/**
* Controls built-in items in the text selection context menu.
* Omitting the prop or any field reproduces today's exact menu.
* Custom app-provided actions are controlled separately via `contextMenuItems`.
* @default { format: true, copyAsMarkdown: true }
* @platform ios, android, macos
*/
selectionMenuConfig?: InputSelectionMenuConfig;
/**
* Controls which items appear inside the Format submenu.
* Only effective when `selectionMenuConfig.format` is `true` (the default).
* Omitting the prop or any field shows all items.
* @default { bold: true, italic: true, underline: true, strikethrough: true, spoiler: true, link: true }
* @platform ios, android, macos
*/
formatMenuConfig?: FormatMenuConfig;
linkRegex?: RegExp | null;
/**
* Paragraph writing direction.
* - `'first-strong'` (default): resolves each paragraph from its first strong
* directional character. Neutral-only paragraphs fall back to the view's
* resolved layout direction (inherits ancestor `direction` style). Library
* extension — matches Android's `TEXT_DIRECTION_FIRST_STRONG`.
* - `'auto'`: React Native parity. iOS TextKit follows the app's
* `userInterfaceLayoutDirection`; mixed-direction paragraphs do not
* auto-resolve.
* - `'ltr'` / `'rtl'`: force base direction on every paragraph.
*
* Android ignores this prop; the platform's `EditText` always uses
* `TEXT_DIRECTION_FIRST_STRONG` per paragraph.
* @default 'first-strong'
* @platform ios
*/
writingDirection?: 'auto' | 'ltr' | 'rtl' | 'first-strong';
}
type PendingRequest<T> = {
resolve: (value: T) => void;
reject: (error: Error) => void;
};
function getNativeRef(ref: React.RefObject<NativeRef | null>): NativeRef {
if (ref.current == null) {
throw new Error(
'EnrichedMarkdownTextInput: native ref is not attached. Ensure the component is mounted.'
);
}
return ref.current;
}
export const EnrichedMarkdownTextInput = ({
ref,
markdownStyle,
style,
defaultValue,
placeholder,
placeholderTextColor,
editable = true,
autoFocus = false,
scrollEnabled = true,
markdownShortcuts,
autoCapitalize = 'sentences',
multiline = true,
cursorColor,
selectionColor,
onChangeText,
onChangeMarkdown,
onChangeSelection,
onChangeState,
onKeyPress,
onCaretRectChange,
onLinkDetected,
mentionIndicators,
onStartMention,
onChangeMention,
onEndMention,
onFocus,
onBlur,
onPress,
onPressIn,
onPressOut,
hitSlop,
rejectResponderTermination = true,
contextMenuItems,
selectionMenuConfig,
formatMenuConfig,
linkRegex: _linkRegex,
writingDirection = 'first-strong',
...rest
}: EnrichedMarkdownTextInputProps) => {
const nativeRef = useRef<NativeRef | null>(null);
// Freeze `defaultValue` at mount (RN TextInput semantics): post-mount changes are ignored.
const initialDefaultValue = useRef(defaultValue).current;
const nextRequestId = useRef(1);
const pendingRequests = useRef(new Map<number, PendingRequest<string>>());
const pendingCaretRectRequests = useRef(
new Map<number, PendingRequest<CaretRect>>()
);
const contextMenuCallbacksRef = useRef<
Map<string, ContextMenuItem['onPress']>
>(new Map());
useEffect(() => {
const callbacksMap = new Map<string, ContextMenuItem['onPress']>();
if (contextMenuItems) {
for (const item of contextMenuItems) {
callbacksMap.set(item.text, item.onPress);
}
}
contextMenuCallbacksRef.current = callbacksMap;
}, [contextMenuItems]);
const nativeContextMenuItems = useMemo(
() =>
contextMenuItems
?.filter((item) => item.visible !== false)
.map((item) => ({ text: item.text, icon: item.icon })),
[contextMenuItems]
);
useEffect(() => {
const pending = pendingRequests.current;
const pendingCaretRect = pendingCaretRectRequests.current;
return () => {
const err = new Error('Component unmounted');
pending.forEach(({ reject }) => reject(err));
pending.clear();
pendingCaretRect.forEach(({ reject }) => reject(err));
pendingCaretRect.clear();
};
}, []);
/**
* Mirrors the built-in TextInput's TextInputState integration so scroll
* containers' keyboardShouldPersistTaps logic and Keyboard.dismiss treat
* this input like a native TextInput. See utils/textInputState.ts for the
* full rationale. The unmount cleanup blurs through blurTextInput so both
* the registry entry and the native focus are released, matching the
* TextInput unmount path.
*/
useLayoutEffect(() => {
const instance = nativeRef.current;
if (instance == null) return;
TextInputState.registerInput(instance);
return () => {
if (TextInputState.currentlyFocusedInput() === instance) {
TextInputState.blurTextInput(instance);
}
TextInputState.unregisterInput(instance);
};
}, []);
const normalizedStyle = normalizeMarkdownTextInputStyle(markdownStyle);
const normalizedSelectionMenuConfig = useMemo(() => {
const format = normalizeMenuItem(
selectionMenuConfig?.format,
true,
'Format'
);
const copyAsMarkdown = normalizeMenuItem(
selectionMenuConfig?.copyAsMarkdown,
true,
'Copy as Markdown'
);
return {
format: format.enabled,
formatLabel: format.label,
copyAsMarkdown: copyAsMarkdown.enabled,
copyAsMarkdownLabel: copyAsMarkdown.label,
};
}, [selectionMenuConfig]);
const normalizedFormatMenuConfig = useMemo(() => {
const bold = normalizeMenuItem(formatMenuConfig?.bold, true, 'Bold');
const italic = normalizeMenuItem(formatMenuConfig?.italic, true, 'Italic');
const underline = normalizeMenuItem(
formatMenuConfig?.underline,
true,
'Underline'
);
const strikethrough = normalizeMenuItem(
formatMenuConfig?.strikethrough,
true,
'Strikethrough'
);
const spoiler = normalizeMenuItem(
formatMenuConfig?.spoiler,
true,
'Spoiler'
);
const link = normalizeMenuItem(formatMenuConfig?.link, true, 'Link');
return {
bold: bold.enabled,
boldLabel: bold.label,
italic: italic.enabled,
italicLabel: italic.label,
underline: underline.enabled,
underlineLabel: underline.label,
strikethrough: strikethrough.enabled,
strikethroughLabel: strikethrough.label,
spoiler: spoiler.enabled,
spoilerLabel: spoiler.label,
link: link.enabled,
linkLabel: link.label,
};
}, [formatMenuConfig]);
const normalizedMarkdownShortcuts = useMemo(
() => normalizeMarkdownShortcuts(markdownShortcuts),
[markdownShortcuts]
);
const linkRegex = useMemo(
() => toNativeRegexConfig(_linkRegex),
[_linkRegex]
);
const handleLinkDetected = useCallback(
(e: NativeSyntheticEvent<OnLinkDetected>) => {
const { text, url, start, end } = e.nativeEvent;
onLinkDetected?.({ text, url, start, end });
},
[onLinkDetected]
);
const handleChangeText = useCallback(
(e: NativeSyntheticEvent<OnChangeTextEvent>) => {
onChangeText?.(e.nativeEvent.value);
},
[onChangeText]
);
const handleChangeMarkdown = useCallback(
(e: NativeSyntheticEvent<OnChangeMarkdownEvent>) => {
onChangeMarkdown?.(e.nativeEvent.value);
},
[onChangeMarkdown]
);
const handleChangeSelection = useCallback(
(e: NativeSyntheticEvent<OnChangeSelectionEvent>) => {
const { start, end } = e.nativeEvent;
onChangeSelection?.({ start, end });
},
[onChangeSelection]
);
const handleChangeState = useCallback(
(e: NativeSyntheticEvent<OnChangeStateEvent>) => {
const {
bold,
italic,
underline,
strikethrough,
spoiler,
link,
heading,
unorderedList,
orderedList,
} = e.nativeEvent;
onChangeState?.({
bold,
italic,
underline,
strikethrough,
spoiler,
link,
heading: { ...heading, level: toHeadingLevel(heading.level) },
unorderedList,
orderedList,
});
},
[onChangeState]
);
const handleCaretRectChange = useCallback(
(e: NativeSyntheticEvent<OnCaretRectChangeEvent>) => {
const { x, y, width, height } = e.nativeEvent;
onCaretRectChange?.({ x, y, width, height });
},
[onCaretRectChange]
);
const handleStartMention = useCallback(
(e: NativeSyntheticEvent<OnStartMentionEvent>) => {
onStartMention?.(e.nativeEvent);
},
[onStartMention]
);
const handleChangeMention = useCallback(
(e: NativeSyntheticEvent<OnChangeMentionEvent>) => {
onChangeMention?.(e.nativeEvent);
},
[onChangeMention]
);
const handleEndMention = useCallback(
(e: NativeSyntheticEvent<OnEndMentionEvent>) => {
onEndMention?.(e.nativeEvent);
},
[onEndMention]
);
const handleFocus = useCallback(() => {
TextInputState.focusInput(nativeRef.current);
onFocus?.();
}, [onFocus]);
const handleBlur = useCallback(() => {
TextInputState.blurInput(nativeRef.current);
onBlur?.();
}, [onBlur]);
/**
* React Native TextInput attaches usePressability to the native host so taps
* claim the JS touch responder and ancestor Pressable handlers do not also
* run.
* https://github.com/react/react-native/blob/v0.86.2/packages/react-native/Libraries/Components/TextInput/TextInput.js#L582-L616
*/
const pressabilityConfig = useMemo(
() => ({
cancelable:
Platform.OS === 'ios' ? !rejectResponderTermination : undefined,
hitSlop,
onPress: (event: GestureResponderEvent) => {
onPress?.(event);
if (editable !== false) {
// Same call TextInput's host focus() makes. A tap on the text view
// focuses it natively, which makes this a no-op; it matters when the
// press lands outside the native view, e.g. in the hitSlop area.
TextInputState.focusTextInput(nativeRef.current);
}
},
onPressIn,
onPressOut,
}),
[
editable,
hitSlop,
onPress,
onPressIn,
onPressOut,
rejectResponderTermination,
]
);
// TextInput handles onBlur and onFocus events
// so omitting onBlur and onFocus pressability handlers here.
//
// Same logic as https://github.com/react/react-native/blob/v0.86.2/packages/react-native/Libraries/Components/TextInput/TextInput.js#L629
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onBlur: _onBlur,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onFocus: _onFocus,
...pressabilityHandlers
} = usePressability(pressabilityConfig) ?? {};
const handleRequestMarkdownResult = useCallback(
(e: NativeSyntheticEvent<OnRequestMarkdownResultEvent>) => {
const { requestId, markdown } = e.nativeEvent;
const pending = pendingRequests.current.get(requestId);
if (!pending) return;
pending.resolve(markdown);
pendingRequests.current.delete(requestId);
},
[]
);
const handleRequestCaretRectResult = useCallback(
(e: NativeSyntheticEvent<OnRequestCaretRectResultEvent>) => {
const { requestId, x, y, width, height } = e.nativeEvent;
const pending = pendingCaretRectRequests.current.get(requestId);
if (!pending) return;
pending.resolve({ x, y, width, height });
pendingCaretRectRequests.current.delete(requestId);
},
[]
);
const handleContextMenuItemPress = useCallback(
(e: NativeSyntheticEvent<OnContextMenuItemPressEvent>) => {
const {
itemText,
selectedText,
selectionStart,
selectionEnd,
styleState,
} = e.nativeEvent;
const callback = contextMenuCallbacksRef.current.get(itemText);
callback?.({
text: selectedText,
selection: { start: selectionStart, end: selectionEnd },
styleState: {
...styleState,
heading: {
...styleState.heading,
level: toHeadingLevel(styleState.heading.level),
},
},
});
},
[]
);
useImperativeHandle(ref, () => {
const node = getNativeRef(nativeRef);
// Codegen's ViewRef resolves to `never` with RN 0.84's function-based
// HostComponent type — the cast is safe at runtime.
const commandRef = node as Parameters<(typeof Commands)['focus']>[0];
return {
measure: (callback) => node.measure(callback),
measureInWindow: (callback) => node.measureInWindow(callback),
measureLayout: (relativeToNativeNode, onSuccess, onFail) =>
node.measureLayout(relativeToNativeNode, onSuccess, onFail),
focus: () => Commands.focus(commandRef),
blur: () => Commands.blur(commandRef),
setValue: (markdown) => Commands.setValue(commandRef, markdown),
setSelection: (start, end) =>
Commands.setSelection(commandRef, start, end),
toggleBold: () => Commands.toggleBold(commandRef),
toggleItalic: () => Commands.toggleItalic(commandRef),
toggleUnderline: () => Commands.toggleUnderline(commandRef),
toggleStrikethrough: () => Commands.toggleStrikethrough(commandRef),
toggleSpoiler: () => Commands.toggleSpoiler(commandRef),
toggleHeading: (level) => Commands.toggleHeading(commandRef, level),
toggleUnorderedList: () => Commands.toggleUnorderedList(commandRef),
toggleOrderedList: () => Commands.toggleOrderedList(commandRef),
indentList: () => Commands.indentList(commandRef),
outdentList: () => Commands.outdentList(commandRef),
setLink: (url) => Commands.setLink(commandRef, url),
insertLink: (text, url) => Commands.insertLink(commandRef, text, url),
insertText: (text) => Commands.insertText(commandRef, text),
insertMention: (displayText, url) =>
Commands.insertMention(commandRef, displayText, url),
startMention: (indicator) => Commands.startMention(commandRef, indicator),
removeLink: () => Commands.removeLink(commandRef),
copyToClipboard: () => Commands.copyToClipboard(commandRef),
getMarkdown: () =>
new Promise<string>((resolve, reject) => {
const requestId = nextRequestId.current++;
pendingRequests.current.set(requestId, { resolve, reject });
Commands.requestMarkdown(commandRef, requestId);
}),
getCaretRect: () =>
new Promise<CaretRect>((resolve, reject) => {
const requestId = nextRequestId.current++;
pendingCaretRectRequests.current.set(requestId, {
resolve,
reject,
});
Commands.requestCaretRect(commandRef, requestId);
}),
};
});
return (
<EnrichedMarkdownTextInputNativeComponent
ref={nativeRef}
style={style}
markdownStyle={normalizedStyle}
defaultValue={initialDefaultValue}
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
editable={editable}
autoFocus={autoFocus}
scrollEnabled={scrollEnabled}
markdownShortcuts={normalizedMarkdownShortcuts}
autoCapitalize={autoCapitalize}
multiline={multiline}
cursorColor={cursorColor}
selectionColor={selectionColor}
isOnChangeMarkdownSet={onChangeMarkdown !== undefined}
onChangeText={handleChangeText as NativeProps['onChangeText']}
onChangeMarkdown={handleChangeMarkdown as NativeProps['onChangeMarkdown']}
onChangeSelection={
handleChangeSelection as NativeProps['onChangeSelection']
}
onChangeState={handleChangeState as NativeProps['onChangeState']}
onInputKeyPress={onKeyPress as NativeProps['onInputKeyPress']}
onLinkDetected={handleLinkDetected as NativeProps['onLinkDetected']}
onInputFocus={handleFocus as NativeProps['onInputFocus']}
onInputBlur={handleBlur as NativeProps['onInputBlur']}
onRequestMarkdownResult={
handleRequestMarkdownResult as NativeProps['onRequestMarkdownResult']
}
onRequestCaretRectResult={
handleRequestCaretRectResult as NativeProps['onRequestCaretRectResult']
}
onCaretRectChange={
handleCaretRectChange as NativeProps['onCaretRectChange']
}
contextMenuItems={nativeContextMenuItems}
selectionMenuConfig={normalizedSelectionMenuConfig}
formatMenuConfig={normalizedFormatMenuConfig}
mentionIndicators={mentionIndicators}
onContextMenuItemPress={
handleContextMenuItemPress as NativeProps['onContextMenuItemPress']
}
linkRegex={linkRegex}
writingDirection={writingDirection}
onStartMention={handleStartMention as NativeProps['onStartMention']}
onChangeMention={handleChangeMention as NativeProps['onChangeMention']}
onEndMention={handleEndMention as NativeProps['onEndMention']}
// Explicitly pass through hitSlop because the native view subclasses
// RCTViewComponentView and makes use of hitSlop too
hitSlop={hitSlop}
{...rest}
{...pressabilityHandlers}
/>
);
};
export default EnrichedMarkdownTextInput;