import { forwardRef, useImperativeHandle, useMemo, useRef } from 'react'; import { callback, getHostComponent } from 'react-native-nitro-modules'; import type { CodeEditorViewMethods, CodeEditorViewProps, NativeChangeEvent, NativeContentSize, NativeDecoration, NativeGap, NativeScrollEvent, NativeSearchHit, NativeSelection, NativeTheme, NativeTokenStyle, } from './specs/CodeEditorView.nitro'; import CodeEditorViewConfig from './specs/CodeEditorViewConfig'; import { githubLight } from './themes/githubLight'; import type { CodeEditorProps, CodeEditorRef, ContentInsets, Decoration, SearchHit, Theme, } from './types'; const NativeCodeEditor = getHostComponent( 'CodeEditorView', () => CodeEditorViewConfig, ); type NativeRef = CodeEditorViewMethods; // A prop that turns undefined reaches Nitro as null, which it rejects, so optional props default to unset values. // The insets default is one object, so it never crosses to native again on a re-render. const NO_INSETS: ContentInsets = {}; const NO_DECORATIONS: NativeDecoration[] = []; const UNUSED = { start: -1, end: -1, line: -1, rows: 0, color: '', style: 'solid', thickness: 0, offset: 0, } as const; /** One flat struct per item, the shape the native spec takes, with the underline defaults filled in. */ export function toNativeDecorations(decorations: Decoration[]): NativeDecoration[] { return decorations.map((d): NativeDecoration => { if (d.kind === 'underline') { return { ...UNUSED, kind: d.kind, start: d.range.start, end: d.range.end, color: d.color, style: d.style ?? 'solid', thickness: d.thickness ?? 1, offset: d.offset ?? 2, }; } if (d.kind === 'gap') return { ...UNUSED, kind: d.kind, line: d.line, rows: d.rows }; return { ...UNUSED, kind: d.kind, line: d.line, color: d.color }; }); } // Nitro crosses "no match" as a total of 0. const toHit = (hit: NativeSearchHit): SearchHit | null => (hit.total === 0 ? null : hit); /** Every token crosses to native as a full struct. The string shorthand exists only in TypeScript. */ export function normalizeTheme(theme: Theme): NativeTheme { const tokens: Record = {}; for (const [name, style] of Object.entries(theme.tokens)) { tokens[name] = typeof style === 'string' ? { color: style, bold: false, italic: false } : { color: style.color, bold: style.bold ?? false, italic: style.italic ?? false }; } const { tokens: _tokens, ...colors } = theme; return { ...colors, tokens }; } export const CodeEditor = forwardRef(function CodeEditor( { initialValue = '', language = '', theme = githubLight, fontFamily = '', fontSize = 14, lineHeight = 1.4, lineNumbers = true, softWrap = false, readOnly = false, highlightActiveLine = true, matchBrackets = true, placeholder = '', scrollEnabled = true, contentInsets = NO_INSETS, textPadding = 8, avoidKeyboard = false, tabSize = 4, insertSpaces = true, autoIndent = true, autoCloseBrackets = true, suggestions = 'off', autoFocus = false, onReady, onScroll, onSelectionChange, onChange, onChangeText, onFocus, onBlur, onContentSizeChange, decorations, decorationsRevision = -1, onGapLayout, ...viewProps }, ref, ) { const native = useRef(null); const onReadyRef = useRef(onReady); onReadyRef.current = onReady; const onScrollRef = useRef(onScroll); onScrollRef.current = onScroll; const onSelectionChangeRef = useRef(onSelectionChange); onSelectionChangeRef.current = onSelectionChange; const onChangeRef = useRef(onChange); onChangeRef.current = onChange; const onChangeTextRef = useRef(onChangeText); onChangeTextRef.current = onChangeText; // Native builds the full text only for a listener; once wired it stays, since a removed prop reaches Nitro as null. const textListened = useRef(false); if (onChangeText) textListened.current = true; const onFocusRef = useRef(onFocus); onFocusRef.current = onFocus; const onBlurRef = useRef(onBlur); onBlurRef.current = onBlur; const onContentSizeChangeRef = useRef(onContentSizeChange); onContentSizeChangeRef.current = onContentSizeChange; const nativeTheme = useMemo(() => normalizeTheme(theme), [theme]); // Nitro re-sends a function prop and re-invokes hybridRef whenever the wrapped object changes, // so every callback() is created once and reads the latest handler through a ref. const hybridRef = useMemo( () => callback((r: NativeRef) => { native.current = r; }), [], ); const onReadyCallback = useMemo(() => callback(() => onReadyRef.current?.()), []); const onScrollCallback = useMemo( () => callback((e: NativeScrollEvent) => onScrollRef.current?.(e)), [], ); const onSelectionChangeCallback = useMemo( () => callback((s: NativeSelection) => onSelectionChangeRef.current?.(s)), [], ); const onChangeCallback = useMemo( () => callback((e: NativeChangeEvent) => onChangeRef.current?.(e)), [], ); const onChangeTextCallback = useMemo( () => callback((text: string) => onChangeTextRef.current?.(text)), [], ); const onFocusCallback = useMemo(() => callback(() => onFocusRef.current?.()), []); const onBlurCallback = useMemo(() => callback(() => onBlurRef.current?.()), []); const onContentSizeChangeCallback = useMemo( () => callback((e: NativeContentSize) => onContentSizeChangeRef.current?.(e)), [], ); const nativeDecorations = useMemo( () => (decorations ? toNativeDecorations(decorations) : NO_DECORATIONS), [decorations], ); const onGapLayoutRef = useRef(onGapLayout); onGapLayoutRef.current = onGapLayout; // Native builds the gap list only for a listener. Once wired it stays, like onChangeText. const gapsListened = useRef(false); if (onGapLayout) gapsListened.current = true; const onGapLayoutCallback = useMemo( () => callback((gaps: NativeGap[]) => onGapLayoutRef.current?.({ gaps })), [], ); // Until the native view reports in, reads see an empty document, so calling the ref before onReady is safe. useImperativeHandle(ref, () => ({ getText: () => native.current?.getText() ?? '', getTextInRange: (range) => native.current?.getTextInRange(range.start, range.end) ?? '', getLength: () => native.current?.getLength() ?? 0, getLineCount: () => native.current?.getLineCount() ?? 1, getLine: (index) => native.current?.getLine(index) ?? '', getSelection: () => { const s = native.current?.getSelection(); return s ? { anchor: s.anchor, head: s.head } : { anchor: 0, head: 0 }; }, getRevision: () => native.current?.getRevision() ?? 0, offsetToPosition: (offset) => native.current?.offsetToPosition(offset) ?? { line: 0, column: 0 }, positionToOffset: (pos) => native.current?.positionToOffset(pos.line, pos.column) ?? 0, getCursorRect: () => native.current?.getCursorRect() ?? null, getGapRect: (line) => native.current?.getGapRect(line) ?? null, getVisibleLines: () => native.current?.getVisibleLines() ?? { first: 0, last: 0 }, canUndo: () => native.current?.canUndo() ?? false, canRedo: () => native.current?.canRedo() ?? false, isFocused: () => native.current?.isFocused() ?? false, setText: (text, opts) => native.current?.setText(text, opts?.keepHistory ?? false) ?? Promise.resolve(), setSelection: (sel) => native.current?.setSelection(sel.anchor, sel.head) ?? Promise.resolve(), selectAll: () => native.current?.selectAll() ?? Promise.resolve(), scrollToLine: (line, opts) => native.current?.scrollToLine(line, opts?.animated ?? false) ?? Promise.resolve(), revealSelection: () => native.current?.revealSelection() ?? Promise.resolve(), replaceRange: (range, text) => native.current?.replaceRange(range.start, range.end, text) ?? Promise.resolve(), insertText: (text) => native.current?.insertText(text) ?? Promise.resolve(), undo: () => native.current?.undo() ?? Promise.resolve(), redo: () => native.current?.redo() ?? Promise.resolve(), indent: () => native.current?.indent() ?? Promise.resolve(), outdent: () => native.current?.outdent() ?? Promise.resolve(), cut: () => native.current?.cut() ?? Promise.resolve(), copy: () => native.current?.copy() ?? Promise.resolve(), paste: () => native.current?.paste() ?? Promise.resolve(), focus: () => native.current?.focus() ?? Promise.resolve(), blur: () => native.current?.blur() ?? Promise.resolve(), setSearch: async (query) => { const result = await native.current?.setSearch({ text: query.text, regex: query.regex ?? false, caseSensitive: query.caseSensitive ?? false, wholeWord: query.wholeWord ?? false, }); return { count: result?.count ?? 0, limited: result?.limited ?? false }; }, findNext: () => native.current?.findNext().then(toHit) ?? Promise.resolve(null), findPrevious: () => native.current?.findPrevious().then(toHit) ?? Promise.resolve(null), replaceCurrent: (text) => native.current?.replaceCurrent(text) ?? Promise.resolve(false), replaceAll: (text) => native.current?.replaceAll(text) ?? Promise.resolve(0), clearSearch: () => native.current?.clearSearch() ?? Promise.resolve(), moveCaret: (direction, opts) => native.current?.moveCaret(direction, opts?.select ?? false) ?? Promise.resolve(), typeText: (text) => native.current?.typeText(text) ?? Promise.resolve(), deleteBackward: () => native.current?.deleteBackward() ?? Promise.resolve(), deleteForward: () => native.current?.deleteForward() ?? Promise.resolve(), })); return ( ); });