refactor: 统一处理 class 属性权限:public protected private

This commit is contained in:
wangfupeng 2020-06-15 11:42:38 +08:00
parent 57e7f50dd4
commit 63e9da46d3
14 changed files with 97 additions and 89 deletions

View File

@ -8,7 +8,7 @@ import { UA } from '../utils/util'
import Editor from './index'
class Command {
editor: Editor
public editor: Editor
constructor(editor: Editor) {
this.editor = editor
@ -19,10 +19,10 @@ class Command {
* @param name name
* @param value value
*/
do(name: string): void
do(name: string, value: string): void
do(name: string, value: DomElement): void
do(name: string, value?: string | DomElement): void {
public do(name: string): void
public do(name: string, value: string): void
public do(name: string, value: DomElement): void
public do(name: string, value?: string | DomElement): void {
const editor = this.editor
// // 使用 styleWithCSS —— 暂不使用
@ -44,14 +44,14 @@ class Command {
// 执行
switch (name) {
case 'insertHTML':
this._insertHTML(value as string)
this.insertHTML(value as string)
break
case 'insertElem':
this._insertElem(value as DomElement)
this.insertElem(value as DomElement)
break
default:
// 默认 command
this._execCommand(name, value as string)
this.execCommand(name, value as string)
break
}
@ -70,14 +70,14 @@ class Command {
* html
* @param html html
*/
_insertHTML(html: string): void {
private insertHTML(html: string): void {
const editor = this.editor
const range = editor.selection.getRange()
if (range == null) return
if (this.queryCommandSupported('insertHTML')) {
// W3C
this._execCommand('insertHTML', html)
this.execCommand('insertHTML', html)
} else if (range.insertNode) {
// IE
range.deleteContents()
@ -93,7 +93,7 @@ class Command {
* DOM
* @param $elem DOM
*/
_insertElem($elem: DomElement): void {
private insertElem($elem: DomElement): void {
const editor = this.editor
const range = editor.selection.getRange()
if (range == null) return
@ -109,7 +109,7 @@ class Command {
* @param name name
* @param value value
*/
_execCommand(name: string, value: string): void {
private execCommand(name: string, value: string): void {
document.execCommand(name, false, value)
}
@ -117,7 +117,7 @@ class Command {
* document.queryCommandValue
* @param name name
*/
queryCommandValue(name: string): string {
public queryCommandValue(name: string): string {
return document.queryCommandValue(name)
}
@ -125,7 +125,7 @@ class Command {
* document.queryCommandState
* @param name name
*/
queryCommandState(name: string): boolean {
public queryCommandState(name: string): boolean {
return document.queryCommandState(name)
}
@ -133,7 +133,7 @@ class Command {
* document.queryCommandSupported
* @param name name
*/
queryCommandSupported(name: string): boolean {
public queryCommandSupported(name: string): boolean {
return document.queryCommandSupported(name)
}
}

View File

@ -19,22 +19,22 @@ import initConfig from './init-fns/init-config'
let EDITOR_ID = 1
class Editor {
id: string
toolbarSelector: string
textSelector: string | undefined
customConfig: ConfigType
config: ConfigType
$toolbarElem: DomElement
$textContainerElem: DomElement
$textElem: DomElement
toolbarElemId: string
textElemId: string
isFocus: boolean
selection: SelectionAndRangeAPI
cmd: CommandAPI
txt: Text
menus: Menus
change: Function
public id: string
public toolbarSelector: string
public textSelector: string | undefined
public customConfig: ConfigType
public config: ConfigType
public $toolbarElem: DomElement
public $textContainerElem: DomElement
public $textElem: DomElement
public toolbarElemId: string
public textElemId: string
public isFocus: boolean
public selection: SelectionAndRangeAPI
public cmd: CommandAPI
public txt: Text
public menus: Menus
public change: Function
/**
*
@ -73,14 +73,14 @@ class Editor {
*
* @param newLine
*/
initSelection(newLine?: boolean): void {
public initSelection(newLine?: boolean): void {
initSelection(this, newLine)
}
/**
*
*/
create(): void {
public create(): void {
// 初始化配置
initConfig(this)

View File

@ -8,8 +8,8 @@ import { UA } from '../utils/util'
import Editor from './index'
class SelectionAndRange {
editor: Editor
_currentRange: Range | null
public editor: Editor
private _currentRange: Range | null
constructor(editor: Editor) {
this.editor = editor
@ -19,7 +19,7 @@ class SelectionAndRange {
/**
* range
*/
getRange(): Range | null {
public getRange(): Range | null {
return this._currentRange
}
@ -27,7 +27,7 @@ class SelectionAndRange {
*
* @param _range
*/
saveRange(_range?: Range): void {
public saveRange(_range?: Range): void {
if (_range) {
// 保存已有选区
this._currentRange = _range
@ -65,7 +65,7 @@ class SelectionAndRange {
*
* @param toStart true false
*/
collapseRange(toStart: boolean = false): void {
public collapseRange(toStart: boolean = false): void {
const range = this._currentRange
if (range) {
range.collapse(toStart)
@ -75,7 +75,7 @@ class SelectionAndRange {
/**
*
*/
getSelectionText(): string {
public getSelectionText(): string {
const range = this._currentRange
if (range) {
return range.toString()
@ -88,7 +88,7 @@ class SelectionAndRange {
* DOM
* @param range
*/
getSelectionContainerElem(range?: Range): DomElement | undefined {
public getSelectionContainerElem(range?: Range): DomElement | undefined {
let r: Range | null | undefined
r = range || this._currentRange
let elem: Node
@ -102,7 +102,7 @@ class SelectionAndRange {
* DOM
* @param range
*/
getSelectionStartElem(range?: Range): DomElement | undefined {
public getSelectionStartElem(range?: Range): DomElement | undefined {
let r: Range | null | undefined
r = range || this._currentRange
let elem: Node
@ -116,7 +116,7 @@ class SelectionAndRange {
* DOM
* @param range
*/
getSelectionEndElem(range?: Range): DomElement | undefined {
public getSelectionEndElem(range?: Range): DomElement | undefined {
let r: Range | null | undefined
r = range || this._currentRange
let elem: Node
@ -129,7 +129,7 @@ class SelectionAndRange {
/**
*
*/
isSelectionEmpty(): boolean {
public isSelectionEmpty(): boolean {
const range = this._currentRange
if (range && range.startContainer) {
if (range.startContainer === range.endContainer) {
@ -144,7 +144,7 @@ class SelectionAndRange {
/**
*
*/
restoreSelection(): void {
public restoreSelection(): void {
const selection = window.getSelection()
const r = this._currentRange
if (selection && r) {
@ -156,7 +156,7 @@ class SelectionAndRange {
/**
* &#8203
*/
createEmptyRange(): void {
public createEmptyRange(): void {
const editor = this.editor
const range = this.getRange()
let $elem: DomElement
@ -195,7 +195,7 @@ class SelectionAndRange {
* @param toStart true false
* @param isContent $elem
*/
createRangeByElem($elem: DomElement, toStart?: boolean, isContent?: boolean): void {
public createRangeByElem($elem: DomElement, toStart?: boolean, isContent?: boolean): void {
if (!$elem.length) {
return
}

View File

@ -21,7 +21,7 @@ class Bold extends BtnMenu implements MenuActive {
/**
*
*/
clickHandler(): void {
public clickHandler(): void {
const editor = this.editor
const isSelectEmpty = editor.selection.isSelectionEmpty()
@ -43,7 +43,7 @@ class Bold extends BtnMenu implements MenuActive {
/**
*
*/
tryChangeActive(): void {
public tryChangeActive(): void {
const editor = this.editor
if (editor.cmd.queryCommandState('bold')) {
this.active()

View File

@ -35,7 +35,7 @@ class Head extends DropListMenu implements MenuActive {
*
* @param value value
*/
command(value: string): void {
public command(value: string): void {
const editor = this.editor
const $selectionElem = editor.selection.getSelectionContainerElem()
if ($selectionElem && editor.$textElem.equal($selectionElem)) {
@ -50,7 +50,7 @@ class Head extends DropListMenu implements MenuActive {
/**
*
*/
tryChangeActive() {
public tryChangeActive() {
const editor = this.editor
const reg = /^h/i
const cmdValue = editor.cmd.queryCommandValue('formatBlock')

View File

@ -6,11 +6,11 @@
import Editor from '../editor/index'
import Menu from './menu-constructors/Menu'
import MenuConstructorList from './menu-list'
import { MenuActive } from './menu-constructors/Menu'
// import { MenuActive } from './menu-constructors/Menu'
class Menus {
editor: Editor
menuList: Menu[]
public editor: Editor
public menuList: Menu[]
constructor(editor: Editor) {
this.editor = editor

View File

@ -20,11 +20,11 @@ class Link extends PanelMenu implements MenuActive {
/**
*
*/
clickHandler(): void {
public clickHandler(): void {
const editor = this.editor
let $linkElem
if (this._active) {
if (this.isActive) {
// 菜单被激活,说明选区在链接里
$linkElem = editor.selection.getSelectionContainerElem()
if (!$linkElem) {
@ -50,7 +50,7 @@ class Link extends PanelMenu implements MenuActive {
* @param text
* @param link
*/
createPanel(text: string, link: string): void {
private createPanel(text: string, link: string): void {
const conf = createPanelConf(this.editor, text, link)
const panel = new Panel(this, conf)
panel.create()
@ -61,7 +61,7 @@ class Link extends PanelMenu implements MenuActive {
/**
* active
*/
tryChangeActive() {
public tryChangeActive() {
const editor = this.editor
if (isActive(editor)) {
this.active()

View File

@ -21,13 +21,14 @@ export type DropListConf = {
}
class DropList {
menu: DropListMenu
conf: DropListConf
hideTimeoutId: number
showTimeoutId: number
$container: DomElement
_rendered: boolean
_show: boolean
private menu: DropListMenu
private conf: DropListConf
private $container: DomElement
private rendered: boolean
private _show: boolean
public hideTimeoutId: number
public showTimeoutId: number
constructor(menu: DropListMenu, conf: DropListConf) {
this.hideTimeoutId = 0
@ -80,14 +81,14 @@ class DropList {
// 记录属性
this.$container = $container
this._rendered = false
this.rendered = false
this._show = false
}
/**
* DropList
*/
show() {
public show() {
if (this.hideTimeoutId) {
// 清除之前的定时隐藏
clearTimeout(this.hideTimeoutId)
@ -99,7 +100,7 @@ class DropList {
if (this._show) {
return
}
if (this._rendered) {
if (this.rendered) {
// 显示
$container.show()
} else {
@ -110,7 +111,7 @@ class DropList {
// 加入到 DOM
$menuELem.append($container)
this._rendered = true
this.rendered = true
}
// 修改属性
@ -120,7 +121,7 @@ class DropList {
/**
* DropList
*/
hide() {
public hide() {
if (this.showTimeoutId) {
// 清除之前的定时显示
clearTimeout(this.showTimeoutId)

View File

@ -9,7 +9,7 @@ import Menu from './Menu'
import DropList, { DropListConf } from './DropList'
class DropListMenu extends Menu {
dropList: DropList
public dropList: DropList
constructor($elem: DomElement, editor: Editor, conf: DropListConf) {
super($elem, editor)

View File

@ -14,9 +14,9 @@ export interface MenuActive {
}
class Menu {
$elem: DomElement
editor: Editor
_active: boolean // 菜单是否处于激活状态如选中一段加粗文字时bold 菜单要被激活(即高亮显示)
public $elem: DomElement
public editor: Editor
private _active: boolean // 菜单是否处于激活状态如选中一段加粗文字时bold 菜单要被激活(即高亮显示)
constructor($elem: DomElement, editor: Editor) {
this.$elem = $elem
@ -54,6 +54,13 @@ class Menu {
this._active = false
this.$elem.removeClass('w-e-active')
}
/**
*
*/
public get isActive() {
return this._active
}
}
export default Menu

View File

@ -28,9 +28,9 @@ export type PanelConf = {
}
class Panel {
menu: PanelMenu
conf: PanelConf
$container: DomElement
private menu: PanelMenu
private conf: PanelConf
private $container: DomElement
constructor(menu: PanelMenu, conf: PanelConf) {
this.menu = menu
@ -41,7 +41,7 @@ class Panel {
/**
* panel
*/
create(): void {
public create(): void {
const menu = this.menu
if (CREATED_MENUS.has(menu)) {
// 创建过了
@ -177,7 +177,7 @@ class Panel {
/**
* penal
*/
remove(): void {
public remove(): void {
const menu = this.menu
const $container = this.$container
if ($container) {

View File

@ -9,7 +9,7 @@ import Menu from './Menu'
import Panel from './Panel'
class PanelMenu extends Menu {
panel: Panel | undefined
public panel: Panel | undefined
constructor($elem: DomElement, editor: Editor) {
super($elem, editor)

View File

@ -19,8 +19,8 @@ type TextEventHooks = {
}
class Text {
editor: Editor
eventHooks: TextEventHooks // Text 各个事件的钩子函数,如 keyup 时要执行哪些函数
public editor: Editor
public eventHooks: TextEventHooks // Text 各个事件的钩子函数,如 keyup 时要执行哪些函数
constructor(editor: Editor) {
this.editor = editor
@ -38,7 +38,7 @@ class Text {
/**
*
*/
init(): void {
public init(): void {
// 实时保存选取范围
this._saveRange()
@ -52,7 +52,7 @@ class Text {
/**
*
*/
clear(): void {
public clear(): void {
this.html('<p><br></p>')
}
@ -60,7 +60,7 @@ class Text {
* / html
* @param val html
*/
html(val?: string): void | string {
public html(val?: string): void | string {
const editor = this.editor
const $textElem = editor.$textElem
@ -84,7 +84,7 @@ class Text {
* /
* @param val text
*/
text(val?: string): void | string {
public text(val?: string): void | string {
const editor = this.editor
const $textElem = editor.$textElem
@ -106,7 +106,7 @@ class Text {
* html
* @param html html
*/
append(html: string): void {
public append(html: string): void {
const editor = this.editor
const $textElem = editor.$textElem
$textElem.append($(html))
@ -118,7 +118,7 @@ class Text {
/**
*
*/
_saveRange(): void {
private _saveRange(): void {
const editor = this.editor
const $textElem = editor.$textElem
@ -146,7 +146,7 @@ class Text {
/**
*
*/
_bindEvent(): void {
private _bindEvent(): void {
const $textElem = this.editor.$textElem
const eventHooks = this.eventHooks

View File

@ -16,5 +16,5 @@ test('初始化 menus', () => {
test('修改菜单激活状态', () => {
const menus = editor.menus
menus.changeActive()
expect(menus.menuList[0]._active).toBeFalsy()
expect(menus.menuList[0].isActive).toBeFalsy()
})