forked from scratchfoundation/scratch-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom Scratch variable model and creation event classes (s…
…cratchfoundation#86) * feat: add a custom Scratch variable model * feat: add a custom Scratch variable creation event * chore: format index.js * feat: import and register the Scratch variable model and creation event classes * feat: add serialization/running support to the Scratch variable creation event * chore: add license headers * refactor: replace repeated variableMap accesses with a variable
- Loading branch information
Showing
3 changed files
with
144 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import * as Blockly from "blockly/core"; | ||
|
||
class ScratchVariableCreate extends Blockly.Events.VarCreate { | ||
constructor(variable) { | ||
super(variable); | ||
if (!variable) return; | ||
|
||
this.isLocal = variable.isLocal; | ||
this.isCloud = variable.isCloud; | ||
} | ||
|
||
toJson() { | ||
const json = super.toJson(); | ||
json["isLocal"] = this.isLocal; | ||
json["isCloud"] = this.isCloud; | ||
return json; | ||
} | ||
|
||
static fromJson(json, workspace, event) { | ||
const newEvent = super.fromJson(json, workspace, event); | ||
newEvent.isLocal = json["isLocal"]; | ||
newEvent.isCloud = json["isCloud"]; | ||
return newEvent; | ||
} | ||
|
||
run(forward) { | ||
const workspace = this.getEventWorkspace_(); | ||
const variableMap = workspace.getVariableMap(); | ||
if (forward) { | ||
const VariableModel = Blockly.registry.getObject( | ||
Blockly.registry.Type.VARIABLE_MODEL, | ||
Blockly.registry.DEFAULT, | ||
true | ||
); | ||
const variable = new VariableModel( | ||
workspace, | ||
this.varName, | ||
this.varType, | ||
this.varId, | ||
this.isLocal, | ||
this.isCloud | ||
); | ||
variableMap.addVariable(variable); | ||
Blockly.Events.fire( | ||
new (Blockly.Events.get(Blockly.Events.VAR_CREATE))(variable) | ||
); | ||
} else { | ||
const variable = variableMap.getVariableById(this.varId); | ||
if (variable) { | ||
variableMap.deleteVariable(variable); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Blockly.registry.register( | ||
Blockly.registry.Type.EVENT, | ||
Blockly.Events.VAR_CREATE, | ||
ScratchVariableCreate, | ||
true | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import * as Blockly from "blockly/core"; | ||
|
||
class ScratchVariableModel extends Blockly.VariableModel { | ||
constructor(workspace, name, type, id, isLocal, isCloud) { | ||
super(workspace, name, type, id); | ||
// isLocal and isCloud may not be passed when creating broadcast message | ||
// variables, which conveniently are neither local nor cloud. | ||
this.isLocal = !!isLocal; | ||
this.isCloud = !!isCloud; | ||
} | ||
} | ||
|
||
Blockly.registry.register( | ||
Blockly.registry.Type.VARIABLE_MODEL, | ||
Blockly.registry.DEFAULT, | ||
ScratchVariableModel, | ||
true | ||
); |