Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue 2602 #2603

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/components/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
27 changes: 16 additions & 11 deletions src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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._blocks.replace(blockIndex, updatedBlock);
}

this.blockDidMutated(BlockChangedMutationType, newBlock, {
this.blockDidMutated(BlockChangedMutationType, updatedBlock, {
index: blockIndex,
});

return newBlock;
return updatedBlock;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions types/tools/block-tool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down