-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix(markdown-toolbar): fixed markdown formatting toolbar toggle behaviour and duplicate marker handling #39479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fix markdown formatting toolbar toggle behavior and marker handling |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import type { IMessage } from '@rocket.chat/core-typings'; | ||
| import { Emitter } from '@rocket.chat/emitter'; | ||
| import { Accounts } from 'meteor/accounts-base'; | ||
| import { Tracker } from 'meteor/tracker'; | ||
| import type { RefObject } from 'react'; | ||
|
|
||
| import { limitQuoteChain } from './limitQuoteChain'; | ||
|
|
@@ -223,48 +222,83 @@ export const createComposerAPI = ( | |
| }; | ||
|
|
||
| const wrapSelection = (pattern: string): void => { | ||
| const { selectionEnd = input.value.length, selectionStart = 0 } = input; | ||
| const initText = input.value.slice(0, selectionStart); | ||
| const selectedText = input.value.slice(selectionStart, selectionEnd); | ||
| const finalText = input.value.slice(selectionEnd, input.value.length); | ||
| const token = '{{text}}'; | ||
| const i = pattern.indexOf(token); | ||
| if (i === -1) return; | ||
|
|
||
| const startPattern = pattern.slice(0, i); | ||
| const endPattern = pattern.slice(i + token.length); | ||
|
|
||
| const text = input.value; | ||
| let { selectionStart: start, selectionEnd: end } = input; | ||
|
|
||
| focus(); | ||
|
|
||
| const startPattern = pattern.slice(0, pattern.indexOf('{{text}}')); | ||
| const startPatternFound = input.value.slice(selectionStart - startPattern.length, selectionStart) === startPattern; | ||
| const before = text.slice(0, start); | ||
| const selected = text.slice(start, end); | ||
| const after = text.slice(end); | ||
|
|
||
| if (startPatternFound) { | ||
| const endPattern = pattern.slice(pattern.indexOf('{{text}}') + '{{text}}'.length); | ||
| const endPatternFound = input.value.slice(selectionEnd, selectionEnd + endPattern.length) === endPattern; | ||
| const left = before.lastIndexOf(startPattern); | ||
| const rightRelative = after.indexOf(endPattern); | ||
| const right = rightRelative === -1 ? -1 : end + rightRelative; | ||
|
|
||
| if (endPatternFound) { | ||
| insertText(selectedText); | ||
| input.selectionStart = selectionStart - startPattern.length; | ||
| input.selectionEnd = selectionEnd + endPattern.length; | ||
| const inside = | ||
| left !== -1 && | ||
| right !== -1 && | ||
| left + startPattern.length <= start && | ||
| right >= end; | ||
|
|
||
| if (!document.execCommand?.('insertText', false, selectedText)) { | ||
| input.value = initText.slice(0, initText.length - startPattern.length) + selectedText + finalText.slice(endPattern.length); | ||
| } | ||
| if (inside) { | ||
| const unwrapStart = left; | ||
| const unwrapEnd = right + endPattern.length; | ||
|
|
||
| input.selectionStart = selectionStart - startPattern.length; | ||
| input.selectionEnd = input.selectionStart + selectedText.length; | ||
| triggerEvent(input, 'input'); | ||
| triggerEvent(input, 'change'); | ||
| const inner = text.slice( | ||
| left + startPattern.length, | ||
| right | ||
| ); | ||
|
|
||
| focus(); | ||
| return; | ||
| input.setSelectionRange(unwrapStart, unwrapEnd); | ||
|
|
||
| if (!document.execCommand?.('insertText', false, inner)) { | ||
| input.value = | ||
| text.slice(0, unwrapStart) + | ||
| inner + | ||
| text.slice(unwrapEnd); | ||
| } | ||
|
|
||
| const pos = unwrapStart; | ||
| input.setSelectionRange(pos, pos + inner.length); | ||
|
|
||
| triggerEvent(input, 'input'); | ||
| triggerEvent(input, 'change'); | ||
| focus(); | ||
| return; | ||
| } | ||
|
|
||
| if (!document.execCommand?.('insertText', false, pattern.replace('{{text}}', selectedText))) { | ||
| input.value = initText + pattern.replace('{{text}}', selectedText) + finalText; | ||
| if (!selected && before.endsWith(startPattern)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Formatting toggle can delete the closing marker of an existing valid markdown span when the caret is immediately after it. Prompt for AI agents |
||
| const newBefore = before.slice(0, before.length - startPattern.length); | ||
| input.value = newBefore + after; | ||
| const pos = newBefore.length; | ||
| input.setSelectionRange(pos, pos); | ||
| triggerEvent(input, 'input'); | ||
| triggerEvent(input, 'change'); | ||
| focus(); | ||
| return; | ||
| } | ||
|
Comment on lines
+241
to
287
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delimiter matching is not pair-aware for symmetric markers. For π€ Prompt for AI Agents |
||
|
|
||
| input.selectionStart = selectionStart + pattern.indexOf('{{text}}'); | ||
| input.selectionEnd = input.selectionStart + selectedText.length; | ||
| const wrapped = `${startPattern}${selected}${endPattern}`; | ||
|
|
||
| input.setSelectionRange(start, end); | ||
|
|
||
| if (!document.execCommand?.('insertText', false, wrapped)) { | ||
| input.value = before + wrapped + after; | ||
| } | ||
|
|
||
| const caret = start + startPattern.length; | ||
| input.setSelectionRange(caret, caret + selected.length); | ||
|
|
||
| triggerEvent(input, 'input'); | ||
| triggerEvent(input, 'change'); | ||
|
|
||
| focus(); | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { MessageComposerAction } from '@rocket.chat/ui-composer'; | ||
| import { memo } from 'react'; | ||
| import { memo, useEffect, useState } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import FormattingToolbarDropdown from './FormattingToolbarDropdown'; | ||
|
|
@@ -16,6 +16,26 @@ type MessageBoxFormattingToolbarProps = { | |
|
|
||
| const MessageBoxFormattingToolbar = ({ items, variant = 'large', composer, disabled }: MessageBoxFormattingToolbarProps) => { | ||
| const { t } = useTranslation(); | ||
| const [activeModes, setActiveModes] = useState<Record<string, boolean>>({}); | ||
|
|
||
| useEffect(() => { | ||
| const textarea = composer.composerRef.current?.querySelector('textarea'); | ||
| if (!textarea) return; | ||
|
|
||
| const handleInput = () => { | ||
| if (!textarea.value) { | ||
| setActiveModes({}); | ||
| } | ||
| }; | ||
|
|
||
| textarea.addEventListener('input', handleInput); | ||
| return () => textarea.removeEventListener('input', handleInput); | ||
| }, [composer]); | ||
|
|
||
| const toggleMode = (pattern: string, label: string) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Prompt for AI agents |
||
| composer.wrapSelection(pattern); | ||
| setActiveModes((prev) => ({ ...prev, [label]: !prev[label] })); | ||
| }; | ||
|
Comment on lines
+35
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
π€ Prompt for AI Agents |
||
|
|
||
| if (variant === 'small') { | ||
| const collapsedItems = [...items]; | ||
|
|
@@ -26,11 +46,14 @@ const MessageBoxFormattingToolbar = ({ items, variant = 'large', composer, disab | |
| {'icon' in featuredFormatter && ( | ||
| <MessageComposerAction | ||
| onClick={() => | ||
| isPromptButton(featuredFormatter) ? featuredFormatter.prompt(composer) : composer.wrapSelection(featuredFormatter.pattern) | ||
| isPromptButton(featuredFormatter) | ||
| ? featuredFormatter.prompt(composer) | ||
| : toggleMode(featuredFormatter.pattern, featuredFormatter.label) | ||
| } | ||
| icon={featuredFormatter.icon} | ||
| title={t(featuredFormatter.label)} | ||
| disabled={disabled} | ||
| pressed={!!activeModes[featuredFormatter.label]} | ||
| /> | ||
| )} | ||
| <FormattingToolbarDropdown composer={composer} items={collapsedItems} disabled={disabled} /> | ||
|
|
@@ -43,33 +66,21 @@ const MessageBoxFormattingToolbar = ({ items, variant = 'large', composer, disab | |
| {items.map((formatter) => | ||
| 'icon' in formatter ? ( | ||
| <MessageComposerAction | ||
| disabled={disabled} | ||
| icon={formatter.icon} | ||
| key={formatter.label} | ||
| data-id={formatter.label} | ||
| icon={formatter.icon} | ||
| title={t(formatter.label)} | ||
| onClick={(): void => { | ||
| if (isPromptButton(formatter)) { | ||
| formatter.prompt(composer); | ||
| return; | ||
| } | ||
| if ('link' in formatter) { | ||
| window.open(formatter.link, '_blank', 'rel=noreferrer noopener'); | ||
| return; | ||
| } | ||
| composer.wrapSelection(formatter.pattern); | ||
| disabled={disabled} | ||
| pressed={!!activeModes[formatter.label]} | ||
| onClick={() => { | ||
| if (isPromptButton(formatter)) return formatter.prompt(composer); | ||
| if ('link' in formatter) return window.open(formatter.link, '_blank', 'rel=noreferrer noopener'); | ||
| toggleMode(formatter.pattern, formatter.label); | ||
| }} | ||
| /> | ||
| ) : ( | ||
| <span key={formatter.label} {...(disabled && { style: { pointerEvents: 'none' } })} title={formatter.label}> | ||
| <a href={formatter.link} target='_blank' rel='noopener noreferrer'> | ||
| {formatter.text()} | ||
| </a> | ||
| </span> | ||
| ), | ||
| ) : null, | ||
|
Comment on lines
66
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The large toolbar now drops This branch renders only formatters with an π€ Prompt for AI Agents |
||
| )} | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default memo(MessageBoxFormattingToolbar); | ||
| export default memo(MessageBoxFormattingToolbar); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1:
lastIndexOf(startPattern)is not pair-aware: for symmetric delimiters like**and~~, it can match a closing delimiter instead of an opening one. For example, with the cursor between**foo**and**bar**,lastIndexOf('**')inbeforefinds the closer of the first span, whileindexOf('**')inafterfinds the opener of the second span, causing the code to believe the cursor is inside a formatted region and incorrectly unwrap across both spans. This needs balanced pair matching.Prompt for AI agents