Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/markdoc-placeholder-ime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Fix first-character IME composition breaking on empty Markdoc fields by rendering the editor placeholder with a CSS pseudo-element node decoration and skipping redundant ProseMirror state updates.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export function useEditorView(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mountRef, onEditorStateChange, config]);
useLayoutEffect(() => {
viewRef.current?.updateState(state);
if (viewRef.current && viewRef.current.state !== state) {
viewRef.current.updateState(state);
}
}, [state]);
return {
view: viewRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export function placeholderPlugin(text: string) {
doc.firstChild?.isTextblock &&
doc.firstChild.content.size === 0
) {
let placeholder = document.createElement('span');
placeholder.className = classes.placeholder;
placeholder.textContent = text;

return DecorationSet.create(doc, [Decoration.widget(1, placeholder)]);
return DecorationSet.create(doc, [
Decoration.node(0, doc.firstChild.nodeSize, {
class: classes.placeholder,
'data-placeholder': text,
}),
]);
}
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/** @vitest-environment jsdom */
/** @jsxRuntime classic */
/** @jsx jsx */
import { fireEvent } from '@testing-library/react';
import { EditorState } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
import { afterEach, expect, test, vi } from 'vitest';
import { jsx, renderEditor } from './utils';

afterEach(() => {
vi.restoreAllMocks();
});

test('renders empty editor placeholder on the textblock node instead of a widget child span', async () => {
const { contentElement, state, user } = renderEditor(
<doc>
<paragraph>
<cursor />
</paragraph>
</doc>
);

const paragraph = contentElement.querySelector('p');
expect(paragraph).not.toBeNull();
expect(paragraph!.classList.contains('ProseMirror-placeholder')).toBe(true);
expect(paragraph!.getAttribute('data-placeholder')).toBe(
'Start writing or press "/" for commands…'
);
expect(
contentElement.querySelector('span.ProseMirror-placeholder')
).toBeNull();

const removedPlaceholderSpans: Node[] = [];
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
for (const removed of Array.from(mutation.removedNodes)) {
if (
removed instanceof HTMLElement &&
removed.classList.contains('ProseMirror-placeholder')
) {
removedPlaceholderSpans.push(removed);
}
}
}
});
observer.observe(paragraph!, { childList: true, subtree: true });

fireEvent.compositionStart(contentElement);
await user.keyboard('n');
fireEvent.compositionEnd(contentElement, { data: 'n' });
await Promise.resolve();
observer.disconnect();

expect(removedPlaceholderSpans).toHaveLength(0);
expect(paragraph!.classList.contains('ProseMirror-placeholder')).toBe(false);
expect(paragraph!.hasAttribute('data-placeholder')).toBe(false);
expect(state()).toEqual(
<doc>
<paragraph>
<text>
n<cursor />
</text>
</paragraph>
</doc>
);

await user.keyboard('{Backspace}');
expect(paragraph!.classList.contains('ProseMirror-placeholder')).toBe(true);
expect(paragraph!.getAttribute('data-placeholder')).toBe(
'Start writing or press "/" for commands…'
);
});

test('does not redundantly call view.updateState when React re-renders with the same dispatched state', async () => {
const updateStateSpy = vi.spyOn(EditorView.prototype, 'updateState');

const { user } = renderEditor(
<doc>
<paragraph>
<cursor />
</paragraph>
</doc>
);

updateStateSpy.mockClear();

await user.keyboard('a');

// dispatchTransaction calls updateState once; the React useLayoutEffect should
// not call updateState a second time for the exact same EditorState instance.
expect(updateStateSpy).toHaveBeenCalledTimes(1);
expect(updateStateSpy.mock.calls[0][0]).toBeInstanceOf(EditorState);
});
7 changes: 5 additions & 2 deletions packages/keystatic/src/form/fields/markdoc/editor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ export const prosemirrorStyles = css`
caret-color: transparent;
}

/* Style the placeholder element */
.${classes.placeholder} {
/* Style the placeholder pseudo-element */
.${classes.placeholder}::before {
color: ${tokenSchema.color.foreground.neutralTertiary};
content: attr(data-placeholder);
float: left;
height: 0;
pointer-events: none;
}

Expand Down
Loading