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
43 changes: 42 additions & 1 deletion packages/editor/src/note/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ describe("browser-safe editor controls", () => {
toJSON.mockRestore();
});

it("does not serialize ignored content echoes while focused", async () => {
it("defers external content until focus leaves the editor", async () => {
const ref = createRef<NoteEditorRef>();
const props = {
ref,
Expand All @@ -373,9 +373,50 @@ describe("browser-safe editor controls", () => {

expect(ref.current?.view?.state.doc.textContent).toBe("old");
expect(toJSON).not.toHaveBeenCalled();

act(() => ref.current?.view?.dom.blur());

expect(ref.current?.view?.state.doc.textContent).toBe("new");
toJSON.mockRestore();
});

it("keeps external content deferred while focus is in editor popups", async () => {
const ref = createRef<NoteEditorRef>();
const props = {
ref,
handleChange: vi.fn(),
enforceTitleHeading: false,
};
const rendered = render(
createElement(NoteEditor, {
...props,
initialContent: baseDoc,
}),
);
await waitFor(() => expect(ref.current?.view).not.toBeNull());
act(() => ref.current?.view?.focus());

rendered.rerender(
createElement(NoteEditor, {
...props,
initialContent: nextDoc,
}),
);

const popup = document.createElement("div");
popup.dataset.editorEscapeConsumer = "";
const popupButton = document.createElement("button");
popup.append(popupButton);
document.body.append(popup);

act(() => popupButton.focus());
expect(ref.current?.view?.state.doc.textContent).toBe("old");

act(() => popupButton.blur());
expect(ref.current?.view?.state.doc.textContent).toBe("new");
popup.remove();
});

it("flushes the current document through the change handler immediately", async () => {
const ref = createRef<NoteEditorRef>();
const handleChange = vi.fn();
Expand Down
20 changes: 19 additions & 1 deletion packages/editor/src/note/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,22 @@ export const NoteEditor = forwardRef<NoteEditorRef, NoteEditorProps>(

useEffect(() => {
let retryTimeout: ReturnType<typeof setTimeout> | undefined;
let deferredPopup: HTMLElement | undefined;
let deferredView: EditorView | undefined;

const syncContent = (event?: FocusEvent) => {
const popup =
event?.relatedTarget instanceof Element
? event.relatedTarget.closest<HTMLElement>(
"[data-editor-escape-consumer]",
)
: null;
if (popup) {
deferredPopup = popup;
popup.addEventListener("focusout", syncContent, { once: true });
return;
}

const syncContent = () => {
const view = viewRef.current;
if (!view) return;
if (previousContentRef.current === reconciledInitialContent) return;
Expand All @@ -835,6 +849,8 @@ export const NoteEditor = forwardRef<NoteEditorRef, NoteEditorProps>(
}

if (view.hasFocus() && !syncContentWhenFocused) {
deferredView = view;
view.dom.addEventListener("blur", syncContent, { once: true });
Comment thread
cursor[bot] marked this conversation as resolved.
return;
}

Expand Down Expand Up @@ -889,6 +905,8 @@ export const NoteEditor = forwardRef<NoteEditorRef, NoteEditorProps>(
if (retryTimeout) {
clearTimeout(retryTimeout);
}
deferredPopup?.removeEventListener("focusout", syncContent);
deferredView?.dom.removeEventListener("blur", syncContent);
};
}, [
reconciledInitialContent,
Expand Down
Loading