deserialize new format for exported scripts

snap8
Jens Mönig 2022-03-15 13:27:36 +01:00
rodzic 938c01dd10
commit 39953da704
5 zmienionych plików z 68 dodań i 39 usunięć

Wyświetl plik

@ -13,6 +13,9 @@
* **Documentation Updates:**
* **Translation Updates:**
### 2022-03-15
* blocks, store, gui: deserialize new format for exported scripts
### 2022-03-14
* gui, byob: refactored library serialization
* blocks, byob, gui: new format for exported scripts, under construction

Wyświetl plik

@ -16,11 +16,11 @@
<script src="src/morphic.js?version=2022-01-28"></script>
<script src="src/symbols.js?version=2021-03-03"></script>
<script src="src/widgets.js?version=2021-17-09"></script>
<script src="src/blocks.js?version=2022-03-14"></script>
<script src="src/blocks.js?version=2022-03-15"></script>
<script src="src/threads.js?version=2022-03-03"></script>
<script src="src/objects.js?version=2022-03-11"></script>
<script src="src/scenes.js?version=2022-03-03"></script>
<script src="src/gui.js?version=2022-03-14"></script>
<script src="src/gui.js?version=2022-03-15"></script>
<script src="src/paint.js?version=2021-07-05"></script>
<script src="src/lists.js?version=2022-02-07"></script>
<script src="src/byob.js?version=2022-03-14"></script>
@ -30,7 +30,7 @@
<script src="src/maps.js?version=2021-06-15"></script>
<script src="src/extensions.js?version=2022-02-08"></script>
<script src="src/xml.js?version=2021-07-05"></script>
<script src="src/store.js?version=2022-03-09"></script>
<script src="src/store.js?version=2022-03-15"></script>
<script src="src/locale.js?version=2022-03-04"></script>
<script src="src/cloud.js?version=2021-02-04"></script>
<script src="src/api.js?version=2022-01-03"></script>

Wyświetl plik

@ -161,7 +161,7 @@ CostumeIconMorph, SoundIconMorph, SVG_Costume*/
// Global stuff ////////////////////////////////////////////////////////
modules.blocks = '2022-March-14';
modules.blocks = '2022-March-15';
var SyntaxElementMorph;
var BlockMorph;
@ -3967,23 +3967,7 @@ BlockMorph.prototype.exportResultPic = function () {
// BlockMorph exporting a script
/*
BlockMorph.prototype.exportScript = function () { // +++
var ide = this.parentThatIsA(IDE_Morph),
blockEditor = this.parentThatIsA(BlockEditorMorph);
if (!ide && blockEditor) {
ide = blockEditor.target.parentThatIsA(IDE_Morph);
}
if (ide) {
ide.saveXMLAs(
ide.serializer.serialize(this),
top.selector + ' script',
false);
}
};
*/
BlockMorph.prototype.exportScript = function () { // +++
BlockMorph.prototype.exportScript = function () {
// assumes this is the script's top block
var ide = this.parentThatIsA(IDE_Morph),
blockEditor = this.parentThatIsA(BlockEditorMorph),
@ -4014,14 +3998,14 @@ BlockMorph.prototype.exportScript = function () { // +++
}
});
str = '<stack app="' +
str = '<script app="' +
ide.serializer.app +
'" version="' +
ide.serializer.version +
'">' +
(dependencies.length ? ide.blocksLibraryXML(dependencies, false) : '') +
ide.serializer.serialize(this) +
'</stack>';
'</script>';
ide.saveXMLAs(
str,

Wyświetl plik

@ -86,7 +86,7 @@ BlockVisibilityDialogMorph, ThreadManager*/
// Global stuff ////////////////////////////////////////////////////////
modules.gui = '2022-March-14';
modules.gui = '2022-March-15';
// Declarations
@ -5789,20 +5789,17 @@ IDE_Morph.prototype.openScriptString = function (str) {
};
IDE_Morph.prototype.rawOpenScriptString = function (str) {
var xml,
script,
scripts = this.currentSprite.scripts;
var scripts = this.currentSprite.scripts,
script;
if (Process.prototype.isCatchingErrors) {
try {
xml = this.serializer.parse(str, this.currentSprite);
script = this.serializer.loadScript(xml, this.currentSprite);
script = this.deserializeScriptString(str);
} catch (err) {
this.showMessage('Load failed: ' + err);
}
} else {
xml = this.serializer.loadScript(str, this.currentSprite);
script = this.serializer.loadScript(xml, this.currentSprite);
script = this.deserializeScriptString(str);
}
script.setPosition(this.world().hand.position());
scripts.add(script);
@ -5821,6 +5818,36 @@ IDE_Morph.prototype.rawOpenScriptString = function (str) {
);
};
IDE_Morph.prototype.deserializeScriptString = function (str) {
var xml = this.serializer.parse(str, this.currentSprite),
blocksModel = xml.childNamed('blocks'),
scriptModel = xml.childNamed('script') || xml,
blocks;
if (blocksModel) {
// load the custom block definitions the script depends on
blocks = this.serializer.loadBlocksModel(blocksModel, this.stage);
blocks.global.forEach(def => {
def.receiver = this.stage;
this.stage.globalBlocks.push(def);
this.stage.replaceDoubleDefinitionsFor(def);
});
blocks.local.forEach(def => {
def.receiver = this.currentSprite;
this.currentSprite.customBlocks.push(def);
this.currentSprite.replaceDoubleDefinitionsFor(def);
});
this.flushPaletteCache();
this.refreshPalette();
this.createCategories();
this.categories.refreshEmpty();
this.createPaletteHandle();
this.categories.fixLayout();
this.fixLayout();
}
return this.serializer.loadScriptModule(scriptModel, this.currentSprite);
};
IDE_Morph.prototype.openDataString = function (str, name, type) {
var msg;
this.nextSteps([

Wyświetl plik

@ -63,7 +63,7 @@ Project*/
// Global stuff ////////////////////////////////////////////////////////
modules.store = '2022-March-09';
modules.store = '2022-March-15';
// XML_Serializer ///////////////////////////////////////////////////////
/*
@ -226,8 +226,7 @@ XML_Serializer.prototype.format = function (string) {
// XML_Serializer loading:
XML_Serializer.prototype.load = function (xmlString) {
// public - answer a new object which is represented by the given
// XML string.
// answer a new object which is represented by the given XML string.
nop(xmlString);
throw new Error(
'loading should be implemented in heir of XML_Serializer'
@ -663,15 +662,21 @@ SnapSerializer.prototype.loadScene = function (xmlNode, remixID) {
SnapSerializer.prototype.loadBlocks = function (xmlString, targetStage) {
// public - answer a new dictionary of custom block definitions
// represented by the given XML String
var stage, model;
var model = this.parse(xmlString);
if (+model.attributes.version > this.version) {
throw 'Module uses newer version of Serializer';
}
return this.loadBlocksModel(model, targetStage);
};
SnapSerializer.prototype.loadBlocksModel = function (model, targetStage) {
// public - answer a new dictionary of custom block definitions
// represented by the given already parsed XML Node
var stage;
this.scene = new Scene();
this.scene.targetStage = targetStage; // for secondary block def look-up
stage = this.scene.stage;
model = this.parse(xmlString);
if (+model.attributes.version > this.version) {
throw 'Module uses newer version of Serializer';
}
model.palette = model.childNamed('palette');
if (model.palette) {
this.loadPalette(model.palette).forEach((value, key) =>
@ -1149,6 +1154,16 @@ SnapSerializer.prototype.loadScriptsArray = function (model, object) {
return scripts;
};
SnapSerializer.prototype.loadScriptModule = function (model, object) {
// return a new script represented by the given xml model,
// note: custom block definitions referenced here must be loaded before
var script;
this.scene = new Scene(object.parentThatIsA(StageMorph));
script = this.loadScript(model, object);
this.scene = new Scene();
return script;
};
SnapSerializer.prototype.loadScript = function (model, object) {
// private
var topBlock, block, nextBlock;