Skip to content

Commit ff4c8b1

Browse files
Added temp fix for shortcuts and input rules in tables (#561)
1 parent 33d8637 commit ff4c8b1

File tree

5 files changed

+114
-36
lines changed

5 files changed

+114
-36
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Editor } from "@tiptap/core";
2+
import { getBlockInfoFromPos } from "./getBlockInfoFromPos";
3+
4+
// Used to get the content type of the block that the text cursor is in. This is
5+
// a band-aid fix to prevent input rules and keyboard shortcuts from triggering
6+
// in tables, but really those should be extended to work with block selections.
7+
export const getCurrentBlockContentType = (editor: Editor) => {
8+
const { contentType } = getBlockInfoFromPos(
9+
editor.state.doc,
10+
editor.state.selection.from
11+
);
12+
13+
return contentType.spec.content;
14+
};

packages/core/src/blocks/HeadingBlockContent/HeadingBlockContent.ts

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "../../schema";
77
import { createDefaultBlockDOMOutputSpec } from "../defaultBlockHelpers";
88
import { defaultProps } from "../defaultProps";
9+
import { getCurrentBlockContentType } from "../../api/getCurrentBlockContentType";
910

1011
export const headingPropSchema = {
1112
...defaultProps,
@@ -45,6 +46,10 @@ const HeadingBlockContent = createStronglyTypedTiptapNode({
4546
return new InputRule({
4647
find: new RegExp(`^(#{${level}})\\s$`),
4748
handler: ({ state, chain, range }) => {
49+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
50+
return;
51+
}
52+
4853
chain()
4954
.BNUpdateBlock(state.selection.from, {
5055
type: "heading",
@@ -62,27 +67,51 @@ const HeadingBlockContent = createStronglyTypedTiptapNode({
6267

6368
addKeyboardShortcuts() {
6469
return {
65-
"Mod-Alt-1": () =>
66-
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
67-
type: "heading",
68-
props: {
69-
level: 1 as any,
70-
},
71-
}),
72-
"Mod-Alt-2": () =>
73-
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
74-
type: "heading",
75-
props: {
76-
level: 2 as any,
77-
},
78-
}),
79-
"Mod-Alt-3": () =>
80-
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
81-
type: "heading",
82-
props: {
83-
level: 3 as any,
84-
},
85-
}),
70+
"Mod-Alt-1": () => {
71+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
72+
return true;
73+
}
74+
75+
return this.editor.commands.BNUpdateBlock(
76+
this.editor.state.selection.anchor,
77+
{
78+
type: "heading",
79+
props: {
80+
level: 1 as any,
81+
},
82+
}
83+
);
84+
},
85+
"Mod-Alt-2": () => {
86+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
87+
return true;
88+
}
89+
90+
return this.editor.commands.BNUpdateBlock(
91+
this.editor.state.selection.anchor,
92+
{
93+
type: "heading",
94+
props: {
95+
level: 2 as any,
96+
},
97+
}
98+
);
99+
},
100+
"Mod-Alt-3": () => {
101+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
102+
return true;
103+
}
104+
105+
return this.editor.commands.BNUpdateBlock(
106+
this.editor.state.selection.anchor,
107+
{
108+
type: "heading",
109+
props: {
110+
level: 3 as any,
111+
},
112+
}
113+
);
114+
},
86115
};
87116
},
88117
parseHTML() {

packages/core/src/blocks/ListItemBlockContent/BulletListItemBlockContent/BulletListItemBlockContent.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { createDefaultBlockDOMOutputSpec } from "../../defaultBlockHelpers";
88
import { defaultProps } from "../../defaultProps";
99
import { handleEnter } from "../ListItemKeyboardShortcuts";
10+
import { getCurrentBlockContentType } from "../../../api/getCurrentBlockContentType";
1011

1112
export const bulletListItemPropSchema = {
1213
...defaultProps,
@@ -22,6 +23,10 @@ const BulletListItemBlockContent = createStronglyTypedTiptapNode({
2223
new InputRule({
2324
find: new RegExp(`^[-+*]\\s$`),
2425
handler: ({ state, chain, range }) => {
26+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
27+
return;
28+
}
29+
2530
chain()
2631
.BNUpdateBlock(state.selection.from, {
2732
type: "bulletListItem",
@@ -37,11 +42,19 @@ const BulletListItemBlockContent = createStronglyTypedTiptapNode({
3742
addKeyboardShortcuts() {
3843
return {
3944
Enter: () => handleEnter(this.editor),
40-
"Mod-Shift-8": () =>
41-
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
42-
type: "bulletListItem",
43-
props: {},
44-
}),
45+
"Mod-Shift-8": () => {
46+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
47+
return true;
48+
}
49+
50+
return this.editor.commands.BNUpdateBlock(
51+
this.editor.state.selection.anchor,
52+
{
53+
type: "bulletListItem",
54+
props: {},
55+
}
56+
);
57+
},
4558
};
4659
},
4760

packages/core/src/blocks/ListItemBlockContent/NumberedListItemBlockContent/NumberedListItemBlockContent.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createDefaultBlockDOMOutputSpec } from "../../defaultBlockHelpers";
88
import { defaultProps } from "../../defaultProps";
99
import { handleEnter } from "../ListItemKeyboardShortcuts";
1010
import { NumberedListIndexingPlugin } from "./NumberedListIndexingPlugin";
11+
import { getCurrentBlockContentType } from "../../../api/getCurrentBlockContentType";
1112

1213
export const numberedListItemPropSchema = {
1314
...defaultProps,
@@ -37,6 +38,10 @@ const NumberedListItemBlockContent = createStronglyTypedTiptapNode({
3738
new InputRule({
3839
find: new RegExp(`^1\\.\\s$`),
3940
handler: ({ state, chain, range }) => {
41+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
42+
return;
43+
}
44+
4045
chain()
4146
.BNUpdateBlock(state.selection.from, {
4247
type: "numberedListItem",
@@ -52,11 +57,19 @@ const NumberedListItemBlockContent = createStronglyTypedTiptapNode({
5257
addKeyboardShortcuts() {
5358
return {
5459
Enter: () => handleEnter(this.editor),
55-
"Mod-Shift-7": () =>
56-
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
57-
type: "numberedListItem",
58-
props: {},
59-
}),
60+
"Mod-Shift-7": () => {
61+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
62+
return true;
63+
}
64+
65+
return this.editor.commands.BNUpdateBlock(
66+
this.editor.state.selection.anchor,
67+
{
68+
type: "numberedListItem",
69+
props: {},
70+
}
71+
);
72+
},
6073
};
6174
},
6275

packages/core/src/blocks/ParagraphBlockContent/ParagraphBlockContent.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
import { createDefaultBlockDOMOutputSpec } from "../defaultBlockHelpers";
66
import { defaultProps } from "../defaultProps";
77
import { handleEnter } from "../ListItemBlockContent/ListItemKeyboardShortcuts";
8+
import { getCurrentBlockContentType } from "../../api/getCurrentBlockContentType";
89

910
export const paragraphPropSchema = {
1011
...defaultProps,
@@ -18,11 +19,19 @@ export const ParagraphBlockContent = createStronglyTypedTiptapNode({
1819
addKeyboardShortcuts() {
1920
return {
2021
Enter: () => handleEnter(this.editor),
21-
"Mod-Alt-0": () =>
22-
this.editor.commands.BNUpdateBlock(this.editor.state.selection.anchor, {
23-
type: "paragraph",
24-
props: {},
25-
}),
22+
"Mod-Alt-0": () => {
23+
if (getCurrentBlockContentType(this.editor) !== "inline*") {
24+
return true;
25+
}
26+
27+
return this.editor.commands.BNUpdateBlock(
28+
this.editor.state.selection.anchor,
29+
{
30+
type: "paragraph",
31+
props: {},
32+
}
33+
);
34+
},
2635
};
2736
},
2837

0 commit comments

Comments
 (0)