FormulaDialog 插入公式
WFormulaDialog 将 WFormulaEditor 与 WModal 组合,适用于富文本、试题、内容管理等场景。桌面居中,移动端从底部弹出。
使用
先完成公式组件的安装与样式引入。弹窗使用独立草稿:取消或关闭不改原值;确认非空公式后更新 v-model、触发 confirm 并请求关闭。再次打开时读取最新的外部值。
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
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
visible | boolean | false | 使用 v-model:visible 控制显示 |
modelValue | string | '' | 使用 v-model 绑定已确认的原始 LaTeX |
title | string | '插入公式' | 弹窗标题 |
width | number | string | 1012 | 桌面宽度,移动端铺满底部 |
confirmText | string | '确认插入' | 确认按钮文案 |
cancelText | string | '取消' | 取消按钮文案 |
autoFocus | boolean | false | 是否启用原生弹窗自动聚焦 |
mode | 'latex' | 'visual' | 'latex' | 初始编辑模式 |
同时支持 WFormulaEditor 的 subjects、readonly、disabled、showPalette、showPreview、placeholder,以及同样的 tools 作用域插槽。只读或禁用状态下不可确认修改。
| 事件 | 参数 | 说明 |
|---|---|---|
update:visible | boolean | 请求更新显示状态 |
update:modelValue | string | 确认时更新原值;取消不触发 |
confirm | FormulaResult | 已确认的非空公式 |
cancel | — | 取消按钮 |
close | — | 关闭按钮、遮罩或 Escape |
error | Error | 公式引擎加载失败 |
可导入 WFormulaDialogProps、WFormulaDialogEmits、FormulaResult。从 Editor 迁移时,将原 header 中去掉外围 $ 的公式交给 v-model,将原 success.data.decodedLatex 的使用处改为读取 confirm 结果的 inline。