Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/components/block/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ function BlockAPI(
return block.save();
},

/**
* Update Block data
*
* @param {BlockToolData} data - data to update
* @returns {Promise<boolean>}
*/
update(data: BlockToolData): Promise<boolean> {
return block.update(data);
},

/**
* Validate Block data
*
Expand Down
16 changes: 16 additions & 0 deletions src/components/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export enum BlockToolAPI {
RENDERED = 'rendered',
MOVED = 'moved',
UPDATED = 'updated',
UPDATE = 'update',
REMOVED = 'removed',
// eslint-disable-next-line @typescript-eslint/naming-convention
ON_PASTE = 'onPaste',
Expand Down Expand Up @@ -600,6 +601,21 @@ export default class Block extends EventsDispatcher<BlockEvents> {
await this.toolInstance.merge(data);
}

/**
* Call plugins update method
*
* @param {BlockToolData} data - data to update
*/
public async update(data: BlockToolData): Promise<boolean> {
let updated = false;

if (this.toolInstance.update instanceof Function) {
updated = await this.toolInstance.update(data);
}

return updated;
}

/**
* Extracts data from Block
* Groups Tool's save processing time
Expand Down
8 changes: 1 addition & 7 deletions src/components/modules/api/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,9 @@ export default class BlocksAPI extends Module {
return;
}

const blockIndex = BlockManager.getBlockIndex(block);

BlockManager.insert({
BlockManager.update({
id: block.id,
tool: block.name,
data,
index: blockIndex,
replace: true,
tunes: block.tunes,
});
};
}
56 changes: 56 additions & 0 deletions src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,62 @@ export default class BlockManager extends Module {
return block;
}

/**
* Update block by id
*
* keep the tool name
* only update data of block
*
* @param {object} options - insert options
* @param {string} [options.id] - block's unique id
* @param {object} [options.data] - plugin data
* @returns {void}
*/
public update({
id = undefined,
data = {},
}: {
id?: string;
data?: BlockToolData;
} = {}): void {
const block = this.blocks.find(item => item.id === id);

if (block.update(data)) {
this.blockDidMutated(BlockMutationType.Changed, block, {
id,
});

return;
}

const index = this._blocks.array.findIndex(item => item.id === id);

if (index === -1) {
_.log(`blockManamger.update(): invalid index of block id: ${id}`, 'warn');

return;
}

const tool = this._blocks[index].tool.name;
const newBlock = this.composeBlock({
id,
tool,
data,
tunes: {},
});

this._blocks.insert(index, newBlock, true);

/**
* Force call of didMutated event on Block changed
*/
this.blockDidMutated(BlockMutationType.Changed, newBlock, {
id,
});

return;
}

/**
* Replace current working block
*
Expand Down
9 changes: 9 additions & 0 deletions types/api/block.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export interface BlockAPI {
*/
save(): Promise<void|SavedData>;

/**
* Update Block data
*
* @param {BlockToolData} data
*
* @return {Promise<boolean>}
*/
update(data: BlockToolData): Promise<boolean>;

/**
* Validate Block data
*
Expand Down
6 changes: 6 additions & 0 deletions types/tools/block-tool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface BlockTool extends BaseTool {
*/
merge?(blockData: BlockToolData): void;

/**
* Method that specified how to update Block's data.
* @param {BlockToolData} blockData
*/
update?(blockData: BlockToolData): boolean;

/**
* On paste callback. Fired when pasted content can be substituted by a Tool
* @param {PasteEvent} event
Expand Down