Skip to content

Commit 4b40c57

Browse files
committed
refactor: use higher-level APIs
1 parent 52c7075 commit 4b40c57

File tree

7 files changed

+106
-128
lines changed

7 files changed

+106
-128
lines changed

packages/core/src/blocks/Heading/block.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { updateBlockTr } from "../../api/blockManipulation/commands/updateBlock/updateBlock.js";
2-
import { getBlockInfoFromTransaction } from "../../api/getBlockInfoFromPos.js";
31
import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
42
import { createBlockNoteExtension } from "../../editor/BlockNoteExtension.js";
53
import { defaultProps } from "../defaultProps.js";
@@ -96,27 +94,24 @@ export const createHeadingBlockSpec = createBlockSpec(
9694
keyboardShortcuts: Object.fromEntries(
9795
levels.map((level) => [
9896
`Mod-Alt-${level}`,
99-
({ editor }) =>
100-
editor.transact((tr) => {
101-
// TODO this is weird, why do we need it?
102-
// https://github.com/TypeCellOS/BlockNote/pull/561
103-
const blockInfo = getBlockInfoFromTransaction(tr);
97+
({ editor }) => {
98+
const cursorPosition = editor.getTextCursorPosition();
10499

105-
if (
106-
!blockInfo.isBlockContainer ||
107-
blockInfo.blockContent.node.type.spec.content !== "inline*"
108-
) {
109-
return true;
110-
}
100+
if (
101+
editor.schema.blockSchema[cursorPosition.block.type].content !==
102+
"inline"
103+
) {
104+
return false;
105+
}
111106

112-
updateBlockTr(tr, blockInfo.bnBlock.beforePos, {
113-
type: "heading",
114-
props: {
115-
level: level as any,
116-
},
117-
});
118-
return true;
119-
}),
107+
editor.updateBlock(cursorPosition.block, {
108+
type: "heading",
109+
props: {
110+
level: level as any,
111+
},
112+
});
113+
return true;
114+
},
120115
]) ?? [],
121116
),
122117
inputRules: levels.map((level) => ({

packages/core/src/blocks/ListItem/BulletListItem/block.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { updateBlockTr } from "../../../api/blockManipulation/commands/updateBlock/updateBlock.js";
2-
import { getBlockInfoFromTransaction } from "../../../api/getBlockInfoFromPos.js";
3-
import { createBlockConfig, createBlockSpec } from "../../../schema/index.js";
41
import { createBlockNoteExtension } from "../../../editor/BlockNoteExtension.js";
2+
import { createBlockConfig, createBlockSpec } from "../../../schema/index.js";
53
import { defaultProps } from "../../defaultProps.js";
64
import { handleEnter } from "../../utils/listItemEnterHandler.js";
75
import { getListItemContent } from "../getListItemContent.js";
@@ -66,23 +64,22 @@ export const createBulletListItemBlockSpec = createBlockSpec(
6664
Enter: ({ editor }) => {
6765
return handleEnter(editor, "bulletListItem");
6866
},
69-
"Mod-Shift-8": ({ editor }) =>
70-
editor.transact((tr) => {
71-
const blockInfo = getBlockInfoFromTransaction(tr);
67+
"Mod-Shift-8": ({ editor }) => {
68+
const cursorPosition = editor.getTextCursorPosition();
7269

73-
if (
74-
!blockInfo.isBlockContainer ||
75-
blockInfo.blockContent.node.type.spec.content !== "inline*"
76-
) {
77-
return true;
78-
}
70+
if (
71+
editor.schema.blockSchema[cursorPosition.block.type].content !==
72+
"inline"
73+
) {
74+
return false;
75+
}
7976

80-
updateBlockTr(tr, blockInfo.bnBlock.beforePos, {
81-
type: "bulletListItem",
82-
props: {},
83-
});
84-
return true;
85-
}),
77+
editor.updateBlock(cursorPosition.block, {
78+
type: "bulletListItem",
79+
props: {},
80+
});
81+
return true;
82+
},
8683
},
8784
inputRules: [
8885
{

packages/core/src/blocks/ListItem/CheckListItem/block.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { updateBlockTr } from "../../../api/blockManipulation/commands/updateBlock/updateBlock.js";
2-
import { getBlockInfoFromTransaction } from "../../../api/getBlockInfoFromPos.js";
3-
import { createBlockConfig, createBlockSpec } from "../../../schema/index.js";
41
import { createBlockNoteExtension } from "../../../editor/BlockNoteExtension.js";
2+
import { createBlockConfig, createBlockSpec } from "../../../schema/index.js";
53
import { defaultProps } from "../../defaultProps.js";
64
import { handleEnter } from "../../utils/listItemEnterHandler.js";
75
import { getListItemContent } from "../getListItemContent.js";
@@ -97,23 +95,22 @@ export const createCheckListItemBlockSpec = createBlockSpec(
9795
Enter: ({ editor }) => {
9896
return handleEnter(editor, "checkListItem");
9997
},
100-
"Mod-Shift-9": ({ editor }) =>
101-
editor.transact((tr) => {
102-
const blockInfo = getBlockInfoFromTransaction(tr);
98+
"Mod-Shift-9": ({ editor }) => {
99+
const cursorPosition = editor.getTextCursorPosition();
103100

104-
if (
105-
!blockInfo.isBlockContainer ||
106-
blockInfo.blockContent.node.type.spec.content !== "inline*"
107-
) {
108-
return true;
109-
}
101+
if (
102+
editor.schema.blockSchema[cursorPosition.block.type].content !==
103+
"inline"
104+
) {
105+
return false;
106+
}
110107

111-
updateBlockTr(tr, blockInfo.bnBlock.beforePos, {
112-
type: "checkListItem",
113-
props: {},
114-
});
115-
return true;
116-
}),
108+
editor.updateBlock(cursorPosition.block, {
109+
type: "checkListItem",
110+
props: {},
111+
});
112+
return true;
113+
},
117114
},
118115
inputRules: [
119116
{

packages/core/src/blocks/ListItem/NumberedListItem/block.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { updateBlockTr } from "../../../api/blockManipulation/commands/updateBlock/updateBlock.js";
2-
import { getBlockInfoFromTransaction } from "../../../api/getBlockInfoFromPos.js";
3-
import { createBlockConfig, createBlockSpec } from "../../../schema/index.js";
41
import { createBlockNoteExtension } from "../../../editor/BlockNoteExtension.js";
2+
import { createBlockConfig, createBlockSpec } from "../../../schema/index.js";
53
import { defaultProps } from "../../defaultProps.js";
64
import { handleEnter } from "../../utils/listItemEnterHandler.js";
75
import { getListItemContent } from "../getListItemContent.js";
@@ -81,23 +79,22 @@ export const createNumberedListItemBlockSpec = createBlockSpec(
8179
Enter: ({ editor }) => {
8280
return handleEnter(editor, "numberedListItem");
8381
},
84-
"Mod-Shift-7": ({ editor }) =>
85-
editor.transact((tr) => {
86-
const blockInfo = getBlockInfoFromTransaction(tr);
82+
"Mod-Shift-7": ({ editor }) => {
83+
const cursorPosition = editor.getTextCursorPosition();
8784

88-
if (
89-
!blockInfo.isBlockContainer ||
90-
blockInfo.blockContent.node.type.spec.content !== "inline*"
91-
) {
92-
return true;
93-
}
85+
if (
86+
editor.schema.blockSchema[cursorPosition.block.type].content !==
87+
"inline"
88+
) {
89+
return false;
90+
}
9491

95-
updateBlockTr(tr, blockInfo.bnBlock.beforePos, {
96-
type: "numberedListItem",
97-
props: {},
98-
});
99-
return true;
100-
}),
92+
editor.updateBlock(cursorPosition.block, {
93+
type: "numberedListItem",
94+
props: {},
95+
});
96+
return true;
97+
},
10198
},
10299
plugins: [NumberedListIndexingDecorationPlugin()],
103100
}),

packages/core/src/blocks/ListItem/ToggleListItem/block.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { updateBlockTr } from "../../../api/blockManipulation/commands/updateBlock/updateBlock.js";
2-
import { getBlockInfoFromTransaction } from "../../../api/getBlockInfoFromPos.js";
3-
import { createBlockConfig, createBlockSpec } from "../../../schema/index.js";
41
import { createBlockNoteExtension } from "../../../editor/BlockNoteExtension.js";
2+
import { createBlockConfig, createBlockSpec } from "../../../schema/index.js";
53
import { defaultProps } from "../../defaultProps.js";
64
import { createToggleWrapper } from "../../ToggleWrapper/createToggleWrapper.js";
75
import { handleEnter } from "../../utils/listItemEnterHandler.js";
@@ -44,23 +42,22 @@ export const createToggleListItemBlockSpec = createBlockSpec(
4442
Enter: ({ editor }) => {
4543
return handleEnter(editor, "toggleListItem");
4644
},
47-
"Mod-Shift-6": ({ editor }) =>
48-
editor.transact((tr) => {
49-
const blockInfo = getBlockInfoFromTransaction(tr);
45+
"Mod-Shift-6": ({ editor }) => {
46+
const cursorPosition = editor.getTextCursorPosition();
5047

51-
if (
52-
!blockInfo.isBlockContainer ||
53-
blockInfo.blockContent.node.type.spec.content !== "inline*"
54-
) {
55-
return true;
56-
}
48+
if (
49+
editor.schema.blockSchema[cursorPosition.block.type].content !==
50+
"inline"
51+
) {
52+
return false;
53+
}
5754

58-
updateBlockTr(tr, blockInfo.bnBlock.beforePos, {
59-
type: "toggleListItem",
60-
props: {},
61-
});
62-
return true;
63-
}),
55+
editor.updateBlock(cursorPosition.block, {
56+
type: "toggleListItem",
57+
props: {},
58+
});
59+
return true;
60+
},
6461
},
6562
}),
6663
],

packages/core/src/blocks/Paragraph/block.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { updateBlockTr } from "../../api/blockManipulation/commands/updateBlock/updateBlock.js";
2-
import { getBlockInfoFromTransaction } from "../../api/getBlockInfoFromPos.js";
3-
import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
41
import { createBlockNoteExtension } from "../../editor/BlockNoteExtension.js";
2+
import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
53
import { defaultProps } from "../defaultProps.js";
64

75
export const createParagraphBlockConfig = createBlockConfig(
@@ -44,23 +42,22 @@ export const createParagraphBlockSpec = createBlockSpec(
4442
createBlockNoteExtension({
4543
key: "paragraph-shortcuts",
4644
keyboardShortcuts: {
47-
"Mod-Alt-0": ({ editor }) =>
48-
editor.transact((tr) => {
49-
const blockInfo = getBlockInfoFromTransaction(tr);
45+
"Mod-Alt-0": ({ editor }) => {
46+
const cursorPosition = editor.getTextCursorPosition();
5047

51-
if (
52-
!blockInfo.isBlockContainer ||
53-
blockInfo.blockContent.node.type.spec.content !== "inline*"
54-
) {
55-
return true;
56-
}
48+
if (
49+
editor.schema.blockSchema[cursorPosition.block.type].content !==
50+
"inline"
51+
) {
52+
return false;
53+
}
5754

58-
updateBlockTr(tr, blockInfo.bnBlock.beforePos, {
59-
type: "paragraph",
60-
props: {},
61-
});
62-
return true;
63-
}),
55+
editor.updateBlock(cursorPosition.block, {
56+
type: "paragraph",
57+
props: {},
58+
});
59+
return true;
60+
},
6461
},
6562
}),
6663
],

packages/core/src/blocks/Quote/block.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { updateBlockTr } from "../../api/blockManipulation/commands/updateBlock/updateBlock.js";
2-
import { getBlockInfoFromTransaction } from "../../api/getBlockInfoFromPos.js";
3-
import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
41
import { createBlockNoteExtension } from "../../editor/BlockNoteExtension.js";
2+
import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
53
import { defaultProps } from "../defaultProps.js";
64

75
export const createQuoteBlockConfig = createBlockConfig(
@@ -42,22 +40,22 @@ export const createQuoteBlockSpec = createBlockSpec(
4240
createBlockNoteExtension({
4341
key: "quote-block-shortcuts",
4442
keyboardShortcuts: {
45-
"Mod-Alt-q": ({ editor }) =>
46-
editor.transact((tr) => {
47-
const blockInfo = getBlockInfoFromTransaction(tr);
43+
"Mod-Alt-q": ({ editor }) => {
44+
const cursorPosition = editor.getTextCursorPosition();
4845

49-
if (
50-
!blockInfo.isBlockContainer ||
51-
blockInfo.blockContent.node.type.spec.content !== "inline*"
52-
) {
53-
return true;
54-
}
46+
if (
47+
editor.schema.blockSchema[cursorPosition.block.type].content !==
48+
"inline"
49+
) {
50+
return false;
51+
}
5552

56-
updateBlockTr(tr, blockInfo.bnBlock.beforePos, {
57-
type: "quote",
58-
});
59-
return true;
60-
}),
53+
editor.updateBlock(cursorPosition.block, {
54+
type: "quote",
55+
props: {},
56+
});
57+
return true;
58+
},
6159
},
6260
inputRules: [
6361
{

0 commit comments

Comments
 (0)