fix(editor): update the editable's attributes without rebuilding the view

This commit is contained in:
Erwan Leboucher committed 2026-08-17 15:30:32 +02:00
1 parent 7b99f8902d
commit b382f45ceb
3 files changed
+59 -12

No files matched your search

@@ -68,4 +68,27 @@ describe('ProseMirrorEditable', () => {
expect(editable).toHaveAttribute('enterkeyhint', 'send');
expect(container.firstElementChild).toBe(editable);
});
it('updates editable semantics without rebuilding the document', () => {
const controller = new ProseMirrorEditorController();
const ref = createRef<ProseMirrorEditableHandle>();
const { container, rerender } = render(
<ProseMirrorEditable controller={controller} placeholder="Send a message" ref={ref} />
);
act(() => {
ref.current?.setDocument([{ type: BlockType.Paragraph, children: [{ text: 'draft' }] }]);
});
const paragraph = container.querySelector('.ProseMirror > p');
rerender(
<ProseMirrorEditable controller={controller} placeholder="Reply to Alice" ref={ref} />
);
expect(container.querySelector('.ProseMirror > p')).toBe(paragraph);
expect(container.querySelector('.ProseMirror')).toHaveAttribute(
'data-placeholder',
'Reply to Alice'
);
});
});
@@ -1,5 +1,5 @@
import type { HTMLAttributes, KeyboardEventHandler } from 'react';
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
import { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from 'react';
import type { EditorDocument } from './model';
import type { ProseMirrorEditorController } from './prosemirrorController';
@@ -63,18 +63,29 @@ export const ProseMirrorEditable = forwardRef<ProseMirrorEditableHandle, ProseMi
[controller]
);
useEffect(() => {
const root = rootRef.current;
if (!root) return undefined;
// ProseMirror owns these; React must not also render them.
return controller.mount(root, {
// ProseMirror owns these; React must not also render them.
const attributes = useMemo(
() => ({
...(editorClassName ? { class: editorClassName } : {}),
...(placeholder ? { 'data-placeholder': placeholder, 'aria-label': placeholder } : {}),
...(editableName ? { 'data-editable-name': editableName } : {}),
...(enterKeyHint ? { enterkeyhint: enterKeyHint } : {}),
role: 'textbox',
});
}, [controller, editableName, editorClassName, enterKeyHint, placeholder]);
}),
[editableName, editorClassName, enterKeyHint, placeholder]
);
const attributesRef = useRef(attributes);
attributesRef.current = attributes;
useEffect(() => {
const root = rootRef.current;
if (!root) return undefined;
return controller.mount(root, attributesRef.current);
}, [controller]);
useEffect(() => {
controller.setAttributes(attributes);
}, [attributes, controller]);
useEffect(
() => controller.subscribe((document) => onDocumentChange?.(document)),
@@ -34,6 +34,7 @@ export type EditorAutocompleteQuery<TPrefix extends string> = {
* retain an EditorState or EditorView.
*/
export class ProseMirrorEditorController {
private attributes: Record<string, string> = {};
private document: EditorDocument;
private listeners = new Set<(document: EditorDocument) => void>();
private renderContext: EditorRenderContext = defaultEditorRenderContext;
@@ -43,6 +44,20 @@ export class ProseMirrorEditorController {
this.document = structuredClone(initialDocument);
}
setAttributes(attributes: Record<string, string>): void {
const unchanged =
Object.keys(attributes).length === Object.keys(this.attributes).length &&
Object.entries(attributes).every(([key, value]) => this.attributes[key] === value);
if (unchanged) return;
this.attributes = attributes;
this.view?.setProps({ attributes: this.viewAttributes });
}
private viewAttributes = (state: EditorState): Record<string, string> => ({
...this.attributes,
'data-placeholder-visible': String(isProseMirrorDocumentEmpty(state.doc)),
});
/** Rebuilding node views is a full redraw, so only react to a real change. */
setRenderContext(context: EditorRenderContext): void {
if (context === this.renderContext) return;
@@ -110,6 +125,7 @@ export class ProseMirrorEditorController {
mount(element: HTMLElement, attributes?: Record<string, string>): () => void {
this.view?.destroy();
this.attributes = attributes ?? {};
const state = EditorState.create({
doc: toProseMirrorDocument(this.document),
plugins: [
@@ -125,10 +141,7 @@ export class ProseMirrorEditorController {
this.view = new EditorView(
{ mount: element },
{
attributes: (viewState) => ({
...attributes,
'data-placeholder-visible': String(isProseMirrorDocumentEmpty(viewState.doc)),
}),
attributes: this.viewAttributes,
handlePaste: (view, event) => {
const text = event.clipboardData?.getData('text/plain');
if (text === undefined || text === '') return false;