# CodeBlockNode
Class Inherits TextNode
## About
`CodeBlockNode` displays and edits preformatted code without syntax highlighting. It preserves whitespace, exports semantic code markup, and includes a copy action in its popover toolbar.
## Methods
| Name | Parameters | Return Type | Description |
| --- | :---: | :---: | --- |
| ActionItem | index As Integer | ToolbarItem | Returns the contextual toolbar item at the specified index. |
| AllowsToolbarItem | item As ToolbarItem | Boolean | Returns whether the toolbar item is applicable to this node's current state. |
| ClearFormatting | (None) | (None) | Removes supported formatting and restores this content's formatting defaults. |
| Constructor | value As String = "" <br> language As String = "" | (None) | Creates a new code block node instance initialized from the supplied values. |
| Duplicate | (None) | Node | Returns an independent copy of this node and its configurable state. |
| EditableContentPadding | (None) | Double | Returns the space between the node's edges and its text, in points. |
| EditingApplyStyle | start As Integer <br> length As Integer <br> style As TextStyle <br> fieldName As String | (None) | Applies the specified style field to a range in the active editable content. |
| EditingClearFormatting | start As Integer <br> length As Integer | (None) | Clears character formatting from the specified editable range. |
| EditingHandleKey | key As String <br> shiftKey As Boolean <br> menuShortcutKey As Boolean <br> ByRef selectionStart As Integer <br> ByRef selectionLength As Integer <br> ByRef caretPosition As Integer <br> style As TextStyle | (None) | Handles a key press and updates the supplied selection and caret positions. |
| EditingHandlesKey | key As String <br> shiftKey As Boolean <br> menuShortcutKey As Boolean | Boolean | Returns whether the editable content handles the specified key combination. |
| EditingInsertText | position As Integer <br> value As String <br> style As TextStyle | (None) | Inserts styled text at the specified position. |
| EditingRestoreState | state As Variant | (None) | Restores a previously captured editing state. |
| EditingSupportsStyles | (None) | Boolean | Returns whether the editable content supports character-level styling. |
| ExportStyles | stylesheet As GraffitiProEditor.Stylesheet | (None) | Adds the stylesheet rules this node type's HTML output uses. |
| FromHTML | element As GraffitiProEditor.HTMLSupport.HTMLElement | Boolean | Imports supported content and formatting from an HTML element and returns whether it succeeded. |
| FromJSON | data As JSONItem | Boolean | Restores state from JSON data and returns whether it succeeded. |
| HTMLImportPriority | element As GraffitiProEditor.HTMLSupport.HTMLElement | Integer | Returns this node type's priority when resolving an HTML element importer. |
| HTMLTags | (None) | String | Returns the HTML tag names supported by this node type. |
| InspectorSections | (None) | InspectorSection | Returns the user-editable code block properties exposed by the inspector. |
| LastActionIndex | (None) | Integer | Returns the last zero-based action index supplied by this node, or -1 when there are no actions. |
| LocalizeToolbarItem | strings As GraffitiProEditor.Editor | (None) | Sets this node type's toolbar item text from the supplied editor's localization properties. The editor calls it when the type is registered, when the editor opens, and when a locale pack is applied to the editor. A custom node type can override it to localize its own toolbar item. |
| PerformAction | index As Integer <br> selectedItem As DesktopMenuItem | (None) | Handles the contextual toolbar action at the specified index. |
| PerformToolbarAction | editor As Editor <br> selectedItem As DesktopMenuItem | (None) | Handles activation of this node type's main-toolbar item. |
| Render | g As Graphics <br> bounds As Rect <br> selected As Boolean | (None) | Draws the node within the supplied document bounds. |
| StyleAt | index As Integer | TextStyle | Returns the character style at the specified text index. |
| ToHTML | (None) | String | Returns an HTML representation of this content. |
| ToJSON | (None) | JSONItem | Returns a JSON representation of this object's persistent state. |
| ToMarkdown | (None) | String | Returns a Markdown representation of this content. |
| TypeName | (None) | String | Returns the display name for this node type. |
## Properties
| Name | Type | Default Value | Description |
| --- | :---: | :---: | --- |
| Language | String | (None) | Gets or sets the language. |
| TabWidth | Integer | 2 | Gets or sets the tab width. |
## Notes
Code blocks preserve preformatted content and use one base text presentation.
### Editing and Font
Character-level rich-text styling is intentionally disabled for code blocks. Configure the node's base font through its font properties or subclass defaults. The built-in default uses Menlo on macOS and Courier on Windows.
While editing, Tab inserts spaces at the caret instead of moving focus. `TabWidth` determines how many spaces are inserted and defaults to 2. When text is selected, pressing Tab replaces the selection with the indentation.
### JSON Format
`language` is a string and `tabWidth` is an integer. Code text is stored in the inherited `TextNode` content snapshot. Character-level style data is not used to present code blocks.
`language` is omitted when empty and `tabWidth` when it is `2`; missing names restore those defaults.
## Examples
Register a code-block prototype before inserting or importing code blocks.
### Insert Code
```xojo
Editor.AddNode(New GraffitiProEditor.CodeBlockNode("MessageBox(""Hello"")", "xojo"))
```