diff --git a/.changeset/add-back-msg-toolbar.md b/.changeset/add-back-msg-toolbar.md new file mode 100644 index 000000000..391d8ff33 --- /dev/null +++ b/.changeset/add-back-msg-toolbar.md @@ -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 diff --git a/src/app/components/editor/MarkdownToolbar.tsx b/src/app/components/editor/MarkdownToolbar.tsx new file mode 100644 index 000000000..c4f40eaac --- /dev/null +++ b/src/app/components/editor/MarkdownToolbar.tsx @@ -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 ( + + + {text} + {shortCode && ( + + + {shortCode} + + + )} + + + ); +} + +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 ( + + {(triggerRef) => ( + + + + )} + + ); +} + +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 ( + + {(triggerRef) => ( + + + + )} + + ); +} + +function MarkdownHeadingButton() { + const editor = useSlate(); + const [anchor, setAnchor] = useState(); + const modKey = isMacOS() ? KeySymbol.Command : 'Ctrl'; + + const handleMenuSelect = (prefix: string) => { + setAnchor(undefined); + applyMarkdownBlockPrefix(editor, prefix); + ReactEditor.focus(editor); + }; + + const handleMenuOpen: MouseEventHandler = (evt) => { + setAnchor(evt.currentTarget.getBoundingClientRect()); + }; + + return ( + setAnchor(undefined), + clickOutsideDeactivates: true, + isKeyForward: (evt: KeyboardEvent) => + evt.key === 'ArrowDown' || evt.key === 'ArrowRight', + isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp' || evt.key === 'ArrowLeft', + escapeDeactivates: stopPropagation, + }} + > + + + } + delay={500} + > + {(triggerRef) => ( + handleMenuSelect('# ')} + size="400" + radii="300" + > + + + )} + + } + delay={500} + > + {(triggerRef) => ( + handleMenuSelect('## ')} + size="400" + radii="300" + > + + + )} + + } + delay={500} + > + {(triggerRef) => ( + handleMenuSelect('### ')} + size="400" + radii="300" + > + + + )} + + + + + } + > + + + + + + ); +} + +export function MarkdownToolbar() { + const modKey = isMacOS() ? KeySymbol.Command : 'Ctrl'; + + return ( + + + + + } + /> + } + /> + } + /> + } + /> + } + /> + } + /> + + + + } + /> + } + /> + } + /> + } + /> + + + + + + ); +} diff --git a/src/app/components/editor/index.ts b/src/app/components/editor/index.ts index 09905d5f2..46ca31a11 100644 --- a/src/app/components/editor/index.ts +++ b/src/app/components/editor/index.ts @@ -6,4 +6,5 @@ export * from './keyboard'; export * from './output'; export * from './input'; +export * from './MarkdownToolbar'; export * from './types'; diff --git a/src/app/components/editor/keyboard.ts b/src/app/components/editor/keyboard.ts index 7d9b18d58..05eb43213 100644 --- a/src/app/components/editor/keyboard.ts +++ b/src/app/components/editor/keyboard.ts @@ -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; diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 448dee7a3..25d20ca93 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -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( 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( pluralkitProxyMessageHandler.init(); }, [pluralkitProxyMessageHandler]); + useEffect(() => { + if (!editorToolbar) setComposerToolbarOpen(false); + }, [editorToolbar, setComposerToolbarOpen]); + const [pkCompatEnable] = useSetting(settingsAtom, 'pkCompat'); const [pmpProxyingEnable] = useSetting(settingsAtom, 'pmpProxying'); const emojiBtnRef = useRef(null); @@ -1505,6 +1516,24 @@ export const RoomInput = forwardRef( )} + {editorToolbar && ( + setComposerToolbarOpen(!composerToolbarOpen)} + > + + + )} + {(emojiBoardTab: EmojiBoardTab | undefined, setEmojiBoardTab) => ( ( } + bottom={ + editorToolbar && + composerToolbarOpen && ( +
+ + +
+ ) + } /> {showSchedulePicker && ( ) { 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 }>) { } /> + + } + /> +