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

fix: Emoji suggestion slash menu does not display #984

Merged
merged 5 commits into from
Sep 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
25 changes: 18 additions & 7 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,15 +1159,26 @@ export class BlockNoteEditor<
};
}

public openSuggestionMenu(triggerCharacter: string) {
public openSuggestionMenu(
triggerCharacter: string,
pluginState?: {
deleteTriggerCharacter?: boolean;
ignoreQueryLength?: boolean;
}
) {
const tr = this.prosemirrorView.state.tr;
const transaction =
pluginState && pluginState.deleteTriggerCharacter
? tr.insertText(triggerCharacter)
: tr;

this.prosemirrorView.focus();
this.prosemirrorView.dispatch(
this.prosemirrorView.state.tr
.scrollIntoView()
.setMeta(this.suggestionMenus.plugin, {
triggerCharacter: triggerCharacter,
fromUserInput: false,
})
transaction.scrollIntoView().setMeta(this.suggestionMenus.plugin, {
triggerCharacter: triggerCharacter,
deleteTriggerCharacter: pluginState?.deleteTriggerCharacter || false,
ignoreQueryLength: pluginState?.ignoreQueryLength || false,
})
);
}
}
23 changes: 16 additions & 7 deletions packages/core/src/extensions/SuggestionMenu/SuggestionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const findBlock = findParentNode((node) => node.type.name === "blockContainer");

export type SuggestionMenuState = UiElementPosition & {
query: string;
ignoreQueryLength?: boolean;
};

class SuggestionMenuView<
Expand All @@ -34,7 +35,10 @@ class SuggestionMenuView<
throw new Error("Attempting to update uninitialized suggestions menu");
}

emitUpdate(menuName, this.state);
emitUpdate(menuName, {
...this.state,
ignoreQueryLength: this.pluginState?.ignoreQueryLength,
});
};

this.rootEl = this.editor._tiptapEditor.view.root;
Expand Down Expand Up @@ -120,7 +124,7 @@ class SuggestionMenuView<
.deleteRange({
from:
this.pluginState.queryStartPos! -
(this.pluginState.fromUserInput
(this.pluginState.deleteTriggerCharacter
? this.pluginState.triggerCharacter!.length
: 0),
to: this.editor._tiptapEditor.state.selection.from,
Expand All @@ -132,10 +136,11 @@ class SuggestionMenuView<
type SuggestionPluginState =
| {
triggerCharacter: string;
fromUserInput: boolean;
deleteTriggerCharacter: boolean;
queryStartPos: number;
query: string;
decorationId: string;
ignoreQueryLength?: boolean;
}
| undefined;

Expand Down Expand Up @@ -194,7 +199,8 @@ export class SuggestionMenuProseMirrorPlugin<
// or null if it should be hidden.
const suggestionPluginTransactionMeta: {
triggerCharacter: string;
fromUserInput?: boolean;
deleteTriggerCharacter?: boolean;
ignoreQueryLength?: boolean;
} | null = transaction.getMeta(suggestionMenuPluginKey);

// Only opens a menu of no menu is already open
Expand All @@ -206,11 +212,14 @@ export class SuggestionMenuProseMirrorPlugin<
return {
triggerCharacter:
suggestionPluginTransactionMeta.triggerCharacter,
fromUserInput:
suggestionPluginTransactionMeta.fromUserInput !== false,
deleteTriggerCharacter:
suggestionPluginTransactionMeta.deleteTriggerCharacter !==
false,
queryStartPos: newState.selection.from,
query: "",
decorationId: `id_${Math.floor(Math.random() * 0xffffffff)}`,
ignoreQueryLength:
suggestionPluginTransactionMeta?.ignoreQueryLength,
};
}

Expand Down Expand Up @@ -285,7 +294,7 @@ export class SuggestionMenuProseMirrorPlugin<

// If the menu was opened programmatically by another extension, it may not use a trigger character. In this
// case, the decoration is set on the whole block instead, as the decoration range would otherwise be empty.
if (!suggestionPluginState.fromUserInput) {
if (!suggestionPluginState.deleteTriggerCharacter) {
const blockNode = findBlock(state.selection);
if (blockNode) {
return DecorationSet.create(state.doc, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ export function getDefaultSlashMenuItems<
}

items.push({
onItemClick: () => editor.openSuggestionMenu(":"),
onItemClick: () => {
editor.openSuggestionMenu(":", {
deleteTriggerCharacter: true,
ignoreQueryLength: true,
});
},
key: "emoji",
...editor.dictionary.slash_menu.emoji,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ export function GridSuggestionMenuController<
if (
!isMounted ||
!state ||
(minQueryLength &&
(!state?.ignoreQueryLength &&
minQueryLength &&
(state.query.startsWith(" ") || state.query.length < minQueryLength))
) {
return null;
Expand Down
Loading