mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
fix(editor): exisiting slice import should be move instead of add (#9616)
Closes: [BS-2339](https://linear.app/affine-design/issue/BS-2339/%E6%8B%96%E6%8B%BD%E9%87%8A%E6%94%BE%E5%90%8E%E7%9A%84%E4%BD%8D%E7%BD%AE%E4%B8%8D%E5%AF%B9)
This commit is contained in:
@@ -58,73 +58,48 @@ export class DocCRUD {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const hasBlock = this._yBlocks.has(id);
|
||||||
|
if (hasBlock) {
|
||||||
|
throw new BlockSuiteError(
|
||||||
|
ErrorCode.ModelCRUDError,
|
||||||
|
`Should not add existing block: ${id}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const parentFlavour = parent
|
const parentFlavour = parent
|
||||||
? this._yBlocks.get(parent)?.get('sys:flavour')
|
? this._yBlocks.get(parent)?.get('sys:flavour')
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
this._schema.validate(flavour, parentFlavour as string);
|
this._schema.validate(flavour, parentFlavour as string);
|
||||||
|
|
||||||
const hasBlock = this._yBlocks.has(id);
|
const yBlock = new Y.Map() as YBlock;
|
||||||
|
this._yBlocks.set(id, yBlock);
|
||||||
|
|
||||||
if (hasBlock) {
|
const version = schema.version;
|
||||||
const yBlock = this._yBlocks.get(id);
|
const children = (
|
||||||
const existedParent = this.getParent(id);
|
initialProps.children as undefined | (string | BlockModel)[]
|
||||||
if (yBlock && existedParent) {
|
)?.map(child => (typeof child === 'string' ? child : child.id));
|
||||||
const yParent = this._yBlocks.get(existedParent) as YBlock;
|
|
||||||
const yParentChildren = yParent.get('sys:children') as Y.Array<string>;
|
|
||||||
const index = yParentChildren.toArray().indexOf(id);
|
|
||||||
yParentChildren.delete(index, 1);
|
|
||||||
if (
|
|
||||||
parentIndex != null &&
|
|
||||||
index != null &&
|
|
||||||
existedParent === parent &&
|
|
||||||
index < parentIndex
|
|
||||||
) {
|
|
||||||
parentIndex--;
|
|
||||||
}
|
|
||||||
const props = {
|
|
||||||
...initialProps,
|
|
||||||
};
|
|
||||||
delete props.id;
|
|
||||||
delete props.flavour;
|
|
||||||
delete props.children;
|
|
||||||
|
|
||||||
Object.entries(props).forEach(([key, value]) => {
|
yBlock.set('sys:id', id);
|
||||||
if (value === undefined) return;
|
yBlock.set('sys:flavour', flavour);
|
||||||
|
yBlock.set('sys:version', version);
|
||||||
|
yBlock.set('sys:children', Y.Array.from(children ?? []));
|
||||||
|
|
||||||
yBlock.set(`prop:${key}`, native2Y(value));
|
const defaultProps = schema.model.props?.(internalPrimitives) ?? {};
|
||||||
});
|
const props = {
|
||||||
}
|
...defaultProps,
|
||||||
} else {
|
...initialProps,
|
||||||
const yBlock = new Y.Map() as YBlock;
|
};
|
||||||
this._yBlocks.set(id, yBlock);
|
|
||||||
|
|
||||||
const version = schema.version;
|
delete props.id;
|
||||||
const children = (
|
delete props.flavour;
|
||||||
initialProps.children as undefined | (string | BlockModel)[]
|
delete props.children;
|
||||||
)?.map(child => (typeof child === 'string' ? child : child.id));
|
|
||||||
|
|
||||||
yBlock.set('sys:id', id);
|
Object.entries(props).forEach(([key, value]) => {
|
||||||
yBlock.set('sys:flavour', flavour);
|
if (value === undefined) return;
|
||||||
yBlock.set('sys:version', version);
|
|
||||||
yBlock.set('sys:children', Y.Array.from(children ?? []));
|
|
||||||
|
|
||||||
const defaultProps = schema.model.props?.(internalPrimitives) ?? {};
|
yBlock.set(`prop:${key}`, native2Y(value));
|
||||||
const props = {
|
});
|
||||||
...defaultProps,
|
|
||||||
...initialProps,
|
|
||||||
};
|
|
||||||
|
|
||||||
delete props.id;
|
|
||||||
delete props.flavour;
|
|
||||||
delete props.children;
|
|
||||||
|
|
||||||
Object.entries(props).forEach(([key, value]) => {
|
|
||||||
if (value === undefined) return;
|
|
||||||
|
|
||||||
yBlock.set(`prop:${key}`, native2Y(value));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const parentId =
|
const parentId =
|
||||||
parent ?? (schema.model.role === 'root' ? null : this.root);
|
parent ?? (schema.model.role === 'root' ? null : this.root);
|
||||||
@@ -355,14 +330,12 @@ export class DocCRUD {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetIndex = targetParentChildren
|
let targetIndex = targetParentChildren
|
||||||
.toArray()
|
.toArray()
|
||||||
.findIndex(id => id === targetSibling);
|
.findIndex(id => id === targetSibling);
|
||||||
if (targetIndex === -1) {
|
if (targetIndex === -1) {
|
||||||
throw new BlockSuiteError(
|
console.error('Target sibling not found, just insert to the end');
|
||||||
ErrorCode.ModelCRUDError,
|
targetIndex = targetParentChildren.length;
|
||||||
'Target sibling not found'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
insertIndex = shouldInsertBeforeSibling
|
insertIndex = shouldInsertBeforeSibling
|
||||||
? targetIndex
|
? targetIndex
|
||||||
|
|||||||
@@ -256,7 +256,26 @@ export class Job {
|
|||||||
|
|
||||||
const blockTree = await this._convertFlatSnapshots(flatSnapshots);
|
const blockTree = await this._convertFlatSnapshots(flatSnapshots);
|
||||||
|
|
||||||
await this._insertBlockTree(blockTree.children, doc, parent, index);
|
const first = content[0];
|
||||||
|
// check if the slice is already in the doc
|
||||||
|
if (first && doc.hasBlock(first.id)) {
|
||||||
|
// if the slice is already in the doc, we need to move the blocks instead of adding them
|
||||||
|
const models = flatSnapshots
|
||||||
|
.map(flat => doc.getBlock(flat.snapshot.id)?.model)
|
||||||
|
.filter(Boolean) as BlockModel[];
|
||||||
|
const parentModel = parent ? doc.getBlock(parent)?.model : undefined;
|
||||||
|
if (!parentModel) {
|
||||||
|
throw new BlockSuiteError(
|
||||||
|
ErrorCode.TransformerError,
|
||||||
|
'Parent block not found in doc when moving slice'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const targetSibling =
|
||||||
|
index !== undefined ? parentModel.children[index] : null;
|
||||||
|
doc.moveBlocks(models, parentModel, targetSibling);
|
||||||
|
} else {
|
||||||
|
await this._insertBlockTree(blockTree.children, doc, parent, index);
|
||||||
|
}
|
||||||
|
|
||||||
const contentBlocks = blockTree.children
|
const contentBlocks = blockTree.children
|
||||||
.map(tree => doc.getBlockById(tree.draft.id))
|
.map(tree => doc.getBlockById(tree.draft.id))
|
||||||
|
|||||||
Reference in New Issue
Block a user