Skip to content

feat: Input rule scoping #1799

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

Open
wants to merge 1 commit 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
5 changes: 4 additions & 1 deletion docs/pages/docs/editor-basics/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ type BlockNoteEditorOptions = {
levels?: number[];
};
initialContent?: PartialBlock[];
inputRules: "allBlocks" | "paragraphs" | "none";
pasteHandler?: (context: {
event: ClipboardEvent;
editor: BlockNoteEditor;
defaultPasteHandler: (context: {
pasteBehavior?: "prefer-markdown" | "prefer-html";
}) => boolean | undefined;
}) => boolean | undefined;
resolveFileUrl: (url: string) => Promise<string>
resolveFileUrl: (url: string) => Promise<string>;
schema?: BlockNoteSchema;
setIdAttribute?: boolean;
sideMenuDetection?: "viewport" | "editor";
Expand Down Expand Up @@ -76,6 +77,8 @@ The hook takes two optional parameters:

`initialContent:` The content that should be in the editor when it's created, represented as an array of [Partial Blocks](/docs/manipulating-blocks#partial-blocks).

`inputRules`: Configures when input rules should be active. Input rules change the block type when a string is typed at the start of the block. For example, typing "# " changes the block to a heading level 1 and "- " changes it to a bullet list item. "allBlocks" means the input rules are active regardless of the initial block type, "paragraphs" means the input rules are only active within paragraph blocks, and "none" means the input rules are never active. Defaults to "allBlocks".

`heading`: Configuration for headings. Allows you to configure the number of levels of headings that should be available in the editor. Defaults to `[1, 2, 3]`. Configurable up to 6 levels of headings.

`pasteHandler`: A function that can be used to override the default paste behavior. See [Paste Handling](/docs/advanced/paste-handling) for more.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const HeadingBlockContent = createStronglyTypedTiptapNode({
const blockInfo = getBlockInfoFromSelection(state);
if (
!blockInfo.isBlockContainer ||
blockInfo.blockContent.node.type.spec.content !== "inline*"
blockInfo.blockContent.node.type.spec.content !== "inline*" ||
this.options.inputRules === "none" ||
(this.options.inputRules === "paragraphs" &&
blockInfo.blockNoteType !== "paragraph")
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ const BulletListItemBlockContent = createStronglyTypedTiptapNode({
const blockInfo = getBlockInfoFromSelection(state);
if (
!blockInfo.isBlockContainer ||
blockInfo.blockContent.node.type.spec.content !== "inline*"
blockInfo.blockContent.node.type.spec.content !== "inline*" ||
this.options.inputRules === "none" ||
(this.options.inputRules === "paragraphs" &&
blockInfo.blockNoteType !== "paragraph")
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ const checkListItemBlockContent = createStronglyTypedTiptapNode({
const blockInfo = getBlockInfoFromSelection(state);
if (
!blockInfo.isBlockContainer ||
blockInfo.blockContent.node.type.spec.content !== "inline*"
blockInfo.blockContent.node.type.spec.content !== "inline*" ||
this.options.inputRules === "none" ||
(this.options.inputRules === "paragraphs" &&
blockInfo.blockNoteType !== "paragraph")
) {
return;
}
Expand All @@ -65,7 +68,10 @@ const checkListItemBlockContent = createStronglyTypedTiptapNode({

if (
!blockInfo.isBlockContainer ||
blockInfo.blockContent.node.type.spec.content !== "inline*"
blockInfo.blockContent.node.type.spec.content !== "inline*" ||
this.options.inputRules === "none" ||
(this.options.inputRules === "paragraphs" &&
blockInfo.blockNoteType !== "paragraph")
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ const NumberedListItemBlockContent = createStronglyTypedTiptapNode({
if (
!blockInfo.isBlockContainer ||
blockInfo.blockContent.node.type.spec.content !== "inline*" ||
blockInfo.blockNoteType === "numberedListItem"
this.options.inputRules === "none" ||
(this.options.inputRules === "paragraphs" &&
blockInfo.blockNoteType !== "paragraph")
) {
return;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ export type BlockNoteEditorOptions<
NoInfer<SSchema>
>[];

/**
* Configures when input rules should be active. Input rules change the block
* type when a string is typed at the start of the block. For example, typing
* "# " changes the block to a heading level 1 and "- " changes it to a
* bullet list item. "allBlocks" means the input rules are active regardless
* of the initial block type, "paragraphs" means the input rules are only
* active within paragraph blocks, and "none" means the input rules are never
* active. Defaults to "allBlocks".
*/
inputRules: "allBlocks" | "paragraphs" | "none";

/**
* @deprecated, provide placeholders via dictionary instead
*/
Expand Down Expand Up @@ -642,6 +653,7 @@ export class BlockNoteEditor<
sideMenuDetection: newOptions.sideMenuDetection || "viewport",
comments: newOptions.comments,
pasteHandler: newOptions.pasteHandler,
inputRules: newOptions.inputRules || "allBlocks",
});

// add extensions from _tiptapOptions
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/editor/BlockNoteExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type ExtensionOptions<
threadStore: ThreadStore;
};
pasteHandler: BlockNoteEditorOptions<any, any, any>["pasteHandler"];
inputRules: "allBlocks" | "paragraphs" | "none";
};

/**
Expand Down Expand Up @@ -286,6 +287,7 @@ const getTipTapExtensions = <
blockSpec.implementation.node.configure({
editor: opts.editor,
domAttributes: opts.domAttributes,
inputRules: opts.inputRules,
}),
];
}),
Expand Down
Loading