feature: possibilities to merge blocks of different types

This commit is contained in:
GuillaumeOnepilot 2023-12-16 21:31:36 +01:00
parent 5e8fe06dd6
commit bd839d8d85
6 changed files with 40 additions and 6 deletions

View file

@ -549,9 +549,10 @@ export default class Block extends EventsDispatcher<BlockEvents> {
* Call plugins merge method
*
* @param {BlockToolData} data - data to merge
* @param {string} type - type of the block to merge
*/
public async mergeWith(data: BlockToolData): Promise<void> {
await this.toolInstance.merge(data);
public async mergeWith(data: BlockToolData, type: string): Promise<void> {
await this.toolInstance.merge(data, type);
}
/**
@ -994,4 +995,11 @@ 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

@ -474,7 +474,7 @@ export default class BlockManager extends Module {
const blockToMergeData = await blockToMerge.data;
if (!_.isEmpty(blockToMergeData)) {
await targetBlock.mergeWith(blockToMergeData);
await targetBlock.mergeWith(blockToMergeData, targetBlock.name);
}
this.removeBlock(blockToMerge);

View file

@ -89,7 +89,9 @@ export enum InternalBlockToolSettings {
/**
* Tool paste config
*/
PasteConfig = 'pasteConfig'
PasteConfig = 'pasteConfig',
MergeableBlocks = 'mergeableBlocks'
}
/**

View file

@ -35,7 +35,7 @@ export default class BlockTool extends BaseTool<IBlockTool> {
/**
* Tool's constructable blueprint
*/
protected constructable: BlockToolConstructable;
public constructable: BlockToolConstructable;
/**
* Creates new Tool instance
@ -212,4 +212,11 @@ export default class BlockTool extends BaseTool<IBlockTool> {
return baseConfig;
}
/**
*
*/
public get mergeableBlocks(): string[]|undefined {
return this.constructable[InternalBlockToolSettings.MergeableBlocks];
}
}

View file

@ -14,6 +14,10 @@ import { isFunction, isString, log } from '../utils';
* @param blockToMerge - block to merge from
*/
export function areBlocksMergeable(targetBlock: Block, blockToMerge: Block): boolean {
if (blockToMerge.mergeableWithBlocks?.includes(targetBlock.name)) {
return true;
}
return targetBlock.mergeable && targetBlock.name === blockToMerge.name;
}

View file

@ -12,6 +12,12 @@ 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
*/
@ -40,8 +46,9 @@ export interface BlockTool extends BaseTool {
* Method that specified how to merge two Blocks with same type.
* Called by backspace at the beginning of the Block
* @param {BlockToolData} blockData
* @param {string} type
*/
merge?(blockData: BlockToolData): void;
merge?(blockData: BlockToolData, type: string): void;
/**
* On paste callback. Fired when pasted content can be substituted by a Tool
@ -92,6 +99,12 @@ 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
*/