feat:add editor destroy function

pr675
Hufe921 3 years ago
parent 75dae9266d
commit 790d669401

@ -49,13 +49,22 @@ export class ContextMenu {
] ]
this.contextMenuContainerList = [] this.contextMenuContainerList = []
this.contextMenuRelationShip = new Map() this.contextMenuRelationShip = new Map()
this._addEvent()
}
private _addEvent() {
// 接管菜单权限 // 接管菜单权限
document.addEventListener('contextmenu', this._proxyContextMenuEvent.bind(this)) document.addEventListener('contextmenu', this._proxyContextMenuEvent)
// 副作用处理 // 副作用处理
document.addEventListener('mousedown', this._handleEffect.bind(this)) document.addEventListener('mousedown', this._handleEffect)
}
public removeEvent() {
document.removeEventListener('contextmenu', this._proxyContextMenuEvent)
document.removeEventListener('mousedown', this._handleEffect)
} }
private _proxyContextMenuEvent(evt: MouseEvent) { private _proxyContextMenuEvent = (evt: MouseEvent) => {
this.context = this._getContext() this.context = this._getContext()
const renderList: IRegisterContextMenu[] = [] const renderList: IRegisterContextMenu[] = []
let isRegisterContextMenu = false let isRegisterContextMenu = false
@ -82,7 +91,7 @@ export class ContextMenu {
evt.preventDefault() evt.preventDefault()
} }
private _handleEffect(evt: MouseEvent) { private _handleEffect = (evt: MouseEvent) => {
if (this.contextMenuContainerList.length) { if (this.contextMenuContainerList.length) {
// 点击非右键菜单内 // 点击非右键菜单内
const contextMenuDom = findParent( const contextMenuDom = findParent(

@ -63,6 +63,7 @@ export class Draw {
private listener: Listener private listener: Listener
private canvasEvent: CanvasEvent private canvasEvent: CanvasEvent
private globalEvent: GlobalEvent
private cursor: Cursor private cursor: Cursor
private range: RangeManager private range: RangeManager
private margin: Margin private margin: Margin
@ -91,6 +92,8 @@ export class Draw {
private blockParticle: BlockParticle private blockParticle: BlockParticle
private control: Control private control: Control
private workerManager: WorkerManager private workerManager: WorkerManager
private scrollObserver: ScrollObserver
private selectionObserver: SelectionObserver
private rowList: IRow[] private rowList: IRow[]
private painterStyle: IElementStyle | null private painterStyle: IElementStyle | null
@ -99,12 +102,12 @@ export class Draw {
private intersectionPageNo: number private intersectionPageNo: number
constructor( constructor(
container: HTMLDivElement, rootContainer: HTMLElement,
options: DeepRequired<IEditorOption>, options: DeepRequired<IEditorOption>,
elementList: IElement[], elementList: IElement[],
listener: Listener listener: Listener
) { ) {
this.container = container this.container = this._wrapContainer(rootContainer)
this.pageList = [] this.pageList = []
this.ctxList = [] this.ctxList = []
this.pageNo = 0 this.pageNo = 0
@ -145,14 +148,14 @@ export class Draw {
this.blockParticle = new BlockParticle(this) this.blockParticle = new BlockParticle(this)
this.control = new Control(this) this.control = new Control(this)
new ScrollObserver(this) this.scrollObserver = new ScrollObserver(this)
new SelectionObserver() this.selectionObserver = new SelectionObserver()
this.canvasEvent = new CanvasEvent(this) this.canvasEvent = new CanvasEvent(this)
this.cursor = new Cursor(this, this.canvasEvent) this.cursor = new Cursor(this, this.canvasEvent)
this.canvasEvent.register() this.canvasEvent.register()
const globalEvent = new GlobalEvent(this, this.canvasEvent) this.globalEvent = new GlobalEvent(this, this.canvasEvent)
globalEvent.register() this.globalEvent.register()
this.workerManager = new WorkerManager(this) this.workerManager = new WorkerManager(this)
@ -513,6 +516,12 @@ export class Draw {
} }
} }
private _wrapContainer(rootContainer: HTMLElement): HTMLDivElement {
const container = document.createElement('div')
rootContainer.append(container)
return container
}
private _formatContainer() { private _formatContainer() {
// 容器宽度需跟随纸张宽度 // 容器宽度需跟随纸张宽度
this.container.style.position = 'relative' this.container.style.position = 'relative'
@ -1171,4 +1180,11 @@ export class Draw {
}) })
} }
public destroy() {
this.container.remove()
this.globalEvent.removeEvent()
this.scrollObserver.removeEvent()
this.selectionObserver.removeEvent()
}
} }

@ -38,21 +38,24 @@ export class GlobalEvent {
public register() { public register() {
this.cursor = this.draw.getCursor() this.cursor = this.draw.getCursor()
document.addEventListener('keyup', () => { this.addEvent()
this.setRangeStyle() }
})
document.addEventListener('click', (evt) => { private addEvent() {
this.recoverEffect(evt) document.addEventListener('keyup', this.setRangeStyle)
}) document.addEventListener('click', this.recoverEffect)
document.addEventListener('mouseup', () => { document.addEventListener('mouseup', this.setDragState)
this.setDragState() document.addEventListener('wheel', this.setPageScale, { passive: false })
}) }
document.addEventListener('wheel', (evt: WheelEvent) => {
this.setPageScale(evt) public removeEvent() {
}, { passive: false }) document.removeEventListener('keyup', this.setRangeStyle)
document.removeEventListener('click', this.recoverEffect)
document.removeEventListener('mouseup', this.setDragState)
document.removeEventListener('wheel', this.setPageScale)
} }
public recoverEffect(evt: MouseEvent) { public recoverEffect = (evt: MouseEvent) => {
if (!this.cursor) return if (!this.cursor) return
const cursorDom = this.cursor.getCursorDom() const cursorDom = this.cursor.getCursorDom()
const agentDom = this.cursor.getAgentDom() const agentDom = this.cursor.getAgentDom()
@ -80,15 +83,15 @@ export class GlobalEvent {
this.control.destroyControl() this.control.destroyControl()
} }
public setDragState() { public setDragState = () => {
this.canvasEvent.setIsAllowDrag(false) this.canvasEvent.setIsAllowDrag(false)
} }
public setRangeStyle() { public setRangeStyle = () => {
this.range.setRangeStyle() this.range.setRangeStyle()
} }
public setPageScale(evt: WheelEvent) { public setPageScale = (evt: WheelEvent) => {
if (!evt.ctrlKey) return if (!evt.ctrlKey) return
evt.preventDefault() evt.preventDefault()
const { scale } = this.options const { scale } = this.options

@ -21,10 +21,18 @@ export class ScrollObserver {
this._observer() this._observer()
} }
}) })
document.addEventListener('scroll', debounce(this._observer.bind(this), 150)) this._addEvent()
} }
private _observer() { private _addEvent() {
document.addEventListener('scroll', this._observer)
}
public removeEvent() {
document.removeEventListener('scroll', this._observer)
}
private _observer = debounce(() => {
const rect = this.pageContainer.getBoundingClientRect() const rect = this.pageContainer.getBoundingClientRect()
const top = Math.abs(rect.top) const top = Math.abs(rect.top)
const bottom = top + window.innerHeight const bottom = top + window.innerHeight
@ -55,6 +63,6 @@ export class ScrollObserver {
} }
this.draw.setIntersectionPageNo(intersectionPageNo) this.draw.setIntersectionPageNo(intersectionPageNo)
this.draw.setVisiblePageNoList(visiblePageNoList) this.draw.setVisiblePageNoList(visiblePageNoList)
} }, 150)
} }

@ -20,22 +20,31 @@ export class SelectionObserver {
this.isMoving = false this.isMoving = false
this.clientWidth = 0 this.clientWidth = 0
this.clientHeight = 0 this.clientHeight = 0
this._addEvent()
}
private _addEvent() {
document.addEventListener('mousedown', this._mousedown)
document.addEventListener('mousemove', this._mousemove)
document.addEventListener('mouseup', this._mouseup)
}
document.addEventListener('mousedown', this._mousedown.bind(this)) public removeEvent() {
document.addEventListener('mousemove', this._mousemove.bind(this)) document.removeEventListener('mousedown', this._mousedown)
document.addEventListener('mouseup', this._mouseup.bind(this)) document.removeEventListener('mousemove', this._mousemove)
document.removeEventListener('mouseup', this._mouseup)
} }
private _mousedown() { private _mousedown = () => {
this.isMousedown = true this.isMousedown = true
} }
private _mouseup() { private _mouseup = () => {
this.isMousedown = false this.isMousedown = false
this._stopMove() this._stopMove()
} }
private _mousemove(evt: MouseEvent) { private _mousemove = (evt: MouseEvent) => {
if (!this.isMousedown) return if (!this.isMousedown) return
const { x, y } = evt const { x, y } = evt
if (y < this.tippingPoints[0]) { if (y < this.tippingPoints[0]) {

@ -18,12 +18,20 @@ export class Shortcut {
...richtextKeys ...richtextKeys
]) ])
// 全局快捷键 // 全局快捷键
document.addEventListener('keydown', this._globalKeydown.bind(this)) this._addEvent()
// 编辑器快捷键 // 编辑器快捷键
const agentDom = draw.getCursor().getAgentDom() const agentDom = draw.getCursor().getAgentDom()
agentDom.addEventListener('keydown', this._agentKeydown.bind(this)) agentDom.addEventListener('keydown', this._agentKeydown.bind(this))
} }
private _addEvent() {
document.addEventListener('keydown', this._globalKeydown)
}
public removeEvent() {
document.removeEventListener('keydown', this._globalKeydown)
}
private _addShortcutList(payload: IRegisterShortcut[]) { private _addShortcutList(payload: IRegisterShortcut[]) {
for (let s = 0; s < payload.length; s++) { for (let s = 0; s < payload.length; s++) {
const shortCut = payload[s] const shortCut = payload[s]
@ -39,7 +47,7 @@ export class Shortcut {
this._addShortcutList(payload) this._addShortcutList(payload)
} }
private _globalKeydown(evt: KeyboardEvent) { private _globalKeydown = (evt: KeyboardEvent) => {
if (!this.globalShortcutList.length) return if (!this.globalShortcutList.length) return
this._execute(evt, this.globalShortcutList) this._execute(evt, this.globalShortcutList)
} }

@ -34,6 +34,7 @@ export default class Editor {
public command: Command public command: Command
public listener: Listener public listener: Listener
public register: Register public register: Register
public destroy: Function
constructor(container: HTMLDivElement, elementList: IElement[], options: IEditorOption = {}) { constructor(container: HTMLDivElement, elementList: IElement[], options: IEditorOption = {}) {
const headerOptions: Required<IHeader> = { const headerOptions: Required<IHeader> = {
@ -111,6 +112,12 @@ export default class Editor {
contextMenu, contextMenu,
shortcut shortcut
}) })
// 注册销毁方法
this.destroy = () => {
draw.destroy()
shortcut.removeEvent()
contextMenu.removeEvent()
}
} }
} }

@ -605,7 +605,7 @@ ul {
background-image: url('./assets/images/print.svg'); background-image: url('./assets/images/print.svg');
} }
.editor { .editor>div {
margin: 80px auto; margin: 80px auto;
} }

Loading…
Cancel
Save