Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
12944qwerty committed Oct 14, 2022
1 parent 5efdda4 commit 93b4782
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 207 deletions.
211 changes: 4 additions & 207 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,223 +14,20 @@
<script src="./TagScript/lists.js"></script>
<script src="./TagScript/variables.js"></script>
<script src="./TagScript/math.js"></script>
<script type=module>
<script type="module" src="./index.js"></script>

// Wait for Blockly to finish loading.

// const IS_UNCOMPRESSED = !window.bootstrapInfo.compressed; // See bootstrap.js
var workspace = null;

function start() {
setBackgroundColour();

var toolbox = getToolboxElement();
var match = location.search.match(/side=([^&]+)/);
var autoimport = !!location.search.match(/autoimport=([^&]+)/);
// Create main workspace.
workspace = Blockly.inject('blocklyDiv',
{
comments: true,
collapse: true,
disable: true,
grid:
{
spacing: 25,
length: 3,
colour: '#ccc',
snap: true
},
horizontalLayout: false,
maxBlocks: Infinity,
maxInstances: {'test_basic_limit_instances': 3},
maxTrashcanContents: 256,
media: 'https://unpkg.com/blockly/media/',
oneBasedIndex: true,
readOnly: false,
move: {
scrollbars: true,
drag: true,
wheel: false,
},
toolbox: toolbox,
toolboxPosition: 'start',
renderer: 'geras',
zoom:
{
controls: true,
wheel: true,
startScale: 1.0,
maxScale: 4,
minScale: 0.25,
scaleSpeed: 1.1
}
});
workspace.configureContextMenu = configureContextMenu;
// Restore previously displayed text.
if (localStorage) {
var text = localStorage.getItem('textarea');
if (text) {
document.getElementById('importExportjson').value = text;
}
}

load();

addEventHandlers();
}

function setBackgroundColour() {
document.body.style.backgroundColor = '#60fcfc'; // Familiar lilac.
}

function getToolboxElement() {
return document.getElementById('toolbox-categories');
}

function saveJson() {
var output = document.getElementById('importExportjson');
var state = Blockly.serialization.workspaces.save(workspace);
var save = JSON.stringify(state, null, 2);
output.value = save;
localStorage.setItem('textarea', save);
}

function load() {
var input = document.getElementById('importExportjson');
if (!input.value) {
return;
}
var valid = saveIsValid(input.value);
if (valid.json) {
var state = JSON.parse(input.value);
Blockly.serialization.workspaces.load(state, workspace);
} else if (valid.xml) {
var xml = Blockly.Xml.textToDom(input.value);
Blockly.Xml.domToWorkspace(xml, workspace);
}

toCode();
}


function toCode() {
var output = document.getElementById('importExport');
output.value = TagScript.workspaceToCode(workspace);
}

// Disable the "Load" button if the save state is invalid.
// Preserve text between page reloads.
function taChange() {
toCode()
saveJson();
}

function saveIsValid(save) {
var validJson = true;
try {
JSON.parse(save);
} catch (e) {
validJson = false;
}
var validXml = true
try {
Blockly.Xml.textToDom(save);
} catch (e) {
validXml = false;
}
return {
json: validJson,
xml: validXml
}
}


function configureContextMenu(menuOptions, e) {
var screenshotOption = {
text: 'Download Screenshot',
enabled: workspace.getTopBlocks().length,
callback: function() {
downloadScreenshot(workspace);
}
};
menuOptions.push(screenshotOption);

// Adds a default-sized workspace comment to the workspace.
menuOptions.push(Blockly.ContextMenu.workspaceCommentOption(workspace, e));
}

function addEventHandlers() {
document.getElementById('importExportjson').addEventListener('change', taChange);
document.getElementById('importExportjson').addEventListener('keyup', taChange);

document.getElementById('show')
.addEventListener('click', function() { workspace.setVisible(true); });
document.getElementById('hide')
.addEventListener('click', function() { workspace.setVisible(false); });

workspace.addChangeListener(taChange)
}

// Call start(). Because this <script> has type=module, it is
// automatically deferred, so it will not be run until after the
// document has been parsed, but before firing DOMContentLoaded.
start();

</script>

<style>
html, body {
height: 100%;
}
body {
background-color: #fff;
font-family: sans-serif;
overflow: hidden;
}
h1 {
font-weight: normal;
font-size: 140%;
}
#blocklyDiv {
float: right;
height: 95%;
width: 70%;
}
#importExport {
font-family: monospace;
}

.ioLabel>.blocklyFlyoutLabelText {
font-style: italic;
}

#blocklyDiv.renderingDebug .blockRenderDebug {
display: block;
}

.playgroundToggleOptions {
list-style: none;
padding: 0;
}
.playgroundToggleOptions li {
margin-top: 1em;
}

.zelos-renderer .blocklyFlyoutButton .blocklyText {
font-size: 1.5rem;
}
</style>
<link rel="stylesheet" href="style.css">
</head>
<body>

<div id="blocklyDiv"></div>

<h1>TagScript Block Editor</h1>
<p class="version">by 12944qwerty - v0.0.1</p>

<p>
<input id="show" type="button" value="Show"></input> -
<input id="hide" type="button" value="Hide"></input> -
<input id="clear" type="button" value="Clear" onclick="(function() { localStorage.setItem('toolbox'); load() })()"></input>
<input id="clear" type="button" value="Clear"></input>
</p>
<p>
TagScript Code
Expand Down
149 changes: 149 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
var workspace = null;

function start() {
setBackgroundColour();

var toolbox = getToolboxElement();
// Create main workspace.
workspace = Blockly.inject('blocklyDiv', {
comments: true,
collapse: true,
disable: true,
grid: {
spacing: 25,
length: 3,
colour: '#ccc',
snap: true
},
horizontalLayout: false,
maxBlocks: Infinity,
maxInstances: {'test_basic_limit_instances': 3},
maxTrashcanContents: 256,
media: 'https://unpkg.com/blockly/media/',
oneBasedIndex: true,
readOnly: false,
move: {
scrollbars: true,
drag: true,
wheel: false,
},
toolbox: toolbox,
toolboxPosition: 'start',
renderer: 'geras',
zoom: {
controls: true,
wheel: true,
startScale: 1.0,
maxScale: 4,
minScale: 0.25,
scaleSpeed: 1.1
}
});

if (localStorage) {
var text = localStorage.getItem('textarea');
if (text) {
document.getElementById('importExportjson').value = text;
}
}

load();

addEventHandlers();
}

function setBackgroundColour() {
document.body.style.backgroundColor = '#60fcfc'; // Familiar lilac.
}

function getToolboxElement() {
return document.getElementById('toolbox-categories');
}

function saveJson() {
var output = document.getElementById('importExportjson');
var state = Blockly.serialization.workspaces.save(workspace);
var save = JSON.stringify(state, null, 2);
output.value = save;
localStorage.setItem('textarea', save);
}

function load() {
var input = document.getElementById('importExportjson');
if (!input.value) {
return;
}
var valid = saveIsValid(input.value);
if (valid.json) {
var state = JSON.parse(input.value);
Blockly.serialization.workspaces.load(state, workspace);
} else if (valid.xml) {
var xml = Blockly.Xml.textToDom(input.value);
Blockly.Xml.domToWorkspace(xml, workspace);
}

toCode();
}


function toCode() {
var output = document.getElementById('importExport');
output.value = TagScript.workspaceToCode(workspace);
}

// Disable the "Load" button if the save state is invalid.
// Preserve text between page reloads.
function taChange() {
toCode()
saveJson();
}

function saveIsValid(save) {
var validJson = true;
try {
JSON.parse(save);
} catch (e) {
validJson = false;
}
var validXml = true
try {
Blockly.Xml.textToDom(save);
} catch (e) {
validXml = false;
}
return {
json: validJson,
xml: validXml
}
}


function configureContextMenu(menuOptions, e) {
var screenshotOption = {
text: 'Download Screenshot',
enabled: workspace.getTopBlocks().length,
callback: function() {
downloadScreenshot(workspace);
}
};
menuOptions.push(screenshotOption);

// Adds a default-sized workspace comment to the workspace.
menuOptions.push(Blockly.ContextMenu.workspaceCommentOption(workspace, e));
}

function addEventHandlers() {
document.getElementById('importExportjson').addEventListener('change', taChange);
document.getElementById('importExportjson').addEventListener('keyup', taChange);

document.getElementById('show')
.addEventListener('click', function() { workspace.setVisible(true); });
document.getElementById('hide')
.addEventListener('click', function() { workspace.setVisible(false); });
document.getElementById('clear')
.addEventListener('click', function() { document.getElementById('importExportjson').value = '{}'; load() });

workspace.addChangeListener(taChange)
}

start();
Loading

0 comments on commit 93b4782

Please sign in to comment.