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: AI block UI #980

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3e1983b
Added AI block
matthewlipski Aug 1, 2024
0da498e
Added inline and slash menu AI
matthewlipski Aug 6, 2024
d48d91e
Small fix
matthewlipski Aug 6, 2024
82a56a9
UX improvements & refactor
matthewlipski Aug 6, 2024
5c66cfe
Extracted AI to separate package & changed AI block toolbar UX
matthewlipski Sep 9, 2024
22db2b4
Finished initial package split
matthewlipski Sep 9, 2024
e0f60a8
Moved last AI references to AI package
matthewlipski Sep 9, 2024
a2bab5d
Reverted minor unneeded changes
matthewlipski Sep 9, 2024
bcf9d31
refactor architecture
YousefED Sep 10, 2024
b814336
add extensions
YousefED Sep 10, 2024
cfc1bed
Refactored AI dictionary
matthewlipski Sep 10, 2024
ec36733
clean dictionary
YousefED Sep 10, 2024
78ac784
fix
YousefED Sep 10, 2024
2970e9d
fix
YousefED Sep 10, 2024
78924bb
Made AI button use suggestion menu components
matthewlipski Sep 11, 2024
4083cd9
Added keyboard navigation to AI button
matthewlipski Sep 11, 2024
d0d82a4
Refactored AI button
matthewlipski Sep 11, 2024
2df84f5
Changed AI from suggestion menu to propriety menu
matthewlipski Sep 11, 2024
644aa15
Minor changes
matthewlipski Sep 11, 2024
0fcb46a
Prevented focus swapping on suggestion menu items
matthewlipski Sep 11, 2024
251e82b
- AI Menu input spans full block width
matthewlipski Sep 11, 2024
736a8ff
Fixed AI Menu position for empty blocks
matthewlipski Sep 11, 2024
ffa466d
Made AI block react instead of vanilla
matthewlipski Sep 12, 2024
2c20238
fix build
YousefED Sep 16, 2024
f474949
schema
YousefED Sep 17, 2024
6ddf7b0
improve json schema methods
YousefED Sep 18, 2024
34abf80
Merge remote-tracking branch 'origin/main' into ai-block
YousefED Sep 23, 2024
b3926fe
merge
YousefED Sep 23, 2024
a9d25c9
improve json schema methods
YousefED Sep 23, 2024
2021ce7
fix build
YousefED Sep 23, 2024
10c6f5e
WIP: schemas and selections
YousefED Sep 25, 2024
a554ff1
selections wip
YousefED Sep 25, 2024
6833d9d
update selections
YousefED Sep 25, 2024
39642ce
wip selectionmarkers
YousefED Sep 25, 2024
7693b10
drop core / react structure
YousefED Sep 26, 2024
a8752e1
ai menu
YousefED Sep 26, 2024
979b917
add comment
YousefED Sep 26, 2024
f88a986
misc
YousefED Sep 26, 2024
3b7a80b
Added `size` field to React suggestion menu items
matthewlipski Sep 26, 2024
5f20d34
Merge branch 'ai-block' of github.com:TypeCellOS/BlockNote into ai-block
YousefED Sep 27, 2024
bb02ee6
basis for accept / reject menu
YousefED Sep 27, 2024
da2f06d
selection commands
YousefED Sep 27, 2024
f7bbf0e
Added `.env` file for API key and AI menu buttons for after an AI com…
matthewlipski Sep 27, 2024
6952a2e
Merge remote-tracking branch 'origin/ai-block' into ai-block
matthewlipski Sep 27, 2024
3ae2152
Added loader to AI menu
matthewlipski Sep 27, 2024
08d31a9
Updated styles
matthewlipski Sep 29, 2024
b0993e2
wip
YousefED Sep 30, 2024
5367284
gitignore
YousefED Sep 30, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ release
/test-results/
/playwright-report/
/playwright/.cache/
.env
7 changes: 7 additions & 0 deletions .todo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

review toolbar code
fix design of menus (smaller items, border?)

see if addToHistory: false is needed
track last affected position to update toolbar
enable / disable show selection plugin
5 changes: 4 additions & 1 deletion examples/01-basic/01-minimal/.bnexample.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"playground": true,
"docs": true,
"author": "matthewlipski",
"tags": ["Basic"]
"tags": ["Basic"],
"dependencies": {
"blocknote/ai": "latest"
}
}
89 changes: 86 additions & 3 deletions examples/01-basic/01-minimal/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,95 @@
import {
AIBlock,
AIBlockToolbarProsemirrorPlugin,
AIButton,
AIShowSelectionPlugin,
BlockNoteAIContextProvider,
BlockNoteAIUI,
aiBlockTypeSelectItems,
en as aiEN,
getAISlashMenuItems,
useBlockNoteAIContext,
} from "@blocknote/ai";

import "@blocknote/ai/style.css";
import {
BlockNoteEditor,
BlockNoteSchema,
defaultBlockSpecs,
en,
filterSuggestionItems,
} from "@blocknote/core";
import "@blocknote/core/fonts/inter.css";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import { useCreateBlockNote } from "@blocknote/react";
import {
FormattingToolbar,
FormattingToolbarController,
SuggestionMenuController,
blockTypeSelectItems,
getDefaultReactSlashMenuItems,
getFormattingToolbarItems,
useCreateBlockNote,
} from "@blocknote/react";

const schema = BlockNoteSchema.create({
blockSpecs: {
...defaultBlockSpecs,
ai: AIBlock,
},
});
export default function App() {
// Creates a new editor instance.
const editor = useCreateBlockNote();
const editor = useCreateBlockNote({
schema,
dictionary: {
...en,
ai: aiEN,
} as any,
extensions: {
// TODO: things will break when user provides different keys. Define name on plugins instead?
aiBlockToolbar: new AIBlockToolbarProsemirrorPlugin(),
aiSelection: new AIShowSelectionPlugin(),
},
});

// Renders the editor instance using a React component.
return <BlockNoteView editor={editor} />;
return (
<BlockNoteView editor={editor} formattingToolbar={false} slashMenu={false}>
<BlockNoteAIContextProvider>
<BlockNoteAIUI />
<FormattingToolbarController
formattingToolbar={() => (
<FormattingToolbar>
{...getFormattingToolbarItems([
...blockTypeSelectItems(editor.dictionary),
...aiBlockTypeSelectItems(aiEN),
])}
<AIButton />
</FormattingToolbar>
)}
/>
<SuggestionMenu editor={editor} />
</BlockNoteAIContextProvider>
{/* TODO: Side Menu customization */}
</BlockNoteView>
);
}

function SuggestionMenu(props: { editor: BlockNoteEditor<any, any, any> }) {
const ctx = useBlockNoteAIContext();
return (
<SuggestionMenuController
triggerCharacter="/"
getItems={async (query) =>
filterSuggestionItems(
[
...getDefaultReactSlashMenuItems(props.editor),
...getAISlashMenuItems(props.editor as any, ctx), // TODO
],
query
)
}
/>
);
}
3 changes: 2 additions & 1 deletion examples/01-basic/01-minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@blocknote/mantine": "latest",
"@blocknote/shadcn": "latest",
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react-dom": "^18.3.1",
"blocknote/ai": "latest"
},
"devDependencies": {
"@types/react": "^18.0.25",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function BlueButton() {
}, editor);

return (
<Components.FormattingToolbar.Button
<Components.Toolbar.Button
mainTooltip={"Blue Text & Background"}
onClick={() => {
editor.toggleStyles({
Expand All @@ -37,6 +37,6 @@ export function BlueButton() {
}}
isSelected={isSelected}>
Blue
</Components.FormattingToolbar.Button>
</Components.Toolbar.Button>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { HiOutlineGlobeAlt } from "react-icons/hi";

// Custom Slash Menu item to insert a block after the current one.
const insertHelloWorldItem = (editor: BlockNoteEditor) => ({
name: "hello_world",
title: "Insert Hello World",
onItemClick: () => {
// Block that the text cursor is currently in.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const FileReplaceButton = () => {
return (
<Components.Generic.Popover.Root opened={isOpen} position={"bottom"}>
<Components.Generic.Popover.Trigger>
<Components.FormattingToolbar.Button
<Components.Toolbar.Button
className={"bn-button"}
onClick={() => setIsOpen(!isOpen)}
isSelected={isOpen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export function AlertButton(props: LinkToolbarProps) {
const Components = useComponentsContext()!;

return (
<Components.LinkToolbar.Button
<Components.Toolbar.Button
mainTooltip={"Open Alert with URL"}
onClick={() => {
window.alert(`Link URL: ${props.url}`);
}}>
Open Alert
</Components.LinkToolbar.Button>
</Components.Toolbar.Button>
);
}
1 change: 1 addition & 0 deletions examples/06-custom-schema/01-alert-block/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const schema = BlockNoteSchema.create({

// Slash menu item to insert an Alert block
const insertAlert = (editor: typeof schema.BlockNoteEditor) => ({
name: "alert",
title: "Alert",
onItemClick: () => {
insertOrUpdateBlock(editor, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const getMentionMenuItems = (
const users = ["Steve", "Bob", "Joe", "Mike"];

return users.map((user) => ({
name: user.toLowerCase(),
title: user,
onItemClick: () => {
editor.insertInlineContent([
Expand Down
2 changes: 1 addition & 1 deletion examples/06-custom-schema/03-font-style/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const SetFontStyleButton = () => {
const Components = useComponentsContext()!;

return (
<Components.FormattingToolbar.Button
<Components.Toolbar.Button
label="Set Font"
mainTooltip={"Set Font"}
icon={<RiText />}
Expand Down
1 change: 1 addition & 0 deletions examples/06-custom-schema/04-pdf-file-block/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const schema = BlockNoteSchema.create({

// Slash menu item to insert a PDF block
const insertPDF = (editor: typeof schema.BlockNoteEditor) => ({
name: "pdf",
title: "PDF",
onItemClick: () => {
insertOrUpdateBlock(editor, {
Expand Down
8 changes: 4 additions & 4 deletions examples/06-custom-schema/react-custom-styles/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const CustomFormattingToolbar = (props: FormattingToolbarProps) => {

return (
<FormattingToolbar>
<Components.FormattingToolbar.Button
<Components.Toolbar.Button
mainTooltip={"small"}
onClick={() => {
editor.toggleStyles({
Expand All @@ -64,8 +64,8 @@ const CustomFormattingToolbar = (props: FormattingToolbarProps) => {
}}
isSelected={activeStyles.small}>
Small
</Components.FormattingToolbar.Button>
<Components.FormattingToolbar.Button
</Components.Toolbar.Button>
<Components.Toolbar.Button
mainTooltip={"font size"}
onClick={() => {
editor.toggleStyles({
Expand All @@ -74,7 +74,7 @@ const CustomFormattingToolbar = (props: FormattingToolbarProps) => {
}}
isSelected={!!activeStyles.fontSize}>
Font size
</Components.FormattingToolbar.Button>
</Components.Toolbar.Button>
</FormattingToolbar>
);
};
Expand Down
8 changes: 4 additions & 4 deletions examples/vanilla-js/react-vanilla-custom-styles/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const CustomFormattingToolbar = (props: FormattingToolbarProps) => {

return (
<FormattingToolbar>
<Components.FormattingToolbar.Button
<Components.Toolbar.Button
mainTooltip={"small"}
onClick={() => {
editor.toggleStyles({
Expand All @@ -76,8 +76,8 @@ const CustomFormattingToolbar = (props: FormattingToolbarProps) => {
}}
isSelected={activeStyles.small}>
Small
</Components.FormattingToolbar.Button>
<Components.FormattingToolbar.Button
</Components.Toolbar.Button>
<Components.Toolbar.Button
mainTooltip={"font size"}
onClick={() => {
editor.toggleStyles({
Expand All @@ -86,7 +86,7 @@ const CustomFormattingToolbar = (props: FormattingToolbarProps) => {
}}
isSelected={!!activeStyles.fontSize}>
Font size
</Components.FormattingToolbar.Button>
</Components.Toolbar.Button>
</FormattingToolbar>
);
};
Expand Down
Loading
Loading