Skip to content
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

Fixed issue where undoing empty mentions or non-existent users caused… #3107

Merged
merged 5 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-beers-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-mention": patch
---

Fixed issue where undoing empty mentions or non-existent users caused system crashes.
13 changes: 9 additions & 4 deletions apps/www/src/registry/default/plate-ui/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
useEventEditorSelectors,
usePlateSelectors,
} from '@udecode/plate-common';
import { createVirtualRef } from '@udecode/plate-floating';
import {
createVirtualRef,
getBoundingClientRect,
} from '@udecode/plate-floating';

export const ComboboxItem = withRef<'div', ComboboxContentItemProps>(
({ combobox, index, item, onRenderItem, className, ...rest }, ref) => {
Expand Down Expand Up @@ -59,11 +62,13 @@ export function ComboboxContent(props: ComboboxContentProps) {
const state = useComboboxContentState({ items, combobox });
const { menuProps, targetRange } = useComboboxContent(state);

const virtualRef = createVirtualRef(editor, targetRange ?? undefined, {
fallbackRect: getBoundingClientRect(editor, editor.selection!),
});

return (
<Popover.Root open>
<Popover.PopoverAnchor
virtualRef={createVirtualRef(editor, targetRange ?? undefined)}
/>
<Popover.PopoverAnchor virtualRef={virtualRef} />

<Popover.Portal container={portalElement}>
<Popover.Content
Expand Down
8 changes: 7 additions & 1 deletion packages/mention/src/handlers/mentionOnKeyDownHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isHotkey, PlateEditor, Value } from '@udecode/plate-common';
import {
isHotkey,
moveSelection,
PlateEditor,
Value,
} from '@udecode/plate-common';

import { findMentionInput } from '../queries/index';
import { removeMentionInput } from '../transforms/index';
Expand All @@ -17,6 +22,7 @@ export const mentionOnKeyDownHandler: <V extends Value>(
if (currentMentionInput) {
event.preventDefault();
removeMentionInput(editor, currentMentionInput[1]);
moveSelection(editor, { unit: 'word' });
return true;
}
return false;
Expand Down
13 changes: 7 additions & 6 deletions packages/mention/src/transforms/removeMentionInput.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
EText,
getNode,
insertText,
getNodeString,
PlateEditor,
unwrapNodes,
replaceNode,
Value,
withoutNormalizing,
} from '@udecode/plate-common';
Expand All @@ -20,10 +21,10 @@ export const removeMentionInput = <V extends Value>(

const { trigger } = node;

insertText(editor, trigger, {
at: { path: [...path, 0], offset: 0 },
});
unwrapNodes(editor, {
const text = getNodeString(node);

replaceNode(editor, {
at: path,
nodes: { text: `${trigger}${text}` } as EText<V>,
});
});
5 changes: 4 additions & 1 deletion packages/mention/src/withMention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getPlugin,
getPointBefore,
getRange,
moveSelection,
PlateEditor,
setSelection,
TNode,
Expand Down Expand Up @@ -81,7 +82,8 @@ export const withMention = <
editor.deleteBackward = (unit) => {
const currentMentionInput = findMentionInput(editor);
if (currentMentionInput && getNodeString(currentMentionInput[0]) === '') {
return removeMentionInput(editor, currentMentionInput[1]);
removeMentionInput(editor, currentMentionInput[1]);
return moveSelection(editor, { unit: 'word' });
}

deleteBackward(unit);
Expand Down Expand Up @@ -151,6 +153,7 @@ export const withMention = <

if (previousMentionInputPath && !currentMentionInputPath) {
removeMentionInput(editor, previousMentionInputPath);
moveSelection(editor, { unit: 'word' });
}

if (currentMentionInputPath) {
Expand Down