Skip to content

Commit 800f7d9

Browse files
authored
fix: allow opening another suggestion menu if another is triggered #1473 (#1591)
* fix: allow opening another suggestion menu if another is triggered #1473 * fix: moving selection to new block should hide the menu * fix: close the menu if it already exists
1 parent b7d906e commit 800f7d9

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

packages/core/src/extensions/SuggestionMenu/SuggestionPlugin.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ export class SuggestionMenuProseMirrorPlugin<
196196
},
197197

198198
// Apply changes to the plugin state from an editor transaction.
199-
apply(transaction, prev, _oldState, newState): SuggestionPluginState {
199+
apply: (
200+
transaction,
201+
prev,
202+
_oldState,
203+
newState
204+
): SuggestionPluginState => {
200205
// TODO: More clearly define which transactions should be ignored.
201206
if (transaction.getMeta("orderedListIndexing") !== undefined) {
202207
return prev;
@@ -215,12 +220,14 @@ export class SuggestionMenuProseMirrorPlugin<
215220
ignoreQueryLength?: boolean;
216221
} | null = transaction.getMeta(suggestionMenuPluginKey);
217222

218-
// Only opens a menu of no menu is already open
219223
if (
220224
typeof suggestionPluginTransactionMeta === "object" &&
221-
suggestionPluginTransactionMeta !== null &&
222-
prev === undefined
225+
suggestionPluginTransactionMeta !== null
223226
) {
227+
if (prev) {
228+
// Close the previous menu if it exists
229+
this.closeMenu();
230+
}
224231
const trackedPosition = trackPosition(
225232
editor,
226233
newState.selection.from -
@@ -262,7 +269,11 @@ export class SuggestionMenuProseMirrorPlugin<
262269
transaction.getMeta("pointer") ||
263270
// Moving the caret before the character which triggered the menu should hide it.
264271
(prev.triggerCharacter !== undefined &&
265-
newState.selection.from < prev.queryStartPos())
272+
newState.selection.from < prev.queryStartPos()) ||
273+
// Moving the caret to a new block should hide the menu.
274+
!newState.selection.$from.sameParent(
275+
newState.doc.resolve(prev.queryStartPos())
276+
)
266277
) {
267278
return undefined;
268279
}
@@ -281,14 +292,7 @@ export class SuggestionMenuProseMirrorPlugin<
281292

282293
props: {
283294
handleTextInput(view, _from, _to, text) {
284-
const suggestionPluginState: SuggestionPluginState = (
285-
this as Plugin
286-
).getState(view.state);
287-
288-
if (
289-
triggerCharacters.includes(text) &&
290-
suggestionPluginState === undefined
291-
) {
295+
if (triggerCharacters.includes(text)) {
292296
view.dispatch(
293297
view.state.tr
294298
.insertText(text)

0 commit comments

Comments
 (0)