Skip to content

Commit 3b2cddd

Browse files
committed
refactor: make nest block use the transactions API
1 parent 7a21426 commit 3b2cddd

File tree

1 file changed

+57
-58
lines changed
  • packages/core/src/api/blockManipulation/commands/nestBlock

1 file changed

+57
-58
lines changed

packages/core/src/api/blockManipulation/commands/nestBlock/nestBlock.ts

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Fragment, NodeType, Slice } from "prosemirror-model";
2-
import { EditorState, Transaction } from "prosemirror-state";
2+
import { Transaction } from "prosemirror-state";
33
import { ReplaceAroundStep } from "prosemirror-transform";
44

55
import { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js";
@@ -11,68 +11,67 @@ import { getBlockInfoFromTransaction } from "../../../getBlockInfoFromPos.js";
1111
*
1212
* The original function derives too many information from the parentnode and itemtype
1313
*/
14-
function sinkListItem(itemType: NodeType, groupType: NodeType) {
15-
return function (state: EditorState, dispatch?: (tr: Transaction) => void) {
16-
const { $from, $to } = state.selection;
17-
const range = $from.blockRange(
18-
$to,
19-
(node) =>
20-
node.childCount > 0 &&
21-
(node.type.name === "blockGroup" || node.type.name === "column"), // change necessary to not look at first item child type
22-
);
23-
if (!range) {
24-
return false;
25-
}
26-
const startIndex = range.startIndex;
27-
if (startIndex === 0) {
28-
return false;
29-
}
30-
const parent = range.parent;
31-
const nodeBefore = parent.child(startIndex - 1);
32-
if (nodeBefore.type !== itemType) {
33-
return false;
34-
}
35-
if (dispatch) {
36-
const nestedBefore =
37-
nodeBefore.lastChild && nodeBefore.lastChild.type === groupType; // change necessary to check groupType instead of parent.type
38-
const inner = Fragment.from(nestedBefore ? itemType.create() : null);
39-
const slice = new Slice(
40-
Fragment.from(
41-
itemType.create(null, Fragment.from(groupType.create(null, inner))), // change necessary to create "groupType" instead of parent.type
42-
),
43-
nestedBefore ? 3 : 1,
44-
0,
45-
);
14+
function sinkListItem(
15+
tr: Transaction,
16+
itemType: NodeType,
17+
groupType: NodeType,
18+
) {
19+
const { $from, $to } = tr.selection;
20+
const range = $from.blockRange(
21+
$to,
22+
(node) =>
23+
node.childCount > 0 &&
24+
(node.type.name === "blockGroup" || node.type.name === "column"), // change necessary to not look at first item child type
25+
);
26+
if (!range) {
27+
return false;
28+
}
29+
const startIndex = range.startIndex;
30+
if (startIndex === 0) {
31+
return false;
32+
}
33+
const parent = range.parent;
34+
const nodeBefore = parent.child(startIndex - 1);
35+
if (nodeBefore.type !== itemType) {
36+
return false;
37+
}
38+
const nestedBefore =
39+
nodeBefore.lastChild && nodeBefore.lastChild.type === groupType; // change necessary to check groupType instead of parent.type
40+
const inner = Fragment.from(nestedBefore ? itemType.create() : null);
41+
const slice = new Slice(
42+
Fragment.from(
43+
itemType.create(null, Fragment.from(groupType.create(null, inner))), // change necessary to create "groupType" instead of parent.type
44+
),
45+
nestedBefore ? 3 : 1,
46+
0,
47+
);
48+
49+
const before = range.start;
50+
const after = range.end;
51+
52+
tr.step(
53+
new ReplaceAroundStep(
54+
before - (nestedBefore ? 3 : 1),
55+
after,
56+
before,
57+
after,
58+
slice,
59+
1,
60+
true,
61+
),
62+
).scrollIntoView();
4663

47-
const before = range.start;
48-
const after = range.end;
49-
dispatch(
50-
state.tr
51-
.step(
52-
new ReplaceAroundStep(
53-
before - (nestedBefore ? 3 : 1),
54-
after,
55-
before,
56-
after,
57-
slice,
58-
1,
59-
true,
60-
),
61-
)
62-
.scrollIntoView(),
63-
);
64-
}
65-
return true;
66-
};
64+
return true;
6765
}
6866

6967
export function nestBlock(editor: BlockNoteEditor<any, any, any>) {
70-
return editor.exec((state, dispatch) =>
68+
return editor.transact((tr) => {
7169
sinkListItem(
72-
state.schema.nodes["blockContainer"],
73-
state.schema.nodes["blockGroup"],
74-
)(state, dispatch),
75-
);
70+
tr,
71+
editor.pmSchema.nodes["blockContainer"],
72+
editor.pmSchema.nodes["blockGroup"],
73+
);
74+
});
7675
}
7776

7877
export function unnestBlock(editor: BlockNoteEditor<any, any, any>) {

0 commit comments

Comments
 (0)