diff --git a/.changeset/markdoc-placeholder-ime.md b/.changeset/markdoc-placeholder-ime.md
new file mode 100644
index 000000000..aa5a64b47
--- /dev/null
+++ b/.changeset/markdoc-placeholder-ime.md
@@ -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.
diff --git a/packages/keystatic/src/form/fields/markdoc/editor/editor-view.tsx b/packages/keystatic/src/form/fields/markdoc/editor/editor-view.tsx
index 130ad5712..5e7aafe85 100644
--- a/packages/keystatic/src/form/fields/markdoc/editor/editor-view.tsx
+++ b/packages/keystatic/src/form/fields/markdoc/editor/editor-view.tsx
@@ -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,
diff --git a/packages/keystatic/src/form/fields/markdoc/editor/placeholder.ts b/packages/keystatic/src/form/fields/markdoc/editor/placeholder.ts
index 1bf139c0f..1e10b91e3 100644
--- a/packages/keystatic/src/form/fields/markdoc/editor/placeholder.ts
+++ b/packages/keystatic/src/form/fields/markdoc/editor/placeholder.ts
@@ -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,
+ }),
+ ]);
}
},
},
diff --git a/packages/keystatic/src/form/fields/markdoc/editor/tests/placeholder-ime.test.tsx b/packages/keystatic/src/form/fields/markdoc/editor/tests/placeholder-ime.test.tsx
new file mode 100644
index 000000000..18d921517
--- /dev/null
+++ b/packages/keystatic/src/form/fields/markdoc/editor/tests/placeholder-ime.test.tsx
@@ -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(
+
+
+
+
+
+ );
+
+ 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(
+
+
+
+ n
+
+
+
+ );
+
+ 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(
+
+
+
+
+
+ );
+
+ 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);
+});
diff --git a/packages/keystatic/src/form/fields/markdoc/editor/utils.ts b/packages/keystatic/src/form/fields/markdoc/editor/utils.ts
index fb50f65f7..45ceea8b8 100644
--- a/packages/keystatic/src/form/fields/markdoc/editor/utils.ts
+++ b/packages/keystatic/src/form/fields/markdoc/editor/utils.ts
@@ -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;
}