diff --git a/src/app/components/editor/Editor.css.ts b/src/app/components/editor/Editor.css.ts index 97053431e..79bf483fd 100644 --- a/src/app/components/editor/Editor.css.ts +++ b/src/app/components/editor/Editor.css.ts @@ -65,7 +65,7 @@ export const EditorTextarea = style([ { flexGrow: 1, height: 'auto', - padding: `${toRem(13)} 0 0`, + padding: `${toRem(13)} 0`, position: 'relative', whiteSpace: 'pre-wrap', wordWrap: 'break-word', @@ -104,10 +104,6 @@ export const EditorTextarea = style([ }, ]); -export const EditorTextareaInline = style({ - paddingBottom: toRem(13), -}); - export const EditorResponsiveAfterMultiline = style([ EditorOptions, { diff --git a/src/app/components/editor/Editor.tsx b/src/app/components/editor/Editor.tsx index c007212d7..e82569649 100644 --- a/src/app/components/editor/Editor.tsx +++ b/src/app/components/editor/Editor.tsx @@ -79,14 +79,12 @@ export const CustomEditor = forwardRef( ref ) => { const [shortcutOverrides] = useSetting(settingsAtom, 'shortcutOverrides'); - const [alwaysInlineEditor] = useSetting(settingsAtom, 'alwaysInlineEditor'); const rootRef = useRef(null); const focusScrollTimerRef = useRef(); // Buttons stay inline however tall the composer grows; only the audio // recorder stacks, because its controls need a row of their own. - const layoutIsMultiline = !alwaysInlineEditor && forceMultilineLayout; - const showResponsiveAfterInFooter = Boolean(responsiveAfter) && layoutIsMultiline; + const showResponsiveAfterInFooter = Boolean(responsiveAfter) && forceMultilineLayout; useEffect(() => () => window.clearTimeout(focusScrollTimerRef.current), []); const handleKeyDown: KeyboardEventHandler = useCallback( @@ -135,13 +133,13 @@ export const CustomEditor = forwardRef(
{top} {before && ( ( )} ( ( {(after || (responsiveAfter && !showResponsiveAfterInFooter)) && ( { 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; diff --git a/src/app/features/settings/general/General.tsx b/src/app/features/settings/general/General.tsx index e2e4f7dc4..65d21a626 100644 --- a/src/app/features/settings/general/General.tsx +++ b/src/app/features/settings/general/General.tsx @@ -445,10 +445,6 @@ function EditorButtonOrderRow({ function Editor() { const [enterForNewline, setEnterForNewline] = useSetting(settingsAtom, 'enterForNewline'); const [editorToolbar, setEditorToolbar] = useSetting(settingsAtom, 'editorToolbar'); - const [alwaysInlineEditor, setAlwaysInlineEditor] = useSetting( - settingsAtom, - 'alwaysInlineEditor' - ); const [editorOldAddFile, setEditorOldAddFile] = useSetting(settingsAtom, 'editorOldAddFile'); const [editorMicButton, setEditorMicButton] = useSetting(settingsAtom, 'editorMicButton'); const [editorEmojiButton, setEditorEmojiButton] = useSetting(settingsAtom, 'editorEmojiButton'); @@ -539,13 +535,6 @@ function Editor() { value={editorStickerButton} onChange={setEditorStickerButton} /> -