mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-06 08:42:23 -08:00
fix: improve new composer (#1830)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes # #### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
This commit is contained in:
8 files changed
+66
-39
No files matched your search
@@ -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,
|
||||
{
|
||||
|
||||
@@ -79,14 +79,12 @@ export const CustomEditor = forwardRef<HTMLDivElement, CustomEditorProps>(
|
||||
ref
|
||||
) => {
|
||||
const [shortcutOverrides] = useSetting(settingsAtom, 'shortcutOverrides');
|
||||
const [alwaysInlineEditor] = useSetting(settingsAtom, 'alwaysInlineEditor');
|
||||
const rootRef = useRef<HTMLDivElement | null>(null);
|
||||
const focusScrollTimerRef = useRef<number>();
|
||||
|
||||
// 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<HTMLDivElement, CustomEditorProps>(
|
||||
<div ref={setRootRef} className={`${css.Editor} ${className ?? ''}`}>
|
||||
{top}
|
||||
<Box
|
||||
className={`${css.EditorRow} ${layoutIsMultiline ? css.EditorRowMultiline : ''} ${showResponsiveAfterInFooter ? css.EditorRowMultilineWithResponsiveAfter : ''}`}
|
||||
className={`${css.EditorRow} ${forceMultilineLayout ? css.EditorRowMultiline : ''} ${showResponsiveAfterInFooter ? css.EditorRowMultilineWithResponsiveAfter : ''}`}
|
||||
alignItems="Start"
|
||||
style={{ display: after ? 'grid' : 'flex' }}
|
||||
>
|
||||
{before && (
|
||||
<Box
|
||||
className={`${css.EditorOptions} ${layoutIsMultiline ? css.EditorOptionsMultiline : ''}`}
|
||||
className={`${css.EditorOptions} ${forceMultilineLayout ? css.EditorOptionsMultiline : ''}`}
|
||||
alignItems="Center"
|
||||
gap="100"
|
||||
shrink="No"
|
||||
@@ -150,7 +148,7 @@ export const CustomEditor = forwardRef<HTMLDivElement, CustomEditorProps>(
|
||||
</Box>
|
||||
)}
|
||||
<Scroll
|
||||
className={`${css.EditorTextareaScroll} ${layoutIsMultiline ? css.EditorTextareaScrollMultiline : ''}`}
|
||||
className={`${css.EditorTextareaScroll} ${forceMultilineLayout ? css.EditorTextareaScrollMultiline : ''}`}
|
||||
variant={variant}
|
||||
style={{ maxHeight: showResponsiveAfterInFooter ? undefined : maxHeight }}
|
||||
size="300"
|
||||
@@ -160,7 +158,7 @@ export const CustomEditor = forwardRef<HTMLDivElement, CustomEditorProps>(
|
||||
<ProseMirrorEditable
|
||||
controller={editor}
|
||||
editableName={editableName}
|
||||
editorClassName={`${css.EditorTextarea} ${alwaysInlineEditor ? css.EditorTextareaInline : ''}`}
|
||||
editorClassName={css.EditorTextarea}
|
||||
placeholder={placeholder}
|
||||
enterKeyHint={enterKeyHint}
|
||||
onDocumentChange={handleDocumentChange}
|
||||
@@ -189,7 +187,7 @@ export const CustomEditor = forwardRef<HTMLDivElement, CustomEditorProps>(
|
||||
</Scroll>
|
||||
{(after || (responsiveAfter && !showResponsiveAfterInFooter)) && (
|
||||
<Box
|
||||
className={`${css.EditorOptions} ${layoutIsMultiline ? `${css.EditorOptionsMultiline} ${css.EditorOptionsAfterMultiline}` : ''}`}
|
||||
className={`${css.EditorOptions} ${forceMultilineLayout ? `${css.EditorOptionsMultiline} ${css.EditorOptionsAfterMultiline}` : ''}`}
|
||||
alignItems="Center"
|
||||
gap="100"
|
||||
shrink="No"
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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}
|
||||
/>
|
||||
<SettingToggle
|
||||
title="Always Inline Editor"
|
||||
focusId="always-inline-editor"
|
||||
description="Always keep the last line of the text editor inline with UI buttons."
|
||||
value={alwaysInlineEditor}
|
||||
onChange={setAlwaysInlineEditor}
|
||||
/>
|
||||
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
|
||||
<SettingTile
|
||||
title="Composer Button Order"
|
||||
|
||||
@@ -75,7 +75,6 @@ export const settingsLinkFocusIdsBySection: Record<SettingsSectionId, readonly s
|
||||
'always-play-call-sound',
|
||||
'custom-call-ringtone',
|
||||
'custom-call-ringback',
|
||||
'always-inline-editor',
|
||||
'composer-button-order',
|
||||
'show-emoji-button',
|
||||
'show-gif-button',
|
||||
|
||||
@@ -132,7 +132,6 @@ export interface Settings {
|
||||
editorStickerButton: boolean;
|
||||
editorButtonOrder: EditorButtonId[];
|
||||
composerToolbarOpen: boolean;
|
||||
alwaysInlineEditor: boolean;
|
||||
messageLayout: MessageLayout;
|
||||
messageSpacing: MessageSpacing;
|
||||
hideMembershipEvents: boolean;
|
||||
@@ -319,7 +318,6 @@ export const defaultSettings: Settings = {
|
||||
editorStickerButton: false,
|
||||
editorButtonOrder: [...EDITOR_BUTTON_ORDER_DEFAULT],
|
||||
composerToolbarOpen: false,
|
||||
alwaysInlineEditor: false,
|
||||
messageLayout: 0,
|
||||
messageSpacing: '400',
|
||||
hideMembershipEvents: false,
|
||||
|
||||
Reference in new issue
Block a user