Skip to content

fix: Custom blocks without inline content #312

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 3 commits into from
Aug 18, 2023
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
8 changes: 4 additions & 4 deletions packages/core/src/api/nodeConversions/nodeConversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
Styles,
ToggledStyle,
} from "../../extensions/Blocks/api/inlineContentTypes";
import { getBlockInfoFromPos } from "../../extensions/Blocks/helpers/getBlockInfoFromPos";
import UniqueID from "../../extensions/UniqueID/UniqueID";
import { UnreachableCaseError } from "../../shared/utils";
import { getBlockInfo } from "../../extensions/Blocks/helpers/getBlockInfoFromPos";

const toggleStyles = new Set<ToggledStyle>([
"bold",
Expand Down Expand Up @@ -369,7 +369,7 @@ export function nodeToBlock<BSchema extends BlockSchema>(
return cachedBlock;
}

const blockInfo = getBlockInfoFromPos(node, 0)!;
const blockInfo = getBlockInfo(node);

let id = blockInfo.id;

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

const props: any = {};
for (const [attr, value] of Object.entries({
...blockInfo.node.attrs,
...node.attrs,
...blockInfo.contentNode.attrs,
})) {
const blockSpec = blockSchema[blockInfo.contentType.name];
Expand Down Expand Up @@ -414,7 +414,7 @@ export function nodeToBlock<BSchema extends BlockSchema>(
const children: Block<BSchema>[] = [];
for (let i = 0; i < blockInfo.numChildBlocks; i++) {
children.push(
nodeToBlock(blockInfo.node.lastChild!.child(i), blockSchema, blockCache)
nodeToBlock(node.lastChild!.child(i), blockSchema, blockCache)
);
}

Expand Down
31 changes: 26 additions & 5 deletions packages/core/src/extensions/Blocks/helpers/getBlockInfoFromPos.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
import { Node, NodeType } from "prosemirror-model";

export type BlockInfo = {
export type BlockInfoWithoutPositions = {
id: string;
node: Node;
contentNode: Node;
contentType: NodeType;
numChildBlocks: number;
};

export type BlockInfo = BlockInfoWithoutPositions & {
startPos: number;
endPos: number;
depth: number;
};

/**
* Helper function for `getBlockInfoFromPos`, returns information regarding
* provided blockContainer node.
* @param blockContainer The blockContainer node to retrieve info for.
*/
export function getBlockInfo(blockContainer: Node): BlockInfoWithoutPositions {
const id = blockContainer.attrs["id"];
const contentNode = blockContainer.firstChild!;
const contentType = contentNode.type;
const numChildBlocks =
blockContainer.childCount === 2 ? blockContainer.lastChild!.childCount : 0;

return {
id,
node: blockContainer,
contentNode,
contentType,
numChildBlocks,
};
}

/**
* Retrieves information regarding the nearest blockContainer node in a
* ProseMirror doc, relative to a position.
Expand Down Expand Up @@ -71,10 +95,7 @@ export function getBlockInfoFromPos(doc: Node, pos: number): BlockInfo {
node = $pos.node(depth);
}

const id = node.attrs["id"];
const contentNode = node.firstChild!;
const contentType = contentNode.type;
const numChildBlocks = node.childCount === 2 ? node.lastChild!.childCount : 0;
const { id, contentNode, contentType, numChildBlocks } = getBlockInfo(node);

const startPos = $pos.start(depth);
const endPos = $pos.end(depth);
Expand Down