(block: BlockAPI, allBlockTools: BlockToolAdapter[])
| 48 | * @param allBlockTools - all block tools available in the editor |
| 49 | */ |
| 50 | export async function getConvertibleToolsForBlock(block: BlockAPI, allBlockTools: BlockToolAdapter[]): Promise<BlockToolAdapter[]> { |
| 51 | const savedData = await block.save() as SavedData; |
| 52 | const blockData = savedData.data; |
| 53 | |
| 54 | /** |
| 55 | * Checking that the block's tool has an «export» rule |
| 56 | */ |
| 57 | const blockTool = allBlockTools.find((tool) => tool.name === block.name); |
| 58 | |
| 59 | if (blockTool !== undefined && !isToolConvertable(blockTool, 'export')) { |
| 60 | return []; |
| 61 | } |
| 62 | |
| 63 | return allBlockTools.reduce((result, tool) => { |
| 64 | /** |
| 65 | * Skip tools without «import» rule specified |
| 66 | */ |
| 67 | if (!isToolConvertable(tool, 'import')) { |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Skip tools that does not specify toolbox |
| 73 | */ |
| 74 | if (tool.toolbox === undefined) { |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | /** Filter out invalid toolbox entries */ |
| 79 | const actualToolboxItems = tool.toolbox.filter((toolboxItem) => { |
| 80 | /** |
| 81 | * Skip items that don't pass 'toolbox' property or do not have an icon |
| 82 | */ |
| 83 | if (isEmpty(toolboxItem) || toolboxItem.icon === undefined) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if (toolboxItem.data !== undefined) { |
| 88 | /** |
| 89 | * When a tool has several toolbox entries, we need to make sure we do not add |
| 90 | * toolbox item with the same data to the resulting array. This helps exclude duplicates |
| 91 | */ |
| 92 | if (isSameBlockData(toolboxItem.data, blockData)) { |
| 93 | return false; |
| 94 | } |
| 95 | } else if (tool.name === block.name) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | return true; |
| 100 | }); |
| 101 | |
| 102 | result.push({ |
| 103 | ...tool, |
| 104 | toolbox: actualToolboxItems, |
| 105 | } as BlockToolAdapter); |
| 106 | |
| 107 | return result; |
no test coverage detected