fix: issue 2602

This commit is contained in:
kelin.zrh 2024-02-02 15:53:04 +08:00
parent b619946e8f
commit 5572331088
3 changed files with 32 additions and 11 deletions

View file

@ -728,6 +728,17 @@ export default class Block extends EventsDispatcher<BlockEvents> {
return convertBlockDataToString(blockData, this.tool.conversionConfig);
}
public supportUpdate(): boolean {
return !!this.toolInstance.update;
}
public update(newData: Partial<BlockToolData>): void {
if (!this.toolInstance.update) {
return;
}
this.toolInstance.update(newData);
}
/**
* Make default Block wrappers and put Tool`s content there
*

View file

@ -344,23 +344,28 @@ export default class BlockManager extends Module {
*/
public async update(block: Block, data: Partial<BlockToolData>): Promise<Block> {
const existingData = await block.data;
const newBlock = this.composeBlock({
id: block.id,
tool: block.name,
data: Object.assign({}, existingData, data),
tunes: block.tunes,
});
const blockIndex = this.getBlockIndex(block);
let updatedBlock = null;
this._blocks.replace(blockIndex, newBlock);
if (block.supportUpdate()) {
block.update(Object.assign({}, existingData, data));
updatedBlock = block;
} else {
updatedBlock = this.composeBlock({
id: block.id,
tool: block.name,
data: Object.assign({}, existingData, data),
tunes: block.tunes,
});
this.blockDidMutated(BlockChangedMutationType, newBlock, {
this._blocks.replace(blockIndex, updatedBlock);
}
this.blockDidMutated(BlockChangedMutationType, updatedBlock, {
index: blockIndex,
});
return newBlock;
return updatedBlock;
}
/**

View file

@ -64,6 +64,11 @@ export interface BlockTool extends BaseTool {
*/
rendered?(): void;
/**
* Call to update block content
*/
update?(data: Partial<BlockToolData>): void;
/**
* Called each time block content is updated
*/