开始使用
COMPONENTSFormulaDialog

FormulaDialog 插入公式 ​

WFormulaDialog 将 WFormulaEditor 与 WModal 组合,适用于富文本、试题、内容管理等场景。桌面居中,移动端从底部弹出。

使用 ​

先完成公式组件的安装与样式引入。弹窗使用独立草稿:取消或关闭不改原值;确认非空公式后更新 v-model、触发 confirm 并请求关闭。再次打开时读取最新的外部值。

PREVIEW
vue
<script setup lang="ts">
import { shallowRef } from 'vue'
import { WButton, WFormulaDialog } from '@wizdomt/ui'
import type { FormulaResult } from '@wizdomt/ui'

const visible = shallowRef(false)
const latex = shallowRef('')
const inserted = shallowRef('尚未插入公式')
function insert(result: FormulaResult) {
  inserted.value = result.inline
}
</script>

<template>
  <WButton label="插入公式" @click="visible = true" />
  <code role="status">{{ inserted }}</code>
  <WFormulaDialog v-model:visible="visible" v-model="latex" @confirm="insert" />
</template>

confirm 的结果同时包含三种格式,业务方按编辑器需求插入,不依赖 Editor 的文档结构:

ts
interface FormulaResult {
  latex: string // x^2
  inline: string // $x^2$
  display: string // $$x^2$$
}

API ​

属性类型默认值说明
visiblebooleanfalse使用 v-model:visible 控制显示
modelValuestring''使用 v-model 绑定已确认的原始 LaTeX
titlestring'插入公式'弹窗标题
widthnumber | string1012桌面宽度,移动端铺满底部
confirmTextstring'确认插入'确认按钮文案
cancelTextstring'取消'取消按钮文案
autoFocusbooleanfalse是否启用原生弹窗自动聚焦
mode'latex' | 'visual''latex'初始编辑模式

同时支持 WFormulaEditor 的 subjects、readonly、disabled、showPalette、showPreview、placeholder,以及同样的 tools 作用域插槽。只读或禁用状态下不可确认修改。

事件参数说明
update:visibleboolean请求更新显示状态
update:modelValuestring确认时更新原值;取消不触发
confirmFormulaResult已确认的非空公式
cancel—取消按钮
close—关闭按钮、遮罩或 Escape
errorError公式引擎加载失败

可导入 WFormulaDialogProps、WFormulaDialogEmits、FormulaResult。从 Editor 迁移时,将原 header 中去掉外围 $ 的公式交给 v-model,将原 success.data.decodedLatex 的使用处改为读取 confirm 结果的 inline。

Wizdomt UI Vue 3 · PrimeVue 4