Skip to content

Commit bcef721

Browse files
committed
feat: support new "block-updated" event. (#2221)
1 parent c180115 commit bcef721

File tree

6 files changed

+98
-7
lines changed

6 files changed

+98
-7
lines changed

src/components/block/api.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ function BlockAPI(
104104
return block.save();
105105
},
106106

107+
/**
108+
* Update Block data
109+
*
110+
* @param {BlockToolData} data - data to update
111+
* @returns {Promise<boolean>}
112+
*/
113+
update(data: BlockToolData): Promise<boolean> {
114+
return block.update(data);
115+
},
116+
107117
/**
108118
* Validate Block data
109119
*

src/components/block/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export enum BlockToolAPI {
7878
RENDERED = 'rendered',
7979
MOVED = 'moved',
8080
UPDATED = 'updated',
81+
UPDATE = 'update',
8182
REMOVED = 'removed',
8283
// eslint-disable-next-line @typescript-eslint/naming-convention
8384
ON_PASTE = 'onPaste',
@@ -600,6 +601,21 @@ export default class Block extends EventsDispatcher<BlockEvents> {
600601
await this.toolInstance.merge(data);
601602
}
602603

604+
/**
605+
* Call plugins update method
606+
*
607+
* @param {BlockToolData} data - data to update
608+
*/
609+
public async update(data: BlockToolData): Promise<boolean> {
610+
let updated = false;
611+
612+
if (this.toolInstance.update instanceof Function) {
613+
updated = await this.toolInstance.update(data);
614+
}
615+
616+
return updated;
617+
}
618+
603619
/**
604620
* Extracts data from Block
605621
* Groups Tool's save processing time

src/components/modules/api/blocks.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,9 @@ export default class BlocksAPI extends Module {
298298
return;
299299
}
300300

301-
const blockIndex = BlockManager.getBlockIndex(block);
302-
303-
BlockManager.insert({
301+
BlockManager.update({
304302
id: block.id,
305-
tool: block.name,
306303
data,
307-
index: blockIndex,
308-
replace: true,
309-
tunes: block.tunes,
310304
});
311305
};
312306
}

src/components/modules/blockManager.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,62 @@ export default class BlockManager extends Module {
321321
return block;
322322
}
323323

324+
/**
325+
* Update block by id
326+
*
327+
* keep the tool name
328+
* only update data of block
329+
*
330+
* @param {object} options - insert options
331+
* @param {string} [options.id] - block's unique id
332+
* @param {object} [options.data] - plugin data
333+
* @returns {void}
334+
*/
335+
public update({
336+
id = undefined,
337+
data = {},
338+
}: {
339+
id?: string;
340+
data?: BlockToolData;
341+
} = {}): void {
342+
const block = this.blocks.find(item => item.id === id);
343+
344+
if (block.update(data)) {
345+
this.blockDidMutated(BlockMutationType.Changed, block, {
346+
id,
347+
});
348+
349+
return;
350+
}
351+
352+
const index = this._blocks.array.findIndex(item => item.id === id);
353+
354+
if (index === -1) {
355+
_.log(`blockManamger.update(): invalid index of block id: ${id}`, 'warn');
356+
357+
return;
358+
}
359+
360+
const tool = this._blocks[index].tool.name;
361+
const newBlock = this.composeBlock({
362+
id,
363+
tool,
364+
data,
365+
tunes: {},
366+
});
367+
368+
this._blocks.insert(index, newBlock, true);
369+
370+
/**
371+
* Force call of didMutated event on Block changed
372+
*/
373+
this.blockDidMutated(BlockMutationType.Changed, newBlock, {
374+
id,
375+
});
376+
377+
return;
378+
}
379+
324380
/**
325381
* Replace current working block
326382
*

types/api/block.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ export interface BlockAPI {
5959
*/
6060
save(): Promise<void|SavedData>;
6161

62+
/**
63+
* Update Block data
64+
*
65+
* @param {BlockToolData} data
66+
*
67+
* @return {Promise<boolean>}
68+
*/
69+
update(data: BlockToolData): Promise<boolean>;
70+
6271
/**
6372
* Validate Block data
6473
*

types/tools/block-tool.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export interface BlockTool extends BaseTool {
4343
*/
4444
merge?(blockData: BlockToolData): void;
4545

46+
/**
47+
* Method that specified how to update Block's data.
48+
* @param {BlockToolData} blockData
49+
*/
50+
update?(blockData: BlockToolData): boolean;
51+
4652
/**
4753
* On paste callback. Fired when pasted content can be substituted by a Tool
4854
* @param {PasteEvent} event

0 commit comments

Comments
 (0)