mirror of
https://github.com/codex-team/editor.js
synced 2026-03-14 22:55:47 +01:00
Feat (Conversion-config): pass target tool config to the conversionConfig.import method (#2848)
* pass config to the conversionConfig.import method - Now `convertStringToBlockData` method passes target tool config the import method - Fixed types in convesion config file (somehow imprort could return function that returns string, but import should return method that would return ToolData) this caused just type error that never been reached because types were actually ignored - Added test that checks, that import method actualy gets passed config * update changelog * eslint fix * updated test description * jsdoc improved * typos in changelog
This commit is contained in:
parent
3cd24c4da1
commit
da4257a67f
6 changed files with 92 additions and 11 deletions
|
|
@ -3,7 +3,7 @@ import type { BlockTool, BlockToolConstructorOptions } from '../../../../types';
|
|||
/**
|
||||
* Simple structure for Tool data
|
||||
*/
|
||||
interface MockToolData {
|
||||
export interface MockToolData {
|
||||
text: string;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type EditorJS from '../../../../types/index';
|
||||
import type { ConversionConfig, ToolboxConfig } from '../../../../types';
|
||||
import ToolMock from '../../fixtures/tools/ToolMock';
|
||||
import type { ConversionConfig, ToolboxConfig, ToolConfig } from '../../../../types';
|
||||
import ToolMock, { type MockToolData } from '../../fixtures/tools/ToolMock';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
/**
|
||||
|
|
@ -444,5 +444,84 @@ describe('api.blocks', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass tool config to the conversionConfig.import method of the tool', function () {
|
||||
const existingBlock = {
|
||||
id: 'test-id-123',
|
||||
type: 'paragraph',
|
||||
data: {
|
||||
text: 'Some text',
|
||||
},
|
||||
};
|
||||
|
||||
const conversionTargetToolConfig = {
|
||||
defaultStyle: 'defaultStyle',
|
||||
};
|
||||
|
||||
/**
|
||||
* Mock of Tool with conversionConfig
|
||||
*/
|
||||
class ToolWithConversionConfig extends ToolMock {
|
||||
/**
|
||||
* Specify conversion config of the tool
|
||||
*/
|
||||
public static get conversionConfig(): {
|
||||
/**
|
||||
* Method that is responsible for conversion from data to string
|
||||
*/
|
||||
export: (data: string) => string;
|
||||
|
||||
/**
|
||||
* Method that is responsible for conversion from string to data
|
||||
* Should return stringified config to see, if Editor actually passed tool config to it
|
||||
*/
|
||||
import: (content: string, config: ToolConfig) => MockToolData;
|
||||
} {
|
||||
return {
|
||||
export: (data) => data,
|
||||
/**
|
||||
* Passed config should be returned
|
||||
*/
|
||||
import: (_content, config) => {
|
||||
return { text: JSON.stringify(config) };
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
cy.createEditor({
|
||||
tools: {
|
||||
conversionTargetTool: {
|
||||
class: ToolWithConversionConfig,
|
||||
config: conversionTargetToolConfig,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
blocks: [
|
||||
existingBlock,
|
||||
],
|
||||
},
|
||||
}).then(async (editor) => {
|
||||
const { convert } = editor.blocks;
|
||||
|
||||
await convert(existingBlock.id, 'conversionTargetTool');
|
||||
|
||||
// wait for block to be converted
|
||||
cy.wait(100).then(async () => {
|
||||
/**
|
||||
* Check that block was converted
|
||||
*/
|
||||
const { blocks } = await editor.save();
|
||||
|
||||
expect(blocks.length).to.eq(1);
|
||||
expect(blocks[0].type).to.eq('conversionTargetTool');
|
||||
|
||||
/**
|
||||
* Check that tool converted returned config as a result of import
|
||||
*/
|
||||
expect(blocks[0].data.text).to.eq(JSON.stringify(conversionTargetToolConfig));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue