From 33773c01f33302e1bd7542c1ea0510ed22a66517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Sun, 7 Feb 2021 20:38:33 +0100 Subject: [PATCH] add allow option to suggestion extension --- packages/extension-mention/src/mention.ts | 6 ++++++ packages/suggestion/src/suggestion.ts | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/extension-mention/src/mention.ts b/packages/extension-mention/src/mention.ts index f0dba98f..c4331795 100644 --- a/packages/extension-mention/src/mention.ts +++ b/packages/extension-mention/src/mention.ts @@ -23,6 +23,12 @@ export const Mention = Node.create({ .insertText(' ') .run() }, + allow: ({ editor, range }) => { + // TODO: circular type. everthing explodes :—( + return (editor as any) + .can() + .replaceRange(range, 'mention') + }, }, }, diff --git a/packages/suggestion/src/suggestion.ts b/packages/suggestion/src/suggestion.ts index c7d0f576..1531a0e0 100644 --- a/packages/suggestion/src/suggestion.ts +++ b/packages/suggestion/src/suggestion.ts @@ -22,6 +22,10 @@ export interface SuggestionOptions { onExit?: (props: SuggestionProps) => void, onKeyDown?: (props: SuggestionKeyDownProps) => boolean, }, + allow?: (props: { + editor: Editor, + range: Range, + }) => boolean, } export interface SuggestionProps { @@ -51,6 +55,7 @@ export function Suggestion({ command = () => null, items = () => [], render = () => ({}), + allow = () => true, }: SuggestionOptions) { const renderer = render?.() @@ -149,7 +154,7 @@ export function Suggestion({ const decorationId = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}` // If we found a match, update the current state to show it - if (match) { + if (match && allow({ editor, range: match.range })) { next.active = true next.decorationId = prev.decorationId ? prev.decorationId : decorationId next.range = match.range