Skip to content

Commit fed7ee0

Browse files
authored
feat: Added button to Advanced Playground to import state. (#2483)
1 parent bb8904e commit fed7ee0

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

plugins/dev-tools/src/playground/index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ import {id} from './id';
2424
import {addCodeEditor} from './monaco';
2525
import {addGUIControls} from './options';
2626
import {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;

plugins/dev-tools/src/playground/ui.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ export function renderPlayground(container) {
109109
const tabButtons = document.createElement('div');
110110
tabButtons.style.position = 'absolute';
111111
tabButtons.style.height = '30px';
112-
tabButtons.style.width = '50px';
113112
tabButtons.style.top = '0';
114113
tabButtons.style.right = '0';
115114
tabButtons.style.display = 'flex';
@@ -166,3 +165,15 @@ export function renderCheckbox(id, label) {
166165
checkboxLabel.setAttribute('for', id);
167166
return [checkbox, checkboxLabel];
168167
}
168+
169+
/**
170+
* Render a button.
171+
* @param {string} label The text content of the button.
172+
* @returns {HTMLButtonElement} The button element.
173+
*/
174+
export function renderButton(label) {
175+
const button = document.createElement('button');
176+
button.setAttribute('type', 'button');
177+
button.textContent = label;
178+
return button;
179+
}

0 commit comments

Comments
 (0)