Skip to content

Commit de866c2

Browse files
fix: fixed mentions data on post creation (#1779)
1 parent 44c6d23 commit de866c2

5 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/components/CommentEditor/formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const serialize = (nodes: EditorValue): CommentSerializedProps => {
1717
comment.mentions.push({
1818
id: children.value,
1919
name: children.name,
20-
username: children.name,
20+
username: children.username,
2121
});
2222
break;
2323
default:

src/components/PostCreate/formatter.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { ELEMENT_MENTION, getNodeString } from '@udecode/plate';
1+
import {
2+
ELEMENT_MENTION,
3+
ELEMENT_MENTION_INPUT,
4+
getNodeString,
5+
} from '@udecode/plate';
26

37
import {
48
EditorValue,
@@ -29,13 +33,34 @@ export const serialize = (nodes: EditorValue): Partial<Post> => {
2933

3034
const checkAttributes = (children: RootBlock) => {
3135
switch (children.type) {
36+
case ELEMENT_MENTION_INPUT: {
37+
const username = children.children[0].text.trim();
38+
if (!post.mentions) {
39+
post.mentions = [
40+
{
41+
id: username,
42+
name: username,
43+
username: username,
44+
},
45+
];
46+
}
47+
48+
if (!post.mentions.map(mention => mention.id).includes(username)) {
49+
post.mentions.push({
50+
id: username,
51+
name: username,
52+
username: username,
53+
});
54+
}
55+
break;
56+
}
3257
case ELEMENT_MENTION:
3358
if (!post.mentions) {
3459
post.mentions = [
3560
{
3661
id: children.value,
3762
name: children.name as string,
38-
username: children.name as string,
63+
username: children.username as string,
3964
},
4065
];
4166
}
@@ -46,7 +71,7 @@ export const serialize = (nodes: EditorValue): Partial<Post> => {
4671
post.mentions.push({
4772
id: children.value,
4873
name: children.name as string,
49-
username: children.name as string,
74+
username: children.username as string,
5075
});
5176
}
5277
break;

src/components/common/Editor/Editor.interface.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import {
5151
TTodoListItemElement,
5252
WithOverride,
5353
TComboboxItem,
54+
TMentionInputElement,
55+
ELEMENT_MENTION_INPUT,
5456
} from '@udecode/plate';
5557

5658
import { ELEMENT_EMOJI, TEmojiElement } from './plugins/EmojiPicker';
@@ -116,6 +118,11 @@ export interface ShowMoreElement extends TElement {
116118
children: [EmptyText];
117119
}
118120

121+
export interface MentionInputElement extends TMentionInputElement {
122+
type: typeof ELEMENT_MENTION_INPUT;
123+
children: [PlainText];
124+
}
125+
119126
export type InlineElement =
120127
| LinkElement
121128
| MentionElement
@@ -255,7 +262,8 @@ export type RootBlock =
255262
| HashtagElement
256263
| EmojiElement
257264
| ImageListElement
258-
| ShowMoreElement;
265+
| ShowMoreElement
266+
| MentionInputElement;
259267

260268
export type EditorValue = RootBlock[];
261269

src/components/common/NodeViewer/NodeViewer.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import {
1818
createUnderlinePlugin,
1919
ELEMENT_MEDIA_EMBED,
2020
ELEMENT_MENTION,
21+
ELEMENT_MENTION_INPUT,
2122
ImageElement,
2223
MentionElement,
24+
MentionInputElement,
2325
Plate,
2426
withProps,
2527
} from '@udecode/plate';
@@ -127,6 +129,9 @@ export const NodeViewer: React.FC<NodeViewerProps> = props => {
127129
},
128130
},
129131
}),
132+
[ELEMENT_MENTION_INPUT]: withProps(MentionInputElement, {
133+
renderLabel: mentionable => '@' + mentionable.children[0].text,
134+
}),
130135
[ELEMENT_SHOW_MORE]: withProps(ShowMoreElement, {
131136
onToggle: toggleShowMore,
132137
}),

src/components/common/NodeViewer/formatter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ELEMENT_IMAGE,
33
ELEMENT_MEDIA_EMBED,
44
ELEMENT_MENTION,
5+
ELEMENT_MENTION_INPUT,
56
ELEMENT_PARAGRAPH,
67
getNodeString,
78
} from '@udecode/plate';
@@ -74,6 +75,10 @@ export const formatToString = (
7475
return children.username;
7576
}
7677

78+
if (children.type === ELEMENT_MENTION_INPUT) {
79+
return children.username;
80+
}
81+
7782
return getNodeString(children);
7883
})
7984
.join(' ')

0 commit comments

Comments
 (0)