Skip to content

Commit 26b9b1c

Browse files
committed
Removed codeBlock and heading editor options & updated custom code block examples
1 parent 5e3cad6 commit 26b9b1c

File tree

11 files changed

+135
-86
lines changed

11 files changed

+135
-86
lines changed

examples/04-theming/06-code-block/src/App.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ import { BlockNoteView } from "@blocknote/mantine";
33
import "@blocknote/mantine/style.css";
44
import { useCreateBlockNote } from "@blocknote/react";
55
// This packages some of the most used languages in on-demand bundle
6-
import { codeBlock } from "@blocknote/code-block";
6+
import { codeBlock as cb } from "@blocknote/code-block";
7+
import {
8+
BlockNoteSchema,
9+
createCodeBlockSpec,
10+
defaultBlockSpecs,
11+
} from "@blocknote/core";
712

813
export default function App() {
914
// Creates a new editor instance.
1015
const editor = useCreateBlockNote({
11-
codeBlock,
16+
schema: BlockNoteSchema.create({
17+
blockSpecs: {
18+
...defaultBlockSpecs,
19+
codeBlock: createCodeBlockSpec(cb),
20+
},
21+
}),
1222
initialContent: [
1323
{
1424
type: "codeBlock",

examples/04-theming/07-custom-code-block/src/App.tsx

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,43 @@ import "@blocknote/mantine/style.css";
44
import { useCreateBlockNote } from "@blocknote/react";
55
// Bundle created from `npx shiki-codegen --langs typescript,javascript,react --themes light-plus,dark-plus --engine javascript --precompiled ./shiki.bundle.ts`
66
import { createHighlighter } from "./shiki.bundle";
7+
import {
8+
BlockNoteSchema,
9+
createCodeBlockSpec,
10+
defaultBlockSpecs,
11+
} from "@blocknote/core";
712

813
export default function App() {
914
// Creates a new editor instance.
1015
const editor = useCreateBlockNote({
11-
codeBlock: {
12-
indentLineWithTab: true,
13-
defaultLanguage: "typescript",
14-
supportedLanguages: {
15-
typescript: {
16-
name: "TypeScript",
17-
aliases: ["ts"],
18-
},
19-
javascript: {
20-
name: "JavaScript",
21-
aliases: ["js"],
22-
},
23-
vue: {
24-
name: "Vue",
25-
},
26-
},
27-
// This creates a highlighter, it can be asynchronous to load it afterwards
28-
createHighlighter: () =>
29-
createHighlighter({
30-
themes: ["dark-plus", "light-plus"],
31-
langs: [],
16+
schema: BlockNoteSchema.create({
17+
blockSpecs: {
18+
...defaultBlockSpecs,
19+
codeBlock: createCodeBlockSpec({
20+
indentLineWithTab: true,
21+
defaultLanguage: "typescript",
22+
supportedLanguages: {
23+
typescript: {
24+
name: "TypeScript",
25+
aliases: ["ts"],
26+
},
27+
javascript: {
28+
name: "JavaScript",
29+
aliases: ["js"],
30+
},
31+
vue: {
32+
name: "Vue",
33+
},
34+
},
35+
// This creates a highlighter, it can be asynchronous to load it afterwards
36+
createHighlighter: () =>
37+
createHighlighter({
38+
themes: ["dark-plus", "light-plus"],
39+
langs: [],
40+
}),
3241
}),
33-
},
42+
},
43+
}),
3444
initialContent: [
3545
{
3646
type: "codeBlock",

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ import {
7070
markdownToBlocks,
7171
markdownToHTML,
7272
} from "../api/parsers/markdown/parseMarkdown.js";
73-
import { CodeBlockOptions } from "../blocks/Code/block.js";
7473
import { editorHasBlockWithType } from "../blocks/defaultBlockTypeGuards.js";
7574
import type { ThreadStore, User } from "../comments/index.js";
7675
import { BlockChangePlugin } from "../extensions/BlockChange/BlockChangePlugin.js";
@@ -190,11 +189,6 @@ export type BlockNoteEditorOptions<
190189
showCursorLabels?: "always" | "activity";
191190
};
192191

193-
/**
194-
* Options for code blocks.
195-
*/
196-
codeBlock?: CodeBlockOptions;
197-
198192
/**
199193
* Configuration for the comments feature, requires a `threadStore`.
200194
*
@@ -253,18 +247,6 @@ export type BlockNoteEditorOptions<
253247
class?: string;
254248
}) => Plugin;
255249

256-
/**
257-
* Configuration for headings
258-
*/
259-
heading?: {
260-
/**
261-
* The levels of headings that should be available in the editor.
262-
* @note Configurable up to 6 levels of headings.
263-
* @default [1, 2, 3]
264-
*/
265-
levels?: (1 | 2 | 3 | 4 | 5 | 6)[];
266-
};
267-
268250
/**
269251
* The content that should be in the editor when it's created, represented as an array of {@link PartialBlock} objects.
270252
*
@@ -585,10 +567,6 @@ export class BlockNoteEditor<
585567
cellTextColor: boolean;
586568
headers: boolean;
587569
};
588-
codeBlock: CodeBlockOptions;
589-
heading: {
590-
levels: (1 | 2 | 3 | 4 | 5 | 6)[];
591-
};
592570
};
593571

594572
public static create<
@@ -638,15 +616,6 @@ export class BlockNoteEditor<
638616
cellTextColor: options?.tables?.cellTextColor ?? false,
639617
headers: options?.tables?.headers ?? false,
640618
},
641-
codeBlock: {
642-
indentLineWithTab: options?.codeBlock?.indentLineWithTab ?? true,
643-
defaultLanguage: options?.codeBlock?.defaultLanguage ?? "text",
644-
supportedLanguages: options?.codeBlock?.supportedLanguages ?? {},
645-
createHighlighter: options?.codeBlock?.createHighlighter ?? undefined,
646-
},
647-
heading: {
648-
levels: options?.heading?.levels ?? [1, 2, 3],
649-
},
650619
};
651620

652621
// apply defaults

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

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,51 @@ export function getDefaultSlashMenuItems<
354354
...editor.dictionary.slash_menu.toggle_heading_3,
355355
},
356356
);
357+
}
357358

358-
editor.settings.heading.levels
359-
.filter((level): level is 4 | 5 | 6 => level > 3)
360-
.forEach((level) => {
361-
items.push({
362-
onItemClick: () => {
363-
insertOrUpdateBlock(editor, {
364-
type: "heading",
365-
props: { level: level },
366-
});
367-
},
368-
key: `heading_${level}`,
369-
...editor.dictionary.slash_menu[`heading_${level}`],
370-
});
371-
});
359+
if (
360+
editorHasBlockWithType(editor, "heading", {
361+
level: {
362+
default: 1,
363+
values: [1, 2, 3, 4, 5, 6],
364+
},
365+
})
366+
) {
367+
items.push(
368+
{
369+
onItemClick: () => {
370+
insertOrUpdateBlock(editor, {
371+
type: "heading",
372+
props: { level: 4 },
373+
});
374+
},
375+
badge: formatKeyboardShortcut("Mod-Alt-4"),
376+
key: "heading_4",
377+
...editor.dictionary.slash_menu.heading_4,
378+
},
379+
{
380+
onItemClick: () => {
381+
insertOrUpdateBlock(editor, {
382+
type: "heading",
383+
props: { level: 5 },
384+
});
385+
},
386+
badge: formatKeyboardShortcut("Mod-Alt-5"),
387+
key: "heading_5",
388+
...editor.dictionary.slash_menu.heading_5,
389+
},
390+
{
391+
onItemClick: () => {
392+
insertOrUpdateBlock(editor, {
393+
type: "heading",
394+
props: { level: 6 },
395+
});
396+
},
397+
// badge: formatKeyboardShortcut("Mod-Alt-6"),
398+
key: "heading_6",
399+
...editor.dictionary.slash_menu.heading_6,
400+
},
401+
);
372402
}
373403

374404
items.push({

packages/core/src/schema/blocks/createSpec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ export function createBlockSpec<
307307
| (TOptions extends undefined
308308
? () => BlockNoteExtension<any>[]
309309
: (options: Partial<TOptions>) => BlockNoteExtension<any>[]),
310-
): (options?: TOptions) => BlockSpec<TName, TProps, TContent> {
311-
return (options = {} as TOptions) => {
310+
): (options?: Partial<TOptions>) => BlockSpec<TName, TProps, TContent> {
311+
return (options = {} as Partial<TOptions>) => {
312312
const blockConfig =
313313
typeof blockConfigOrCreator === "function"
314314
? blockConfigOrCreator(options as any)

tests/src/unit/core/createTestEditor.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@ export const createTestEditor = <
2323
(window as any).__TEST_OPTIONS = (window as any).__TEST_OPTIONS || {};
2424

2525
editor = BlockNoteEditor.create({
26-
codeBlock: {
27-
supportedLanguages: {
28-
javascript: {
29-
name: "JavaScript",
30-
aliases: ["js"],
31-
},
32-
python: {
33-
name: "Python",
34-
aliases: ["py"],
35-
},
36-
},
37-
},
38-
heading: {
39-
levels: [1, 2, 3, 4, 5, 6],
40-
},
4126
schema,
4227
trailingBlock: false,
4328
uploadFile: uploadToTmpFilesDotOrg_DEV_ONLY,

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/codeBlock/contains-newlines.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
data-content-type="codeBlock"
77
data-language="javascript"
88
>
9+
<div>
10+
<select>
11+
<option value="javascript">JavaScript</option>
12+
<option value="python">Python</option>
13+
</select>
14+
</div>
915
<pre>
1016
<code class="bn-inline-content">const hello ='world';console.log(hello);</code>
1117
</pre>

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/codeBlock/defaultLanguage.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
33
<div class="bn-block" data-node-type="blockContainer" data-id="1">
44
<div class="bn-block-content" data-content-type="codeBlock">
5+
<div>
6+
<select>
7+
<option value="javascript">JavaScript</option>
8+
<option value="python">Python</option>
9+
</select>
10+
</div>
511
<pre>
612
<code class="bn-inline-content">console.log('Hello, world!');</code>
713
</pre>

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/codeBlock/empty.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
33
<div class="bn-block" data-node-type="blockContainer" data-id="1">
44
<div class="bn-block-content" data-content-type="codeBlock">
5+
<div>
6+
<select>
7+
<option value="javascript">JavaScript</option>
8+
<option value="python">Python</option>
9+
</select>
10+
</div>
511
<pre>
612
<code class="bn-inline-content"></code>
713
</pre>

tests/src/unit/core/formatConversion/export/__snapshots__/blocknoteHTML/codeBlock/python.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
data-content-type="codeBlock"
77
data-language="python"
88
>
9+
<div>
10+
<select>
11+
<option value="javascript">JavaScript</option>
12+
<option value="python">Python</option>
13+
</select>
14+
</div>
915
<pre>
1016
<code class="bn-inline-content">print('Hello, world!')</code>
1117
</pre>

0 commit comments

Comments
 (0)