Skip to content

Commit e4f2fee

Browse files
authored
ScratchBlocks/block.js -- mutation support for extensions
1 parent dbd46af commit e4f2fee

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

core/block.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,34 @@ Blockly.Block.prototype.jsonInit = function(json) {
13461346
if (json['category'] !== undefined) {
13471347
this.setCategory(json['category']);
13481348
}
1349+
1350+
if (json['mutations']) {
1351+
const handler = json['mutations'];
1352+
this.mutationToDom = function () {
1353+
// save mutations
1354+
const serializer = Object.create(null);
1355+
handler.serialize(this, serializer);
1356+
const xmlElement = document.createElement("mutation");
1357+
for (const [name, value] of Object.entries(serializer)) {
1358+
// force names to be lowercase, otherwise it wont save
1359+
xmlElement.setAttribute(name.toLowerCase(), value);
1360+
}
1361+
return xmlElement;
1362+
}
1363+
1364+
this.domToMutation = function(xmlElement) {
1365+
// load mutations
1366+
const deserialized = {};
1367+
for (const attr of xmlElement.attributes) {
1368+
deserialized[attr.name] = attr.value;
1369+
}
1370+
1371+
handler.deserialize(this, deserialized);
1372+
}
1373+
1374+
// optional mutation initialize
1375+
if (handler.init) handler.init(this);
1376+
}
13491377
};
13501378

13511379
/**

0 commit comments

Comments
 (0)