new BlockPyEditor(main, tag)
An object that manages the various editors, where users can edit their program. Also manages the
movement between editors.
There are currently four editors:
- Blocks: A Blockly instance
- Text: A CodeMirror instance
- Instructor: Features for changing the assignment and environment settings
- Upload: (Incomplete) A menu for uploading and running code from a desktop file.
This:
Parameters:
Name | Type | Description |
---|---|---|
main |
Object | The main BlockPy instance |
tag |
HTMLElement | The HTML object this is attached to. |
Members
(static) CATEGORY_MAP
Maps short category names in the toolbox to the full XML used to
represent that category as usual. This is kind of a clunky mechanism
for managing the different categories, and doesn't allow us to specify
individual blocks.
previousLine
Properties:
Name | Type | Description |
---|---|---|
previousLine |
Number | Keeps track of the previously highlighted line. |
Methods
addAvailableModule(name)
Makes the module available in the availableModules multi-select menu by adding
it to the list.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The name of the module (human-friendly version, as opposed to the slug) to be added. |
changeProgram(name)
Updates the current file being edited in the editors.
This appears to be deprecated.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The name of the file being edited (e.g, "__main__", "starting_code") |
copyImage()
Shows a dialog window with the current block workspace encoded as a
downloadable PNG image.
getBlocksFromXml() → {XMLDom}
Helper function for retrieving the current Blockly workspace as
an XML DOM object.
Returns:
The blocks in the current workspace.
- Type
- XMLDom
getHighlightMap()
DEPRECATED, thankfully
Builds up an array indicating the relevant block ID for a given step.
Operates on the current this.blockly instance
It works by injecting __HIGHLIGHT__(id); at the start of every line of code
and then extracting that with regular expressions. This makes it vulnerable
if someone decides to use __HIGHLIGHT__ in their code. I'm betting on that
never being a problem, though. Still, this was a miserable way of accomplishing
the desired behavior.
getPngFromBlocks(callback)
Generates a PNG version of the current workspace. This PNG is stored in a Base-64 encoded
string as part of a data URL (e.g., "data:image/png;base64,...").
TODO: There seems to be some problems capturing blocks that don't start with
statement level blocks (e.g., expression blocks).
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | A function to be called with the results. This function should take two parameters, the URL (as a string) of the generated base64-encoded PNG and the IMG tag. |
hideBlockMenu()
Hides the Block tab, which involves shrinking it and hiding the Blockly instance.
hideInstructorMenu()
Hides the Instructor tab, which shrinking it.
hideSplitMenu()
Hides the Text tab, which involves shrinking it and hiding its CodeMirror too.
hideTextMenu()
Hides the Text tab, which involves shrinking it and hiding its CodeMirror too.
hideUploadMenu()
Hides the Upload tab, which shrinking it.
highlightBlock(block)
Highlights a block in Blockly. Unfortunately, this is the same as selecting it.
Parameters:
Name | Type | Description |
---|---|---|
block |
Number | The ID of the block object to highlight. |
highlightError(line)
Highlights a line of code in the CodeMirror instance. This applies the "error" style
which is meant to suggest that a line is wrong.
Parameters:
Name | Type | Description |
---|---|---|
line |
Number | The line of code to highlight. I think this is zero indexed? |
highlightLine(line)
Highlights a line of code in the CodeMirror instance. This applies the "active" style
which is meant to bring attention to a line, but not suggest it is wrong.
Parameters:
Name | Type | Description |
---|---|---|
line |
Number | The line of code to highlight. I think this is zero indexed? |
initBlockly()
Initializes the Blockly instance (handles all the blocks). This includes
attaching a number of ChangeListeners that can keep the internal code
representation updated and enforce type checking.
initInstructor()
Initializes the Instructor tab, which has a number of buttons and menus for
manipulating assignments and the environment. One important job is to register the
SummerNote instance used for editing the Introduction of the assignment.
initText()
Initializes the CodeMirror instance. This handles text editing (with syntax highlighting)
and also attaches a listener for change events to update the internal code represntation.
refreshBlockHighlight(line)
Used to restore a block's highlight when travelling from the code tab. This
uses a mapping between the blocks and text that is generated from the parser.
The parser has stored the relevant line numbers for each block in the XML of the
block. Very sophisticated, and sadly fairly fragile.
TODO: I believe there's some kind of off-by-one error here...
Parameters:
Name | Type | Description |
---|---|---|
line |
Number | The line of code to highlight. I think this is zero indexed? |
refreshHighlight()
Assuming that a line has been highlighted previously, this will set the
line to be highlighted again. Used when we need to restore a highlight.
resetBlockSilence()
Resets the silenceBlock after a short delay
setBlocksFromXml()
Helper function for setting the current Blockly workspace to
whatever XML DOM is given. This clears out any existing blocks.
setLevel()
Eventually will be used to update "levels" of sophistication of the code interface.
Currently unimplemented and unused.
setMode(mode)
Dispatch method to set the mode to the given argument.
If the mode is invalid, an editor error is reported. If the
Parameters:
Name | Type | Description |
---|---|---|
mode |
String | The new mode to set to ("Blocks", "Text", "Upload", or "Instructor") |
setModeToBlocks()
Sets the current editor mode to Blocks, hiding the other menus.
Also forces the block side to update.
There is a chance this could fail, if the text side is irredeemably
awful. So then the editor bounces back to the text side.
setModeToInstructor()
Sets the current editor mode to the Instructor mode, hiding the other menus.
setModeToSplit()
Sets the current editor mode to Split mode, hiding the other menus.
setModeToText()
Sets the current editor mode to Text, hiding the other menus.
Also forces the text side to update.
setModeToUpload()
Sets the current editor mode to Upload mode, hiding the other menus.
setText(code)
Actually changes the value of the CodeMirror instance
Parameters:
Name | Type | Description |
---|---|---|
code |
String | The new code for the CodeMirror |
showBlockMenu()
Shows the Block tab, which involves restoring its height and showing the Blockly instance.
showInstructorMenu()
Shows the Instructor tab, which involves restoring its height.
showSplitMenu()
Shows the Text tab, which requires restoring its height, showing AND refreshing
the CodeMirror instance.
showTextMenu()
Shows the Text tab, which requires restoring its height, showing AND refreshing
the CodeMirror instance.
showUploadMenu()
Shows the Upload tab, which involves restoring its height.
unhighlightBlock()
Removes the outline around a block. Currently unused.
unhighlightLines()
Removes any highlight in the text code editor.
updateBlocks()
Attempts to update the model for the current code file from the
block workspace. Might be prevented if an update event was already
percolating.
updateBlocksFromModel() → {Boolean}
Updates the block editor from the current code file in the
model. Might be prevented if an update event was already
percolating. This can also report an error if one occurs.
Returns:
Returns true upon success.
- Type
- Boolean
updateTextFromModel()
Updates the text editor from the current code file in the
model. Might be prevented if an update event was already
percolating.
updateToolbox(only_set) → (nullable) {String}
Creates an updated representation of the Toolboxes XML as currently specified in the
model, using whatever modules have been added or removed. This method can either set it
or just retrieve it for future use.
Parameters:
Name | Type | Description |
---|---|---|
only_set |
Boolean | Whether to return the XML string or to actually set the XML. False means that it will not update the toolbox! |
Returns:
Possibly returns the XML of the toolbox as a string.
- Type
- String