Code Blocks in Angular Block Editor component
18 Nov 20184 minutes to read
The Syncfusion Block Editor allows you to render code snippets with syntax highlighting by setting the block’s blockType property to Code. You can customize the available programming languages and set a default language using the properties property.
Global Code Settings
You can configure global settings for code blocks using the codeBlockSettings property in the Block Editor root configuration. This ensures consistent behavior for syntax highlighting and language options across all code blocks.
The codeBlockSettings property supports the following options:
| Property | Description | Default Value |
|---|---|---|
| languages | Specifies the array of language options for syntax highlighting. | [] |
| defaultLanguage | Defines the default language to use for syntax highlighting. | ‘plaintext’ |
Configure code properties
For Code blocks, you can specify the language for syntax highlighting using the properties property. This property supports the following options:
- language: The default language value used for syntax highlighting.
Block type & properties
// Adding Code block
{
blockType: 'Code',
content: [
{
const x = 10;
}
],
properties: {
language: 'javascript'
}
}The following example demonstrates how to configure and render a Code block within the Block Editor.
import { Component } from '@angular/core';
import { BlockEditorModule } from "@syncfusion/ej2-angular-blockeditor";
import { BlockModel, ContentType, CodeBlockSettingsModel } from "@syncfusion/ej2-blockeditor";
@Component({
imports: [BlockEditorModule],
standalone: true,
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
public blocksData: BlockModel[] = [
{
blockType: 'Code',
content: [
{
contentType: ContentType.Text,
content: 'function greeting() {\n console.log("Hello, world!");\n}'
}
]
}
];
public codeBlockData: CodeBlockSettingsModel = {
defaultLanguage: 'javascript',
languages: [
{ language: 'javascript', label: 'JavaScript' },
{ language: 'typescript', label: 'TypeScript' },
{ language: 'html', label: 'HTML' },
{ language: 'css', label: 'CSS' }
]
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));<div id="container">
<ejs-blockeditor id="blockeditor" [blocks]="blocksData" [codeBlockSettings]="codeBlockData" />
</div>