Skip to content

fix: Keyboard shortcuts & input rules in tables #561

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

Merged
merged 1 commit into from
Mar 14, 2024
Merged
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
14 changes: 14 additions & 0 deletions packages/core/src/api/getCurrentBlockContentType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Editor } from "@tiptap/core";
import { getBlockInfoFromPos } from "./getBlockInfoFromPos";

// Used to get the content type of the block that the text cursor is in. This is
// a band-aid fix to prevent input rules and keyboard shortcuts from triggering
// in tables, but really those should be extended to work with block selections.
export const getCurrentBlockContentType = (editor: Editor) => {
const { contentType } = getBlockInfoFromPos(
editor.state.doc,
editor.state.selection.from
);

return contentType.spec.content;
};
71 changes: 50 additions & 21 deletions packages/core/src/blocks/HeadingBlockContent/HeadingBlockContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "../../schema";
import { createDefaultBlockDOMOutputSpec } from "../defaultBlockHelpers";
import { defaultProps } from "../defaultProps";
import { getCurrentBlockContentType } from "../../api/getCurrentBlockContentType";

export const headingPropSchema = {
...defaultProps,
Expand Down Expand Up @@ -45,6 +46,10 @@ const HeadingBlockContent = createStronglyTypedTiptapNode({
return new InputRule({
find: new RegExp(`^(#{${level}})\\s$`),
handler: ({ state, chain, range }) => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return;
}

chain()
.BNUpdateBlock(state.selection.from, {
type: "heading",
Expand All @@ -62,27 +67,51 @@ const HeadingBlockContent = createStronglyTypedTiptapNode({

addKeyboardShortcuts() {
return {
"Mod-Alt-1": () =>
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
type: "heading",
props: {
level: 1 as any,
},
}),
"Mod-Alt-2": () =>
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
type: "heading",
props: {
level: 2 as any,
},
}),
"Mod-Alt-3": () =>
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
type: "heading",
props: {
level: 3 as any,
},
}),
"Mod-Alt-1": () => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return true;
}

return this.editor.commands.BNUpdateBlock(
this.editor.state.selection.anchor,
{
type: "heading",
props: {
level: 1 as any,
},
}
);
},
"Mod-Alt-2": () => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return true;
}

return this.editor.commands.BNUpdateBlock(
this.editor.state.selection.anchor,
{
type: "heading",
props: {
level: 2 as any,
},
}
);
},
"Mod-Alt-3": () => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return true;
}

return this.editor.commands.BNUpdateBlock(
this.editor.state.selection.anchor,
{
type: "heading",
props: {
level: 3 as any,
},
}
);
},
};
},
parseHTML() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { createDefaultBlockDOMOutputSpec } from "../../defaultBlockHelpers";
import { defaultProps } from "../../defaultProps";
import { handleEnter } from "../ListItemKeyboardShortcuts";
import { getCurrentBlockContentType } from "../../../api/getCurrentBlockContentType";

export const bulletListItemPropSchema = {
...defaultProps,
Expand All @@ -22,6 +23,10 @@ const BulletListItemBlockContent = createStronglyTypedTiptapNode({
new InputRule({
find: new RegExp(`^[-+*]\\s$`),
handler: ({ state, chain, range }) => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return;
}

chain()
.BNUpdateBlock(state.selection.from, {
type: "bulletListItem",
Expand All @@ -37,11 +42,19 @@ const BulletListItemBlockContent = createStronglyTypedTiptapNode({
addKeyboardShortcuts() {
return {
Enter: () => handleEnter(this.editor),
"Mod-Shift-8": () =>
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
type: "bulletListItem",
props: {},
}),
"Mod-Shift-8": () => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return true;
}

return this.editor.commands.BNUpdateBlock(
this.editor.state.selection.anchor,
{
type: "bulletListItem",
props: {},
}
);
},
};
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createDefaultBlockDOMOutputSpec } from "../../defaultBlockHelpers";
import { defaultProps } from "../../defaultProps";
import { handleEnter } from "../ListItemKeyboardShortcuts";
import { NumberedListIndexingPlugin } from "./NumberedListIndexingPlugin";
import { getCurrentBlockContentType } from "../../../api/getCurrentBlockContentType";

export const numberedListItemPropSchema = {
...defaultProps,
Expand Down Expand Up @@ -37,6 +38,10 @@ const NumberedListItemBlockContent = createStronglyTypedTiptapNode({
new InputRule({
find: new RegExp(`^1\\.\\s$`),
handler: ({ state, chain, range }) => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return;
}

chain()
.BNUpdateBlock(state.selection.from, {
type: "numberedListItem",
Expand All @@ -52,11 +57,19 @@ const NumberedListItemBlockContent = createStronglyTypedTiptapNode({
addKeyboardShortcuts() {
return {
Enter: () => handleEnter(this.editor),
"Mod-Shift-7": () =>
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
type: "numberedListItem",
props: {},
}),
"Mod-Shift-7": () => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return true;
}

return this.editor.commands.BNUpdateBlock(
this.editor.state.selection.anchor,
{
type: "numberedListItem",
props: {},
}
);
},
};
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { createDefaultBlockDOMOutputSpec } from "../defaultBlockHelpers";
import { defaultProps } from "../defaultProps";
import { handleEnter } from "../ListItemBlockContent/ListItemKeyboardShortcuts";
import { getCurrentBlockContentType } from "../../api/getCurrentBlockContentType";

export const paragraphPropSchema = {
...defaultProps,
Expand All @@ -18,11 +19,19 @@ export const ParagraphBlockContent = createStronglyTypedTiptapNode({
addKeyboardShortcuts() {
return {
Enter: () => handleEnter(this.editor),
"Mod-Alt-0": () =>
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
type: "paragraph",
props: {},
}),
"Mod-Alt-0": () => {
if (getCurrentBlockContentType(this.editor) !== "inline*") {
return true;
}

return this.editor.commands.BNUpdateBlock(
this.editor.state.selection.anchor,
{
type: "paragraph",
props: {},
}
);
},
};
},

Expand Down