mirror of
https://github.com/codex-team/editor.js
synced 2024-06-09 09:22:16 +02:00
feat: use convert config instead of defined property
This commit is contained in:
parent
a4b56317eb
commit
810935b609
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -90,8 +90,6 @@ export enum InternalBlockToolSettings {
|
|||
* Tool paste config
|
||||
*/
|
||||
PasteConfig = 'pasteConfig',
|
||||
|
||||
MergeableBlocks = 'mergeableBlocks'
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -212,11 +212,4 @@ export default class BlockTool extends BaseTool<IBlockTool> {
|
|||
|
||||
return baseConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public get mergeableBlocks(): string[]|undefined {
|
||||
return this.constructable[InternalBlockToolSettings.MergeableBlocks];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
11
types/tools/block-tool.d.ts
vendored
11
types/tools/block-tool.d.ts
vendored
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue