Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function (blockId) {
// Not sure any better way to access the scratch-blocks workspace than this...
const block = ScratchBlocks.getMainWorkspace().getBlockById(blockId);
const blockSvg = block.getSvgRoot().cloneNode(true /* deep */);
// Once we have the cloned SVG, do the rest in a setTimeout to prevent
// blocking the drag end from finishing promptly.
return new Promise(resolve => {
setTimeout(() => {
// Strip entities that cannot be inlined
blockSvg.innerHTML = blockSvg.innerHTML.replace(/ /g, ' ');
// Create an <svg> element to put the cloned blockSvg inside
const NS = 'http://www.w3.org/2000/svg';
const svg = document.createElementNS(NS, 'svg');
svg.appendChild(blockSvg);
// Needs to be on the DOM to get CSS properties and correct sizing
document.body.appendChild(svg);</svg>