Skip to content

xtf-editor

组件说明

xtf-editor 是富文本编辑器组件,基于 uni-app 的 editor 原生组件封装。支持工具栏操作、图片插入、只读模式和自定义样式,适用于文章编辑、备注输入和内容发布等场景。


基础用法

1. 最简示例

vue
<template>
  <xtf-editor v-model="content" placeholder="请输入内容..." />
</template>

<script>
export default {
  data() {
    return {
      content: ''
    }
  }
}
</script>

2. 自定义工具栏

通过 toolbarActions 设置工具栏按钮:

vue
<template>
  <xtf-editor v-model="content" placeholder="请输入内容..." :toolbar-actions="toolbarActions" />
</template>

<script>
export default {
  data() {
    return {
      content: '',
      toolbarActions: [
        { key: 'bold', type: 'format', name: 'bold', icon: 'format_bold', label: '加粗' },
        { key: 'italic', type: 'format', name: 'italic', icon: 'format_italic', label: '斜体' },
        {
          key: 'underline',
          type: 'format',
          name: 'underline',
          icon: 'format_underlined',
          label: '下划线'
        },
        {
          key: 'ordered',
          type: 'format',
          name: 'list',
          value: 'ordered',
          icon: 'format_list_numbered',
          label: '有序列表'
        },
        {
          key: 'bullet',
          type: 'format',
          name: 'list',
          value: 'bullet',
          icon: 'format_list_bulleted',
          label: '无序列表'
        },
        { key: 'clear', type: 'command', command: 'clear', icon: 'format_clear', label: '清除格式' }
      ]
    }
  }
}
</script>

3. 只读模式

通过 readOnly 设置只读模式:

vue
<template>
  <xtf-editor v-model="content" read-only :show-toolbar="false" />
</template>

<script>
export default {
  data() {
    return {
      content: '<p>这是只读的富文本内容。</p>'
    }
  }
}
</script>

全部属性

属性类型默认值作用描述适用范围
value / modelValueString | Object''编辑器内容双向绑定
placeholderString''占位提示文字占位符
readOnlyBooleanfalse是否只读只读
disabledBooleanfalse是否禁用禁用
minHeightString | Number360最小高度(rpx)高度
showToolbarBooleantrue是否显示工具栏工具栏
toolbarActionsArray[]工具栏按钮列表工具栏
showImgSizeBooleantrue图片是否显示尺寸图片
showImgToolbarBooleantrue图片是否显示工具栏图片
showImgResizeBooleantrue图片是否可调整大小图片
customClassString''自定义类名样式覆盖
customStyleString | Object''自定义样式动态样式覆盖
editorClassString''原生 editor 节点的自定义类名编辑区样式覆盖
editorStyleString | Object''原生 editor 节点样式,会与 minHeight 合并编辑区样式覆盖

事件

事件名称触发时机回调参数参数说明
change内容变化时触发({ html, text, delta })HTML、纯文本与 Delta 内容快照
focus获得焦点时触发(event)原生 editor 焦点事件对象
blur失去焦点时触发(event)原生 editor 失焦事件对象
ready编辑器初始化完成时触发--
statuschange格式状态变化时触发(status)格式状态
insert-image通过 insertImage 成功插入图片时触发(payload)与实际调用原生 editorContext.insertImage 一致:传入 options 时为原对象;选择图片时为 { src, alt: 'image' }

源码核验补充

inputupdate:modelValueupdate:value 均传出 HTML;change{ html, text, delta }ready{ editorCtx }。公开方法为 formatundoredoclearremoveFormatinsertDividerinsertDateinsertTextinsertImagegetContentssetContentsinsertImage(options)options.src 存在时原样传给原生 editorContext.insertImage 并触发 insert-image;未传 src 时会调用 uni.chooseImage,再以 { src, alt: 'image' } 插入。工具项需使用 type: 'format'name/value,或 type: 'command'command

事件使用示例

vue
<template>
  <xtf-editor
    v-model="content"
    editor-class="article-editor"
    :editor-style="{ backgroundColor: '#ffffff', minHeight: '480rpx' }"
    @ready="onReady"
    @change="onChange"
    @focus="onFocus"
    @blur="onBlur"
    @statuschange="onStatusChange"
    @insert-image="onInsertImage"
  />
</template>

<script>
export default {
  data() {
    return { content: '' }
  },
  methods: {
    onReady({ editorCtx }) {
      console.log('编辑器已就绪:', !!editorCtx)
    },
    onChange({ html, text }) {
      console.log('内容变化:', html, text)
    },
    onFocus(event) {
      console.log('获得焦点:', event.type)
    },
    onBlur(event) {
      console.log('失去焦点:', event.type)
    },
    onStatusChange(status) {
      console.log('格式状态:', status)
    },
    onInsertImage(options) {
      console.log('插入图片:', options.src)
    }
  }
}
</script>

主题说明

  • 工具栏使用 xtf-icon 组件显示图标
  • 激活工具使用主题色高亮
  • 编辑区域使用原生 editor 组件
  • 只读模式隐藏工具栏并禁用交互

如需全局调整主题,请在 xtf-config-provider 或主题配置中覆盖相关 CSS 变量。

MIT Licensed