68 lines
2.5 KiB
TypeScript
68 lines
2.5 KiB
TypeScript
import type { IButtonMenu, IDomEditor } from '@wangeditor/editor'
|
|
import { Boot } from '@wangeditor/editor'
|
|
|
|
// 当前的点击处理器,始终指向最新传入的函数
|
|
let currentClickHandler: (() => void) | null = null
|
|
|
|
// 是否已经注册过菜单
|
|
let hasRegistered = false
|
|
|
|
class PreviewButtonMenu implements IButtonMenu {
|
|
title = '预览'
|
|
iconSvg = `
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 16 16">
|
|
<path fill="currentColor" fill-rule="evenodd" d="M12 4.57a.5.5 0 0 0-.024-.235l-.013-.063a1.5 1.5 0 0 0-.18-.434c-.092-.15-.222-.28-.482-.54L8.711.707c-.259-.26-.389-.39-.54-.483a1.5 1.5 0 0 0-.496-.193a.5.5 0 0 0-.235-.024C7.329.004 7.194.004 7.015.004h-2.21c-1.68 0-2.52 0-3.16.327a3.02 3.02 0 0 0-1.31 1.31C.008 2.283.008 3.12.008 4.8v6.4c0 1.68 0 2.52.327 3.16a3.02 3.02 0 0 0 1.31 1.31c.642.327 1.48.327 3.16.327h2.423c.401 0 .602-.523.347-.832a.45.45 0 0 0-.345-.168H4.8c-.857 0-1.44-.001-1.89-.038c-.438-.036-.663-.1-.819-.18a2 2 0 0 1-.874-.874c-.08-.156-.145-.38-.18-.819c-.036-.45-.037-1.03-.037-1.89v-6.4c0-.857 0-1.44.037-1.89c.036-.438.101-.663.18-.819c.192-.376.498-.682.874-.874c.156-.08.381-.145.82-.18C3.36.997 3.94.997 4.8.997H7v3.5a.5.5 0 0 0 .5.5H11v.547c0 .25.207.45.456.473c.285.025.543-.188.543-.474V4.99c0-.178 0-.313-.005-.425zM8 1.41L10.59 4H8z" clip-rule="evenodd"/>
|
|
<path fill="currentColor" fill-rule="evenodd" d="M11 15c.834 0 1.61-.255 2.25-.691l1.47 1.47a.749.749 0 1 0 1.06-1.06l-1.47-1.47c.436-.641.691-1.41.691-2.25c0-2.21-1.79-4-4-4s-4 1.79-4 4s1.79 4 4 4zm0-1c1.66 0 3-1.34 3-3s-1.34-3-3-3s-3 1.34-3 3s1.34 3 3 3" clip-rule="evenodd"/>
|
|
</svg>
|
|
`
|
|
tag = 'button'
|
|
|
|
getValue(): string | boolean {
|
|
return ''
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return false
|
|
}
|
|
|
|
isDisabled(): boolean {
|
|
return false
|
|
}
|
|
|
|
exec(editor: IDomEditor): void {
|
|
if (this.isDisabled())
|
|
return
|
|
currentClickHandler?.() // 始终执行最新的回调
|
|
}
|
|
}
|
|
|
|
// 提供注册函数,允许多次调用,但只注册一次菜单
|
|
export function registerPreviewMenu(handleClick: () => void) {
|
|
currentClickHandler = handleClick
|
|
|
|
if (hasRegistered)
|
|
return
|
|
|
|
try {
|
|
Boot.registerMenu({
|
|
key: 'preview',
|
|
factory: () => new PreviewButtonMenu(),
|
|
})
|
|
hasRegistered = true
|
|
}
|
|
catch (e) {
|
|
if (!/duplicate/i.test(String(e))) {
|
|
console.warn('注册预览菜单失败:', e)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 菜单插入配置
|
|
export const toolbarConfig = {
|
|
excludeKeys: ['codeBlock', 'fullScreen', 'code'],
|
|
insertKeys: {
|
|
index: 32,
|
|
keys: ['preview'],
|
|
},
|
|
}
|