Skip to content

Commit da4257a

Browse files
authored
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
1 parent 3cd24c4 commit da4257a

File tree

6 files changed

+92
-11
lines changed

6 files changed

+92
-11
lines changed

docs/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### 2.31.0
44

55
- `New` - Inline tools (those with `isReadOnlySupported` specified) can now be used in read-only mode
6+
- `Improvement` - Block manager passes target tool config to the `conversionConfig.import` method on conversion
67
- `Fix` - Fix selection of first block in read-only initialization with "autofocus=true"
78
- `Fix` - Incorrect caret position after blocks merging in Safari
89
- `Fix` - Several toolbox items exported by the one tool have the same shortcut displayed in toolbox
@@ -49,11 +50,11 @@
4950
- `New` – "Convert to" control is now also available in Block Tunes
5051
- `New` — Editor.js now supports contenteditable placeholders out of the box. Just add `data-placeholder` or `data-placeholder-active` attribute to make it work. The first one will work like native placeholder while the second one will show placeholder only when block is current.
5152
- `Improvement` — Now Paragraph placeholder will be shown for the current paragraph, not only the first one.
52-
- `Improvment` - The API `blocks.update` now accepts `tunes` data as optional third argument and makes `data` - block data as optional.
53+
- `Improvement` - The API `blocks.update` now accepts `tunes` data as optional third argument and makes `data` - block data as optional.
5354
- `Improvement` — The ability to merge blocks of different types (if both tools provide the conversionConfig)
5455
- `Improvement` - The API `blocks.convert()` now returns the new block API
5556
- `Improvement` - The API `caret.setToBlock()` now can accept either BlockAPI or block index or block id
56-
- `Impovement`*MenuConfig*`TunesMenuConfig` type is deprecated, use the `MenuConfig` instead
57+
- `Improvement`*MenuConfig*`TunesMenuConfig` type is deprecated, use the `MenuConfig` instead
5758
`Improvement`*Types*`BlockToolConstructorOptions` type improved, `block` and `config` are not optional anymore
5859
- `Improvement` - The Plus button and Block Tunes toggler are now better aligned with large line-height blocks, such as Headings
5960
- `Improvement` — Creating links on Android devices: now the mobile keyboard will have an "Enter" key for accepting the inserted link.

src/components/modules/blockManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ export default class BlockManager extends Module {
863863
/**
864864
* Now using Conversion Config "import" we compose a new Block data
865865
*/
866-
let newBlockData = convertStringToBlockData(cleanData, replacingTool.conversionConfig);
866+
let newBlockData = convertStringToBlockData(cleanData, replacingTool.conversionConfig, replacingTool.settings);
867867

868868
/**
869869
* Optional data overrides.

src/components/utils/blocks.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BlockAPI } from '../../../types';
1+
import type { BlockAPI, ToolConfig } from '../../../types';
22
import type { ConversionConfig } from '../../../types/configs/conversion-config';
33
import type { SavedData } from '../../../types/data-formats';
44
import type { BlockToolData } from '../../../types/tools/block-tool-data';
@@ -174,12 +174,13 @@ export function convertBlockDataToString(blockData: BlockToolData, conversionCon
174174
*
175175
* @param stringToImport - string to convert
176176
* @param conversionConfig - tool's conversion config
177+
* @param targetToolConfig - target tool config, used in conversionConfig.import method
177178
*/
178-
export function convertStringToBlockData(stringToImport: string, conversionConfig?: ConversionConfig): BlockToolData {
179+
export function convertStringToBlockData(stringToImport: string, conversionConfig?: ConversionConfig, targetToolConfig?: ToolConfig): BlockToolData {
179180
const importProp = conversionConfig?.import;
180181

181182
if (isFunction(importProp)) {
182-
return importProp(stringToImport);
183+
return importProp(stringToImport, targetToolConfig);
183184
} else if (isString(importProp)) {
184185
return {
185186
[importProp]: stringToImport,

test/cypress/fixtures/tools/ToolMock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { BlockTool, BlockToolConstructorOptions } from '../../../../types';
33
/**
44
* Simple structure for Tool data
55
*/
6-
interface MockToolData {
6+
export interface MockToolData {
77
text: string;
88
}
99

test/cypress/tests/api/blocks.cy.ts

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type EditorJS from '../../../../types/index';
2-
import type { ConversionConfig, ToolboxConfig } from '../../../../types';
3-
import ToolMock from '../../fixtures/tools/ToolMock';
2+
import type { ConversionConfig, ToolboxConfig, ToolConfig } from '../../../../types';
3+
import ToolMock, { type MockToolData } from '../../fixtures/tools/ToolMock';
44
import { nanoid } from 'nanoid';
55

66
/**
@@ -444,5 +444,84 @@ describe('api.blocks', () => {
444444
});
445445
});
446446
});
447+
448+
it('should pass tool config to the conversionConfig.import method of the tool', function () {
449+
const existingBlock = {
450+
id: 'test-id-123',
451+
type: 'paragraph',
452+
data: {
453+
text: 'Some text',
454+
},
455+
};
456+
457+
const conversionTargetToolConfig = {
458+
defaultStyle: 'defaultStyle',
459+
};
460+
461+
/**
462+
* Mock of Tool with conversionConfig
463+
*/
464+
class ToolWithConversionConfig extends ToolMock {
465+
/**
466+
* Specify conversion config of the tool
467+
*/
468+
public static get conversionConfig(): {
469+
/**
470+
* Method that is responsible for conversion from data to string
471+
*/
472+
export: (data: string) => string;
473+
474+
/**
475+
* Method that is responsible for conversion from string to data
476+
* Should return stringified config to see, if Editor actually passed tool config to it
477+
*/
478+
import: (content: string, config: ToolConfig) => MockToolData;
479+
} {
480+
return {
481+
export: (data) => data,
482+
/**
483+
* Passed config should be returned
484+
*/
485+
import: (_content, config) => {
486+
return { text: JSON.stringify(config) };
487+
},
488+
};
489+
}
490+
}
491+
492+
cy.createEditor({
493+
tools: {
494+
conversionTargetTool: {
495+
class: ToolWithConversionConfig,
496+
config: conversionTargetToolConfig,
497+
},
498+
},
499+
data: {
500+
blocks: [
501+
existingBlock,
502+
],
503+
},
504+
}).then(async (editor) => {
505+
const { convert } = editor.blocks;
506+
507+
await convert(existingBlock.id, 'conversionTargetTool');
508+
509+
// wait for block to be converted
510+
cy.wait(100).then(async () => {
511+
/**
512+
* Check that block was converted
513+
*/
514+
const { blocks } = await editor.save();
515+
516+
expect(blocks.length).to.eq(1);
517+
expect(blocks[0].type).to.eq('conversionTargetTool');
518+
519+
/**
520+
* Check that tool converted returned config as a result of import
521+
*/
522+
expect(blocks[0].data.text).to.eq(JSON.stringify(conversionTargetToolConfig));
523+
});
524+
});
525+
});
447526
});
448527
});

types/configs/conversion-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BlockToolData } from '../tools';
1+
import type { BlockToolData, ToolConfig } from '../tools';
22

33
/**
44
* Config allows Tool to specify how it can be converted into/from another Tool
@@ -12,7 +12,7 @@ export interface ConversionConfig {
1212
* 1. String — the key of Tool data object to fill it with imported string on render.
1313
* 2. Function — method that accepts importing string and composes Tool data to render.
1414
*/
15-
import?: ((data: string) => string) | string;
15+
import?: ((data: string, config: ToolConfig) => BlockToolData) | string;
1616

1717
/**
1818
* How to export this Tool to make other Block.

0 commit comments

Comments
 (0)