Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/web/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
useCallback,
useEffect,
useImperativeHandle,
useLayoutEffect,
useInsertionEffect,
useMemo,
useRef,
useState,
Expand Down Expand Up @@ -74,8 +74,8 @@ export const CodeEditor = forwardRef<CodeEditorRef, CodeEditorProps>(function Co
onBlur,
onGapLayout,
});
// Written on commit, so a render React drops never calls its handlers.
useLayoutEffect(() => {
// Written on commit before any layout effect runs, so a render React drops never calls its handlers.
useInsertionEffect(() => {
handlers.current = {
onReady,
onScroll,
Expand Down Expand Up @@ -158,8 +158,8 @@ export const CodeEditor = forwardRef<CodeEditorRef, CodeEditorProps>(function Co
],
);
const latest = useRef({ options, language, decorations, decorationsRevision });
// Written on commit, so the props of a render React drops never reach the view.
useLayoutEffect(() => {
// Written on commit before any layout effect runs, so the props of a render React drops never reach the view.
useInsertionEffect(() => {
latest.current = { options, language, decorations, decorationsRevision };
});
// The decorations last sent, so the view gets them before its first draw and a repeat is not mapped twice.
Expand Down
28 changes: 27 additions & 1 deletion src/web/__tests__/CodeEditor.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, expect, jest, test } from '@jest/globals';
import { act, render } from '@testing-library/react-native';
import { createRef, Suspense, startTransition, use, useState } from 'react';
import { createRef, Suspense, startTransition, use, useLayoutEffect, useState } from 'react';
import type { CodeEditorRef } from '../../types';
import type { ViewOptions } from '../EditorView';

Expand Down Expand Up @@ -265,6 +265,32 @@ test('a render that never commits keeps its decorations, options and handlers of
expect(ready).toEqual([['committed', 14]]);
});

test("a layout effect earlier in the tree that focuses the editor reaches the same commit's onFocus", async () => {
const ref = createRef<CodeEditorRef>();
const calls: string[] = [];
function Focuser({ n }: { n: number }) {
useLayoutEffect(() => {
if (n > 0) void ref.current?.focus();
}, [n]);
return null;
}
function App({ n }: { n: number }) {
return (
<>
<Focuser n={n} />
<CodeEditor ref={ref} onFocus={() => calls.push(`focus ${n}`)} />
</>
);
}
const { rerender } = await render(<App n={0} />);
await act(async () => {
mockCore.resolve({});
await tick();
});
await rerender(<App n={1} />);
expect(calls).toEqual(['focus 1']);
});

test('decorations without a revision are for the current text', async () => {
const items = [{ kind: 'lineBackground' as const, line: 1, color: 'red' }];
await render(<CodeEditor decorations={items} />);
Expand Down
Loading