Skip to content

Commit 6feb4de

Browse files
committed
1 parent 126ab18 commit 6feb4de

File tree

2 files changed

+56
-64
lines changed
  • blocksuite/framework/store/src

2 files changed

+56
-64
lines changed

blocksuite/framework/store/src/model/store/crud.ts

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -58,73 +58,48 @@ export class DocCRUD {
5858
);
5959
}
6060

61+
const hasBlock = this._yBlocks.has(id);
62+
if (hasBlock) {
63+
throw new BlockSuiteError(
64+
ErrorCode.ModelCRUDError,
65+
`Should not add existing block: ${id}`
66+
);
67+
}
68+
6169
const parentFlavour = parent
6270
? this._yBlocks.get(parent)?.get('sys:flavour')
6371
: undefined;
6472

6573
this._schema.validate(flavour, parentFlavour as string);
6674

67-
const hasBlock = this._yBlocks.has(id);
75+
const yBlock = new Y.Map() as YBlock;
76+
this._yBlocks.set(id, yBlock);
6877

69-
if (hasBlock) {
70-
const yBlock = this._yBlocks.get(id);
71-
const existedParent = this.getParent(id);
72-
if (yBlock && existedParent) {
73-
const yParent = this._yBlocks.get(existedParent) as YBlock;
74-
const yParentChildren = yParent.get('sys:children') as Y.Array<string>;
75-
const index = yParentChildren.toArray().indexOf(id);
76-
yParentChildren.delete(index, 1);
77-
if (
78-
parentIndex != null &&
79-
index != null &&
80-
existedParent === parent &&
81-
index < parentIndex
82-
) {
83-
parentIndex--;
84-
}
85-
const props = {
86-
...initialProps,
87-
};
88-
delete props.id;
89-
delete props.flavour;
90-
delete props.children;
78+
const version = schema.version;
79+
const children = (
80+
initialProps.children as undefined | (string | BlockModel)[]
81+
)?.map(child => (typeof child === 'string' ? child : child.id));
9182

92-
Object.entries(props).forEach(([key, value]) => {
93-
if (value === undefined) return;
83+
yBlock.set('sys:id', id);
84+
yBlock.set('sys:flavour', flavour);
85+
yBlock.set('sys:version', version);
86+
yBlock.set('sys:children', Y.Array.from(children ?? []));
9487

95-
yBlock.set(`prop:${key}`, native2Y(value));
96-
});
97-
}
98-
} else {
99-
const yBlock = new Y.Map() as YBlock;
100-
this._yBlocks.set(id, yBlock);
101-
102-
const version = schema.version;
103-
const children = (
104-
initialProps.children as undefined | (string | BlockModel)[]
105-
)?.map(child => (typeof child === 'string' ? child : child.id));
106-
107-
yBlock.set('sys:id', id);
108-
yBlock.set('sys:flavour', flavour);
109-
yBlock.set('sys:version', version);
110-
yBlock.set('sys:children', Y.Array.from(children ?? []));
111-
112-
const defaultProps = schema.model.props?.(internalPrimitives) ?? {};
113-
const props = {
114-
...defaultProps,
115-
...initialProps,
116-
};
117-
118-
delete props.id;
119-
delete props.flavour;
120-
delete props.children;
121-
122-
Object.entries(props).forEach(([key, value]) => {
123-
if (value === undefined) return;
124-
125-
yBlock.set(`prop:${key}`, native2Y(value));
126-
});
127-
}
88+
const defaultProps = schema.model.props?.(internalPrimitives) ?? {};
89+
const props = {
90+
...defaultProps,
91+
...initialProps,
92+
};
93+
94+
delete props.id;
95+
delete props.flavour;
96+
delete props.children;
97+
98+
Object.entries(props).forEach(([key, value]) => {
99+
if (value === undefined) return;
100+
101+
yBlock.set(`prop:${key}`, native2Y(value));
102+
});
128103

129104
const parentId =
130105
parent ?? (schema.model.role === 'root' ? null : this.root);
@@ -355,14 +330,12 @@ export class DocCRUD {
355330
return;
356331
}
357332

358-
const targetIndex = targetParentChildren
333+
let targetIndex = targetParentChildren
359334
.toArray()
360335
.findIndex(id => id === targetSibling);
361336
if (targetIndex === -1) {
362-
throw new BlockSuiteError(
363-
ErrorCode.ModelCRUDError,
364-
'Target sibling not found'
365-
);
337+
console.error('Target sibling not found, just insert to the end');
338+
targetIndex = targetParentChildren.length;
366339
}
367340
insertIndex = shouldInsertBeforeSibling
368341
? targetIndex

blocksuite/framework/store/src/transformer/job.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,26 @@ export class Job {
256256

257257
const blockTree = await this._convertFlatSnapshots(flatSnapshots);
258258

259-
await this._insertBlockTree(blockTree.children, doc, parent, index);
259+
const first = content[0];
260+
// check if the slice is already in the doc
261+
if (first && doc.hasBlock(first.id)) {
262+
// if the slice is already in the doc, we need to move the blocks instead of adding them
263+
const models = flatSnapshots
264+
.map(flat => doc.getBlock(flat.snapshot.id)?.model)
265+
.filter(Boolean) as BlockModel[];
266+
const parentModel = parent ? doc.getBlock(parent)?.model : undefined;
267+
if (!parentModel) {
268+
throw new BlockSuiteError(
269+
ErrorCode.TransformerError,
270+
'Parent block not found in doc when moving slice'
271+
);
272+
}
273+
const targetSibling =
274+
index !== undefined ? parentModel.children[index] : null;
275+
doc.moveBlocks(models, parentModel, targetSibling);
276+
} else {
277+
await this._insertBlockTree(blockTree.children, doc, parent, index);
278+
}
260279

261280
const contentBlocks = blockTree.children
262281
.map(tree => doc.getBlockById(tree.draft.id))

0 commit comments

Comments
 (0)