From b382f45ceb998ef013dfee96c52ed6a1edc90fe1 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Mon, 17 Aug 2026 15:30:32 +0200 Subject: [PATCH] fix(editor): update the editable's attributes without rebuilding the view --- .../editor/ProseMirrorEditable.test.tsx | 23 ++++++++++++++++ .../components/editor/ProseMirrorEditable.tsx | 27 +++++++++++++------ .../editor/prosemirrorController.ts | 21 ++++++++++++--- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/app/components/editor/ProseMirrorEditable.test.tsx b/src/app/components/editor/ProseMirrorEditable.test.tsx index 197640339..4a8a0ba0e 100644 --- a/src/app/components/editor/ProseMirrorEditable.test.tsx +++ b/src/app/components/editor/ProseMirrorEditable.test.tsx @@ -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(); + const { container, rerender } = render( + + ); + + act(() => { + ref.current?.setDocument([{ type: BlockType.Paragraph, children: [{ text: 'draft' }] }]); + }); + const paragraph = container.querySelector('.ProseMirror > p'); + + rerender( + + ); + + expect(container.querySelector('.ProseMirror > p')).toBe(paragraph); + expect(container.querySelector('.ProseMirror')).toHaveAttribute( + 'data-placeholder', + 'Reply to Alice' + ); + }); }); diff --git a/src/app/components/editor/ProseMirrorEditable.tsx b/src/app/components/editor/ProseMirrorEditable.tsx index 92d7edb7a..93af51a0e 100644 --- a/src/app/components/editor/ProseMirrorEditable.tsx +++ b/src/app/components/editor/ProseMirrorEditable.tsx @@ -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 { - 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)), diff --git a/src/app/components/editor/prosemirrorController.ts b/src/app/components/editor/prosemirrorController.ts index bc2c2f05b..6c1a92a15 100644 --- a/src/app/components/editor/prosemirrorController.ts +++ b/src/app/components/editor/prosemirrorController.ts @@ -34,6 +34,7 @@ export type EditorAutocompleteQuery = { * retain an EditorState or EditorView. */ export class ProseMirrorEditorController { + private attributes: Record = {}; 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): 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 => ({ + ...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): () => 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;