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

Added editor option to disable nested blocks #408

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions packages/core/src/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export type BlockNoteEditorOptions<BSchema extends BlockSchema> = {
* Locks the editor from being editable by the user if set to `false`.
*/
editable: boolean;
/**
* Disables block nesting by the user if set to `false`.
*/
enableNestedBlocks: boolean;
/**
* The content that should be in the editor when it's created, represented as an array of partial block objects.
*/
Expand Down Expand Up @@ -154,6 +158,7 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
public blockCache = new WeakMap<Node, Block<BSchema>>();
public readonly schema: BSchema;
public ready = false;
public enableNestedBlocks = true;

public readonly sideMenu: SideMenuProsemirrorPlugin<BSchema>;
public readonly formattingToolbar: FormattingToolbarProsemirrorPlugin<BSchema>;
Expand Down Expand Up @@ -190,6 +195,10 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
);
this.hyperlinkToolbar = new HyperlinkToolbarProsemirrorPlugin(this);
this.imageToolbar = new ImageToolbarProsemirrorPlugin(this);
this.enableNestedBlocks =
options.enableNestedBlocks !== undefined
? options.enableNestedBlocks
: true;

const extensions = getBlockNoteExtensions<BSchema>({
editor: this,
Expand Down Expand Up @@ -755,6 +764,10 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
* Checks if the block containing the text cursor can be nested.
*/
public canNestBlock() {
if (!this.enableNestedBlocks) {
return false;
}

const { startPos, depth } = getBlockInfoFromPos(
this._tiptapEditor.state.doc,
this._tiptapEditor.state.selection.from
Expand All @@ -774,6 +787,10 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
* Checks if the block containing the text cursor is nested.
*/
public canUnnestBlock() {
if (!this.enableNestedBlocks) {
return false;
}

const { depth } = getBlockInfoFromPos(
this._tiptapEditor.state.doc,
this._tiptapEditor.state.selection.from
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/BlockNoteExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const getBlockNoteExtensions = <BSchema extends BlockSchema>(opts: {
Doc,
BlockContainer.configure({
domAttributes: opts.domAttributes,
enableNestedBlocks: opts.editor.enableNestedBlocks,
}),
BlockGroup.configure({
domAttributes: opts.domAttributes,
Expand Down
19 changes: 16 additions & 3 deletions packages/core/src/extensions/Blocks/nodes/BlockContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,18 @@ declare module "@tiptap/core" {
*/
export const BlockContainer = Node.create<{
domAttributes?: BlockNoteDOMAttributes;
enableNestedBlocks?: boolean;
}>({
name: "blockContainer",
group: "blockContainer",
// A block always contains content, and optionally a blockGroup which contains nested blocks
content: "blockContent blockGroup?",
content() {
if (!this.options.enableNestedBlocks) {
return "blockContent";
}

return "blockContent blockGroup?";
},
// Ensures content-specific keyboard handlers trigger first.
priority: 50,
defining: true,
Expand Down Expand Up @@ -621,11 +628,17 @@ export const BlockContainer = Node.create<{
// Always returning true for tab key presses ensures they're not captured by the browser. Otherwise, they blur the
// editor since the browser will try to use tab for keyboard navigation.
Tab: () => {
this.editor.commands.sinkListItem("blockContainer");
if (this.options.enableNestedBlocks) {
this.editor.commands.sinkListItem("blockContainer");
}

return true;
},
"Shift-Tab": () => {
this.editor.commands.liftListItem("blockContainer");
if (this.options.enableNestedBlocks) {
this.editor.commands.liftListItem("blockContainer");
}

return true;
},
"Mod-Alt-0": () =>
Expand Down