/** * @description 标题 * @author wangfupeng */ import DropListMenu from '../menu-constructors/DropListMenu' import $ from '../../utils/dom-core' import Editor from '../../editor/index' import { MenuActive } from '../menu-constructors/Menu' class Head extends DropListMenu implements MenuActive { constructor(editor: Editor) { const $elem = $('
') const dropListConf = { width: 100, title: '设置标题', type: 'list', // droplist 以列表形式展示 list: [ { $elem: $('

H1

'), value: '

' }, { $elem: $('

H2

'), value: '

' }, { $elem: $('

H3

'), value: '

' }, { $elem: $('

H4

'), value: '

' }, { $elem: $('

H5
'), value: '
' }, { $elem: $(`

${editor.i18next.t('menus.dropListMenu.head.正文')}

`), value: '

', }, ], clickHandler: (value: string) => { // 注意 this 是指向当前的 Head 对象 this.command(value) }, } super($elem, editor, dropListConf) } /** * 执行命令 * @param value value */ public command(value: string): void { const editor = this.editor const $selectionElem = editor.selection.getSelectionContainerElem() if ($selectionElem && editor.$textElem.equal($selectionElem)) { // 不能选中多行来设置标题,否则会出现问题 // 例如选中的是

xxx

yyy

来设置标题,设置之后会成为

xxx
yyy

不符合预期 return } editor.cmd.do('formatBlock', value) } /** * 尝试改变菜单激活(高亮)状态 */ public tryChangeActive() { const editor = this.editor const reg = /^h/i const cmdValue = editor.cmd.queryCommandValue('formatBlock') if (reg.test(cmdValue)) { this.active() } else { this.unActive() } } } export default Head