How can I help you?
Code Blocks in Vue Block Editor component
18 Mar 20266 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.
<template>
<div id='container'>
<ejs-blockeditor :blocks="blocksData" :codeBlockSettings="codeBlockData"></ejs-blockeditor>
</div>
</template>
<script setup>
import { BlockEditorComponent as EjsBlockeditor, ContentType } from "@syncfusion/ej2-vue-blockeditor";
const blocksData = [
{
blockType: 'Code',
content: [
{
contentType: ContentType.Text,
content: 'function greeting() {\n console.log("Hello, world!");\n}'
}
]
}
];
const codeBlockData = {
defaultLanguage: 'javascript',
languages: [
{ language: 'javascript', label: 'JavaScript' },
{ language: 'typescript', label: 'TypeScript' },
{ language: 'html', label: 'HTML' },
{ language: 'css', label: 'CSS' }
]
};
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-blockeditor/styles/tailwind3.css';
</style><template>
<div id='container'>
<ejs-blockeditor :blocks="blocksData" :codeBlockSettings="codeBlockData"></ejs-blockeditor>
</div>
</template>
<script>
import { BlockEditorComponent, ContentType } from "@syncfusion/ej2-vue-blockeditor";
export default {
components: {
'ejs-blockeditor': BlockEditorComponent,
},
data() {
return {
blocksData: [
{
blockType: 'Code',
content: [
{
contentType: ContentType.Text,
content: 'function greeting() {\n console.log("Hello, world!");\n}'
}
]
}
],
codeBlockData: {
defaultLanguage: 'javascript',
languages: [
{ language: 'javascript', label: 'JavaScript' },
{ language: 'typescript', label: 'TypeScript' },
{ language: 'html', label: 'HTML' },
{ language: 'css', label: 'CSS' }
]
}
};
},
methods: {
}
};
</script>
<style>
@import '../node_modules/@syncfusion/ej2-base/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-dropdowns/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/tailwind3.css';
@import '../node_modules/@syncfusion/ej2-blockeditor/styles/tailwind3.css';
</style>