Skip to content

Commit 683a515

Browse files
fix: Formatting toolbar opening when inserting file block with trailingBlock: false (BLO-860) (#2704)
1 parent 643ad0b commit 683a515

13 files changed

Lines changed: 260 additions & 5 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"playground": true,
3+
"docs": false,
4+
"author": "matthewlipski",
5+
"tags": ["Basic"]
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# No Trailing Block
2+
3+
This example shows how to disable the automatic creation of a trailing block at the end of the editor by setting the `trailingBlock` option to `false`.
4+
5+
**Relevant Docs:**
6+
7+
- [Editor Setup](/docs/getting-started/editor-setup)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="UTF-8" />
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
5+
<title>No Trailing Block</title>
6+
<script>
7+
<!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY -->
8+
</script>
9+
</head>
10+
<body>
11+
<div id="root"></div>
12+
<script type="module" src="./main.tsx"></script>
13+
</body>
14+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
2+
import React from "react";
3+
import { createRoot } from "react-dom/client";
4+
import App from "./src/App.jsx";
5+
6+
const root = createRoot(document.getElementById("root")!);
7+
root.render(
8+
<React.StrictMode>
9+
<App />
10+
</React.StrictMode>
11+
);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@blocknote/example-basic-no-trailing-block",
3+
"description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
4+
"type": "module",
5+
"private": true,
6+
"version": "0.12.4",
7+
"scripts": {
8+
"start": "vite",
9+
"dev": "vite",
10+
"build:prod": "tsc && vite build",
11+
"preview": "vite preview"
12+
},
13+
"dependencies": {
14+
"@blocknote/ariakit": "latest",
15+
"@blocknote/core": "latest",
16+
"@blocknote/mantine": "latest",
17+
"@blocknote/react": "latest",
18+
"@blocknote/shadcn": "latest",
19+
"@mantine/core": "^8.3.11",
20+
"@mantine/hooks": "^8.3.11",
21+
"@mantine/utils": "^6.0.22",
22+
"react": "^19.2.3",
23+
"react-dom": "^19.2.3"
24+
},
25+
"devDependencies": {
26+
"@types/react": "^19.2.3",
27+
"@types/react-dom": "^19.2.3",
28+
"@vitejs/plugin-react": "^6.0.1",
29+
"vite": "^8.0.8"
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "@blocknote/core/fonts/inter.css";
2+
import { BlockNoteView } from "@blocknote/mantine";
3+
import "@blocknote/mantine/style.css";
4+
import { useCreateBlockNote } from "@blocknote/react";
5+
6+
export default function App() {
7+
// Creates a new editor instance.
8+
const editor = useCreateBlockNote({
9+
trailingBlock: false,
10+
});
11+
12+
// Renders the editor instance using a React component.
13+
return <BlockNoteView editor={editor} />;
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"__comment": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
3+
"compilerOptions": {
4+
"target": "ESNext",
5+
"useDefineForClassFields": true,
6+
"lib": [
7+
"DOM",
8+
"DOM.Iterable",
9+
"ESNext"
10+
],
11+
"allowJs": false,
12+
"skipLibCheck": true,
13+
"esModuleInterop": false,
14+
"allowSyntheticDefaultImports": true,
15+
"strict": true,
16+
"forceConsistentCasingInFileNames": true,
17+
"module": "ESNext",
18+
"moduleResolution": "bundler",
19+
"resolveJsonModule": true,
20+
"isolatedModules": true,
21+
"noEmit": true,
22+
"jsx": "react-jsx",
23+
"composite": true
24+
},
25+
"include": [
26+
"."
27+
],
28+
"__ADD_FOR_LOCAL_DEV_references": [
29+
{
30+
"path": "../../../packages/core/"
31+
},
32+
{
33+
"path": "../../../packages/react/"
34+
}
35+
]
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
2+
import react from "@vitejs/plugin-react";
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
import { defineConfig } from "vite";
6+
// import eslintPlugin from "vite-plugin-eslint";
7+
// https://vitejs.dev/config/
8+
export default defineConfig((conf) => ({
9+
plugins: [react()],
10+
optimizeDeps: {},
11+
build: {
12+
sourcemap: true,
13+
},
14+
resolve: {
15+
alias:
16+
conf.command === "build" ||
17+
!fs.existsSync(path.resolve(__dirname, "../../packages/core/src"))
18+
? {}
19+
: ({
20+
// Comment out the lines below to load a built version of blocknote
21+
// or, keep as is to load live from sources with live reload working
22+
"@blocknote/core": path.resolve(
23+
__dirname,
24+
"../../packages/core/src/"
25+
),
26+
"@blocknote/react": path.resolve(
27+
__dirname,
28+
"../../packages/react/src/"
29+
),
30+
} as any),
31+
},
32+
}));

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "../../schema/index.js";
1010
import { formatKeyboardShortcut } from "../../util/browser.js";
1111
import { FilePanelExtension } from "../FilePanel/FilePanel.js";
12+
import { FormattingToolbarExtension } from "../FormattingToolbar/FormattingToolbar.js";
1213
import { DefaultSuggestionItem } from "./DefaultSuggestionItem.js";
1314
import { SuggestionMenu } from "./SuggestionMenu.js";
1415

@@ -242,6 +243,11 @@ export function getDefaultSlashMenuItems<
242243

243244
// Immediately open the file toolbar
244245
editor.getExtension(FilePanelExtension)?.showMenu(insertedBlock.id);
246+
// Immediately hide the formatting toolbar. This is only necessary for
247+
// when the `trailingBlock` editor option is set to `false` and the
248+
// inserted block is at the end of the document. Otherwise, the
249+
// selection moves to the next block with inline content.
250+
editor.getExtension(FormattingToolbarExtension)?.store.setState(false);
245251
},
246252
key: "image",
247253
...editor.dictionary.slash_menu.image,
@@ -257,6 +263,11 @@ export function getDefaultSlashMenuItems<
257263

258264
// Immediately open the file toolbar
259265
editor.getExtension(FilePanelExtension)?.showMenu(insertedBlock.id);
266+
// Immediately hide the formatting toolbar. This is only necessary for
267+
// when the `trailingBlock` editor option is set to `false` and the
268+
// inserted block is at the end of the document. Otherwise, the
269+
// selection moves to the next block with inline content.
270+
editor.getExtension(FormattingToolbarExtension)?.store.setState(false);
260271
},
261272
key: "video",
262273
...editor.dictionary.slash_menu.video,
@@ -272,6 +283,11 @@ export function getDefaultSlashMenuItems<
272283

273284
// Immediately open the file toolbar
274285
editor.getExtension(FilePanelExtension)?.showMenu(insertedBlock.id);
286+
// Immediately hide the formatting toolbar. This is only necessary for
287+
// when the `trailingBlock` editor option is set to `false` and the
288+
// inserted block is at the end of the document. Otherwise, the
289+
// selection moves to the next block with inline content.
290+
editor.getExtension(FormattingToolbarExtension)?.store.setState(false);
275291
},
276292
key: "audio",
277293
...editor.dictionary.slash_menu.audio,
@@ -287,6 +303,11 @@ export function getDefaultSlashMenuItems<
287303

288304
// Immediately open the file toolbar
289305
editor.getExtension(FilePanelExtension)?.showMenu(insertedBlock.id);
306+
// Immediately hide the formatting toolbar. This is only necessary for
307+
// when the `trailingBlock` editor option is set to `false` and the
308+
// inserted block is at the end of the document. Otherwise, the
309+
// selection moves to the next block with inline content.
310+
editor.getExtension(FormattingToolbarExtension)?.store.setState(false);
290311
},
291312
key: "file",
292313
...editor.dictionary.slash_menu.file,

playground/src/examples.gen.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,25 @@
323323
},
324324
"readme": "This example makes the editor read-only while showing the same content as the [Default Schema Showcase](/examples/basic/default-blocks) example.\n\n**Relevant Docs:**\n\n- [Editor Setup](/docs/getting-started/editor-setup)\n- [Document Structure](/docs/foundations/document-structure)\n- [Default Schema](/docs/foundations/schemas)"
325325
},
326+
{
327+
"projectSlug": "no-trailing-block",
328+
"fullSlug": "basic/no-trailing-block",
329+
"pathFromRoot": "examples/01-basic/17-no-trailing-block",
330+
"config": {
331+
"playground": true,
332+
"docs": false,
333+
"author": "matthewlipski",
334+
"tags": [
335+
"Basic"
336+
]
337+
},
338+
"title": "No Trailing Block",
339+
"group": {
340+
"pathFromRoot": "examples/01-basic",
341+
"slug": "basic"
342+
},
343+
"readme": "This example shows how to disable the automatic creation of a trailing block at the end of the editor by setting the `trailingBlock` option to `false`.\n\n**Relevant Docs:**\n\n- [Editor Setup](/docs/getting-started/editor-setup)"
344+
},
326345
{
327346
"projectSlug": "testing",
328347
"fullSlug": "basic/testing",

0 commit comments

Comments
 (0)