Skip to content

Commit 251870c

Browse files
[RTE] Fix selected format styles in rich text toolbar when editing messages (#4940)
1 parent c1a2403 commit 251870c

7 files changed

Lines changed: 29 additions & 4 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "patch",
3+
"area": "fix",
4+
"workstream": "RTE",
5+
"comment": "Fix for an issue where toolbar didn't correctly show the selected style format during message editing",
6+
"packageName": "@azure/communication-react",
7+
"email": "98852890+vhuseinova-msft@users.noreply.github.com",
8+
"dependentChangeType": "patch"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "patch",
3+
"area": "fix",
4+
"workstream": "RTE",
5+
"comment": "Fix for an issue where toolbar didn't correctly show the selected style format during message editing",
6+
"packageName": "@azure/communication-react",
7+
"email": "98852890+vhuseinova-msft@users.noreply.github.com",
8+
"dependentChangeType": "patch"
9+
}

packages/react-components/src/components/RichTextEditor/Plugins/RichTextToolbarPlugin.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class RichTextToolbarPlugin implements EditorPlugin {
5454
* Updates the format state of the rich text editor and triggers the `onFormatChanged` callback
5555
* if there is any difference between the new and the current format states.
5656
*/
57-
private updateFormat(): void {
57+
updateFormat(): void {
5858
if (this.editor && this.onFormatChanged) {
5959
const newFormatState = getFormatState(this.editor);
6060
// use keys from the format that has more keys or the new format state if there is no current format state

packages/react-components/src/components/RichTextEditor/RichTextEditor.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,9 @@ const createEditorInitialModel = (
401401
};
402402

403403
const setSelectionAfterLastSegment = (model: ReadonlyContentModelBlockGroup, block: ContentModelParagraph): void => {
404-
const marker = createSelectionMarker();
404+
//selection marker should have the same format as the last segment if any
405+
const format = block.segments.length > 0 ? block.segments[block.segments.length - 1].format : undefined;
406+
const marker = createSelectionMarker(format);
405407
block.segments.push(marker);
406408
setSelection(model, marker);
407409
};

packages/react-components/src/components/RichTextEditor/Toolbar/RichTextToolbar.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export const RichTextToolbar = (props: RichTextToolbarProps): JSX.Element => {
5353
useEffect(() => {
5454
// update the format state on editor events
5555
plugin.onFormatChanged = setFormatState;
56+
// plugin editor ready event may happen before onFormatChanged is set
57+
// call update format function to ensure the format state is set
58+
plugin.updateFormat();
5659
}, [plugin]);
5760

5861
const boldButton: ICommandBarItemProps = useMemo(() => {

packages/storybook/stories/MessageThread/MessageThread.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ const MessageThreadStory = (args): JSX.Element => {
525525
if (message.messageType === 'chat') {
526526
message.content = content;
527527
message.editedOn = new Date(Date.now());
528-
if (args.richTextEditor === true) {
528+
// args will get string type when value is updated and page is reloaded (without updating switch again)
529+
if (args.richTextEditor === true || args.richTextEditor === 'true') {
529530
message.contentType = 'html';
530531
}
531532
}

packages/storybook8/stories/Components/MessageThread/MessageThread.story.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ const MessageThreadStory = (args): JSX.Element => {
114114
if (message.messageType === 'chat') {
115115
message.content = content;
116116
message.editedOn = new Date(Date.now());
117-
if (args.richTextEditor === true) {
117+
// args will get string type when value is updated and page is reloaded (without updating switch again)
118+
if (args.richTextEditor === true || args.richTextEditor === 'true') {
118119
message.contentType = 'html';
119120
}
120121
}

0 commit comments

Comments
 (0)