components/blockly-src/demos/blockfactory/factory.js
function updatePreview() {
  // Toggle between LTR/RTL if needed (also used in first display).
  var newDir = document.getElementById('direction').value;
  if (oldDir != newDir) {
    if (previewWorkspace) {
      previewWorkspace.dispose();
    }
    var rtl = newDir == 'rtl';
    previewWorkspace = Blockly.inject('preview',
        {rtl: rtl,
         media: '../../media/',
         scrollbars: true});
    oldDir = newDir;
  }
  previewWorkspace.clear();

  // Fetch the code and determine its format (JSON or JavaScript).
  var format = document.getElementById('format').value;
  if (format == 'Manual') {
    var code = document.getElementById('languageTA').value;
    // If the code is JSON, it will parse, otherwise treat as JS.
    try {
      JSON.parse(code);
      format = 'JSON';
    } catch (e) {
      format = 'JavaScript';
    }
  } else {
    var code = document.getElementById('languagePre').textContent;
  }
  if (!code.trim()) {
    // Nothing to render.  Happens while cloud storage is loading.
    return;
  }

  // Backup Blockly.Blocks object so that main workspace and preview don't
  // collide if user creates a 'factory_base' block, for instance.
  var backupBlocks = Blockly.Blocks;
  try {
    // Make a shallow copy.
    Blockly.Blocks = {};
    for (var prop in backupBlocks) {
      Blockly.Blocks[prop] = backupBlocks[prop];
    }

    if (format == 'JSON') {
      var json = JSON.parse(code);
      Blockly.Blocks[json.id || UNNAMED] = {
        init: function() {
          this.jsonInit(json);
        }
      };
    } else if (format == 'JavaScript') {
      eval(code);
    } else {
      throw 'Unknown format: ' + format;
    }

    // Look for a block on Blockly.Blocks that does not match the backup.
    var blockType = null;
    for (var type in Blockly.Blocks) {
      if (typeof Blockly.Blocks[type].init == 'function' &&
          Blockly.Blocks[type] != backupBlocks[type]) {
        blockType = type;
        break;
      }
    }
    if (!blockType) {
      return;
    }

    // Create the preview block.
    var previewBlock = previewWorkspace.newBlock(blockType);
    previewBlock.initSvg();
    previewBlock.render();
    previewBlock.setMovable(false);
    previewBlock.setDeletable(false);
    previewBlock.moveBy(15, 10);
    previewWorkspace.clearUndo();

    updateGenerator(previewBlock);
  } finally {
    Blockly.Blocks = backupBlocks;
  }
}
function updatePreview() {
  // Toggle between LTR/RTL if needed (also used in first display).
  var newDir = document.getElementById('direction').value;
  if (oldDir != newDir) {
    if (previewWorkspace) {
      previewWorkspace.dispose();
    }
    var rtl = newDir == 'rtl';
    previewWorkspace = Blockly.inject('preview',
        {rtl: rtl,
         media: '../../media/',
         scrollbars: true});
    oldDir = newDir;
  }
  previewWorkspace.clear();

  // Fetch the code and determine its format (JSON or JavaScript).
  var format = document.getElementById('format').value;
  if (format == 'Manual') {
    var code = document.getElementById('languageTA').value;
    // If the code is JSON, it will parse, otherwise treat as JS.
    try {
      JSON.parse(code);
      format = 'JSON';
    } catch (e) {
      format = 'JavaScript';
    }
  } else {
    var code = document.getElementById('languagePre').textContent;
  }
  if (!code.trim()) {
    // Nothing to render.  Happens while cloud storage is loading.
    return;
  }

  // Backup Blockly.Blocks object so that main workspace and preview don't
  // collide if user creates a 'factory_base' block, for instance.
  var backupBlocks = Blockly.Blocks;
  try {
    // Make a shallow copy.
    Blockly.Blocks = {};
    for (var prop in backupBlocks) {
      Blockly.Blocks[prop] = backupBlocks[prop];
    }

    if (format == 'JSON') {
      var json = JSON.parse(code);
      Blockly.Blocks[json.id || UNNAMED] = {
        init: function() {
          this.jsonInit(json);
        }
      };
    } else if (format == 'JavaScript') {
      eval(code);
    } else {
      throw 'Unknown format: ' + format;
    }

    // Look for a block on Blockly.Blocks that does not match the backup.
    var blockType = null;
    for (var type in Blockly.Blocks) {
      if (typeof Blockly.Blocks[type].init == 'function' &&
          Blockly.Blocks[type] != backupBlocks[type]) {
        blockType = type;
        break;
      }
    }
    if (!blockType) {
      return;
    }

    // Create the preview block.
    var previewBlock = previewWorkspace.newBlock(blockType);
    previewBlock.initSvg();
    previewBlock.render();
    previewBlock.setMovable(false);
    previewBlock.setDeletable(false);
    previewBlock.moveBy(15, 10);
    previewWorkspace.clearUndo();

    updateGenerator(previewBlock);
  } finally {
    Blockly.Blocks = backupBlocks;
  }
}
function updatePreview ( ) { // Toggle between LTR/RTL if needed (also used in first display). var newDir = document . getElementById ( 'direction' ) . value ; if ( oldDir != newDir ) { if ( previewWorkspace ) { previewWorkspace . dispose ( ) ; } var rtl = newDir == 'rtl' ; previewWorkspace = Blockly . inject ( 'preview' , { rtl : rtl , media : '../../media/' , scrollbars : true } ) ; oldDir = newDir ; } previewWorkspace . clear ( ) ; // Fetch the code and determine its format (JSON or JavaScript). var format = document . getElementById ( 'format' ) . value ; if ( format == 'Manual' ) { var code = document . getElementById ( 'languageTA' ) . value ; // If the code is JSON, it will parse, otherwise treat as JS. try { JSON . parse ( code ) ; format = 'JSON' ; } catch ( e ) { format = 'JavaScript' ; } } else { var code = document . getElementById ( 'languagePre' ) . textContent ; } if ( ! code . trim ( ) ) { // Nothing to render.  Happens while cloud storage is loading. return ; } // Backup Blockly.Blocks object so that main workspace and preview don't // collide if user creates a 'factory_base' block, for instance. var backupBlocks = Blockly . Blocks ; try { // Make a shallow copy. Blockly . Blocks = { } ; for ( var prop in backupBlocks ) { Blockly . Blocks [ prop ] = backupBlocks [ prop ] ; } if ( format == 'JSON' ) { var json = JSON . parse ( code ) ; Blockly . Blocks [ json . id || UNNAMED ] = { init : function ( ) { this . jsonInit ( json ) ; } } ; } else if ( format == 'JavaScript' ) { eval ( code ) ; } else { throw 'Unknown format: ' + format ; } // Look for a block on Blockly.Blocks that does not match the backup. var blockType = null ; for ( var type in Blockly . Blocks ) { if ( typeof Blockly . Blocks [ type ] . init == 'function' && Blockly . Blocks [ type ] != backupBlocks [ type ] ) { blockType = type ; break ; } } if ( ! blockType ) { return ; } // Create the preview block. var previewBlock = previewWorkspace . newBlock ( blockType ) ; previewBlock . initSvg ( ) ; previewBlock . render ( ) ; previewBlock . setMovable ( false ) ; previewBlock . setDeletable ( false ) ; previewBlock . moveBy ( 15 , 10 ) ; previewWorkspace . clearUndo ( ) ; updateGenerator ( previewBlock ) ; } finally { Blockly . Blocks = backupBlocks ; } }
Update the preview display.
Update the preview display .