@@ -24,7 +24,12 @@ import {id} from './id';
2424import { addCodeEditor } from './monaco' ;
2525import { addGUIControls } from './options' ;
2626import { LocalStorageState } from './state' ;
27- import { renderCheckbox , renderCodeTab , renderPlayground } from './ui' ;
27+ import {
28+ renderButton ,
29+ renderCheckbox ,
30+ renderCodeTab ,
31+ renderPlayground ,
32+ } from './ui' ;
2833
2934// Declare external types to make eslint happy.
3035/* global dat, monaco */
@@ -222,6 +227,7 @@ export function createPlayground(
222227 // Update editor state.
223228 editorXmlContextKey . set ( isXml ) ;
224229 editorJsonContextKey . set ( isJson ) ;
230+ updateImportButtonDisplay ( ) ;
225231 playgroundState . set ( 'activeTab' , tab . state . name ) ;
226232 playgroundState . save ( ) ;
227233 } ;
@@ -341,6 +347,26 @@ export function createPlayground(
341347 playgroundState . save ( ) ;
342348 } ) ;
343349
350+ // Create a button to import state from the editor tab to the workspace.
351+ const importButton = renderButton ( 'Import' ) ;
352+ tabButtons . appendChild ( importButton ) ;
353+ importButton . addEventListener ( 'click' , ( e ) => {
354+ if ( editorXmlContextKey . get ( ) ) {
355+ editor . getAction ( 'import-xml' ) . run ( ) ;
356+ }
357+ if ( editorJsonContextKey . get ( ) ) {
358+ editor . getAction ( 'import-json' ) . run ( ) ;
359+ }
360+ } ) ;
361+ const updateImportButtonDisplay = function ( ) {
362+ // The import button is only relevant for the XML and JSON tabs.
363+ if ( editorXmlContextKey . get ( ) || editorJsonContextKey . get ( ) ) {
364+ importButton . style . display = '' ;
365+ } else {
366+ importButton . style . display = 'none' ;
367+ }
368+ } ;
369+
344370 // Set the initial tab as active.
345371 const activeTab = playgroundState . get ( 'activeTab' ) ;
346372 let currentTab = tabs [ activeTab ] ;
@@ -552,7 +578,10 @@ function registerEditorCommands(editor, playground) {
552578 const xml = editor . getModel ( ) . getValue ( ) ;
553579 const workspace = playground . getWorkspace ( ) ;
554580 try {
555- Blockly . Xml . domToWorkspace ( Blockly . utils . xml . textToDom ( xml ) , workspace ) ;
581+ Blockly . Xml . clearWorkspaceAndLoadFromXml (
582+ Blockly . utils . xml . textToDom ( xml ) ,
583+ workspace ,
584+ ) ;
556585 } catch ( e ) {
557586 // If this fails that's fine.
558587 return false ;
0 commit comments