From d7db46485631de8262f2794fc2aaebd26b506a1f Mon Sep 17 00:00:00 2001 From: kudlajz Date: Wed, 7 Aug 2024 16:25:56 +0200 Subject: [PATCH] Select the variable upon inserting --- .../src/extensions/mentions/lib/insertMention.ts | 7 +++++-- .../slate-editor/src/extensions/mentions/useMentions.ts | 4 +++- .../slate-editor/src/extensions/variables/useVariables.ts | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/slate-editor/src/extensions/mentions/lib/insertMention.ts b/packages/slate-editor/src/extensions/mentions/lib/insertMention.ts index e81422c28..6a5caa6d0 100644 --- a/packages/slate-editor/src/extensions/mentions/lib/insertMention.ts +++ b/packages/slate-editor/src/extensions/mentions/lib/insertMention.ts @@ -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' }); + } } diff --git a/packages/slate-editor/src/extensions/mentions/useMentions.ts b/packages/slate-editor/src/extensions/mentions/useMentions.ts index 0f79b4375..9e9161300 100644 --- a/packages/slate-editor/src/extensions/mentions/useMentions.ts +++ b/packages/slate-editor/src/extensions/mentions/useMentions.ts @@ -11,6 +11,7 @@ import type { MentionElementType, Option } from './types'; interface Parameters { createMentionElement: (option: Option) => MentionElementType; isEnabled?: (target: Range | null) => boolean; + moveCursorAfterInsert?: boolean; options: Option[]; trigger: string; } @@ -28,6 +29,7 @@ export interface Mentions { export function useMentions({ createMentionElement, isEnabled = stubTrue, + moveCursorAfterInsert = true, options, trigger, }: Parameters): Mentions { @@ -44,7 +46,7 @@ export function useMentions({ if (target) { Transforms.select(editor, target); const mentionElement = createMentionElement(option); - insertMention(editor, mentionElement); + insertMention(editor, mentionElement, moveCursorAfterInsert); setTarget(null); } }, diff --git a/packages/slate-editor/src/extensions/variables/useVariables.ts b/packages/slate-editor/src/extensions/variables/useVariables.ts index 925e3289d..976eb8081 100644 --- a/packages/slate-editor/src/extensions/variables/useVariables.ts +++ b/packages/slate-editor/src/extensions/variables/useVariables.ts @@ -44,6 +44,7 @@ export function useVariables( return useMentions({ createMentionElement: (option) => createVariableNode(option.value.key), isEnabled, + moveCursorAfterInsert: false, options, trigger: '%', });