Skip to content

Commit

Permalink
feat: add position context change actuator #733
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Sep 5, 2024
1 parent 2351855 commit 66c73e1
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/en/guide/eventbus.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,13 @@ Usage:
```javascript
instance.eventBus.on('mouseleave', (evt: MouseEvent) => void)
```

## positionContextChange

Feature: The position context change

Usage:

```javascript
instance.eventBus.on('positionContextChange', (payload: IPositionContextChangePayload) => void)
```
10 changes: 10 additions & 0 deletions docs/guide/eventbus.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,13 @@ instance.eventBus.on('mouseenter', (evt: MouseEvent) => void)
```javascript
instance.eventBus.on('mouseleave', (evt: MouseEvent) => void)
```

## positionContextChange

功能:上下文内容发生改变

用法:

```javascript
instance.eventBus.on('positionContextChange', (payload: IPositionContextChangePayload) => void)
```
21 changes: 21 additions & 0 deletions src/editor/core/actuator/Actuator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EventBusMap } from '../../interface/EventBus'
import { Draw } from '../draw/Draw'
import { EventBus } from '../event/eventbus/EventBus'
import { positionContextChange } from './handlers/positionContextChange'

export class Actuator {
private draw: Draw
private eventBus: EventBus<EventBusMap>

constructor(draw: Draw) {
this.draw = draw
this.eventBus = draw.getEventBus()
this.execute()
}

private execute() {
this.eventBus.on('positionContextChange', payload => {
positionContextChange(this.draw, payload)
})
}
}
13 changes: 13 additions & 0 deletions src/editor/core/actuator/handlers/positionContextChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IPositionContextChangePayload } from '../../../interface/Listener'
import { Draw } from '../../draw/Draw'

export function positionContextChange(
draw: Draw,
payload: IPositionContextChangePayload
) {
const { value, oldValue } = payload
// 表格工具移除
if (oldValue.isTable && !value.isTable) {
draw.getTableTool().dispose()
}
}
2 changes: 2 additions & 0 deletions src/editor/core/draw/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import { MouseObserver } from '../observer/MouseObserver'
import { LineNumber } from './frame/LineNumber'
import { PageBorder } from './frame/PageBorder'
import { ITd } from '../../interface/table/Td'
import { Actuator } from '../actuator/Actuator'

export class Draw {
private container: HTMLDivElement
Expand Down Expand Up @@ -249,6 +250,7 @@ export class Draw {
this.globalEvent.register()

this.workerManager = new WorkerManager(this)
new Actuator(this)

const { letterClass } = options
this.LETTER_REG = new RegExp(`[${letterClass.join('')}]`)
Expand Down
8 changes: 8 additions & 0 deletions src/editor/core/position/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { EditorMode, EditorZone } from '../../dataset/enum/Editor'
import { deepClone } from '../../utils'
import { ImageDisplay } from '../../dataset/enum/Common'
import { DeepRequired } from '../../interface/Common'
import { EventBus } from '../event/eventbus/EventBus'
import { EventBusMap } from '../../interface/EventBus'

export class Position {
private cursorPosition: IElementPosition | null
Expand All @@ -28,6 +30,7 @@ export class Position {
private floatPositionList: IFloatPosition[]

private draw: Draw
private eventBus: EventBus<EventBusMap>
private options: DeepRequired<IEditorOption>

constructor(draw: Draw) {
Expand All @@ -40,6 +43,7 @@ export class Position {
}

this.draw = draw
this.eventBus = draw.getEventBus()
this.options = draw.getOptions()
}

Expand Down Expand Up @@ -329,6 +333,10 @@ export class Position {
}

public setPositionContext(payload: IPositionContext) {
this.eventBus.emit('positionContextChange', {
value: payload,
oldValue: this.positionContext
})
this.positionContext = payload
}

Expand Down
2 changes: 2 additions & 0 deletions src/editor/interface/EventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IPageModeChange,
IPageScaleChange,
IPageSizeChange,
IPositionContextChange,
IRangeStyleChange,
ISaved,
IVisiblePageNoListChange,
Expand All @@ -26,4 +27,5 @@ export interface EventBusMap {
mousemove: IMouseEventChange
mouseleave: IMouseEventChange
mouseenter: IMouseEventChange
positionContextChange: IPositionContextChange
}
9 changes: 9 additions & 0 deletions src/editor/interface/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { RowFlex } from '../dataset/enum/Row'
import { IControl } from './Control'
import { IEditorResult } from './Editor'
import { IPositionContext } from './Position'
import { ITextDecoration } from './Text'

export interface IRangeStyle {
Expand Down Expand Up @@ -56,3 +57,11 @@ export type IPageModeChange = (payload: PageMode) => void
export type IZoneChange = (payload: EditorZone) => void

export type IMouseEventChange = (evt: MouseEvent) => void

export interface IPositionContextChangePayload {
value: IPositionContext
oldValue: IPositionContext
}
export type IPositionContextChange = (
payload: IPositionContextChangePayload
) => void

0 comments on commit 66c73e1

Please sign in to comment.