/* menu - header */ import $ from '../../util/dom-core.js' import DropList from '../droplist.js' // 构造函数 function Head(editor) { this.editor = editor this.$elem = $('
') this.type = 'droplist' // 当前是否 active 状态 this._active = false // 初始化 droplist this.droplist = new DropList(this, { width: 100, $title: $('

设置标题

'), type: 'list', // droplist 以列表形式展示 list: [ { $elem: $('

标题

'), value: '

' }, { $elem: $('

副标题

'), value: '

' }, { $elem: $('

标题1

'), value: '

' }, { $elem: $('

标题2

'), value: '

' }, { $elem: $('

标题3
'), value: '
' }, { $elem: $('

正文

'), value: '
' } ], onClick: (value) => { // 注意 this 是指向当前的 Head 对象 this._command(value) } }) } // 原型 Head.prototype = { constructor: Head, // 执行命令 _command: function (value) { const editor = this.editor const $selectionElem = editor.selection.getSelectionContainerElem() if (editor.$textElem.equal($selectionElem)) { // 不能选中多行来设置标题,否则会出现问题 // 例如选中的是

xxx

yyy

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

xxx
yyy

不符合预期 return } editor.cmd.do('formatBlock', value) }, // 试图改变 active 状态 tryChangeActive: function (e) { const editor = this.editor const $elem = this.$elem const reg = /^h/i const cmdValue = editor.cmd.queryCommandValue('formatBlock') if (reg.test(cmdValue)) { this._active = true $elem.addClass('lsiten-e-active') } else { this._active = false $elem.removeClass('lsiten-e-active') } } } export default Head