直接跳到内容

TextArea 多行文本编辑器

功能强大的多行文本编辑组件,支持语法高亮、代码编辑器模式、只读模式等,适合构建代码编辑器、日志查看器等场景。

底层:Textual TextArea

基本用法

vue
<template>
  <TextArea
    v-model="code.value"
    language="python"
    code_editor
    style="height: 1fr;"
  />
</template>

<script lang="py">
from vuepy import ref

code = ref("""def hello():
    print("Hello, World!")
""")
</script>

Props

属性类型默认值说明
textstr""文本内容
languagestrNone语法高亮语言,如 "python""json""markdown"
code_editorboolFalse代码编辑器模式(显示行号 + 启用语法高亮,无需传值,存在即为 True
read_onlyboolFalse只读模式,用户无法编辑内容
themestr"monokai"语法高亮主题名称
tab_behaviorstr"focus"Tab 键行为:"focus" 切换焦点 | "indent" 插入缩进

v-model

v-model 默认绑定属性:text

vue
<TextArea v-model="content.value" />

事件

事件说明
text_area_changed文本内容改变时触发(对应 Textual TextArea.Changed

通过 ref 访问

通过 ref 获取底层 widget 实例后,可访问以下属性:

属性 / 方法说明
.selected_text当前选中的文本内容
.selection当前选区范围(Selection 对象)
.cursor_location光标位置 (行, 列)
vue
<template>
  <VBox>
    <TextArea ref="editor_ref" v-model="code.value" language="python" code_editor />
    <Button label="获取选中文字" @click="get_selected()" />
  </VBox>
</template>

<script lang="py">
from vuepy import ref

editor_ref = ref(None)
code = ref("# 在此输入代码\n")

def get_selected():
    if editor_ref.value:
        widget = editor_ref.value.unwrap()
        print(f"选中内容: {widget.selected_text}")
</script>

通用属性

所有 Textual-vuepy 组件均支持以下属性:

属性类型说明
idstr组件 CSS ID
stylestrTCSS 内联样式
classstrTCSS 类名(空格分隔)
border_titlestr边框标题
border_subtitlestr边框副标题
TextArea 多行文本编辑器已经加载完毕