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

feat: Asynchronous slash menu item fetching #506

Closed
wants to merge 7 commits into from
Closed
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
40 changes: 27 additions & 13 deletions examples/vanilla/src/ui/addSlashMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,47 @@ import {
BaseSlashMenuItem,
BlockNoteEditor,
DefaultBlockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema,
} from "@blocknote/core";
import { createButton } from "./util";

export const addSlashMenu = (editor: BlockNoteEditor) => {
export const addSlashMenu = async (editor: BlockNoteEditor) => {
let element: HTMLElement;

function updateItems(
items: BaseSlashMenuItem<DefaultBlockSchema, any, any>[],
onClick: (item: BaseSlashMenuItem<DefaultBlockSchema, any, any>) => void,
selected: number
async function updateItems(
query: string,
getItems: (
query: string
) => Promise<
BaseSlashMenuItem<
DefaultBlockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema
>[]
>,
onClick: (
item: BaseSlashMenuItem<
DefaultBlockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema
>
) => void
) {
element.innerHTML = "";
const items = await getItems(query);
const domItems = items.map((val, i) => {
const element = createButton(val.name, () => {
onClick(val);
});
element.style.display = "block";
if (selected === i) {
element.style.fontWeight = "bold";
}
return element;
});
element.append(...domItems);
return domItems;
}

editor.slashMenu.onUpdate((slashMenuState) => {
editor.slashMenu.onUpdate(async (slashMenuState) => {
if (!element) {
element = document.createElement("div");
element.style.background = "gray";
Expand All @@ -41,10 +55,10 @@ export const addSlashMenu = (editor: BlockNoteEditor) => {
}

if (slashMenuState.show) {
updateItems(
slashMenuState.filteredItems,
editor.slashMenu.itemCallback,
slashMenuState.keyboardHoveredItemIndex
await updateItems(
slashMenuState.query,
editor.slashMenu.getItems,
editor.slashMenu.executeItem
);

element.style.display = "block";
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export type BlockNoteEditorOptions<
*
* @default defaultSlashMenuItems from `./extensions/SlashMenu`
*/
slashMenuItems: BaseSlashMenuItem<any, any, any>[];
slashMenuItems: (
query: string
) => Promise<BaseSlashMenuItem<any, any, any>[]>;

/**
* The HTML element that should be used as the parent element for the editor.
Expand Down Expand Up @@ -290,7 +292,7 @@ export class BlockNoteEditor<
this.slashMenu = new SlashMenuProsemirrorPlugin(
this,
newOptions.slashMenuItems ||
(getDefaultSlashMenuItems(this.blockSchema) as any)
((query) => getDefaultSlashMenuItems(query, this.blockSchema) as any)
);
this.hyperlinkToolbar = new HyperlinkToolbarProsemirrorPlugin(this);
this.imageToolbar = new ImageToolbarProsemirrorPlugin(this);
Expand Down
Loading
Loading