add back toolbar

This commit is contained in:
7w1 committed 2026-05-06 19:48:23 -05:00
1 parent 22f49b7f2a
commit 336140008d
7 files changed
+344 -7

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
default: patch
---
Adds back the message editor toolbar under an optional setting. No longer uses WYSIWYG, just applies markdown. http://localhost:8080/settings/general?focus=composer-formatting-toolbar&moe.sable.client.action=settings
@@ -0,0 +1,280 @@
import FocusTrap from 'focus-trap-react';
import type { IconSrc, RectCords } from 'folds';
import {
Badge,
Box,
config,
Icon,
IconButton,
Icons,
Line,
Menu,
PopOut,
Scroll,
Text,
Tooltip,
TooltipProvider,
toRem,
} from 'folds';
import type { MouseEventHandler, ReactNode } from 'react';
import { useState } from 'react';
import { ReactEditor, useSlate } from 'slate-react';
import { isMacOS } from '$utils/user-agent';
import { KeySymbol } from '$utils/key-symbol';
import { stopPropagation } from '$utils/keyboard';
import { floatingToolbar } from '$styles/overrides/Composer.css';
import {
applyMarkdownBlockPrefix,
applyMarkdownInline,
BLOCK_HOTKEYS,
INLINE_HOTKEYS,
} from './keyboard';
import * as css from './Editor.css';
function BtnTooltip({ text, shortCode }: { text: string; shortCode?: string }) {
return (
<Tooltip style={{ padding: config.space.S300 }}>
<Box gap="200" direction="Column" alignItems="Center">
<Text align="Center">{text}</Text>
{shortCode && (
<Badge as="kbd" radii="300" size="500">
<Text size="T200" align="Center">
{shortCode}
</Text>
</Badge>
)}
</Box>
</Tooltip>
);
}
type MarkdownInlineButtonProps = {
marker: string;
icon: IconSrc;
tooltip: ReactNode;
};
function MarkdownInlineButton({ marker, icon, tooltip }: MarkdownInlineButtonProps) {
const editor = useSlate();
const handleClick = () => {
applyMarkdownInline(editor, marker);
ReactEditor.focus(editor);
};
return (
<TooltipProvider tooltip={tooltip} delay={500}>
{(triggerRef) => (
<IconButton
ref={triggerRef}
variant="SurfaceVariant"
onClick={handleClick}
size="400"
radii="300"
>
<Icon size="200" src={icon} />
</IconButton>
)}
</TooltipProvider>
);
}
type MarkdownBlockButtonProps = {
prefix: string;
icon: IconSrc;
tooltip: ReactNode;
};
function MarkdownBlockButton({ prefix, icon, tooltip }: MarkdownBlockButtonProps) {
const editor = useSlate();
const handleClick = () => {
applyMarkdownBlockPrefix(editor, prefix);
ReactEditor.focus(editor);
};
return (
<TooltipProvider tooltip={tooltip} delay={500}>
{(triggerRef) => (
<IconButton
ref={triggerRef}
variant="SurfaceVariant"
onClick={handleClick}
size="400"
radii="300"
>
<Icon size="200" src={icon} />
</IconButton>
)}
</TooltipProvider>
);
}
function MarkdownHeadingButton() {
const editor = useSlate();
const [anchor, setAnchor] = useState<RectCords>();
const modKey = isMacOS() ? KeySymbol.Command : 'Ctrl';
const handleMenuSelect = (prefix: string) => {
setAnchor(undefined);
applyMarkdownBlockPrefix(editor, prefix);
ReactEditor.focus(editor);
};
const handleMenuOpen: MouseEventHandler<HTMLButtonElement> = (evt) => {
setAnchor(evt.currentTarget.getBoundingClientRect());
};
return (
<PopOut
anchor={anchor}
offset={5}
position="Top"
content={
<FocusTrap
focusTrapOptions={{
initialFocus: false,
onDeactivate: () => setAnchor(undefined),
clickOutsideDeactivates: true,
isKeyForward: (evt: KeyboardEvent) =>
evt.key === 'ArrowDown' || evt.key === 'ArrowRight',
isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp' || evt.key === 'ArrowLeft',
escapeDeactivates: stopPropagation,
}}
>
<Menu style={{ padding: config.space.S100 }}>
<Box gap="100">
<TooltipProvider
tooltip={<BtnTooltip text="Heading 1" shortCode={`${modKey} + 1`} />}
delay={500}
>
{(triggerRef) => (
<IconButton
ref={triggerRef}
onClick={() => handleMenuSelect('# ')}
size="400"
radii="300"
>
<Icon size="200" src={Icons.Heading1} />
</IconButton>
)}
</TooltipProvider>
<TooltipProvider
tooltip={<BtnTooltip text="Heading 2" shortCode={`${modKey} + 2`} />}
delay={500}
>
{(triggerRef) => (
<IconButton
ref={triggerRef}
onClick={() => handleMenuSelect('## ')}
size="400"
radii="300"
>
<Icon size="200" src={Icons.Heading2} />
</IconButton>
)}
</TooltipProvider>
<TooltipProvider
tooltip={<BtnTooltip text="Heading 3" shortCode={`${modKey} + 3`} />}
delay={500}
>
{(triggerRef) => (
<IconButton
ref={triggerRef}
onClick={() => handleMenuSelect('### ')}
size="400"
radii="300"
>
<Icon size="200" src={Icons.Heading3} />
</IconButton>
)}
</TooltipProvider>
</Box>
</Menu>
</FocusTrap>
}
>
<IconButton
style={{ width: 'unset' }}
variant="SurfaceVariant"
onClick={handleMenuOpen}
size="400"
radii="300"
aria-haspopup="menu"
aria-expanded={!!anchor}
>
<Icon size="200" src={Icons.Heading1} />
<Icon size="200" src={Icons.ChevronBottom} />
</IconButton>
</PopOut>
);
}
export function MarkdownToolbar() {
const modKey = isMacOS() ? KeySymbol.Command : 'Ctrl';
return (
<Box className={`${css.EditorToolbarBase} ${floatingToolbar}`}>
<Scroll direction="Horizontal" size="0">
<Box className={css.EditorToolbar} alignItems="Center" gap="300">
<Box shrink="No" gap="100">
<MarkdownInlineButton
marker={INLINE_HOTKEYS['mod+b']!}
icon={Icons.Bold}
tooltip={<BtnTooltip text="Bold" shortCode={`${modKey} + B`} />}
/>
<MarkdownInlineButton
marker={INLINE_HOTKEYS['mod+i']!}
icon={Icons.Italic}
tooltip={<BtnTooltip text="Italic" shortCode={`${modKey} + I`} />}
/>
<MarkdownInlineButton
marker={INLINE_HOTKEYS['mod+u']!}
icon={Icons.Underline}
tooltip={<BtnTooltip text="Underline" shortCode={`${modKey} + U`} />}
/>
<MarkdownInlineButton
marker={INLINE_HOTKEYS['mod+s']!}
icon={Icons.Strike}
tooltip={<BtnTooltip text="Strike Through" shortCode={`${modKey} + S`} />}
/>
<MarkdownInlineButton
marker={INLINE_HOTKEYS['mod+[']!}
icon={Icons.Code}
tooltip={<BtnTooltip text="Inline Code" shortCode={`${modKey} + [`} />}
/>
<MarkdownInlineButton
marker={INLINE_HOTKEYS['mod+h']!}
icon={Icons.EyeBlind}
tooltip={<BtnTooltip text="Spoiler" shortCode={`${modKey} + H`} />}
/>
</Box>
<Line variant="SurfaceVariant" direction="Vertical" style={{ height: toRem(12) }} />
<Box shrink="No" gap="100">
<MarkdownBlockButton
prefix={BLOCK_HOTKEYS["mod+'"]!}
icon={Icons.BlockQuote}
tooltip={<BtnTooltip text="Block Quote" shortCode={`${modKey} + '`} />}
/>
<MarkdownBlockButton
prefix={BLOCK_HOTKEYS['mod+;']!}
icon={Icons.BlockCode}
tooltip={<BtnTooltip text="Block Code" shortCode={`${modKey} + ;`} />}
/>
<MarkdownBlockButton
prefix={BLOCK_HOTKEYS['mod+7']!}
icon={Icons.OrderList}
tooltip={<BtnTooltip text="Ordered List" shortCode={`${modKey} + 7`} />}
/>
<MarkdownBlockButton
prefix={BLOCK_HOTKEYS['mod+8']!}
icon={Icons.UnorderList}
tooltip={<BtnTooltip text="Unordered List" shortCode={`${modKey} + 8`} />}
/>
<MarkdownHeadingButton />
</Box>
</Box>
</Scroll>
</Box>
);
}
+1
View File
@@ -6,4 +6,5 @@ export * from './keyboard';
export * from './output';
export * from './input';
export * from './MarkdownToolbar';
export * from './types';
+7 -7
View File
@@ -23,7 +23,7 @@ const isHeading1 = isKeyHotkey('mod+1');
const isHeading2 = isKeyHotkey('mod+2');
const isHeading3 = isKeyHotkey('mod+3');
const insertMarkdownInline = (editor: Editor, marker: string) => {
export const applyMarkdownInline = (editor: Editor, marker: string) => {
if (editor.selection && Range.isExpanded(editor.selection)) {
const text = Editor.string(editor, editor.selection);
Transforms.insertText(editor, `${marker}${text}${marker}`);
@@ -33,7 +33,7 @@ const insertMarkdownInline = (editor: Editor, marker: string) => {
}
};
const insertMarkdownBlock = (editor: Editor, prefix: string) => {
export const applyMarkdownBlockPrefix = (editor: Editor, prefix: string) => {
if (editor.selection) {
const path = editor.selection.anchor.path;
const startPoint = Editor.start(editor, path);
@@ -52,7 +52,7 @@ export const toggleKeyboardShortcut = (editor: Editor, event: KeyboardEvent): bo
const blockToggled = BLOCK_KEYS.find((hotkey) => {
if (isKeyHotkey(hotkey, event)) {
event.preventDefault();
insertMarkdownBlock(editor, BLOCK_HOTKEYS[hotkey]!);
applyMarkdownBlockPrefix(editor, BLOCK_HOTKEYS[hotkey]!);
return true;
}
return false;
@@ -61,24 +61,24 @@ export const toggleKeyboardShortcut = (editor: Editor, event: KeyboardEvent): bo
if (isHeading1(event)) {
event.preventDefault();
insertMarkdownBlock(editor, '# ');
applyMarkdownBlockPrefix(editor, '# ');
return true;
}
if (isHeading2(event)) {
event.preventDefault();
insertMarkdownBlock(editor, '## ');
applyMarkdownBlockPrefix(editor, '## ');
return true;
}
if (isHeading3(event)) {
event.preventDefault();
insertMarkdownBlock(editor, '### ');
applyMarkdownBlockPrefix(editor, '### ');
return true;
}
const inlineToggled = INLINE_KEYS.find((hotkey) => {
if (isKeyHotkey(hotkey, event)) {
event.preventDefault();
insertMarkdownInline(editor, INLINE_HOTKEYS[hotkey]!);
applyMarkdownInline(editor, INLINE_HOTKEYS[hotkey]!);
return true;
}
return false;
+38
View File
@@ -22,6 +22,7 @@ import {
Icon,
IconButton,
Icons,
Line,
Menu,
MenuItem,
Overlay,
@@ -58,6 +59,7 @@ import {
ANYWHERE_AUTOCOMPLETE_PREFIXES,
BEGINNING_AUTOCOMPLETE_PREFIXES,
getLinks,
MarkdownToolbar,
replaceWithElement,
BlockType,
} from '$components/editor';
@@ -242,6 +244,11 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const [enterForNewline] = useSetting(settingsAtom, 'enterForNewline');
const [editorToolbar] = useSetting(settingsAtom, 'editorToolbar');
const [composerToolbarOpen, setComposerToolbarOpen] = useSetting(
settingsAtom,
'composerToolbarOpen'
);
const [hideActivity] = useSetting(settingsAtom, 'hideActivity');
const [mentionInReplies] = useSetting(settingsAtom, 'mentionInReplies');
@@ -260,6 +267,10 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
pluralkitProxyMessageHandler.init();
}, [pluralkitProxyMessageHandler]);
useEffect(() => {
if (!editorToolbar) setComposerToolbarOpen(false);
}, [editorToolbar, setComposerToolbarOpen]);
const [pkCompatEnable] = useSetting(settingsAtom, 'pkCompat');
const [pmpProxyingEnable] = useSetting(settingsAtom, 'pmpProxying');
const emojiBtnRef = useRef<HTMLButtonElement>(null);
@@ -1505,6 +1516,24 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
)}
</IconButton>
{editorToolbar && (
<IconButton
variant="SurfaceVariant"
size="300"
radii="300"
title={
composerToolbarOpen ? 'Hide formatting toolbar' : 'Show formatting toolbar'
}
aria-pressed={composerToolbarOpen}
aria-label={
composerToolbarOpen ? 'Hide formatting toolbar' : 'Show formatting toolbar'
}
onClick={() => setComposerToolbarOpen(!composerToolbarOpen)}
>
<Icon src={composerToolbarOpen ? Icons.AlphabetUnderline : Icons.Alphabet} />
</IconButton>
)}
<UseStateProvider initial={undefined}>
{(emojiBoardTab: EmojiBoardTab | undefined, setEmojiBoardTab) => (
<PopOut
@@ -1677,6 +1706,15 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
</Box>
</>
}
bottom={
editorToolbar &&
composerToolbarOpen && (
<div>
<Line variant="SurfaceVariant" size="300" />
<MarkdownToolbar />
</div>
)
}
/>
{showSchedulePicker && (
<SchedulePickerDialog
@@ -415,6 +415,7 @@ function DateAndTime() {
function Editor({ isMobile }: Readonly<{ isMobile: boolean }>) {
const [enterForNewline, setEnterForNewline] = useSetting(settingsAtom, 'enterForNewline');
const [editorToolbar, setEditorToolbar] = useSetting(settingsAtom, 'editorToolbar');
const [hideActivity, setHideActivity] = useSetting(settingsAtom, 'hideActivity');
const [hideReads, setHideReads] = useSetting(settingsAtom, 'hideReads');
const [sendPresence, setSendPresence] = useSetting(settingsAtom, 'sendPresence');
@@ -443,6 +444,14 @@ function Editor({ isMobile }: Readonly<{ isMobile: boolean }>) {
}
/>
</SequenceCard>
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
<SettingTile
title="Message Formatting Toolbar"
focusId="composer-formatting-toolbar"
description="Enable the formatting toolbar in the message composer."
after={<Switch variant="Primary" value={editorToolbar} onChange={setEditorToolbar} />}
/>
</SequenceCard>
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
<SettingTile
title="Hide Typing Indicators"
+4
View File
@@ -72,6 +72,8 @@ export interface Settings {
isWidgetDrawer: boolean;
memberSortFilterIndex: number;
enterForNewline: boolean;
editorToolbar: boolean;
composerToolbarOpen: boolean;
messageLayout: MessageLayout;
messageSpacing: MessageSpacing;
hideMembershipEvents: boolean;
@@ -188,6 +190,8 @@ export const defaultSettings: Settings = {
isWidgetDrawer: false,
memberSortFilterIndex: 0,
enterForNewline: false,
editorToolbar: false,
composerToolbarOpen: false,
messageLayout: 0,
messageSpacing: '400',
hideMembershipEvents: false,