feat: use convert config instead of defined property

This commit is contained in:
GuillaumeOnepilot 2023-12-23 15:14:59 +01:00
parent a4b56317eb
commit 810935b609
6 changed files with 24 additions and 29 deletions

View file

@ -389,6 +389,8 @@ export default class Block extends EventsDispatcher<BlockEvents> {
* @returns {boolean}
*/
public get mergeable(): boolean {
console.log(this.toolInstance.merge);
return _.isFunction(this.toolInstance.merge);
}
@ -995,11 +997,4 @@ export default class Block extends EventsDispatcher<BlockEvents> {
private dropInputsCache(): void {
this.cachedInputs = [];
}
/**
* Get the list of mergeable blocks with the current block
*/
public get mergeableWithBlocks(): string[]|undefined {
return this.tool.mergeableBlocks;
}
}

View file

@ -471,7 +471,24 @@ export default class BlockManager extends Module {
* @returns {Promise} - the sequence that can be continued
*/
public async mergeBlocks(targetBlock: Block, blockToMerge: Block): Promise<void> {
const blockToMergeData = await blockToMerge.data;
const data = await blockToMerge.data;
const getBlockToolData = async (): Promise<BlockToolData> => {
const importConfig = targetBlock.tool.conversionConfig.import;
const exportConfig = blockToMerge.tool.conversionConfig.export;
if (typeof importConfig === 'string') {
return {
[importConfig]: typeof exportConfig === 'string' ? data[exportConfig] : exportConfig(data),
};
}
return importConfig(await blockToMerge.exportDataAsString());
};
const blockToMergeData = targetBlock.name !== blockToMerge.name
? await getBlockToolData()
: data;
if (!_.isEmpty(blockToMergeData)) {
await targetBlock.mergeWith(blockToMergeData, targetBlock.name);

View file

@ -90,8 +90,6 @@ export enum InternalBlockToolSettings {
* Tool paste config
*/
PasteConfig = 'pasteConfig',
MergeableBlocks = 'mergeableBlocks'
}
/**

View file

@ -212,11 +212,4 @@ export default class BlockTool extends BaseTool<IBlockTool> {
return baseConfig;
}
/**
*
*/
public get mergeableBlocks(): string[]|undefined {
return this.constructable[InternalBlockToolSettings.MergeableBlocks];
}
}

View file

@ -9,12 +9,15 @@ import { isFunction, isString, log } from '../utils';
* We can merge two blocks if:
* - they have the same type
* - they have a merge function (.mergeable = true)
* - If they have valid conversions config
*
* @param targetBlock - block to merge to
* @param blockToMerge - block to merge from
*/
export function areBlocksMergeable(targetBlock: Block, blockToMerge: Block): boolean {
if (blockToMerge.mergeableWithBlocks?.includes(targetBlock.name)) {
if (blockToMerge.mergeable &&
blockToMerge.tool.conversionConfig?.export !== undefined &&
targetBlock.tool.conversionConfig?.import !== undefined) {
return true;
}

View file

@ -12,12 +12,6 @@ import { TunesMenuConfig } from './tool-settings';
* @see {@link docs/tools.md}
*/
export interface BlockTool extends BaseTool {
/**
* Block that can be merged with the block
*/
mergeableBlocks?: string[];
/**
* Sanitizer rules description
*/
@ -100,11 +94,6 @@ export interface BlockToolConstructorOptions<D extends object = any, C extends o
export interface BlockToolConstructable extends BaseToolConstructable {
/**
* Block types that can be merged with the block
*/
mergeableBlocks?: string[];
/**
* Tool's Toolbox settings
*/