Skip to content

CodeEditor 代碼編輯器

Description

CodeEditor 組件是一個基於 Microsoft Monaco Editor 的程式碼編輯器,支援多種程式語言語法高亮、智能提示、代碼格式化等功能。

基本用法

基本使用

最基本的代碼編輯器使用方式

當前代碼值:

function hello() {
  console.log('Hello World!');
  return 'Hello from Monaco Editor!';
}

// 調用函數
hello();

多語言支援

多語言支持

支援多種程式語言的語法高亮

主題切換

主題切換

支援亮色和暗色主題

只讀模式

只讀模式

設置為只讀模式的代碼編輯器,無法編輯但可以查看和複製代碼

編輯器狀態:

  • 只讀模式: 是
  • 小地圖: 顯示
  • 代碼長度: 223 字符
  • 行數: 10 行

API

屬性

屬性名說明類型默認值
modelValue綁定的代碼值string''
language程式語言string'javascript'
theme編輯器主題'vs' | 'vs-dark' | 'hc-black' | 'hc-light''vs'
width編輯器寬度string | number'100%'
height編輯器高度string | number'300px'
readOnly是否只讀booleanfalse
loading載入中文字string''
minimap是否顯示小地圖booleantrue
lineNumbers行號顯示模式'on' | 'off' | 'relative' | 'interval''on'
wordWrap文字換行模式'off' | 'on' | 'wordWrapColumn' | 'bounded''off'
fontSize字體大小number14
tabSizeTab 大小number2
insertSpaces是否使用空格代替 Tabbooleantrue
automaticLayout是否自動佈局booleantrue
optionsMonaco Editor 選項monaco.editor.IStandaloneEditorConstructionOptions{}

事件 Events

事件名說明回調參數
update:modelValue值更新事件(value: string)
change內容變化事件(value: string, event: monaco.editor.IModelContentChangedEvent)
ready編輯器準備就緒事件(editor: monaco.editor.IStandaloneCodeEditor)
focus聚焦事件(event: any)
blur失焦事件(event: any)
keydown按鍵事件(event: monaco.IKeyboardEvent)
keyup按鍵釋放事件(event: monaco.IKeyboardEvent)
mousedown滑鼠按下事件(event: monaco.editor.IEditorMouseEvent)
mouseup滑鼠釋放事件(event: monaco.editor.IEditorMouseEvent)
contextmenu右鍵選單事件(event: monaco.editor.IEditorMouseEvent)

插槽 Slots

插槽名說明
loading載入狀態插槽
default預設插槽(載入狀態)

方法 Methods

可以通過 ref 獲取組件實例並調用以下方法:

方法名說明參數返回值
getValue獲取編輯器值-string
setValue設置編輯器值value: stringvoid
focus聚焦編輯器-void
format格式化代碼-void
layout重新佈局-void

Released under the MIT License.