Skip to content

Commit 2980c6c

Browse files
committed
refactor: stash into cached schema property
1 parent 6e1d9d6 commit 2980c6c

File tree

6 files changed

+71
-35
lines changed

6 files changed

+71
-35
lines changed

packages/core/src/api/blockManipulation/commands/insertBlocks/insertBlocks.test.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ function insertBlocks(
1515
placement: "before" | "after" = "before"
1616
) {
1717
return editor.transact((tr) =>
18-
insertBlocksTr(
19-
tr,
20-
editor.pmSchema,
21-
editor.schema,
22-
blocksToInsert,
23-
referenceBlock,
24-
placement,
25-
editor.blockCache
26-
)
18+
insertBlocksTr(tr, blocksToInsert, referenceBlock, placement)
2719
);
2820
}
2921

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
1-
import { Fragment, Node, Schema, Slice } from "prosemirror-model";
1+
import { Fragment, Slice } from "prosemirror-model";
22

33
import { Block, PartialBlock } from "../../../../blocks/defaultBlocks.js";
4-
import type { BlockCache } from "../../../../editor/BlockNoteEditor";
54
import {
65
BlockIdentifier,
76
BlockSchema,
87
InlineContentSchema,
98
StyleSchema,
109
} from "../../../../schema/index.js";
11-
import { blockToNode } from "../../../nodeConversions/blockToNode.js";
12-
import { nodeToBlock } from "../../../nodeConversions/nodeToBlock.js";
10+
import { simpleBlockToNode } from "../../../nodeConversions/blockToNode.js";
11+
import { simpleNodeToBlock } from "../../../nodeConversions/nodeToBlock.js";
1312
import { getNodeById } from "../../../nodeUtil.js";
1413
import { ReplaceStep } from "prosemirror-transform";
1514
import type { Transaction } from "prosemirror-state";
16-
import type { BlockNoteSchema } from "../../../../editor/BlockNoteSchema.js";
15+
import { getSchemaForTransaction } from "../../../pmUtil.js";
1716

1817
export function insertBlocks<
1918
BSchema extends BlockSchema,
2019
I extends InlineContentSchema,
2120
S extends StyleSchema
2221
>(
2322
tr: Transaction,
24-
pmSchema: Schema,
25-
schema: BlockNoteSchema<BSchema, I, S>,
2623
blocksToInsert: PartialBlock<BSchema, I, S>[],
2724
referenceBlock: BlockIdentifier,
28-
placement: "before" | "after" = "before",
29-
blockCache?: BlockCache
25+
placement: "before" | "after" = "before"
3026
): Block<BSchema, I, S>[] {
3127
const id =
3228
typeof referenceBlock === "string" ? referenceBlock : referenceBlock.id;
29+
const schema = getSchemaForTransaction(tr);
3330

34-
const nodesToInsert: Node[] = [];
35-
for (const blockSpec of blocksToInsert) {
36-
nodesToInsert.push(blockToNode(blockSpec, pmSchema, schema.styleSchema));
37-
}
31+
const nodesToInsert = blocksToInsert.map((block) =>
32+
simpleBlockToNode(block, schema)
33+
);
3834

3935
const posInfo = getNodeById(id, tr.doc);
4036
if (!posInfo) {
@@ -52,18 +48,9 @@ export function insertBlocks<
5248

5349
// Now that the `PartialBlock`s have been converted to nodes, we can
5450
// re-convert them into full `Block`s.
55-
const insertedBlocks: Block<BSchema, I, S>[] = [];
56-
for (const node of nodesToInsert) {
57-
insertedBlocks.push(
58-
nodeToBlock(
59-
node,
60-
schema.blockSchema,
61-
schema.inlineContentSchema,
62-
schema.styleSchema,
63-
blockCache
64-
)
65-
);
66-
}
51+
const insertedBlocks = nodesToInsert.map((node) =>
52+
simpleNodeToBlock(node, schema)
53+
);
6754

6855
return insertedBlocks;
6956
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import { getColspan, isPartialTableCell } from "../../util/table.js";
2020
import { UnreachableCaseError } from "../../util/typescript.js";
2121
import { getAbsoluteTableCells } from "../blockManipulation/tables/tables.js";
22+
import { getStyleSchemaForSchema } from "../pmUtil.js";
2223

2324
/**
2425
* Convert a StyledText inline element to a
@@ -300,6 +301,13 @@ function blockOrInlineContentToContentNode(
300301
return contentNode;
301302
}
302303

304+
export function simpleBlockToNode(
305+
block: PartialBlock<any, any, any>,
306+
schema: Schema
307+
) {
308+
return blockToNode(block, schema, getStyleSchemaForSchema(schema));
309+
}
310+
303311
/**
304312
* Converts a BlockNote block to a Prosemirror node.
305313
*/

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Mark, Node } from "@tiptap/pm/model";
1+
import { Mark, Node, Schema } from "@tiptap/pm/model";
22

33
import UniqueID from "../../extensions/UniqueID/UniqueID.js";
44
import type {
@@ -22,6 +22,9 @@ import {
2222
} from "../../schema/inlineContent/types.js";
2323
import { UnreachableCaseError } from "../../util/typescript.js";
2424
import type { BlockCache } from "../../editor/BlockNoteEditor.js";
25+
import { getBlockCacheForSchema, getStyleSchemaForSchema } from "../pmUtil.js";
26+
import { getInlineContentSchemaForSchema } from "../pmUtil.js";
27+
import { getBlockSchemaForSchema } from "../pmUtil.js";
2528

2629
/**
2730
* Converts an internal (prosemirror) table node contentto a BlockNote Tablecontent
@@ -375,6 +378,19 @@ export function nodeToCustomInlineContent<
375378
return ic;
376379
}
377380

381+
export function simpleNodeToBlock(
382+
node: Node,
383+
schema: Schema
384+
): Block<any, any, any> {
385+
return nodeToBlock(
386+
node,
387+
getBlockSchemaForSchema(schema),
388+
getInlineContentSchemaForSchema(schema),
389+
getStyleSchemaForSchema(schema),
390+
getBlockCacheForSchema(schema)
391+
);
392+
}
393+
378394
/**
379395
* Convert a Prosemirror node to a BlockNote block.
380396
*

packages/core/src/api/pmUtil.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { Schema } from "prosemirror-model";
2+
import type { Transaction } from "prosemirror-state";
3+
import type { BlockNoteEditor } from "../editor/BlockNoteEditor.js";
4+
import type { BlockSchema } from "../schema/blocks/types.js";
5+
import type { InlineContentSchema } from "../schema/inlineContent/types.js";
6+
import type { StyleSchema } from "../schema/styles/types.js";
7+
8+
export function getSchemaForTransaction(tr: Transaction) {
9+
return tr.doc.type.schema;
10+
}
11+
12+
export function getBlockNoteEditorForSchema(
13+
schema: Schema
14+
): BlockNoteEditor<BlockSchema, InlineContentSchema, StyleSchema> {
15+
return schema.cached.blockNoteEditor;
16+
}
17+
18+
export function getBlockSchemaForSchema(schema: Schema) {
19+
return getBlockNoteEditorForSchema(schema).schema.blockSchema;
20+
}
21+
22+
export function getInlineContentSchemaForSchema(schema: Schema) {
23+
return getBlockNoteEditorForSchema(schema).schema.inlineContentSchema;
24+
}
25+
26+
export function getStyleSchemaForSchema(schema: Schema) {
27+
return getBlockNoteEditorForSchema(schema).schema.styleSchema;
28+
}
29+
30+
export function getBlockCacheForSchema(schema: Schema) {
31+
return getBlockNoteEditorForSchema(schema).blockCache;
32+
}

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ export class BlockNoteEditor<
728728
// but we still need the schema
729729
this.pmSchema = getSchema(tiptapOptions.extensions!);
730730
}
731+
this.pmSchema.cached.blockNoteEditor = this;
731732
this.emit("create");
732733
}
733734

0 commit comments

Comments
 (0)