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/add tunes data in mutation events #2686

Open
wants to merge 10 commits into
base: next
Choose a base branch
from
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 2.30.1

– `Fix` – Block Tunes data are returned inside Block Mutations
– `New` – Block Tunes now supports nesting items

### 2.30.0
Expand Down
40 changes: 25 additions & 15 deletions src/components/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,21 +552,7 @@ export default class Block extends EventsDispatcher<BlockEvents> {
*/
public async save(): Promise<undefined | SavedData> {
const extractedBlock = await this.toolInstance.save(this.pluginsContent as HTMLElement);
const tunesData: { [name: string]: BlockTuneData } = this.unavailableTunesData;

[
...this.tunesInstances.entries(),
...this.defaultTunesInstances.entries(),
]
.forEach(([name, tune]) => {
if (_.isFunction(tune.save)) {
try {
tunesData[name] = tune.save();
} catch (e) {
_.log(`Tune ${tune.constructor.name} save method throws an Error %o`, 'warn', e);
}
}
});
const tunesData: { [name: string]: BlockTuneData } = this.getTunesData();

/**
* Measuring execution time
Expand Down Expand Up @@ -728,6 +714,30 @@ export default class Block extends EventsDispatcher<BlockEvents> {
return convertBlockDataToString(blockData, this.tool.conversionConfig);
}


/**
* Return the data of the corresponding tunes
*/
public getTunesData(): { [name: string]: BlockTuneData } {
const tunesData: { [name: string]: BlockTuneData } = this.unavailableTunesData;

[
...this.tunesInstances.entries(),
...this.defaultTunesInstances.entries(),
]
.forEach(([name, tune]) => {
if (_.isFunction(tune.save)) {
try {
tunesData[name] = tune.save();
} catch (e) {
_.log(`Tune ${tune.constructor.name} save method throws an Error %o`, 'warn', e);
}
}
});

return tunesData;
}

/**
* Make default Block wrappers and put Tool`s content there
*
Expand Down
5 changes: 4 additions & 1 deletion src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -989,15 +989,18 @@ export default class BlockManager extends Module {
* @param detailData - additional data to pass with change event
*/
private blockDidMutated<Type extends BlockMutationType>(mutationType: Type, block: Block, detailData: BlockMutationEventDetailWithoutTarget<Type>): Block {
const tunesData = block.getTunesData();

const event = new CustomEvent(mutationType, {
detail: {
target: new BlockAPI(block),
tunes: tunesData,
...detailData as BlockMutationEventDetailWithoutTarget<Type>,
},
});

this.eventsDispatcher.emit(BlockChanged, {
event: event as BlockMutationEventMap[Type],
event: event as unknown as BlockMutationEventMap[Type],
});

return block;
Expand Down
1 change: 1 addition & 0 deletions src/tools/paragraph
Submodule paragraph added at 6e4541
3 changes: 3 additions & 0 deletions types/events/block/Base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BlockAPI } from '../../api';
import { BlockTuneData } from '../../block-tunes/block-tune-data';

/**
* Details of CustomEvent fired on block mutation
Expand All @@ -8,4 +9,6 @@ export interface BlockMutationEventDetail {
* Affected block
*/
target: BlockAPI;

tunes?: { [name: string]: BlockTuneData };
}
Loading