Skip to content

Commit

Permalink
feat: add override internal function api #260
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Aug 30, 2023
1 parent 640f262 commit abcaa9b
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export default defineConfig({
{ text: '自定义右键菜单', link: '/guide/contextmenu-custom' }
]
},
{
text: '重写方法',
items: [{ text: '重写方法', link: '/guide/override' }]
},
{
text: 'API',
items: [{ text: '全局API', link: '/guide/api' }]
Expand Down Expand Up @@ -140,6 +144,10 @@ export default defineConfig({
{ text: 'custom', link: '/en/guide/contextmenu-custom' }
]
},
{
text: 'Override',
items: [{ text: 'override', link: '/en/guide/override' }]
},
{
text: 'Api',
items: [{ text: 'api', link: '/en/guide/api' }]
Expand Down
20 changes: 20 additions & 0 deletions docs/en/guide/override.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Override

## How to Use

```javascript
import Editor from "@hufe921/canvas-editor"

const instance = new Editor(container, <IElement[]>data, options)
instance.override.overrideFunction = ()=>{}
```

## paste

Feature: Override internal paste function

Usage:

```javascript
instance.override.paste = (evt: ClipboardEvent) => void
```
20 changes: 20 additions & 0 deletions docs/guide/override.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 重写方法

## 使用方式

```javascript
import Editor from "@hufe921/canvas-editor"

const instance = new Editor(container, <IElement[]>data, options)
instance.override.overrideFunction = ()=>{}
```

## paste

功能:重写粘贴方法

用法:

```javascript
instance.override.paste = (evt: ClipboardEvent) => void
```
6 changes: 6 additions & 0 deletions src/editor/core/cursor/CursorAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export class CursorAgent {
if (isReadonly) return
const clipboardData = evt.clipboardData
if (!clipboardData) return
// 自定义粘贴事件
const { paste } = this.draw.getOverride()
if (paste) {
paste(evt)
return
}
const rangeManager = this.draw.getRange()
const { startIndex } = rangeManager.getRange()
const elementList = this.draw.getElementList()
Expand Down
10 changes: 9 additions & 1 deletion src/editor/core/draw/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import { WORD_LIKE_REG } from '../../dataset/constant/Regular'
import { EventBus } from '../event/eventbus/EventBus'
import { EventBusMap } from '../../interface/EventBus'
import { Group } from './interactive/Group'
import { Override } from '../override/Override'

export class Draw {
private container: HTMLDivElement
Expand All @@ -98,6 +99,7 @@ export class Draw {
private elementList: IElement[]
private listener: Listener
private eventBus: EventBus<EventBusMap>
private override: Override

private i18n: I18n
private canvasEvent: CanvasEvent
Expand Down Expand Up @@ -152,7 +154,8 @@ export class Draw {
options: DeepRequired<IEditorOption>,
data: IEditorData,
listener: Listener,
eventBus: EventBus<EventBusMap>
eventBus: EventBus<EventBusMap>,
override: Override
) {
this.container = this._wrapContainer(rootContainer)
this.pageList = []
Expand All @@ -164,6 +167,7 @@ export class Draw {
this.elementList = data.main
this.listener = listener
this.eventBus = eventBus
this.override = override

this._formatContainer()
this.pageContainer = this._createPageContainer()
Expand Down Expand Up @@ -622,6 +626,10 @@ export class Draw {
return this.eventBus
}

public getOverride(): Override {
return this.override
}

public getCursor(): Cursor {
return this.cursor
}
Expand Down
3 changes: 3 additions & 0 deletions src/editor/core/override/Override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Override {
public paste: ((evt: ClipboardEvent) => void) | undefined
}
7 changes: 6 additions & 1 deletion src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ import { EventBusMap } from './interface/EventBus'
import { IGroup } from './interface/Group'
import { defaultGroupOption } from './dataset/constant/Group'
import { IRangeStyle } from './interface/Listener'
import { Override } from './core/override/Override'

export default class Editor {
public command: Command
public listener: Listener
public eventBus: EventBus<EventBusMap>
public override: Override
public register: Register
public destroy: () => void
public use: UsePlugin
Expand Down Expand Up @@ -193,6 +195,8 @@ export default class Editor {
this.listener = new Listener()
// 事件
this.eventBus = new EventBus<EventBusMap>()
// 重写
this.override = new Override()
// 启动
const draw = new Draw(
container,
Expand All @@ -203,7 +207,8 @@ export default class Editor {
footer: footerElementList
},
this.listener,
this.eventBus
this.eventBus,
this.override
)
// 命令
this.command = new Command(new CommandAdapt(draw))
Expand Down

0 comments on commit abcaa9b

Please sign in to comment.