Skip to content

Commit

Permalink
feat: simplify previous char pattern and move default option to creat…
Browse files Browse the repository at this point in the history
…eMentionPlugin
  • Loading branch information
cacosandon committed Jul 27, 2023
1 parent bf8d5c8 commit c6f5967
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/www/content/docs/mention.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ A function to create the mention node.
</APIItem>
<APIItem name="triggerPreviousCharPattern" type="RegExp" optional>
The pattern that matches the char before the trigger character
(default to `/^$|^\s$/` that matches beginning of line or space)
(default to `/^\s?$/` that matches beginning of line or space)
</APIItem>

<APIItem name="inputCreation" type="{ key: string; value: string }" optional>
Expand Down
1 change: 1 addition & 0 deletions packages/mention/src/createMentionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const createMentionPlugin = createPluginFactory<MentionPlugin>({
withOverrides: withMention,
options: {
trigger: '@',
triggerPreviousCharPattern: /^\s?$/,
createMentionNode: (item) => ({ value: item.text }),
},
plugins: [
Expand Down
7 changes: 2 additions & 5 deletions packages/mention/src/withMention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,8 @@ export const withMention = <
getPointBefore(editor, editor.selection)
)
);

const spaceOrEmptyPattern = /^$|^\s$/;
const previousCharPattern =
triggerPreviousCharPattern ?? spaceOrEmptyPattern;
const matchesPreviousCharPattern = previousCharPattern.test(previousChar);
const matchesPreviousCharPattern =
triggerPreviousCharPattern?.test(previousChar);

if (matchesPreviousCharPattern && text === trigger) {
const data: TMentionInputElement = {
Expand Down

0 comments on commit c6f5967

Please sign in to comment.