Skip to content

Commit efa0ab8

Browse files
committed
fix: reinstate createAndFill hack, to solve unique id issue
1 parent de1f82a commit efa0ab8

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,25 @@ export class BlockNoteEditor<
917917
);
918918
}
919919

920+
// When y-prosemirror creates an empty document, the `blockContainer` node is created with an `id` of `null`.
921+
// This causes the unique id extension to generate a new id for the initial block, which is not what we want
922+
// Since it will be randomly generated & cause there to be more updates to the ydoc
923+
// This is a hack to make it so that anytime `schema.doc.createAndFill` is called, the initial block id is already set to "initialBlockId"
924+
let cache: Node | undefined = undefined;
925+
const oldCreateAndFill = this.pmSchema.nodes.doc.createAndFill;
926+
this.pmSchema.nodes.doc.createAndFill = (...args: any) => {
927+
if (cache) {
928+
return cache;
929+
}
930+
const ret = oldCreateAndFill.apply(this.pmSchema.nodes.doc, args)!;
931+
932+
// create a copy that we can mutate (otherwise, assigning attrs is not safe and corrupts the pm state)
933+
const jsonNode = ret.toJSON();
934+
jsonNode.content[0].content[0].attrs.id = "initialBlockId";
935+
936+
cache = Node.fromJSON(this.pmSchema, jsonNode);
937+
return cache;
938+
};
920939
this.pmSchema.cached.blockNoteEditor = this;
921940

922941
// Initialize managers

packages/core/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor-forked.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "text",
99
},
1010
],
11-
"id": "3",
11+
"id": "1",
1212
"props": {
1313
"backgroundColor": "default",
1414
"textAlignment": "left",
@@ -19,7 +19,7 @@
1919
{
2020
"children": [],
2121
"content": [],
22-
"id": "4",
22+
"id": "initialBlockId",
2323
"props": {
2424
"backgroundColor": "default",
2525
"textAlignment": "left",

packages/core/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "text",
99
},
1010
],
11-
"id": "1",
11+
"id": "0",
1212
"props": {
1313
"backgroundColor": "default",
1414
"textAlignment": "left",
@@ -19,7 +19,7 @@
1919
{
2020
"children": [],
2121
"content": [],
22-
"id": "2",
22+
"id": "initialBlockId",
2323
"props": {
2424
"backgroundColor": "default",
2525
"textAlignment": "left",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<blockgroup><blockcontainer id="3"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello World</paragraph></blockcontainer><blockcontainer id="4"><paragraph backgroundColor="default" textAlignment="left" textColor="default"></paragraph></blockcontainer></blockgroup>
1+
<blockgroup><blockcontainer id="1"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello World</paragraph></blockcontainer><blockcontainer id="initialBlockId"><paragraph backgroundColor="default" textAlignment="left" textColor="default"></paragraph></blockcontainer></blockgroup>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<blockgroup><blockcontainer id="1"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello</paragraph></blockcontainer><blockcontainer id="2"><paragraph backgroundColor="default" textAlignment="left" textColor="default"></paragraph></blockcontainer></blockgroup>
1+
<blockgroup><blockcontainer id="0"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello</paragraph></blockcontainer><blockcontainer id="initialBlockId"><paragraph backgroundColor="default" textAlignment="left" textColor="default"></paragraph></blockcontainer></blockgroup>

packages/core/src/extensions/UniqueID/UniqueID.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,10 @@ const UniqueID = Extension.create({
130130
addProseMirrorPlugins() {
131131
let dragSourceElement: any = null;
132132
let transformPasted = false;
133-
let isFirstCollaborationTransaction = true;
134133
return [
135134
new Plugin({
136135
key: new PluginKey("uniqueID"),
137136
appendTransaction: (transactions, oldState, newState) => {
138-
if (
139-
isFirstCollaborationTransaction &&
140-
transactions.some((tr) => tr.getMeta("y-sync$"))
141-
) {
142-
isFirstCollaborationTransaction = false;
143-
return;
144-
}
145137
const docChanges =
146138
transactions.some((transaction) => transaction.docChanged) &&
147139
!oldState.doc.eq(newState.doc);

0 commit comments

Comments
 (0)