Skip to content

Commit

Permalink
feat: Added button to Advanced Playground to import state.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnesky committed Feb 1, 2025
1 parent 2d4d03f commit 4c26207
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
33 changes: 31 additions & 2 deletions plugins/dev-tools/src/playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import {id} from './id';
import {addCodeEditor} from './monaco';
import {addGUIControls} from './options';
import {LocalStorageState} from './state';
import {renderCheckbox, renderCodeTab, renderPlayground} from './ui';
import {
renderButton,
renderCheckbox,
renderCodeTab,
renderPlayground,
} from './ui';

// Declare external types to make eslint happy.
/* global dat, monaco */
Expand Down Expand Up @@ -222,6 +227,7 @@ export function createPlayground(
// Update editor state.
editorXmlContextKey.set(isXml);
editorJsonContextKey.set(isJson);
updateImportButtonDisplay();
playgroundState.set('activeTab', tab.state.name);
playgroundState.save();
};
Expand Down Expand Up @@ -341,6 +347,26 @@ export function createPlayground(
playgroundState.save();
});

// Create a button to import state from the editor tab to the workspace.
const importButton = renderButton('Import');
tabButtons.appendChild(importButton);
importButton.addEventListener('click', (e) => {
if (editorXmlContextKey.get()) {
editor.getAction('import-xml').run();
}
if (editorJsonContextKey.get()) {
editor.getAction('import-json').run();
}
});
const updateImportButtonDisplay = function () {
// The import button is only relevant for the XML and JSON tabs.
if (editorXmlContextKey.get() || editorJsonContextKey.get()) {
importButton.style.display = '';
} else {
importButton.style.display = 'none';
}
};

// Set the initial tab as active.
const activeTab = playgroundState.get('activeTab');
let currentTab = tabs[activeTab];
Expand Down Expand Up @@ -552,7 +578,10 @@ function registerEditorCommands(editor, playground) {
const xml = editor.getModel().getValue();
const workspace = playground.getWorkspace();
try {
Blockly.Xml.domToWorkspace(Blockly.utils.xml.textToDom(xml), workspace);
Blockly.Xml.clearWorkspaceAndLoadFromXml(
Blockly.utils.xml.textToDom(xml),
workspace,
);
} catch (e) {
// If this fails that's fine.
return false;
Expand Down
13 changes: 12 additions & 1 deletion plugins/dev-tools/src/playground/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export function renderPlayground(container) {
const tabButtons = document.createElement('div');
tabButtons.style.position = 'absolute';
tabButtons.style.height = '30px';
tabButtons.style.width = '50px';
tabButtons.style.top = '0';
tabButtons.style.right = '0';
tabButtons.style.display = 'flex';
Expand Down Expand Up @@ -166,3 +165,15 @@ export function renderCheckbox(id, label) {
checkboxLabel.setAttribute('for', id);
return [checkbox, checkboxLabel];
}

/**
* Render a button.
* @param {string} label The text content of the button.
* @returns {HTMLButtonElement} The button element.
*/
export function renderButton(label) {
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.textContent = label;
return button;
}

0 comments on commit 4c26207

Please sign in to comment.