Skip to content

Commit

Permalink
Select the variable upon inserting
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Aug 7, 2024
1 parent 0683089 commit d7db464
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Transforms } from 'slate';

import type { MentionElementType } from '../types';

export function insertMention(editor: Editor, element: MentionElementType) {
export function insertMention(editor: Editor, element: MentionElementType, moveCursorAfterInsert: boolean) {
Transforms.insertNodes(editor, element);
Transforms.move(editor, { distance: 1, unit: 'offset' });

if (moveCursorAfterInsert) {
Transforms.move(editor, { distance: 1, unit: 'offset' });
}
}
4 changes: 3 additions & 1 deletion packages/slate-editor/src/extensions/mentions/useMentions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { MentionElementType, Option } from './types';
interface Parameters<V> {
createMentionElement: (option: Option<V>) => MentionElementType;
isEnabled?: (target: Range | null) => boolean;
moveCursorAfterInsert?: boolean;
options: Option<V>[];
trigger: string;
}
Expand All @@ -28,6 +29,7 @@ export interface Mentions<V> {
export function useMentions<V>({
createMentionElement,
isEnabled = stubTrue,
moveCursorAfterInsert = true,
options,
trigger,
}: Parameters<V>): Mentions<V> {
Expand All @@ -44,7 +46,7 @@ export function useMentions<V>({
if (target) {
Transforms.select(editor, target);
const mentionElement = createMentionElement(option);
insertMention(editor, mentionElement);
insertMention(editor, mentionElement, moveCursorAfterInsert);
setTarget(null);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function useVariables(
return useMentions<Variable>({
createMentionElement: (option) => createVariableNode(option.value.key),
isEnabled,
moveCursorAfterInsert: false,
options,
trigger: '%',
});
Expand Down

0 comments on commit d7db464

Please sign in to comment.