* Converts passed Block to the new Tool * Uses Conversion Config * * @param blockToConvert - Block that should be converted * @param targetToolName - name of the Tool to convert to * @param blockDataOverrides - optional new Block data overrides
(blockToConvert: Block, targetToolName: string, blockDataOverrides?: BlockToolData)
| 829 | * @param blockDataOverrides - optional new Block data overrides |
| 830 | */ |
| 831 | public async convert(blockToConvert: Block, targetToolName: string, blockDataOverrides?: BlockToolData): Promise<Block> { |
| 832 | /** |
| 833 | * At first, we get current Block data |
| 834 | */ |
| 835 | const savedBlock = await blockToConvert.save(); |
| 836 | |
| 837 | if (!savedBlock) { |
| 838 | throw new Error('Could not convert Block. Failed to extract original Block data.'); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Getting a class of the replacing Tool |
| 843 | */ |
| 844 | const replacingTool = this.Editor.Tools.blockTools.get(targetToolName); |
| 845 | |
| 846 | if (!replacingTool) { |
| 847 | throw new Error(`Could not convert Block. Tool «${targetToolName}» not found.`); |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Using Conversion Config "export" we get a stringified version of the Block data |
| 852 | */ |
| 853 | const exportedData = await blockToConvert.exportDataAsString(); |
| 854 | |
| 855 | /** |
| 856 | * Clean exported data with replacing sanitizer config |
| 857 | */ |
| 858 | const cleanData: string = clean( |
| 859 | exportedData, |
| 860 | replacingTool.sanitizeConfig |
| 861 | ); |
| 862 | |
| 863 | /** |
| 864 | * Now using Conversion Config "import" we compose a new Block data |
| 865 | */ |
| 866 | let newBlockData = convertStringToBlockData(cleanData, replacingTool.conversionConfig, replacingTool.settings); |
| 867 | |
| 868 | /** |
| 869 | * Optional data overrides. |
| 870 | * Used for example, by the Multiple Toolbox Items feature, where a single Tool provides several Toolbox items with "data" overrides |
| 871 | */ |
| 872 | if (blockDataOverrides) { |
| 873 | newBlockData = Object.assign(newBlockData, blockDataOverrides); |
| 874 | } |
| 875 | |
| 876 | return this.replace(blockToConvert, replacingTool.name, newBlockData); |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Sets current Block Index -1 which means unknown |
nothing calls this directly
no test coverage detected