Skip to content

Code Block

This is the Code Block extension. It allows you to create fenced code blocks for displaying multi-line code snippets.

Usage

Default usage of the code block extension and its control button.

Code Block
vue
<template>
  <HLTextEditor :editor="editor">
    <template #header>
      <RTEControlCode :editor="editor" type="block" />
    </template>
    <template #footer>
      <div>Footer</div>
    </template>
  </HLTextEditor>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import {
  RequiredExtensions,
  Editor,
  HLTextEditor,
  RTEControlCode,
  CodeBlock,
} from '@platform-ui/rte'

const content = ref()
const HTML = ref()

const editor = new Editor({
  content: '<p>Click the code block button to insert a fenced code block</p>',
  extensions: [CodeBlock, RequiredExtensions.configure({})],
  onUpdate({ editor }) {
    content.value = editor.getText()
    HTML.value = editor.getHTML()
  },
})
</script>

Commands

js
// Toggle code block
editor?.chain().focus().toggleCodeBlock().run()

// Set code block
editor?.commands.setCodeBlock()

Imports

ts
import {
  RequiredExtensions,
  Editor,
  HLTextEditor,
  RTEControlCode,
  CodeBlock,
} from '@platform-ui/rte'

Props

PropTypeDefaultDescription
editor *Editor-The editor instance
type'inline' | 'block''inline'The type of code formatting
tooltipLabelstringCodeThe tooltip label
tooltipPositionPlacement'top'The tooltip position
languageLanguage'en-US'The language
ariaLabelstringnullThe aria label

Slots

SlotDescriptionDefault
iconThe icon to displayCodeBrowserIcon