Skip to content

Commit

Permalink
refactor: Mentions & custom suggestion menus (#534)
Browse files Browse the repository at this point in the history
* fix: Docs home page demo styling fix (#507)

* Fixed styling for code styles (#517)

* Made suggestions plugin control multiple menus

* Added hook to create suggestion menu

* Fixed add block button behaviour on blocks with different content (#526)

* Small CSS fix (#527)

* fix: List item shortcuts (#528)

* Fixed list item shortcuts

* Updated test screenshots

* Added code toggle formatting toolbar example (#531)

* fix: Errors when hovering image block while editor is uneditable (#533)

* Fixed errors when hovering image block while editor is uneditable

* Small fix

* fix: Formatting toolbar not hiding while dragging blocks (#529)

* Fixed formatting toolbar not hiding while dragging blocks

* Small fix

* Updated Mantine (#535)

* Updated `package-lock.json` (#536)

* v0.11.1

* v0.6.2

* v0.8.1

* v0.8.2

* v0.8.3

* v0.8.4

* v0.8.5

* v0.9.0

* v0.9.1

* Revert "v0.9.1"

This reverts commit 6e9d61f.

* v0.9.1

* Revert "v0.9.1"

This reverts commit ac7abee.

* v0.9.2

* v0.9.3

* v0.9.4

* v0.9.5

* v0.9.6

* v0.10.0

* v0.10.1

* v0.11.0

* v0.11.1

* fix collaboration and editable: false bug (#538)

* pair refactoring

* wip

* fix type annotations on some of the docs (#539)

* Cleanup and continue refactor

* docs: Dark mode styling fix (#548)

* Fixed styles that were overwritten by Mantine globals

* Removed old code

* Implemented PR feedback

* Add Liveblocks info and BlockNote download command (#540)

* Update real-time-collaboration.md

* feat: Example image

* feat: Replace image with video

* Fixed video

---------

Co-authored-by: Matthew Lipski <[email protected]>

* Implemented PR feedback

* fix: Mantine styles (#553)

* Updated Mantine and package-lock

* Added Mantine global styles reset

* Made Mantine global styles not get imported

* Fixed CSS import order

* Updated test & snapshots

* Implemented PR feedback

* Implemented PR feedback

* Fixed paragraph shortcut (#556)

* v0.11.2

* Fixed build issues

* Fixed add block button behaviour

* Small fix

* Small fix

* Fixed tests

---------

Co-authored-by: Yousef <[email protected]>
Co-authored-by: Chris Nicholas <[email protected]>
  • Loading branch information
3 people authored Feb 13, 2024
1 parent 03126c5 commit 93e1d4f
Show file tree
Hide file tree
Showing 72 changed files with 3,396 additions and 2,658 deletions.
62 changes: 26 additions & 36 deletions examples/editor/examples/basic/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import {
uploadToTmpFilesDotOrg_DEV_ONLY,
} from "@blocknote/core";
import {
BlockNoteDefaultUI,
BlockNoteView,
createReactInlineContentSpec,
DefaultSlashMenu,
FormattingToolbarPositioner,
HyperlinkToolbarPositioner,
ImageToolbarPositioner,
SideMenuPositioner,
SlashMenuPositioner,
SuggestionMenuPositioner,
TableHandlesPositioner,
DefaultPositionedSuggestionMenu,
SuggestionMenuItemProps,
useBlockNote,
} from "@blocknote/react";
import "@blocknote/react/style.css";
Expand Down Expand Up @@ -53,17 +48,23 @@ const customInlineContentSchema = {
mention: MentionInlineContent.config,
} satisfies InlineContentSchema;

async function getMentionMenuItems(query: string) {
async function getMentionMenuItems(
editor: BlockNoteEditor<
DefaultBlockSchema,
typeof customInlineContentSchema,
DefaultStyleSchema
>,
query: string,
closeMenu: () => void,
clearQuery: () => void
): Promise<SuggestionMenuItemProps[]> {
const users = ["Steve", "Bob", "Joe", "Mike"];
const items = users.map((user) => ({
const items: SuggestionMenuItemProps[] = users.map((user) => ({
name: user,
execute: (
editor: BlockNoteEditor<
DefaultBlockSchema,
typeof customInlineContentSchema,
DefaultStyleSchema
>
) => {
execute: () => {
closeMenu();
clearQuery();

editor._tiptapEditor.commands.insertContent({
type: "mention",
attrs: {
Expand Down Expand Up @@ -94,32 +95,21 @@ export function App() {
},
},
uploadFile: uploadToTmpFilesDotOrg_DEV_ONLY,
extraSuggestionMenus: [
{
name: "mentions",
triggerCharacter: "@",
getItems: getMentionMenuItems,
},
],
});

// Give tests a way to get prosemirror instance
(window as WindowWithProseMirror).ProseMirror = editor?._tiptapEditor;

// TODO: Figure out cleaner API for adding/changing/removing menus & toolbars
return (
<BlockNoteView className="root" editor={editor}>
<FormattingToolbarPositioner editor={editor} />
<HyperlinkToolbarPositioner editor={editor} />
<SlashMenuPositioner editor={editor} />
<SideMenuPositioner editor={editor} />
<ImageToolbarPositioner editor={editor} />
{editor.blockSchema.table && (
<TableHandlesPositioner editor={editor as any} />
)}
<SuggestionMenuPositioner
editor={editor as any}
suggestionsMenuName={"mentions"}
suggestionsMenuComponent={DefaultSlashMenu}
<BlockNoteDefaultUI editor={editor} />
<DefaultPositionedSuggestionMenu
editor={editor}
triggerCharacter={"@"}
getItems={(query, closeMenu, clearQuery) =>
getMentionMenuItems(editor, query, closeMenu, clearQuery)
}
/>
</BlockNoteView>
);
Expand Down
8 changes: 4 additions & 4 deletions examples/editor/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@blocknote/example-editor",
"private": true,
"version": "0.11.0",
"version": "0.11.2",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
Expand All @@ -10,9 +10,9 @@
"clean": "rimraf dist"
},
"dependencies": {
"@blocknote/core": "^0.11.0",
"@blocknote/react": "^0.11.0",
"@mantine/core": "^7.3.1",
"@blocknote/core": "^0.11.2",
"@blocknote/react": "^0.11.2",
"@mantine/core": "^7.4.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.20.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/editor/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
createBrowserRouter,
} from "react-router-dom";

import "./style.css";
import { App } from "../examples/basic/App";
import { ReactCustomBlocks } from "../examples/react-custom-blocks/App";
import { ReactInlineContent } from "../examples/react-custom-inline-content/App";
import { ReactStyles } from "../examples/react-custom-styles/App";
import { CustomBlocks } from "../examples/vanilla-custom-blocks/App";
import { InlineContent } from "../examples/vanilla-custom-inline-content/App";
import "./style.css";

window.React = React;

Expand Down
4 changes: 1 addition & 3 deletions examples/editor/src/style.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
body {
height: auto;
}
@import url("@mantine/core/styles.css");

.editor {
margin: 8px calc((100% - 731px) / 2) 0;
Expand Down
4 changes: 2 additions & 2 deletions examples/vanilla/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@blocknote/example-vanilla",
"private": true,
"version": "0.11.0",
"version": "0.11.2",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"lint": "eslint src --max-warnings 0"
},
"dependencies": {
"@blocknote/core": "^0.11.0"
"@blocknote/core": "^0.11.2"
},
"devDependencies": {
"eslint": "^8.10.0",
Expand Down
108 changes: 76 additions & 32 deletions examples/vanilla/src/ui/addSlashMenu.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
import {
BlockNoteEditor,
DefaultBlockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema,
SuggestionItem,
} from "@blocknote/core";
import { BlockNoteEditor } from "@blocknote/core";
import { createButton } from "./util";

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

async function updateItems(
query: string,
getItems: (
query: string
) => Promise<
SuggestionItem<
DefaultBlockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema
>[]
>,
onClick: (
item: SuggestionItem<
DefaultBlockSchema,
DefaultInlineContentSchema,
DefaultStyleSchema
>
) => void
getItems: (query: string) => Promise<any[]>
) {
element.innerHTML = "";
const items = await getItems(query);
const domItems = items.map((val, i) => {
const element = createButton(val.name, () => {
onClick(val);
const domItems = items.map((val) => {
const element = createButton(val.text, () => {
val.executeItem();
});
element.style.display = "block";
return element;
Expand All @@ -42,7 +21,7 @@ export const addSlashMenu = async (editor: BlockNoteEditor) => {
return domItems;
}

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

if (slashMenuState.show) {
await updateItems(
slashMenuState.query,
editor.suggestionMenus.slashMenu.getItems,
editor.suggestionMenus.slashMenu.executeItem
);
// TODO: Default vanilla getItems data type?
const getItems = async (query: string) => {
const items = [
{
text: "Heading",
aliases: ["h", "heading1", "h1"],
executeItem: () => {
editor.suggestionMenus.closeMenu();
editor.suggestionMenus.clearQuery();

editor.insertBlocks(
[
{
type: "heading",
},
],
editor.getTextCursorPosition().block,
"after"
);
},
},
{
text: "List",
aliases: ["ul", "li", "list", "bulletlist", "bullet list"],
executeItem: () => {
editor.suggestionMenus.closeMenu();
editor.suggestionMenus.clearQuery();

editor.insertBlocks(
[
{
type: "bulletListItem",
},
],
editor.getTextCursorPosition().block,
"after"
);
},
},
{
text: "Paragraph",
aliases: ["p", "paragraph"],
executeItem: () => {
editor.suggestionMenus.closeMenu();
editor.suggestionMenus.clearQuery();

editor.insertBlocks(
[
{
type: "paragraph",
},
],
editor.getTextCursorPosition().block,
"after"
);
},
},
];

return items.filter(
({ text, aliases }) =>
text.toLowerCase().startsWith(query.toLowerCase()) ||
(aliases &&
aliases.filter((alias) =>
alias.toLowerCase().startsWith(query.toLowerCase())
).length !== 0)
);
};

await updateItems(slashMenuState.query, getItems);

element.style.display = "block";

Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useNx": false,
"useWorkspaces": true,
"version": "0.11.0"
"version": "0.11.2"
}
Loading

0 comments on commit 93e1d4f

Please sign in to comment.