Skip to content

Commit 05feb1e

Browse files
fix: Custom blocks without inline content (#312)
* Updated `nodeConversions.tsx` * Small cleanup * Small cleanup
1 parent 6df7637 commit 05feb1e

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

packages/core/src/api/nodeConversions/nodeConversions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import {
1616
Styles,
1717
ToggledStyle,
1818
} from "../../extensions/Blocks/api/inlineContentTypes";
19-
import { getBlockInfoFromPos } from "../../extensions/Blocks/helpers/getBlockInfoFromPos";
2019
import UniqueID from "../../extensions/UniqueID/UniqueID";
2120
import { UnreachableCaseError } from "../../shared/utils";
21+
import { getBlockInfo } from "../../extensions/Blocks/helpers/getBlockInfoFromPos";
2222

2323
const toggleStyles = new Set<ToggledStyle>([
2424
"bold",
@@ -369,7 +369,7 @@ export function nodeToBlock<BSchema extends BlockSchema>(
369369
return cachedBlock;
370370
}
371371

372-
const blockInfo = getBlockInfoFromPos(node, 0)!;
372+
const blockInfo = getBlockInfo(node);
373373

374374
let id = blockInfo.id;
375375

@@ -380,7 +380,7 @@ export function nodeToBlock<BSchema extends BlockSchema>(
380380

381381
const props: any = {};
382382
for (const [attr, value] of Object.entries({
383-
...blockInfo.node.attrs,
383+
...node.attrs,
384384
...blockInfo.contentNode.attrs,
385385
})) {
386386
const blockSpec = blockSchema[blockInfo.contentType.name];
@@ -414,7 +414,7 @@ export function nodeToBlock<BSchema extends BlockSchema>(
414414
const children: Block<BSchema>[] = [];
415415
for (let i = 0; i < blockInfo.numChildBlocks; i++) {
416416
children.push(
417-
nodeToBlock(blockInfo.node.lastChild!.child(i), blockSchema, blockCache)
417+
nodeToBlock(node.lastChild!.child(i), blockSchema, blockCache)
418418
);
419419
}
420420

packages/core/src/extensions/Blocks/helpers/getBlockInfoFromPos.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
import { Node, NodeType } from "prosemirror-model";
22

3-
export type BlockInfo = {
3+
export type BlockInfoWithoutPositions = {
44
id: string;
55
node: Node;
66
contentNode: Node;
77
contentType: NodeType;
88
numChildBlocks: number;
9+
};
10+
11+
export type BlockInfo = BlockInfoWithoutPositions & {
912
startPos: number;
1013
endPos: number;
1114
depth: number;
1215
};
1316

17+
/**
18+
* Helper function for `getBlockInfoFromPos`, returns information regarding
19+
* provided blockContainer node.
20+
* @param blockContainer The blockContainer node to retrieve info for.
21+
*/
22+
export function getBlockInfo(blockContainer: Node): BlockInfoWithoutPositions {
23+
const id = blockContainer.attrs["id"];
24+
const contentNode = blockContainer.firstChild!;
25+
const contentType = contentNode.type;
26+
const numChildBlocks =
27+
blockContainer.childCount === 2 ? blockContainer.lastChild!.childCount : 0;
28+
29+
return {
30+
id,
31+
node: blockContainer,
32+
contentNode,
33+
contentType,
34+
numChildBlocks,
35+
};
36+
}
37+
1438
/**
1539
* Retrieves information regarding the nearest blockContainer node in a
1640
* ProseMirror doc, relative to a position.
@@ -72,10 +96,7 @@ export function getBlockInfoFromPos(doc: Node, pos: number): BlockInfo {
7296
node = $pos.node(depth);
7397
}
7498

75-
const id = node.attrs["id"];
76-
const contentNode = node.firstChild!;
77-
const contentType = contentNode.type;
78-
const numChildBlocks = node.childCount === 2 ? node.lastChild!.childCount : 0;
99+
const { id, contentNode, contentType, numChildBlocks } = getBlockInfo(node);
79100

80101
const startPos = $pos.start(depth);
81102
const endPos = $pos.end(depth);

0 commit comments

Comments
 (0)